1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup libcsupport |
---|
5 | * |
---|
6 | * @brief calloc() |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2007. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifdef HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #ifdef RTEMS_NEWLIB |
---|
23 | #include "malloc_p.h" |
---|
24 | #include <stdlib.h> |
---|
25 | |
---|
26 | #include <rtems/chain.h> |
---|
27 | |
---|
28 | static RTEMS_CHAIN_DEFINE_EMPTY( _Malloc_GC_list ); |
---|
29 | |
---|
30 | RTEMS_INTERRUPT_LOCK_DEFINE( static, _Malloc_GC_lock, "Malloc GC" ) |
---|
31 | |
---|
32 | static void *_Malloc_Get_deferred_free( void ) |
---|
33 | { |
---|
34 | rtems_interrupt_lock_context lock_context; |
---|
35 | void *p; |
---|
36 | |
---|
37 | rtems_interrupt_lock_acquire( &_Malloc_GC_lock, &lock_context ); |
---|
38 | p = rtems_chain_get_unprotected( &_Malloc_GC_list ); |
---|
39 | rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context ); |
---|
40 | |
---|
41 | return p; |
---|
42 | } |
---|
43 | |
---|
44 | void _Malloc_Process_deferred_frees( void ) |
---|
45 | { |
---|
46 | rtems_chain_node *to_be_freed; |
---|
47 | |
---|
48 | /* |
---|
49 | * If some free's have been deferred, then do them now. |
---|
50 | */ |
---|
51 | while ( ( to_be_freed = _Malloc_Get_deferred_free() ) != NULL ) { |
---|
52 | free( to_be_freed ); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | static void _Malloc_Deferred_free( void *p ) |
---|
57 | { |
---|
58 | rtems_interrupt_lock_context lock_context; |
---|
59 | rtems_chain_node *node; |
---|
60 | |
---|
61 | node = (rtems_chain_node *) p; |
---|
62 | |
---|
63 | rtems_interrupt_lock_acquire( &_Malloc_GC_lock, &lock_context ); |
---|
64 | rtems_chain_initialize_node( node ); |
---|
65 | rtems_chain_append_unprotected( &_Malloc_GC_list, node ); |
---|
66 | rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context ); |
---|
67 | } |
---|
68 | |
---|
69 | void free( |
---|
70 | void *ptr |
---|
71 | ) |
---|
72 | { |
---|
73 | if ( !ptr ) |
---|
74 | return; |
---|
75 | |
---|
76 | /* |
---|
77 | * Do not attempt to free memory if in a critical section or ISR. |
---|
78 | */ |
---|
79 | if ( _Malloc_System_state() != MALLOC_SYSTEM_STATE_NORMAL ) { |
---|
80 | _Malloc_Deferred_free(ptr); |
---|
81 | return; |
---|
82 | } |
---|
83 | |
---|
84 | if ( !_Protected_heap_Free( RTEMS_Malloc_Heap, ptr ) ) { |
---|
85 | rtems_fatal( RTEMS_FATAL_SOURCE_INVALID_HEAP_FREE, (rtems_fatal_code) ptr ); |
---|
86 | } |
---|
87 | } |
---|
88 | #endif |
---|