Mercurial > ecos
annotate packages/kernel/current/include/mempolt2.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_MEMPOLT2_INL |
| 2 #define CYGONCE_KERNEL_MEMPOLT2_INL | |
| 3 | |
| 4 //========================================================================== | |
| 5 // | |
| 2 | 6 // mempolt2.inl |
| 0 | 7 // |
| 2 | 8 // Mempolt2 (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 Mempolt2 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/mempolt2.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_Mempolt2<T>::Cyg_Mempolt2( | |
| 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_Mempolt2<T>::~Cyg_Mempolt2() // 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_Mempolt2<T>::alloc( cyg_int32 size ) | |
| 89 { | |
| 90 CYG_REPORT_FUNCTION(); | |
| 91 | |
| 92 // Prevent preemption | |
| 93 Cyg_Scheduler::lock(); | |
| 94 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 95 | |
| 96 cyg_uint8 *ret; | |
| 97 ret = pool.alloc( size ); | |
| 98 if ( ret ) { | |
| 99 Cyg_Scheduler::unlock(); | |
| 100 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 101 CYG_REPORT_RETVAL( ret ); | |
| 102 return ret; | |
| 103 } | |
| 104 | |
| 105 Cyg_Thread *self = Cyg_Thread::self(); | |
| 106 | |
| 107 Mempolt2WaitInfo waitinfo( size ); | |
| 108 | |
| 109 self->set_wait_info( (CYG_ADDRWORD)&waitinfo ); | |
| 110 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 111 self->sleep(); | |
| 112 queue.enqueue( self ); | |
| 113 | |
| 114 CYG_ASSERT( 1 == Cyg_Scheduler::get_sched_lock(), | |
| 115 "Called with non-zero scheduler lock"); | |
| 116 | |
| 117 // Unlock scheduler and allow other threads to run | |
| 118 Cyg_Scheduler::unlock(); | |
| 119 | |
| 120 cyg_bool result = true; // just used as a flag here | |
| 121 switch( self->get_wake_reason() ) | |
| 122 { | |
| 123 case Cyg_Thread::DESTRUCT: | |
| 124 case Cyg_Thread::BREAK: | |
| 125 result = false; | |
| 126 break; | |
| 127 | |
| 128 case Cyg_Thread::EXIT: | |
| 129 self->exit(); | |
| 130 break; | |
| 131 | |
| 132 default: | |
| 133 break; | |
| 134 } | |
| 135 | |
| 136 if ( ! result ) | |
| 137 ret = NULL; | |
| 138 else | |
| 139 ret = waitinfo.addr; | |
| 140 | |
| 141 CYG_ASSERT( (!result) || (NULL != ret), "Good result but no alloc!" ); | |
| 142 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 143 CYG_REPORT_RETVAL( ret ); | |
| 144 return ret; | |
| 145 } | |
| 146 | |
| 147 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 148 // ------------------------------------------------------------------------- | |
| 149 // get some memory with a timeout | |
| 150 template <class T> | |
| 151 inline cyg_uint8 * | |
| 152 Cyg_Mempolt2<T>::alloc( cyg_int32 size, cyg_tick_count abs_timeout ) | |
| 153 { | |
| 154 CYG_REPORT_FUNCTION(); | |
| 155 | |
| 156 // Prevent preemption | |
| 157 Cyg_Scheduler::lock(); | |
| 158 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 159 | |
| 160 cyg_uint8 *ret; | |
| 161 ret = pool.alloc( size ); | |
| 162 if ( ret ) { | |
| 163 Cyg_Scheduler::unlock(); | |
| 164 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 165 CYG_REPORT_RETVAL( ret ); | |
| 166 return ret; | |
| 167 } | |
| 168 | |
| 169 Cyg_Thread *self = Cyg_Thread::self(); | |
| 170 | |
| 171 Mempolt2WaitInfo waitinfo( size ); | |
| 172 | |
| 173 self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT ); | |
| 174 | |
| 175 // If the timeout is in the past, the wake reason will have been set to | |
| 176 // something other than NONE already. If so, skip the wait and go | |
| 177 // straight to unlock. | |
| 178 | |
| 179 if( Cyg_Thread::NONE == self->get_wake_reason() ) { | |
| 180 self->set_wait_info( (CYG_ADDRWORD)&waitinfo ); | |
| 181 self->sleep(); | |
| 182 queue.enqueue( self ); | |
| 183 } | |
| 184 | |
| 185 CYG_ASSERT( 1 == Cyg_Scheduler::get_sched_lock(), | |
| 186 "Called with non-zero scheduler lock"); | |
| 187 | |
| 188 // Unlock scheduler and allow other threads to run | |
| 189 Cyg_Scheduler::unlock(); | |
| 190 | |
| 191 // clear the timer; if it actually fired, no worries. | |
| 192 self->clear_timer(); | |
| 193 | |
| 194 cyg_bool result = true; // just used as a flag here | |
| 195 switch( self->get_wake_reason() ) | |
| 196 { | |
| 197 case Cyg_Thread::TIMEOUT: | |
| 198 result = false; | |
| 199 break; | |
| 200 | |
| 201 case Cyg_Thread::DESTRUCT: | |
| 202 case Cyg_Thread::BREAK: | |
| 203 result = false; | |
| 204 break; | |
| 205 | |
| 206 case Cyg_Thread::EXIT: | |
| 207 self->exit(); | |
| 208 break; | |
| 209 | |
| 210 default: | |
| 211 break; | |
| 212 } | |
| 213 | |
| 214 if ( ! result ) | |
| 215 ret = NULL; | |
| 216 else | |
| 217 ret = waitinfo.addr; | |
| 218 | |
| 219 CYG_ASSERT( (!result) || (NULL != ret), "Good result but no alloc!" ); | |
| 220 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 221 CYG_REPORT_RETVAL( ret ); | |
| 222 return ret; | |
| 223 } | |
| 224 #endif | |
| 225 | |
| 226 // ------------------------------------------------------------------------- | |
| 227 // get some memory, return NULL if none available | |
| 228 template <class T> | |
| 229 inline cyg_uint8 * | |
| 230 Cyg_Mempolt2<T>::try_alloc( cyg_int32 size ) | |
| 231 { | |
| 232 CYG_REPORT_FUNCTION(); | |
| 233 | |
| 234 // Prevent preemption | |
| 235 Cyg_Scheduler::lock(); | |
| 236 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 237 | |
| 238 cyg_uint8 *ret = pool.alloc( size ); | |
| 239 | |
| 240 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 241 | |
| 242 // Unlock the scheduler and maybe switch threads | |
| 243 Cyg_Scheduler::unlock(); | |
| 244 return ret; | |
| 245 } | |
| 246 | |
| 247 | |
| 248 // ------------------------------------------------------------------------- | |
| 249 // free the memory back to the pool | |
| 250 template <class T> | |
| 251 cyg_bool | |
| 252 Cyg_Mempolt2<T>::free( cyg_uint8 *p, cyg_int32 size ) | |
| 253 { | |
| 254 CYG_REPORT_FUNCTION(); | |
| 255 // Prevent preemption | |
| 256 Cyg_Scheduler::lock(); | |
| 257 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 258 | |
| 259 cyg_int32 ret = pool.free( p, size ); | |
| 260 | |
| 261 // anyone waiting? | |
| 262 if ( !(queue.empty()) ) { | |
| 263 Mempolt2WaitInfo *p; | |
| 264 Cyg_Thread *thread; | |
| 265 | |
| 266 #ifdef CYGIMP_MEM_T_ONEFREE_TO_ONEALLOC | |
| 267 thread = queue.dequeue(); | |
| 268 p = (Mempolt2WaitInfo *)(thread->get_wait_info()); | |
| 269 CYG_ASSERT( NULL == p->addr, "Thread already awoken?" ); | |
| 270 | |
| 271 cyg_uint8 *mem; | |
| 272 mem = pool.alloc( p->size ); | |
| 273 CYG_ASSERT( NULL != mem, "That should have succeeded" ); | |
| 274 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 275 thread->wake(); | |
| 276 // return the successful value to it | |
| 277 p->addr = mem; | |
| 278 #else | |
| 279 Cyg_ThreadQueue holding; | |
| 280 do { | |
| 281 thread = queue.dequeue(); | |
| 282 p = (Mempolt2WaitInfo *)(thread->get_wait_info()); | |
| 283 CYG_ASSERT( NULL == p->addr, "Thread already awoken?" ); | |
| 284 | |
| 285 cyg_uint8 *mem; | |
| 286 if ( NULL != (mem = pool.alloc( p->size )) ) { | |
| 287 // success! awaken the thread | |
| 288 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 289 thread->wake(); | |
| 290 // return the successful value to it | |
| 291 p->addr = mem; | |
| 292 } | |
| 293 else { | |
| 294 // preserve the entry on the holding queue | |
| 295 holding.enqueue( thread ); | |
| 296 } | |
| 297 } while ( !(queue.empty()) ); | |
| 298 | |
| 299 // Now re-queue the unaffected threads back into the pool queue | |
| 300 // (no pun intended) | |
| 301 while ( !(holding.empty()) ) { | |
| 302 queue.enqueue( holding.dequeue() ); | |
| 303 } | |
| 304 #endif // CYGIMP_MEM_T_ONEFREE_TO_ONEALLOC | |
| 305 } | |
| 306 // Unlock the scheduler and maybe switch threads | |
| 307 Cyg_Scheduler::unlock(); | |
| 308 CYG_REPORT_RETVAL( ret ); | |
| 309 return ret; | |
| 310 } | |
| 311 | |
| 312 // ------------------------------------------------------------------------- | |
| 313 // if applicable: return -1 if not fixed size | |
| 314 template <class T> | |
| 315 inline cyg_int32 | |
| 316 Cyg_Mempolt2<T>::get_blocksize() | |
| 317 { | |
| 318 // there should not be any atomicity issues here | |
| 319 return pool.get_blocksize(); | |
| 320 } | |
| 321 | |
| 322 // ------------------------------------------------------------------------- | |
| 323 // these two are obvious and generic, but need atomicity protection (maybe) | |
| 324 template <class T> | |
| 325 inline cyg_int32 | |
| 326 Cyg_Mempolt2<T>::get_totalmem() | |
| 327 { | |
| 328 // Prevent preemption | |
| 329 Cyg_Scheduler::lock(); | |
| 330 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 331 | |
| 332 cyg_int32 ret = pool.get_totalmem(); | |
| 333 | |
| 334 // Unlock the scheduler and maybe switch threads | |
| 335 Cyg_Scheduler::unlock(); | |
| 336 return ret; | |
| 337 } | |
| 338 | |
| 339 template <class T> | |
| 340 inline cyg_int32 | |
| 341 Cyg_Mempolt2<T>::get_freemem() | |
| 342 { | |
| 343 // Prevent preemption | |
| 344 Cyg_Scheduler::lock(); | |
| 345 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 346 | |
| 347 cyg_int32 ret = pool.get_freemem(); | |
| 348 | |
| 349 // Unlock the scheduler and maybe switch threads | |
| 350 Cyg_Scheduler::unlock(); | |
| 351 return ret; | |
| 352 } | |
| 353 | |
| 354 // ------------------------------------------------------------------------- | |
| 355 // get information about the construction parameters for external | |
| 356 // freeing after the destruction of the holding object | |
| 357 template <class T> | |
| 358 inline void | |
| 359 Cyg_Mempolt2<T>::get_arena( | |
| 360 cyg_uint8 * &base, cyg_int32 &size, CYG_ADDRWORD &arg_thru ) | |
| 361 { | |
| 362 // Prevent preemption | |
| 363 Cyg_Scheduler::lock(); | |
| 364 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 365 | |
| 366 pool.get_arena( base, size, arg_thru ); | |
| 367 | |
| 368 // Unlock the scheduler and maybe switch threads | |
| 369 Cyg_Scheduler::unlock(); | |
| 370 } | |
| 371 | |
| 372 // ------------------------------------------------------------------------- | |
| 373 // Return the size of the memory allocation (previously returned | |
| 374 // by alloc() or try_alloc() ) at ptr. Returns -1 if not found | |
| 375 template <class T> | |
| 376 cyg_int32 | |
| 377 Cyg_Mempolt2<T>::get_allocation_size( cyg_uint8 *ptr ) | |
| 378 { | |
| 379 cyg_int32 ret; | |
| 380 | |
| 381 // Prevent preemption | |
| 382 Cyg_Scheduler::lock(); | |
| 383 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 384 | |
| 385 ret = pool.get_allocation_size( ptr ); | |
| 386 | |
| 387 // Unlock the scheduler and maybe switch threads | |
| 388 Cyg_Scheduler::unlock(); | |
| 389 | |
| 390 return ret; | |
| 391 } | |
| 392 | |
| 393 // ------------------------------------------------------------------------- | |
| 394 // debugging/assert function | |
| 395 | |
| 396 #ifdef CYGDBG_USE_ASSERTS | |
| 397 | |
| 398 template <class T> | |
| 399 inline cyg_bool | |
| 2 | 400 Cyg_Mempolt2<T>::check_this(cyg_assert_class_zeal zeal) const |
| 0 | 401 { |
| 402 CYG_REPORT_FUNCTION(); | |
| 403 | |
| 404 if ( Cyg_Thread::DESTRUCT == Cyg_Thread::self()->get_wake_reason() ) | |
| 405 // then the whole thing is invalid, and we know it. | |
| 406 // so return OK, since this check should NOT make an error. | |
| 407 return true; | |
| 408 | |
| 409 // check that we have a non-NULL pointer first | |
| 410 if( this == NULL ) return false; | |
| 411 | |
| 412 return true; | |
| 413 } | |
| 414 #endif | |
| 415 | |
| 416 // ------------------------------------------------------------------------- | |
| 417 #endif // ifndef CYGONCE_KERNEL_MEMPOLT2_INL | |
| 418 // EOF mempolt2.inl |
