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