|
0
|
1 //======================================================================== |
|
|
2 // |
|
|
3 // malloc.cxx |
|
|
4 // |
|
2
|
5 // Implementation of ISO C memory allocation routines |
|
0
|
6 // |
|
|
7 //======================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //======================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour |
|
|
34 // Date: 1999-01-21 |
|
|
35 // Purpose: Provides ISO C calloc(), malloc(), realloc() and free() |
|
|
36 // functions |
|
|
37 // Description: Implementation of ANSI standard allocation routines as per |
|
|
38 // ISO C section 7.10.3 |
|
0
|
39 // Usage: |
|
|
40 // |
|
|
41 //####DESCRIPTIONEND#### |
|
|
42 // |
|
|
43 //======================================================================== |
|
|
44 |
|
|
45 // CONFIGURATION |
|
|
46 |
|
|
47 #include <pkgconf/libc.h> // Configuration header |
|
|
48 |
|
2
|
49 // Do we want these functions? |
|
0
|
50 #ifdef CYGPKG_LIBC_MALLOC |
|
|
51 |
|
|
52 // INCLUDES |
|
|
53 |
|
|
54 #include <cyg/infra/cyg_type.h> // Common type definitions and support |
|
|
55 #include <cyg/infra/cyg_trac.h> // Common tracing support |
|
|
56 #include <cyg/infra/cyg_ass.h> // Common assertion support |
|
|
57 #include <string.h> // For memset() and memcpy() |
|
|
58 #include <stdlib.h> // header for this file |
|
2
|
59 #include <pkgconf/kernel.h> // kernel configuration |
|
0
|
60 #include <cyg/kernel/memvar.hxx> // Kernel variable size block allocator |
|
|
61 |
|
|
62 #include "clibincl/stdlibsupp.hxx" // Additional support for stdlib stuff |
|
|
63 // internal to the C library |
|
|
64 |
|
|
65 // EXPORTED SYMBOLS |
|
|
66 |
|
|
67 externC void * |
|
2
|
68 calloc( size_t, size_t ) CYGBLD_ATTRIB_WEAK_ALIAS(_calloc); |
|
0
|
69 |
|
|
70 externC void |
|
2
|
71 free( void * ) CYGBLD_ATTRIB_WEAK_ALIAS(_free); |
|
0
|
72 |
|
|
73 externC void * |
|
2
|
74 malloc( size_t ) CYGBLD_ATTRIB_WEAK_ALIAS(_malloc); |
|
0
|
75 |
|
|
76 externC void * |
|
2
|
77 realloc( void *, size_t ) CYGBLD_ATTRIB_WEAK_ALIAS(_realloc); |
|
0
|
78 |
|
|
79 |
|
|
80 // STATIC VARIABLES |
|
|
81 |
|
2
|
82 // the data space for the memory pool - can be externally overriden |
|
|
83 // FIXME: but should be in different file for this to work |
|
|
84 cyg_uint8 cyg_libc_malloc_memorypool[ CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE ] |
|
|
85 CYGBLD_ATTRIB_WEAK; |
|
0
|
86 |
|
|
87 // the memory pool object itself |
|
|
88 static Cyg_Mempool_Variable pool CYG_INIT_PRIORITY( LIBC ) = |
|
2
|
89 Cyg_Mempool_Variable( cyg_libc_malloc_memorypool, |
|
|
90 CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE ); |
|
0
|
91 |
|
|
92 |
|
|
93 // FUNCTIONS |
|
|
94 |
|
|
95 void * |
|
|
96 _malloc( size_t size ) |
|
|
97 { |
|
|
98 void *data_ptr; |
|
|
99 |
|
|
100 CYG_REPORT_FUNCNAMETYPE( "_malloc", "returning pointer %08x" ); |
|
|
101 |
|
|
102 CYG_REPORT_FUNCARG1DV( size ); |
|
|
103 |
|
|
104 // first check if size wanted is 0 |
|
|
105 if (!size) { |
|
|
106 CYG_REPORT_RETVAL( NULL ); |
|
|
107 return NULL; |
|
|
108 } // if |
|
|
109 |
|
|
110 // ask the pool for the data |
|
|
111 data_ptr = pool.try_alloc( size ); |
|
|
112 |
|
|
113 // if it isn't NULL is the pointer valid? |
|
|
114 if (data_ptr != NULL) { |
|
|
115 CYG_CHECK_DATA_PTR( data_ptr, |
|
|
116 "allocator returned invalid pointer!" ); |
|
|
117 |
|
|
118 // And just check its alignment |
|
|
119 CYG_ASSERT( !((CYG_ADDRWORD)data_ptr & (sizeof(CYG_ADDRWORD) - 1)), |
|
|
120 "Allocator has returned badly aligned data!"); |
|
|
121 } // if |
|
|
122 |
|
|
123 CYG_REPORT_RETVAL( data_ptr ); |
|
|
124 |
|
|
125 return data_ptr; |
|
|
126 } // _malloc() |
|
|
127 |
|
|
128 |
|
|
129 void |
|
|
130 _free( void *ptr ) |
|
|
131 { |
|
|
132 CYG_REPORT_FUNCNAME( "_free"); |
|
|
133 |
|
|
134 CYG_REPORT_FUNCARG1XV( ptr ); |
|
|
135 |
|
|
136 // if null pointer, do nothing as per spec |
|
|
137 if (ptr==NULL) |
|
|
138 return; |
|
|
139 |
|
|
140 CYG_CHECK_DATA_PTR( ptr, "Pointer to free isn't even valid!" ); |
|
|
141 |
|
|
142 // get pool to free it. Use size==0 for pool to determine the size |
|
|
143 // Also ignore return value - there's nothing we can do with it |
|
|
144 pool.free( (cyg_uint8 *) ptr, 0 ); |
|
|
145 |
|
|
146 CYG_REPORT_RETURN(); |
|
|
147 |
|
|
148 } // _free() |
|
|
149 |
|
|
150 |
|
|
151 void * |
|
|
152 _calloc( size_t nmemb, size_t size ) |
|
|
153 { |
|
|
154 void *data_ptr; |
|
|
155 cyg_ucount32 realsize; |
|
|
156 |
|
|
157 CYG_REPORT_FUNCNAMETYPE( "_calloc", "returning pointer %08x" ); |
|
|
158 |
|
|
159 CYG_REPORT_FUNCARG2DV( nmemb, size ); |
|
|
160 |
|
|
161 realsize = nmemb * size; |
|
|
162 |
|
|
163 data_ptr = _malloc( realsize ); |
|
|
164 |
|
|
165 // Fill with 0's if non-NULL |
|
|
166 if (data_ptr != NULL) |
|
|
167 memset( data_ptr, 0, realsize ); |
|
|
168 |
|
|
169 CYG_REPORT_RETVAL( data_ptr ); |
|
|
170 return data_ptr; |
|
|
171 } // _calloc() |
|
|
172 |
|
|
173 |
|
|
174 externC void * |
|
|
175 _realloc( void *ptr, size_t size ) |
|
|
176 { |
|
|
177 void *ret_ptr; |
|
|
178 cyg_int32 oldsize; |
|
|
179 |
|
|
180 CYG_REPORT_FUNCNAMETYPE( "_realloc", "returning pointer %08x" ); |
|
|
181 |
|
|
182 CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size ); |
|
|
183 |
|
|
184 // if pointer is NULL, we must malloc it |
|
|
185 if (ptr == NULL) { |
|
|
186 ret_ptr = _malloc( size ); |
|
|
187 CYG_REPORT_RETVAL( ret_ptr ); |
|
|
188 return ret_ptr; |
|
|
189 } // if |
|
|
190 |
|
|
191 CYG_CHECK_DATA_PTR( ptr, "_realloc() passed a bogus pointer!" ); |
|
|
192 |
|
|
193 // if size is 0, we must free it |
|
|
194 if (size == 0) { |
|
|
195 _free(ptr); |
|
|
196 CYG_REPORT_RETVAL( NULL ); |
|
|
197 return NULL; |
|
|
198 } // if |
|
|
199 |
|
|
200 |
|
|
201 oldsize = pool.get_allocation_size( (cyg_uint8 *)ptr ); |
|
|
202 |
|
|
203 CYG_ASSERT( oldsize != -1, |
|
|
204 "_realloc() couldn't find allocation size!" ); |
|
|
205 |
|
|
206 ret_ptr = _malloc( size ); |
|
|
207 |
|
|
208 // as long as malloc succeeded |
|
|
209 if (ret_ptr != NULL) { |
|
|
210 // copy old contents |
|
|
211 memcpy( ret_ptr, ptr, size < (size_t) oldsize ? size |
|
|
212 : (size_t) oldsize ); |
|
|
213 |
|
|
214 _free(ptr); |
|
|
215 } // if |
|
|
216 |
|
|
217 CYG_REPORT_RETVAL( ret_ptr ); |
|
|
218 return ret_ptr; |
|
|
219 } // _realloc() |
|
|
220 |
|
|
221 #endif // ifdef CYGPKG_LIBC_MALLOC |
|
|
222 |
|
|
223 // EOF malloc.cxx |