From aab4664d77ba0d2caa2a4d73919356cca366ce4e Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Wed, 22 Oct 2003 16:53:55 +0000 Subject: 2003-10-22 Joel Sherrill PR 511/filesystem * src/malloc.c: Add deferred free and protect against C Program Heap operations while in a dispatch disable critical section or ISR. --- cpukit/libcsupport/ChangeLog | 6 ++++++ cpukit/libcsupport/src/malloc.c | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) (limited to 'cpukit') diff --git a/cpukit/libcsupport/ChangeLog b/cpukit/libcsupport/ChangeLog index cedef7874e..20e3b2f9e3 100644 --- a/cpukit/libcsupport/ChangeLog +++ b/cpukit/libcsupport/ChangeLog @@ -1,3 +1,9 @@ +2003-10-22 Joel Sherrill + + PR 511/filesystem + * src/malloc.c: Add deferred free and protect against C Program Heap + operations while in a dispatch disable critical section or ISR. + 2003-10-02 Phil Torre PR 504/rtems diff --git a/cpukit/libcsupport/src/malloc.c b/cpukit/libcsupport/src/malloc.c index 22221b6da5..f80e2401cb 100644 --- a/cpukit/libcsupport/src/malloc.c +++ b/cpukit/libcsupport/src/malloc.c @@ -33,6 +33,10 @@ #include /* sbrk(2) */ +#include + +Chain_Control RTEMS_Malloc_GC_list; + rtems_id RTEMS_Malloc_Heap; size_t RTEMS_Malloc_Sbrk_amount; @@ -70,6 +74,11 @@ void RTEMS_Malloc_Initialize( rtems_unsigned32 old_address; rtems_unsigned32 u32_address; + /* + * Initialize the garbage collection list to start with nothing on it. + */ + Chain_Initialize_empty(&RTEMS_Malloc_GC_list); + /* * If the starting address is 0 then we are to attempt to * get length worth of memory using sbrk. Make sure we @@ -150,12 +159,29 @@ void *malloc( rtems_unsigned32 the_size; rtems_unsigned32 sbrk_amount; rtems_status_code status; + Chain_Node *to_be_freed; MSBUMP(malloc_calls, 1); if ( !size ) return (void *) 0; + /* + * Do not attempt to allocate memory if in a critical section or ISR. + */ + + if (_Thread_Dispatch_disable_level > 0) + return (void *) 0; + + if (_ISR_Nest_level > 0) + return (void *) 0; + + /* + * If some free's have been deferred, then do them now. + */ + while ((to_be_freed = Chain_Get(&RTEMS_Malloc_GC_list)) != NULL) + free(to_be_freed); + /* * Try to give a segment in the current region if there is not * enough space then try to grow the region using rtems_region_extend(). @@ -267,6 +293,19 @@ void *realloc( MSBUMP(realloc_calls, 1); + /* + * Do not attempt to allocate memory if in a critical section or ISR. + */ + + if (_Thread_Dispatch_disable_level > 0) + return (void *) 0; + + if (_ISR_Nest_level > 0) + return (void *) 0; + + /* + * Continue with calloc(). + */ if ( !ptr ) return malloc( size ); @@ -313,6 +352,15 @@ void free( if ( !ptr ) return; + /* + * Do not attempt to free memory if in a critical section or ISR. + */ + + if ((_Thread_Dispatch_disable_level > 0) || (_ISR_Nest_level > 0)) { + Chain_Append(&RTEMS_Malloc_GC_list, (Chain_Node *)ptr); + return; + } + #ifdef MALLOC_STATS { unsigned32 size; -- cgit v1.2.3