1 | /* |
---|
2 | * RTEMS Malloc Family Implementation |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2007. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #ifdef RTEMS_NEWLIB |
---|
20 | #include <stdlib.h> |
---|
21 | #include <errno.h> |
---|
22 | |
---|
23 | #include "malloc_p.h" |
---|
24 | |
---|
25 | void *malloc( |
---|
26 | size_t size |
---|
27 | ) |
---|
28 | { |
---|
29 | void *return_this; |
---|
30 | void *starting_address; |
---|
31 | uint32_t the_size; |
---|
32 | uint32_t sbrk_amount; |
---|
33 | Chain_Node *to_be_freed; |
---|
34 | |
---|
35 | MSBUMP(malloc_calls, 1); |
---|
36 | |
---|
37 | /* |
---|
38 | * If some free's have been deferred, then do them now. |
---|
39 | */ |
---|
40 | malloc_process_deferred_frees(); |
---|
41 | |
---|
42 | /* |
---|
43 | * Validate the parameters |
---|
44 | */ |
---|
45 | if ( !size ) |
---|
46 | return (void *) 0; |
---|
47 | |
---|
48 | /* |
---|
49 | * Do not attempt to allocate memory if not in correct system state. |
---|
50 | */ |
---|
51 | if ( _System_state_Is_up(_System_state_Get()) && |
---|
52 | !malloc_is_system_state_OK() ) |
---|
53 | return NULL; |
---|
54 | |
---|
55 | /* |
---|
56 | * Walk the heap and verify its integrity |
---|
57 | */ |
---|
58 | #if defined(RTEMS_HEAP_DEBUG) |
---|
59 | _Protected_heap_Walk( &RTEMS_Malloc_Heap, 0, FALSE ); |
---|
60 | #endif |
---|
61 | |
---|
62 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
63 | /* |
---|
64 | * If the support for a boundary area at the end of the heap |
---|
65 | * block allocated is turned on, then adjust the size. |
---|
66 | */ |
---|
67 | if (rtems_malloc_boundary_helpers) |
---|
68 | size += (*rtems_malloc_boundary_helpers->overhead)(); |
---|
69 | #endif |
---|
70 | |
---|
71 | /* |
---|
72 | * Try to give a segment in the current heap if there is not |
---|
73 | * enough space then try to grow the heap. |
---|
74 | * If this fails then return a NULL pointer. |
---|
75 | */ |
---|
76 | |
---|
77 | return_this = _Protected_heap_Allocate( &RTEMS_Malloc_Heap, size ); |
---|
78 | |
---|
79 | if ( !return_this ) { |
---|
80 | /* |
---|
81 | * Round to the "requested sbrk amount" so hopefully we won't have |
---|
82 | * to grow again for a while. This effectively does sbrk() calls |
---|
83 | * in "page" amounts. |
---|
84 | */ |
---|
85 | |
---|
86 | sbrk_amount = RTEMS_Malloc_Sbrk_amount; |
---|
87 | |
---|
88 | if ( sbrk_amount == 0 ) |
---|
89 | return (void *) 0; |
---|
90 | |
---|
91 | the_size = ((size + sbrk_amount) / sbrk_amount * sbrk_amount); |
---|
92 | |
---|
93 | if ((starting_address = (void *)sbrk(the_size)) |
---|
94 | == (void*) -1) |
---|
95 | return (void *) 0; |
---|
96 | |
---|
97 | if ( !_Protected_heap_Extend( |
---|
98 | &RTEMS_Malloc_Heap, starting_address, the_size) ) { |
---|
99 | sbrk(-the_size); |
---|
100 | errno = ENOMEM; |
---|
101 | return (void *) 0; |
---|
102 | } |
---|
103 | |
---|
104 | MSBUMP(space_available, the_size); |
---|
105 | |
---|
106 | return_this = _Protected_heap_Allocate( &RTEMS_Malloc_Heap, size ); |
---|
107 | if ( !return_this ) { |
---|
108 | errno = ENOMEM; |
---|
109 | return (void *) 0; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /* |
---|
114 | * If the user wants us to dirty the allocated memory, then do it. |
---|
115 | */ |
---|
116 | #ifdef MALLOC_DIRTY |
---|
117 | (void) memset(return_this, 0xCF, size); |
---|
118 | #endif |
---|
119 | |
---|
120 | /* |
---|
121 | * If configured, update the statistics |
---|
122 | */ |
---|
123 | if ( rtems_malloc_statistics_helpers ) |
---|
124 | (*rtems_malloc_statistics_helpers->at_malloc)(return_this); |
---|
125 | |
---|
126 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
127 | /* |
---|
128 | * If configured, set the boundary area |
---|
129 | */ |
---|
130 | if (rtems_malloc_boundary_helpers) |
---|
131 | (*rtems_malloc_boundary_helpers->at_malloc)(return_this, size); |
---|
132 | #endif |
---|
133 | |
---|
134 | return return_this; |
---|
135 | } |
---|
136 | |
---|
137 | #endif |
---|