1 | /* |
---|
2 | * RTEMS Malloc -- SBRK Support Plugin |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2007. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <unistd.h> /* sbrk */ |
---|
19 | |
---|
20 | #include <rtems.h> |
---|
21 | #include <rtems/malloc.h> |
---|
22 | #include "malloc_p.h" |
---|
23 | |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | size_t RTEMS_Malloc_Sbrk_amount; |
---|
27 | |
---|
28 | void *malloc_sbrk_initialize( |
---|
29 | void *starting_address, |
---|
30 | size_t length |
---|
31 | ) |
---|
32 | { |
---|
33 | uintptr_t old_address; |
---|
34 | uintptr_t uaddress; |
---|
35 | |
---|
36 | RTEMS_Malloc_Sbrk_amount = length; |
---|
37 | |
---|
38 | /* |
---|
39 | * If the starting address is 0 then we are to attempt to |
---|
40 | * get length worth of memory using sbrk. Make sure we |
---|
41 | * align the address that we get back. |
---|
42 | */ |
---|
43 | |
---|
44 | if (!starting_address) { |
---|
45 | uaddress = (uintptr_t)sbrk(length); |
---|
46 | |
---|
47 | if (uaddress == (uintptr_t) -1) { |
---|
48 | rtems_fatal_error_occurred( RTEMS_NO_MEMORY ); |
---|
49 | /* DOES NOT RETURN!!! */ |
---|
50 | } |
---|
51 | |
---|
52 | if (uaddress & (CPU_HEAP_ALIGNMENT-1)) { |
---|
53 | old_address = uaddress; |
---|
54 | uaddress = (uaddress + CPU_HEAP_ALIGNMENT) & ~(CPU_HEAP_ALIGNMENT-1); |
---|
55 | |
---|
56 | /* |
---|
57 | * adjust the length by whatever we aligned by |
---|
58 | */ |
---|
59 | length -= uaddress - old_address; |
---|
60 | } |
---|
61 | |
---|
62 | starting_address = (void *)uaddress; |
---|
63 | } |
---|
64 | return starting_address; |
---|
65 | } |
---|
66 | |
---|
67 | void *malloc_sbrk_extend_and_allocate( |
---|
68 | size_t size |
---|
69 | ) |
---|
70 | { |
---|
71 | uint32_t sbrk_amount; |
---|
72 | void *starting_address; |
---|
73 | uint32_t the_size; |
---|
74 | void *return_this; |
---|
75 | |
---|
76 | /* |
---|
77 | * Round to the "requested sbrk amount" so hopefully we won't have |
---|
78 | * to grow again for a while. This effectively does sbrk() calls |
---|
79 | * in "page" amounts. |
---|
80 | */ |
---|
81 | |
---|
82 | sbrk_amount = RTEMS_Malloc_Sbrk_amount; |
---|
83 | |
---|
84 | if ( sbrk_amount == 0 ) |
---|
85 | return (void *) 0; |
---|
86 | |
---|
87 | the_size = ((size + sbrk_amount) / sbrk_amount * sbrk_amount); |
---|
88 | |
---|
89 | starting_address = (void *) sbrk(the_size); |
---|
90 | if ( starting_address == (void*) -1 ) |
---|
91 | return (void *) 0; |
---|
92 | |
---|
93 | if ( !_Protected_heap_Extend( |
---|
94 | RTEMS_Malloc_Heap, starting_address, the_size) ) { |
---|
95 | sbrk(-the_size); |
---|
96 | errno = ENOMEM; |
---|
97 | return (void *) 0; |
---|
98 | } |
---|
99 | |
---|
100 | MSBUMP(space_available, the_size); |
---|
101 | |
---|
102 | return_this = _Protected_heap_Allocate( RTEMS_Malloc_Heap, size ); |
---|
103 | return return_this; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | rtems_malloc_sbrk_functions_t rtems_malloc_sbrk_helpers_table = { |
---|
108 | malloc_sbrk_initialize, |
---|
109 | malloc_sbrk_extend_and_allocate |
---|
110 | }; |
---|
111 | |
---|
112 | |
---|