1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief RTEMS Malloc Family Implementation |
---|
5 | * @ingroup libcsupport |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2007. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #ifdef RTEMS_NEWLIB |
---|
22 | #include <stdlib.h> |
---|
23 | #include <errno.h> |
---|
24 | |
---|
25 | #include "malloc_p.h" |
---|
26 | |
---|
27 | #include <rtems/score/sysstate.h> |
---|
28 | |
---|
29 | void *malloc( |
---|
30 | size_t size |
---|
31 | ) |
---|
32 | { |
---|
33 | void *return_this; |
---|
34 | |
---|
35 | MSBUMP(malloc_calls, 1); |
---|
36 | |
---|
37 | /* |
---|
38 | * If some free's have been deferred, then do them now. |
---|
39 | */ |
---|
40 | malloc_deferred_frees_process(); |
---|
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 | * Try to give a segment in the current heap if there is not |
---|
57 | * enough space then try to grow the heap. |
---|
58 | * If this fails then return a NULL pointer. |
---|
59 | */ |
---|
60 | |
---|
61 | return_this = _Protected_heap_Allocate( RTEMS_Malloc_Heap, size ); |
---|
62 | |
---|
63 | if ( !return_this ) { |
---|
64 | return_this = (*rtems_malloc_extend_handler)( RTEMS_Malloc_Heap, size ); |
---|
65 | if ( !return_this ) { |
---|
66 | errno = ENOMEM; |
---|
67 | return (void *) 0; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /* |
---|
72 | * If the user wants us to dirty the allocated memory, then do it. |
---|
73 | */ |
---|
74 | if ( rtems_malloc_dirty_helper ) |
---|
75 | (*rtems_malloc_dirty_helper)( return_this, size ); |
---|
76 | |
---|
77 | /* |
---|
78 | * If configured, update the statistics |
---|
79 | */ |
---|
80 | if ( rtems_malloc_statistics_helpers ) |
---|
81 | (*rtems_malloc_statistics_helpers->at_malloc)(return_this); |
---|
82 | |
---|
83 | return return_this; |
---|
84 | } |
---|
85 | |
---|
86 | #endif |
---|