1 #ifndef CYGONCE_MEMALLOC_MEMPOLT2_INL
2 #define CYGONCE_MEMALLOC_MEMPOLT2_INL
4 //==========================================================================
8 // Mempolt2 (Memory pool template) class declarations
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
47 // Contributors: jlarmour
49 // Purpose: Define Mempolt2 class interface
50 // Description: The class defined here provides the APIs for thread-safe,
51 // kernel-savvy memory managers; make a class with the
52 // underlying allocator as the template parameter.
53 // Usage: #include <cyg/memalloc/mempolt2.hxx>
56 //####DESCRIPTIONEND####
58 //==========================================================================
60 #include <cyg/infra/cyg_ass.h> // assertion support
61 #include <cyg/infra/cyg_trac.h> // tracing support
62 #include <cyg/kernel/thread.inl> // implementation eg. Cyg_Thread::self();
63 #include <cyg/kernel/sched.inl> // implementation eg. Cyg_Scheduler::lock();
65 // -------------------------------------------------------------------------
66 // Constructor; we _require_ these arguments and just pass them through to
67 // the implementation memory pool in use.
69 Cyg_Mempolt2<T>::Cyg_Mempolt2(
72 CYG_ADDRWORD arg_thru) // Constructor
73 : pool( base, size, arg_thru )
79 Cyg_Mempolt2<T>::~Cyg_Mempolt2() // destructor
82 Cyg_Scheduler::lock();
84 while ( ! queue.empty() ) {
85 Cyg_Thread *thread = queue.dequeue();
86 thread->set_wake_reason( Cyg_Thread::DESTRUCT );
90 // Unlock the scheduler and maybe switch threads
91 Cyg_Scheduler::unlock();
94 // -------------------------------------------------------------------------
95 // get some memory; wait if none available
98 Cyg_Mempolt2<T>::alloc( cyg_int32 size )
100 CYG_REPORT_FUNCTION();
102 // Prevent preemption
103 Cyg_Scheduler::lock();
104 CYG_ASSERTCLASS( this, "Bad this pointer");
107 ret = pool.try_alloc( size );
109 Cyg_Scheduler::unlock();
110 CYG_ASSERTCLASS( this, "Bad this pointer");
111 CYG_REPORT_RETVAL( ret );
115 Cyg_Thread *self = Cyg_Thread::self();
117 Mempolt2WaitInfo waitinfo( size );
119 CYG_MEMALLOC_FAIL(size);
121 self->set_wait_info( (CYG_ADDRWORD)&waitinfo );
122 self->set_sleep_reason( Cyg_Thread::WAIT );
124 queue.enqueue( self );
126 CYG_ASSERT( 1 == Cyg_Scheduler::get_sched_lock(),
127 "Called with non-zero scheduler lock");
129 // Unlock scheduler and allow other threads to run
130 Cyg_Scheduler::unlock();
132 cyg_bool result = true; // just used as a flag here
133 switch( self->get_wake_reason() )
135 case Cyg_Thread::DESTRUCT:
136 case Cyg_Thread::BREAK:
140 case Cyg_Thread::EXIT:
153 CYG_ASSERT( (!result) || (NULL != ret), "Good result but no alloc!" );
154 CYG_ASSERTCLASS( this, "Bad this pointer");
155 CYG_REPORT_RETVAL( ret );
159 #ifdef CYGFUN_KERNEL_THREADS_TIMER
160 // -------------------------------------------------------------------------
161 // get some memory with a timeout
164 Cyg_Mempolt2<T>::alloc( cyg_int32 size, cyg_tick_count abs_timeout )
166 CYG_REPORT_FUNCTION();
168 // Prevent preemption
169 Cyg_Scheduler::lock();
170 CYG_ASSERTCLASS( this, "Bad this pointer");
173 ret = pool.try_alloc( size );
175 Cyg_Scheduler::unlock();
176 CYG_ASSERTCLASS( this, "Bad this pointer");
177 CYG_REPORT_RETVAL( ret );
181 Cyg_Thread *self = Cyg_Thread::self();
183 Mempolt2WaitInfo waitinfo( size );
185 self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT );
187 // If the timeout is in the past, the wake reason will have been set to
188 // something other than NONE already. If so, skip the wait and go
189 // straight to unlock.
191 if( Cyg_Thread::NONE == self->get_wake_reason() ) {
193 CYG_MEMALLOC_FAIL(size);
195 self->set_wait_info( (CYG_ADDRWORD)&waitinfo );
197 queue.enqueue( self );
200 CYG_ASSERT( 1 == Cyg_Scheduler::get_sched_lock(),
201 "Called with non-zero scheduler lock");
203 // Unlock scheduler and allow other threads to run
204 Cyg_Scheduler::unlock();
206 // clear the timer; if it actually fired, no worries.
209 cyg_bool result = true; // just used as a flag here
210 switch( self->get_wake_reason() )
212 case Cyg_Thread::TIMEOUT:
216 case Cyg_Thread::DESTRUCT:
217 case Cyg_Thread::BREAK:
221 case Cyg_Thread::EXIT:
234 CYG_ASSERT( (!result) || (NULL != ret), "Good result but no alloc!" );
235 CYG_ASSERTCLASS( this, "Bad this pointer");
236 CYG_REPORT_RETVAL( ret );
241 // -------------------------------------------------------------------------
242 // get some memory, return NULL if none available
245 Cyg_Mempolt2<T>::try_alloc( cyg_int32 size )
247 CYG_REPORT_FUNCTION();
249 // Prevent preemption
250 Cyg_Scheduler::lock();
251 CYG_ASSERTCLASS( this, "Bad this pointer");
253 cyg_uint8 *ret = pool.try_alloc( size );
255 CYG_ASSERTCLASS( this, "Bad this pointer");
257 // Unlock the scheduler and maybe switch threads
258 Cyg_Scheduler::unlock();
260 CYG_MEMALLOC_FAIL_TEST(ret==NULL, size);
266 // -------------------------------------------------------------------------
267 // resize existing allocation, if oldsize is non-NULL, previous
268 // allocation size is placed into it. If previous size not available,
269 // it is set to 0. NB previous allocation size may have been rounded up.
270 // Occasionally the allocation can be adjusted *backwards* as well as,
271 // or instead of forwards, therefore the address of the resized
272 // allocation is returned, or NULL if no resizing was possible.
273 // Note that this differs from ::realloc() in that no attempt is
274 // made to call malloc() if resizing is not possible - that is left
275 // to higher layers. The data is copied from old to new though.
276 // The effects of alloc_ptr==NULL or newsize==0 are undefined
279 Cyg_Mempolt2<T>::resize_alloc( cyg_uint8 *alloc_ptr, cyg_int32 newsize,
282 CYG_REPORT_FUNCTION();
284 // Prevent preemption
285 Cyg_Scheduler::lock();
286 CYG_ASSERTCLASS( this, "Bad this pointer");
288 cyg_uint8 *ret = pool.resize_alloc( alloc_ptr, newsize, oldsize );
290 CYG_ASSERTCLASS( this, "Bad this pointer");
292 // Unlock the scheduler and maybe switch threads
293 Cyg_Scheduler::unlock();
295 CYG_MEMALLOC_FAIL_TEST(ret==NULL, newsize);
301 // -------------------------------------------------------------------------
302 // free the memory back to the pool
305 Cyg_Mempolt2<T>::free( cyg_uint8 *p, cyg_int32 size )
307 CYG_REPORT_FUNCTION();
308 // Prevent preemption
309 Cyg_Scheduler::lock();
310 CYG_ASSERTCLASS( this, "Bad this pointer");
312 cyg_int32 ret = pool.free( p, size );
315 if ( !(queue.empty()) ) {
319 #ifdef CYGIMP_MEM_T_ONEFREE_TO_ONEALLOC
320 thread = queue.dequeue();
321 p = (Mempolt2WaitInfo *)(thread->get_wait_info());
322 CYG_ASSERT( NULL == p->addr, "Thread already awoken?" );
325 mem = pool.try_alloc( p->size );
326 CYG_ASSERT( NULL != mem, "That should have succeeded" );
327 thread->set_wake_reason( Cyg_Thread::DONE );
329 // return the successful value to it
332 Cyg_ThreadQueue holding;
334 thread = queue.dequeue();
335 p = (Mempolt2WaitInfo *)(thread->get_wait_info());
336 CYG_ASSERT( NULL == p->addr, "Thread already awoken?" );
339 if ( NULL != (mem = pool.try_alloc( p->size )) ) {
340 // success! awaken the thread
341 thread->set_wake_reason( Cyg_Thread::DONE );
343 // return the successful value to it
347 // preserve the entry on the holding queue
348 holding.enqueue( thread );
350 } while ( !(queue.empty()) );
352 // Now re-queue the unaffected threads back into the pool queue
354 while ( !(holding.empty()) ) {
355 queue.enqueue( holding.dequeue() );
357 #endif // CYGIMP_MEM_T_ONEFREE_TO_ONEALLOC
359 // Unlock the scheduler and maybe switch threads
360 Cyg_Scheduler::unlock();
361 CYG_REPORT_RETVAL( ret );
365 // -------------------------------------------------------------------------
366 // Get memory pool status
367 // Needs atomicity protection (maybe)
370 Cyg_Mempolt2<T>::get_status( cyg_mempool_status_flag_t flags,
371 Cyg_Mempool_Status &status )
373 // Prevent preemption
374 Cyg_Scheduler::lock();
375 CYG_ASSERTCLASS( this, "Bad this pointer");
377 if (0 != (flags & CYG_MEMPOOL_STAT_WAITING)) {
378 status.waiting = (0 == queue.empty());
380 pool.get_status(flags, status);
382 // Unlock the scheduler and maybe switch threads
383 Cyg_Scheduler::unlock();
386 // -------------------------------------------------------------------------
387 // debugging/assert function
389 #ifdef CYGDBG_USE_ASSERTS
393 Cyg_Mempolt2<T>::check_this(cyg_assert_class_zeal zeal) const
395 CYG_REPORT_FUNCTION();
397 if ( Cyg_Thread::DESTRUCT == Cyg_Thread::self()->get_wake_reason() )
398 // then the whole thing is invalid, and we know it.
399 // so return OK, since this check should NOT make an error.
402 // check that we have a non-NULL pointer first
403 if( this == NULL ) return false;
409 // -------------------------------------------------------------------------
410 #endif // ifndef CYGONCE_MEMALLOC_MEMPOLT2_INL