Mercurial > ecos
annotate packages/kernel/current/include/mempoolt.inl @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children |
| rev | line source |
|---|---|
| 0 | 1 #ifndef CYGONCE_KERNEL_MEMPOOLT_INL |
| 2 #define CYGONCE_KERNEL_MEMPOOLT_INL | |
| 3 | |
| 4 //========================================================================== | |
| 5 // | |
| 2 | 6 // mempoolt.inl |
| 0 | 7 // |
| 2 | 8 // Mempoolt (Memory pool template) class declarations |
| 0 | 9 // |
| 10 //========================================================================== | |
| 11 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
15 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
17 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
30 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
31 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
32 // |
| 0 | 33 //####COPYRIGHTEND#### |
| 34 //========================================================================== | |
| 35 //#####DESCRIPTIONBEGIN#### | |
| 36 // | |
| 2 | 37 // Author(s): hmt |
| 38 // Contributors: hmt | |
| 39 // Date: 1998-02-10 | |
| 40 // Purpose: Define Mempoolt class interface | |
| 0 | 41 |
| 42 // Description: The class defined here provides the APIs for thread-safe, | |
| 43 // kernel-savvy memory managers; make a class with the | |
| 44 // underlying allocator as the template parameter. | |
| 2 | 45 // Usage: #include <cyg/kernel/mempoolt.hxx> |
| 46 // | |
| 0 | 47 // |
| 48 //####DESCRIPTIONEND#### | |
| 49 // | |
| 50 //========================================================================== | |
| 51 | |
| 52 #include <cyg/kernel/thread.inl> // implementation eg. Cyg_Thread::self(); | |
| 53 #include <cyg/kernel/sched.inl> // implementation eg. Cyg_Scheduler::lock(); | |
| 54 | |
| 55 // ------------------------------------------------------------------------- | |
| 56 // Constructor; we _require_ these arguments and just pass them through to | |
| 57 // the implementation memory pool in use. | |
| 58 template <class T> | |
| 59 Cyg_Mempoolt<T>::Cyg_Mempoolt( | |
| 60 cyg_uint8 *base, | |
| 61 cyg_int32 size, | |
| 62 CYG_ADDRWORD arg_thru) // Constructor | |
| 63 : pool( base, size, arg_thru ) | |
| 64 { | |
| 65 } | |
| 66 | |
| 67 | |
| 68 template <class T> | |
| 69 Cyg_Mempoolt<T>::~Cyg_Mempoolt() // destructor | |
| 70 { | |
| 71 // Prevent preemption | |
| 72 Cyg_Scheduler::lock(); | |
| 73 | |
| 74 while ( ! queue.empty() ) { | |
| 75 Cyg_Thread *thread = queue.dequeue(); | |
| 76 thread->set_wake_reason( Cyg_Thread::DESTRUCT ); | |
| 77 thread->wake(); | |
| 78 } | |
| 79 | |
| 80 // Unlock the scheduler and maybe switch threads | |
| 81 Cyg_Scheduler::unlock(); | |
| 82 } | |
| 83 | |
| 84 // ------------------------------------------------------------------------- | |
| 85 // get some memory; wait if none available | |
| 86 template <class T> | |
| 87 inline cyg_uint8 * | |
| 88 Cyg_Mempoolt<T>::alloc( cyg_int32 size ) | |
| 89 { | |
| 90 CYG_REPORT_FUNCTION(); | |
| 91 | |
| 92 Cyg_Thread *self = Cyg_Thread::self(); | |
| 93 | |
| 94 // Prevent preemption | |
| 95 Cyg_Scheduler::lock(); | |
| 96 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 97 | |
| 98 // Loop while we got no memory, sleeping each time around the | |
| 99 // loop. This copes with the possibility of a higher priority thread | |
| 100 // grabbing the freed storage between the wakeup in free() and this | |
| 101 // thread actually starting. | |
| 102 cyg_uint8 *ret; | |
| 103 cyg_bool result = true; | |
| 104 while( result && (NULL == (ret = pool.alloc( size ))) ) { | |
| 105 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 106 self->sleep(); | |
| 107 queue.enqueue( self ); | |
| 108 | |
| 109 CYG_ASSERT( 1 == Cyg_Scheduler::get_sched_lock(), | |
| 110 "Called with non-zero scheduler lock"); | |
| 111 | |
| 112 // Unlock scheduler and allow other threads to run | |
| 113 Cyg_Scheduler::unlock(); | |
| 114 Cyg_Scheduler::lock(); | |
| 115 | |
| 116 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 117 | |
| 118 switch( self->get_wake_reason() ) | |
| 119 { | |
| 120 case Cyg_Thread::DESTRUCT: | |
| 121 case Cyg_Thread::BREAK: | |
| 122 result = false; | |
| 123 break; | |
| 124 | |
| 125 case Cyg_Thread::EXIT: | |
| 126 self->exit(); | |
| 127 break; | |
| 128 | |
| 129 default: | |
| 130 break; | |
| 131 } | |
| 132 } | |
| 133 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 134 | |
| 135 if ( ! result ) | |
| 136 ret = NULL; | |
| 137 | |
| 138 // Unlock the scheduler and maybe switch threads | |
| 139 Cyg_Scheduler::unlock(); | |
| 140 CYG_REPORT_RETVAL( ret ); | |
| 141 return ret; | |
| 142 } | |
| 143 | |
| 144 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 145 // ------------------------------------------------------------------------- | |
| 146 // get some memory with a timeout | |
| 147 template <class T> | |
| 148 inline cyg_uint8 * | |
| 149 Cyg_Mempoolt<T>::alloc( cyg_int32 size, cyg_tick_count abs_timeout ) | |
| 150 { | |
| 151 CYG_REPORT_FUNCTION(); | |
| 152 | |
| 153 Cyg_Thread *self = Cyg_Thread::self(); | |
| 154 | |
| 155 // Prevent preemption | |
| 156 Cyg_Scheduler::lock(); | |
| 157 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 158 | |
| 159 // Loop while we got no memory, sleeping each time around the | |
| 160 // loop. This copes with the possibility of a higher priority thread | |
| 161 // grabbing the freed storage between the wakeup in free() and this | |
| 162 // thread actually starting. | |
| 163 cyg_uint8 *ret; | |
| 164 cyg_bool result = true; | |
| 165 // Set the timer _once_ outside the loop. | |
| 166 self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT ); | |
| 167 | |
| 168 // If the timeout is in the past, the wake reason will have been | |
| 169 // set to something other than NONE already. Set the result false | |
| 170 // to force an immediate return. | |
| 171 | |
| 172 if( self->get_wake_reason() != Cyg_Thread::NONE ) | |
| 173 result = false; | |
| 174 | |
| 175 while( result && (NULL == (ret = pool.alloc( size ))) ) { | |
| 176 self->set_sleep_reason( Cyg_Thread::TIMEOUT ); | |
| 177 self->sleep(); | |
| 178 queue.enqueue( self ); | |
| 179 | |
| 180 CYG_ASSERT( 1 == Cyg_Scheduler::get_sched_lock(), | |
| 181 "Called with non-zero scheduler lock"); | |
| 182 | |
| 183 // Unlock scheduler and allow other threads to run | |
| 184 Cyg_Scheduler::unlock(); | |
| 185 Cyg_Scheduler::lock(); | |
| 186 | |
| 187 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 188 switch( self->get_wake_reason() ) | |
| 189 { | |
| 190 case Cyg_Thread::TIMEOUT: | |
| 191 result = false; | |
| 192 break; | |
| 193 | |
| 194 case Cyg_Thread::DESTRUCT: | |
| 195 case Cyg_Thread::BREAK: | |
| 196 result = false; | |
| 197 break; | |
| 198 | |
| 199 case Cyg_Thread::EXIT: | |
| 200 self->exit(); | |
| 201 break; | |
| 202 | |
| 203 default: | |
| 204 break; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 209 | |
| 210 if ( ! result ) | |
| 211 ret = NULL; | |
| 212 | |
| 213 // clear the timer; if it actually fired, no worries. | |
| 214 self->clear_timer(); | |
| 215 | |
| 216 // Unlock the scheduler and maybe switch threads | |
| 217 Cyg_Scheduler::unlock(); | |
| 218 CYG_REPORT_RETVAL( ret ); | |
| 219 return ret; | |
| 220 } | |
| 221 #endif | |
| 222 | |
| 223 // ------------------------------------------------------------------------- | |
| 224 // get some memory, return NULL if none available | |
| 225 template <class T> | |
| 226 inline cyg_uint8 * | |
| 227 Cyg_Mempoolt<T>::try_alloc( cyg_int32 size ) | |
| 228 { | |
| 229 CYG_REPORT_FUNCTION(); | |
| 230 | |
| 231 // Prevent preemption | |
| 232 Cyg_Scheduler::lock(); | |
| 233 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 234 | |
| 235 cyg_uint8 *ret = pool.alloc( size ); | |
| 236 | |
| 237 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 238 | |
| 239 // Unlock the scheduler and maybe switch threads | |
| 240 Cyg_Scheduler::unlock(); | |
| 241 CYG_REPORT_RETVAL( ret ); | |
| 242 return ret; | |
| 243 } | |
| 244 | |
| 245 | |
| 246 // ------------------------------------------------------------------------- | |
| 247 // free the memory back to the pool | |
| 248 template <class T> | |
| 249 cyg_bool | |
| 250 Cyg_Mempoolt<T>::free( cyg_uint8 *p, cyg_int32 size ) | |
| 251 { | |
| 252 // Prevent preemption | |
| 253 Cyg_Scheduler::lock(); | |
| 254 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 255 | |
| 256 cyg_int32 ret = pool.free( p, size ); | |
| 257 | |
| 258 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 259 | |
| 260 while ( ret && !queue.empty() ) { | |
| 261 // we succeeded and there are people waiting | |
| 262 Cyg_Thread *thread = queue.dequeue(); | |
| 263 | |
| 264 CYG_ASSERTCLASS( thread, "Bad thread pointer"); | |
| 265 | |
| 266 // we wake them all up (ie. broadcast) to cope with variable block | |
| 267 // allocators freeing a big block when lots of small allocs wait. | |
| 268 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 269 thread->wake(); | |
| 270 // we cannot yield here; if a higher prio thread can't satisfy its | |
| 271 // request it would re-queue and we would loop forever | |
| 272 } | |
| 273 // Unlock the scheduler and maybe switch threads | |
| 274 Cyg_Scheduler::unlock(); | |
| 275 return ret; | |
| 276 } | |
| 277 | |
| 278 // ------------------------------------------------------------------------- | |
| 279 // if applicable: return -1 if not fixed size | |
| 280 template <class T> | |
| 281 inline cyg_int32 | |
| 282 Cyg_Mempoolt<T>::get_blocksize() | |
| 283 { | |
| 284 // there should not be any atomicity issues here | |
| 285 return pool.get_blocksize(); | |
| 286 } | |
| 287 | |
| 288 // ------------------------------------------------------------------------- | |
| 289 // these two are obvious and generic, but need atomicity protection (maybe) | |
| 290 template <class T> | |
| 291 inline cyg_int32 | |
| 292 Cyg_Mempoolt<T>::get_totalmem() | |
| 293 { | |
| 294 // Prevent preemption | |
| 295 Cyg_Scheduler::lock(); | |
| 296 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 297 | |
| 298 cyg_int32 ret = pool.get_totalmem(); | |
| 299 | |
| 300 // Unlock the scheduler and maybe switch threads | |
| 301 Cyg_Scheduler::unlock(); | |
| 302 return ret; | |
| 303 } | |
| 304 | |
| 305 template <class T> | |
| 306 inline cyg_int32 | |
| 307 Cyg_Mempoolt<T>::get_freemem() | |
| 308 { | |
| 309 // Prevent preemption | |
| 310 Cyg_Scheduler::lock(); | |
| 311 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 312 | |
| 313 cyg_int32 ret = pool.get_freemem(); | |
| 314 | |
| 315 // Unlock the scheduler and maybe switch threads | |
| 316 Cyg_Scheduler::unlock(); | |
| 317 return ret; | |
| 318 } | |
| 319 | |
| 320 // ------------------------------------------------------------------------- | |
| 321 // get information about the construction parameters for external | |
| 322 // freeing after the destruction of the holding object | |
| 323 template <class T> | |
| 324 inline void | |
| 325 Cyg_Mempoolt<T>::get_arena( | |
| 326 cyg_uint8 * &base, cyg_int32 &size, CYG_ADDRWORD &arg_thru ) | |
| 327 { | |
| 328 // Prevent preemption | |
| 329 Cyg_Scheduler::lock(); | |
| 330 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 331 | |
| 332 pool.get_arena( base, size, arg_thru ); | |
| 333 | |
| 334 // Unlock the scheduler and maybe switch threads | |
| 335 Cyg_Scheduler::unlock(); | |
| 336 } | |
| 337 | |
| 338 // ------------------------------------------------------------------------- | |
| 339 // Return the size of the memory allocation (previously returned | |
| 340 // by alloc() or try_alloc() ) at ptr. Returns -1 if not found | |
| 341 template <class T> | |
| 342 cyg_int32 | |
| 343 Cyg_Mempoolt<T>::get_allocation_size( cyg_uint8 *ptr ) | |
| 344 { | |
| 345 cyg_int32 ret; | |
| 346 | |
| 347 // Prevent preemption | |
| 348 Cyg_Scheduler::lock(); | |
| 349 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 350 | |
| 351 ret = pool.get_allocation_size( ptr ); | |
| 352 | |
| 353 // Unlock the scheduler and maybe switch threads | |
| 354 Cyg_Scheduler::unlock(); | |
| 355 | |
| 356 return ret; | |
| 357 } | |
| 358 | |
| 359 // ------------------------------------------------------------------------- | |
| 360 // debugging/assert function | |
| 361 | |
| 362 #ifdef CYGDBG_USE_ASSERTS | |
| 363 | |
| 364 template <class T> | |
| 365 inline cyg_bool | |
| 2 | 366 Cyg_Mempoolt<T>::check_this(cyg_assert_class_zeal zeal) const |
| 0 | 367 { |
| 368 CYG_REPORT_FUNCTION(); | |
| 369 | |
| 370 if ( Cyg_Thread::DESTRUCT == Cyg_Thread::self()->get_wake_reason() ) | |
| 371 // then the whole thing is invalid, and we know it. | |
| 372 // so return OK, since this check should NOT make an error. | |
| 373 return true; | |
| 374 | |
| 375 // check that we have a non-NULL pointer first | |
| 376 if( this == NULL ) return false; | |
| 377 | |
| 378 return true; | |
| 379 } | |
| 380 #endif | |
| 381 | |
| 382 // ------------------------------------------------------------------------- | |
| 383 #endif // ifndef CYGONCE_KERNEL_MEMPOOLT_INL | |
| 384 // EOF mempoolt.inl |
