Mercurial > ecos
comparison packages/language/c/libc/current/src/stdlib/malloc.cxx @ 0:3111d98ba7b3 ecos-v1_1-release
Initial commit of eCos version 1.1
| author | jlarmour |
|---|---|
| date | Tue, 11 May 1999 11:16:07 +0000 |
| parents | |
| children | 443894e2e912 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3111d98ba7b3 |
|---|---|
| 1 //======================================================================== | |
| 2 // | |
| 3 // malloc.cxx | |
| 4 // | |
| 5 // Implementation of ANSI standard allocation routines | |
| 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 | |
| 25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. | |
| 26 // ------------------------------------------- | |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //======================================================================== | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 32 // Author(s): jlarmour@cygnus.co.uk | |
| 33 // Contributors: jlarmour@cygnus.co.uk | |
| 34 // Date: 1998-02-13 | |
| 35 // Purpose: | |
| 36 // Description: Implementation of ANSI standard allocation routines as per | |
| 37 // ANSI section 7.10.3 | |
| 38 // Usage: | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 // | |
| 42 //======================================================================== | |
| 43 | |
| 44 // CONFIGURATION | |
| 45 | |
| 46 #include <pkgconf/libc.h> // Configuration header | |
| 47 | |
| 48 // Include the C library? | |
| 49 #ifdef CYGPKG_LIBC | |
| 50 | |
| 51 // And do we want these functions? | |
| 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 | |
| 61 #include <cyg/kernel/memvar.hxx> // Kernel variable size block allocator | |
| 62 | |
| 63 #include "clibincl/stdlibsupp.hxx" // Additional support for stdlib stuff | |
| 64 // internal to the C library | |
| 65 | |
| 66 // EXPORTED SYMBOLS | |
| 67 | |
| 68 externC void * | |
| 69 calloc( size_t, size_t ) CYGPRI_LIBC_WEAK_ALIAS("_calloc"); | |
| 70 | |
| 71 externC void | |
| 72 free( void * ) CYGPRI_LIBC_WEAK_ALIAS("_free"); | |
| 73 | |
| 74 externC void * | |
| 75 malloc( size_t ) CYGPRI_LIBC_WEAK_ALIAS("_malloc"); | |
| 76 | |
| 77 externC void * | |
| 78 realloc( void *, size_t ) CYGPRI_LIBC_WEAK_ALIAS("_realloc"); | |
| 79 | |
| 80 | |
| 81 // STATIC VARIABLES | |
| 82 | |
| 83 // the data space for the memory pool | |
| 84 static cyg_uint8 pooldata[ CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE ]; | |
| 85 | |
| 86 // the memory pool object itself | |
| 87 static Cyg_Mempool_Variable pool CYG_INIT_PRIORITY( LIBC ) = | |
| 88 Cyg_Mempool_Variable( pooldata, CYGNUM_LIBC_MALLOC_MEMPOOL_SIZE ); | |
| 89 | |
| 90 | |
| 91 // FUNCTIONS | |
| 92 | |
| 93 void * | |
| 94 _malloc( size_t size ) | |
| 95 { | |
| 96 void *data_ptr; | |
| 97 | |
| 98 CYG_REPORT_FUNCNAMETYPE( "_malloc", "returning pointer %08x" ); | |
| 99 | |
| 100 CYG_REPORT_FUNCARG1DV( size ); | |
| 101 | |
| 102 // first check if size wanted is 0 | |
| 103 if (!size) { | |
| 104 CYG_REPORT_RETVAL( NULL ); | |
| 105 return NULL; | |
| 106 } // if | |
| 107 | |
| 108 // ask the pool for the data | |
| 109 data_ptr = pool.try_alloc( size ); | |
| 110 | |
| 111 // if it isn't NULL is the pointer valid? | |
| 112 if (data_ptr != NULL) { | |
| 113 CYG_CHECK_DATA_PTR( data_ptr, | |
| 114 "allocator returned invalid pointer!" ); | |
| 115 | |
| 116 // And just check its alignment | |
| 117 CYG_ASSERT( !((CYG_ADDRWORD)data_ptr & (sizeof(CYG_ADDRWORD) - 1)), | |
| 118 "Allocator has returned badly aligned data!"); | |
| 119 } // if | |
| 120 | |
| 121 CYG_REPORT_RETVAL( data_ptr ); | |
| 122 | |
| 123 return data_ptr; | |
| 124 } // _malloc() | |
| 125 | |
| 126 | |
| 127 void | |
| 128 _free( void *ptr ) | |
| 129 { | |
| 130 CYG_REPORT_FUNCNAME( "_free"); | |
| 131 | |
| 132 CYG_REPORT_FUNCARG1XV( ptr ); | |
| 133 | |
| 134 // if null pointer, do nothing as per spec | |
| 135 if (ptr==NULL) | |
| 136 return; | |
| 137 | |
| 138 CYG_CHECK_DATA_PTR( ptr, "Pointer to free isn't even valid!" ); | |
| 139 | |
| 140 // get pool to free it. Use size==0 for pool to determine the size | |
| 141 // Also ignore return value - there's nothing we can do with it | |
| 142 pool.free( (cyg_uint8 *) ptr, 0 ); | |
| 143 | |
| 144 CYG_REPORT_RETURN(); | |
| 145 | |
| 146 } // _free() | |
| 147 | |
| 148 | |
| 149 void * | |
| 150 _calloc( size_t nmemb, size_t size ) | |
| 151 { | |
| 152 void *data_ptr; | |
| 153 cyg_ucount32 realsize; | |
| 154 | |
| 155 CYG_REPORT_FUNCNAMETYPE( "_calloc", "returning pointer %08x" ); | |
| 156 | |
| 157 CYG_REPORT_FUNCARG2DV( nmemb, size ); | |
| 158 | |
| 159 realsize = nmemb * size; | |
| 160 | |
| 161 data_ptr = _malloc( realsize ); | |
| 162 | |
| 163 // Fill with 0's if non-NULL | |
| 164 if (data_ptr != NULL) | |
| 165 memset( data_ptr, 0, realsize ); | |
| 166 | |
| 167 CYG_REPORT_RETVAL( data_ptr ); | |
| 168 return data_ptr; | |
| 169 } // _calloc() | |
| 170 | |
| 171 | |
| 172 externC void * | |
| 173 _realloc( void *ptr, size_t size ) | |
| 174 { | |
| 175 void *ret_ptr; | |
| 176 cyg_int32 oldsize; | |
| 177 | |
| 178 CYG_REPORT_FUNCNAMETYPE( "_realloc", "returning pointer %08x" ); | |
| 179 | |
| 180 CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size ); | |
| 181 | |
| 182 // if pointer is NULL, we must malloc it | |
| 183 if (ptr == NULL) { | |
| 184 ret_ptr = _malloc( size ); | |
| 185 CYG_REPORT_RETVAL( ret_ptr ); | |
| 186 return ret_ptr; | |
| 187 } // if | |
| 188 | |
| 189 CYG_CHECK_DATA_PTR( ptr, "_realloc() passed a bogus pointer!" ); | |
| 190 | |
| 191 // if size is 0, we must free it | |
| 192 if (size == 0) { | |
| 193 _free(ptr); | |
| 194 CYG_REPORT_RETVAL( NULL ); | |
| 195 return NULL; | |
| 196 } // if | |
| 197 | |
| 198 | |
| 199 oldsize = pool.get_allocation_size( (cyg_uint8 *)ptr ); | |
| 200 | |
| 201 CYG_ASSERT( oldsize != -1, | |
| 202 "_realloc() couldn't find allocation size!" ); | |
| 203 | |
| 204 ret_ptr = _malloc( size ); | |
| 205 | |
| 206 // as long as malloc succeeded | |
| 207 if (ret_ptr != NULL) { | |
| 208 // copy old contents | |
| 209 memcpy( ret_ptr, ptr, size < (size_t) oldsize ? size | |
| 210 : (size_t) oldsize ); | |
| 211 | |
| 212 _free(ptr); | |
| 213 } // if | |
| 214 | |
| 215 CYG_REPORT_RETVAL( ret_ptr ); | |
| 216 return ret_ptr; | |
| 217 } // _realloc() | |
| 218 | |
| 219 #endif // ifdef CYGPKG_LIBC_MALLOC | |
| 220 | |
| 221 #endif // ifdef CYGPKG_LIBC | |
| 222 | |
| 223 // EOF malloc.cxx |
