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 | Chain_Node *to_be_freed; |
---|
31 | |
---|
32 | MSBUMP(malloc_calls, 1); |
---|
33 | |
---|
34 | /* |
---|
35 | * If some free's have been deferred, then do them now. |
---|
36 | */ |
---|
37 | malloc_process_deferred_frees(); |
---|
38 | |
---|
39 | /* |
---|
40 | * Validate the parameters |
---|
41 | */ |
---|
42 | if ( !size ) |
---|
43 | return (void *) 0; |
---|
44 | |
---|
45 | /* |
---|
46 | * Do not attempt to allocate memory if not in correct system state. |
---|
47 | */ |
---|
48 | if ( _System_state_Is_up(_System_state_Get()) && |
---|
49 | !malloc_is_system_state_OK() ) |
---|
50 | return NULL; |
---|
51 | |
---|
52 | /* |
---|
53 | * Walk the heap and verify its integrity |
---|
54 | */ |
---|
55 | #if defined(RTEMS_HEAP_DEBUG) |
---|
56 | _Protected_heap_Walk( &RTEMS_Malloc_Heap, 0, FALSE ); |
---|
57 | #endif |
---|
58 | |
---|
59 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
60 | /* |
---|
61 | * If the support for a boundary area at the end of the heap |
---|
62 | * block allocated is turned on, then adjust the size. |
---|
63 | */ |
---|
64 | if (rtems_malloc_boundary_helpers) |
---|
65 | size += (*rtems_malloc_boundary_helpers->overhead)(); |
---|
66 | #endif |
---|
67 | |
---|
68 | /* |
---|
69 | * Try to give a segment in the current heap if there is not |
---|
70 | * enough space then try to grow the heap. |
---|
71 | * If this fails then return a NULL pointer. |
---|
72 | */ |
---|
73 | |
---|
74 | return_this = _Protected_heap_Allocate( &RTEMS_Malloc_Heap, size ); |
---|
75 | |
---|
76 | if ( !return_this ) { |
---|
77 | if (rtems_malloc_sbrk_helpers) |
---|
78 | return_this = (*rtems_malloc_sbrk_helpers->extend)( size ); |
---|
79 | if ( !return_this ) { |
---|
80 | errno = ENOMEM; |
---|
81 | return (void *) 0; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /* |
---|
86 | * If the user wants us to dirty the allocated memory, then do it. |
---|
87 | */ |
---|
88 | #ifdef MALLOC_DIRTY |
---|
89 | (void) memset(return_this, 0xCF, size); |
---|
90 | #endif |
---|
91 | |
---|
92 | /* |
---|
93 | * If configured, update the statistics |
---|
94 | */ |
---|
95 | if ( rtems_malloc_statistics_helpers ) |
---|
96 | (*rtems_malloc_statistics_helpers->at_malloc)(return_this); |
---|
97 | |
---|
98 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
99 | /* |
---|
100 | * If configured, set the boundary area |
---|
101 | */ |
---|
102 | if (rtems_malloc_boundary_helpers) |
---|
103 | (*rtems_malloc_boundary_helpers->at_malloc)(return_this, size); |
---|
104 | #endif |
---|
105 | |
---|
106 | return return_this; |
---|
107 | } |
---|
108 | |
---|
109 | #endif |
---|