1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Malloc initialization implementation. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2007. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <rtems.h> |
---|
21 | #include <rtems/malloc.h> |
---|
22 | #include <rtems/score/wkspace.h> |
---|
23 | #include "malloc_p.h" |
---|
24 | |
---|
25 | /* FIXME: Dummy function */ |
---|
26 | #ifndef RTEMS_NEWLIB |
---|
27 | void RTEMS_Malloc_Initialize( |
---|
28 | void *heap_begin, |
---|
29 | uintptr_t heap_size, |
---|
30 | size_t sbrk_amount |
---|
31 | ) |
---|
32 | { |
---|
33 | } |
---|
34 | #else |
---|
35 | rtems_malloc_statistics_t rtems_malloc_statistics; |
---|
36 | |
---|
37 | void RTEMS_Malloc_Initialize( |
---|
38 | void *heap_begin, |
---|
39 | uintptr_t heap_size, |
---|
40 | size_t sbrk_amount |
---|
41 | ) |
---|
42 | { |
---|
43 | bool separate_areas = !rtems_configuration_get_unified_work_area(); |
---|
44 | /* |
---|
45 | * If configured, initialize the statistics support |
---|
46 | */ |
---|
47 | if ( rtems_malloc_statistics_helpers != NULL ) { |
---|
48 | (*rtems_malloc_statistics_helpers->initialize)(); |
---|
49 | } |
---|
50 | |
---|
51 | /* |
---|
52 | * Initialize the garbage collection list to start with nothing on it. |
---|
53 | */ |
---|
54 | malloc_deferred_frees_initialize(); |
---|
55 | |
---|
56 | /* |
---|
57 | * Initialize the optional sbrk support for extending the heap |
---|
58 | */ |
---|
59 | if ( rtems_malloc_sbrk_helpers != NULL ) { |
---|
60 | void *new_heap_begin = (*rtems_malloc_sbrk_helpers->initialize)( |
---|
61 | heap_begin, |
---|
62 | sbrk_amount |
---|
63 | ); |
---|
64 | |
---|
65 | heap_size -= (uintptr_t) new_heap_begin - (uintptr_t) heap_begin; |
---|
66 | heap_begin = new_heap_begin; |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | * If this system is configured to use the same heap for |
---|
71 | * the RTEMS Workspace and C Program Heap, then we need to |
---|
72 | * be very very careful about destroying the initialization |
---|
73 | * that has already been done. |
---|
74 | */ |
---|
75 | |
---|
76 | /* |
---|
77 | * If the BSP is not clearing out the workspace, then it is most likely |
---|
78 | * not clearing out the initial memory for the heap. There is no |
---|
79 | * standard supporting zeroing out the heap memory. But much code |
---|
80 | * with UNIX history seems to assume that memory malloc'ed during |
---|
81 | * initialization (before any free's) is zero'ed. This is true most |
---|
82 | * of the time under UNIX because zero'ing memory when it is first |
---|
83 | * given to a process eliminates the chance of a process seeing data |
---|
84 | * left over from another process. This would be a security violation. |
---|
85 | */ |
---|
86 | |
---|
87 | if ( separate_areas && rtems_configuration_get_do_zero_of_workspace() ) { |
---|
88 | memset( heap_begin, 0, heap_size ); |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * Unfortunately we cannot use assert if this fails because if this |
---|
93 | * has failed we do not have a heap and if we do not have a heap |
---|
94 | * STDIO cannot work because there will be no buffers. |
---|
95 | */ |
---|
96 | |
---|
97 | if ( separate_areas ) { |
---|
98 | uintptr_t status = _Protected_heap_Initialize( |
---|
99 | RTEMS_Malloc_Heap, |
---|
100 | heap_begin, |
---|
101 | heap_size, |
---|
102 | CPU_HEAP_ALIGNMENT |
---|
103 | ); |
---|
104 | if ( status == 0 ) { |
---|
105 | rtems_fatal_error_occurred( RTEMS_NO_MEMORY ); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | MSBUMP( space_available, _Protected_heap_Get_size(RTEMS_Malloc_Heap) ); |
---|
110 | } |
---|
111 | #endif |
---|