Mercurial > flash_v2
comparison packages/services/memalloc/common/current/src/malloc.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | |
| children | 289bf90c6a9b |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 1 //======================================================================== | |
| 2 // | |
| 3 // malloc.cxx | |
| 4 // | |
| 5 // Implementation of ISO C memory allocation routines | |
| 6 // | |
| 7 //======================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Red Hat eCos Public License | |
| 12 // Version 1.1 (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://www.redhat.com/ | |
| 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 Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //======================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jlarmour | |
| 35 // Contributors: | |
| 36 // Date: 2000-04-30 | |
| 37 // Purpose: Provides ISO C calloc(), malloc(), realloc() and free() | |
| 38 // functions | |
| 39 // Description: Implementation of ISO standard allocation routines as per | |
| 40 // ISO C section 7.10.3 | |
| 41 // Usage: | |
| 42 // | |
| 43 //####DESCRIPTIONEND#### | |
| 44 // | |
| 45 //======================================================================== | |
| 46 | |
| 47 // CONFIGURATION | |
| 48 | |
| 49 #include <pkgconf/memalloc.h> // Configuration header | |
| 50 | |
| 51 // Do we want these functions? | |
| 52 #ifdef CYGPKG_MEMALLOC_MALLOC_ALLOCATORS | |
| 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 memmove() | |
| 60 #include <stdlib.h> // header for this file | |
| 61 #include <pkgconf/heaps.hxx> // heap pools information | |
| 62 #include CYGBLD_MEMALLOC_MALLOC_IMPLEMENTATION_HEADER | |
| 63 | |
| 64 // STATIC VARIABLES | |
| 65 | |
| 66 // First deal with the worst case, that the memory layout didn't define a | |
| 67 // heap | |
| 68 #if CYGMEM_HEAP_COUNT == 0 | |
| 69 | |
| 70 // the data space for the memory pool | |
| 71 cyg_uint8 cyg_memalloc_mallocpool_memory[ | |
| 72 CYGNUM_MEMALLOC_FALLBACK_MALLOC_POOL_SIZE ] CYGBLD_ATTRIB_WEAK; | |
| 73 | |
| 74 // the memory pool object itself | |
| 75 CYGCLS_MEMALLOC_MALLOC_IMPL cyg_memalloc_mallocpool | |
| 76 CYGBLD_ATTRIB_INIT_BEFORE( CYG_INIT_LIBC ) = | |
| 77 CYGCLS_MEMALLOC_MALLOC_IMPL( cyg_memalloc_mallocpool_memory, | |
| 78 sizeof( cyg_memalloc_mallocpool_memory ) ); | |
| 79 | |
| 80 # define POOL cyg_memalloc_mallocpool | |
| 81 | |
| 82 #elif CYGMEM_HEAP_COUNT == 1 | |
| 83 // one heap, so it's straightforward | |
| 84 | |
| 85 # define POOL (*cygmem_memalloc_heaps[0]) | |
| 86 | |
| 87 #else | |
| 88 // multiple heaps | |
| 89 | |
| 90 # include <cyg/memalloc/memjoin.hxx> | |
| 91 | |
| 92 Cyg_Mempool_Joined<CYGCLS_MEMALLOC_MALLOC_IMPL> cyg_memalloc_mallocpool | |
| 93 CYGBLD_ATTRIB_INIT_BEFORE( CYG_INIT_LIBC ) = | |
| 94 Cyg_Mempool_Joined<CYGCLS_MEMALLOC_MALLOC_IMPL>( | |
| 95 CYGMEM_HEAP_COUNT, cygmem_memalloc_heaps | |
| 96 ); | |
| 97 | |
| 98 # define POOL cyg_memalloc_mallocpool | |
| 99 | |
| 100 #endif | |
| 101 | |
| 102 // FUNCTIONS | |
| 103 | |
| 104 void * | |
| 105 malloc( size_t size ) | |
| 106 { | |
| 107 void *data_ptr; | |
| 108 | |
| 109 CYG_REPORT_FUNCNAMETYPE( "malloc", "returning pointer %08x" ); | |
| 110 | |
| 111 CYG_REPORT_FUNCARG1DV( size ); | |
| 112 | |
| 113 // first check if size wanted is 0 | |
| 114 if ( 0 == size ) { | |
| 115 CYG_REPORT_RETVAL( NULL ); | |
| 116 return NULL; | |
| 117 } // if | |
| 118 | |
| 119 // ask the pool for the data | |
| 120 data_ptr = POOL.try_alloc( size ); | |
| 121 | |
| 122 // if it isn't NULL is the pointer valid? | |
| 123 if ( NULL != data_ptr ) { | |
| 124 CYG_CHECK_DATA_PTR( data_ptr, | |
| 125 "allocator returned invalid pointer!" ); | |
| 126 | |
| 127 // And just check its alignment | |
| 128 CYG_ASSERT( !((CYG_ADDRWORD)data_ptr & (sizeof(CYG_ADDRWORD) - 1)), | |
| 129 "Allocator has returned badly aligned data!"); | |
| 130 } // if | |
| 131 | |
| 132 CYG_REPORT_RETVAL( data_ptr ); | |
| 133 | |
| 134 return data_ptr; | |
| 135 } // malloc() | |
| 136 | |
| 137 | |
| 138 void | |
| 139 free( void *ptr ) | |
| 140 { | |
| 141 cyg_bool freeret; | |
| 142 | |
| 143 CYG_REPORT_FUNCNAME( "free"); | |
| 144 | |
| 145 CYG_REPORT_FUNCARG1XV( ptr ); | |
| 146 | |
| 147 // if null pointer, do nothing as per spec | |
| 148 if ( NULL==ptr ) | |
| 149 return; | |
| 150 | |
| 151 CYG_CHECK_DATA_PTR( ptr, "Pointer to free isn't even valid!" ); | |
| 152 | |
| 153 // get pool to free it | |
| 154 freeret = POOL.free( (cyg_uint8 *) ptr ); | |
| 155 | |
| 156 CYG_ASSERT( freeret , "Couldn't free!" ); | |
| 157 | |
| 158 CYG_REPORT_RETURN(); | |
| 159 | |
| 160 } // free() | |
| 161 | |
| 162 | |
| 163 void * | |
| 164 calloc( size_t nmemb, size_t size ) | |
| 165 { | |
| 166 void *data_ptr; | |
| 167 cyg_ucount32 realsize; | |
| 168 | |
| 169 CYG_REPORT_FUNCNAMETYPE( "calloc", "returning pointer %08x" ); | |
| 170 | |
| 171 CYG_REPORT_FUNCARG2DV( nmemb, size ); | |
| 172 | |
| 173 realsize = nmemb * size; | |
| 174 | |
| 175 data_ptr = malloc( realsize ); | |
| 176 | |
| 177 // Fill with 0's if non-NULL | |
| 178 if ( data_ptr != NULL ) | |
| 179 memset( data_ptr, 0, realsize ); | |
| 180 | |
| 181 CYG_REPORT_RETVAL( data_ptr ); | |
| 182 return data_ptr; | |
| 183 } // calloc() | |
| 184 | |
| 185 | |
| 186 externC void * | |
| 187 realloc( void *ptr, size_t size ) | |
| 188 { | |
| 189 cyg_int32 oldsize; | |
| 190 | |
| 191 CYG_REPORT_FUNCNAMETYPE( "realloc", "returning pointer %08x" ); | |
| 192 | |
| 193 CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size ); | |
| 194 | |
| 195 // if pointer is NULL, we must malloc it | |
| 196 if ( ptr == NULL ) { | |
| 197 ptr = malloc( size ); | |
| 198 CYG_REPORT_RETVAL( ptr ); | |
| 199 return ptr; | |
| 200 } // if | |
| 201 | |
| 202 CYG_CHECK_DATA_PTR( ptr, "realloc() passed a bogus pointer!" ); | |
| 203 | |
| 204 // if size is 0, we must free it | |
| 205 if (size == 0) { | |
| 206 free(ptr); | |
| 207 CYG_REPORT_RETVAL( NULL ); | |
| 208 return NULL; | |
| 209 } // if | |
| 210 | |
| 211 void *newptr; | |
| 212 | |
| 213 // otherwise try to resize allocation | |
| 214 newptr = POOL.resize_alloc( (cyg_uint8 *)ptr, size, &oldsize ); | |
| 215 | |
| 216 if ( NULL == newptr ) { | |
| 217 // if resize_alloc doesn't return a pointer, it failed, so we | |
| 218 // just have to allocate new space instead, and later copy it | |
| 219 | |
| 220 CYG_ASSERT( oldsize != 0, | |
| 221 "resize_alloc() couldn't determine allocation size!" ); | |
| 222 | |
| 223 newptr = malloc( size ); | |
| 224 | |
| 225 if ( NULL != newptr ) { | |
| 226 memcpy( newptr, ptr, size < (size_t) oldsize ? size | |
| 227 : (size_t) oldsize ); | |
| 228 free( ptr ); | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 CYG_REPORT_RETVAL( newptr ); | |
| 233 return newptr; | |
| 234 } // realloc() | |
| 235 | |
| 236 | |
| 237 externC struct mallinfo | |
| 238 mallinfo( void ) | |
| 239 { | |
| 240 struct mallinfo ret = { 0 }; // initialize to all zeros | |
| 241 Cyg_Mempool_Status stat; | |
| 242 | |
| 243 CYG_REPORT_FUNCTION(); | |
| 244 | |
| 245 POOL.get_status( CYG_MEMPOOL_STAT_ARENASIZE| | |
| 246 CYG_MEMPOOL_STAT_FREEBLOCKS| | |
| 247 CYG_MEMPOOL_STAT_TOTALALLOCATED| | |
| 248 CYG_MEMPOOL_STAT_TOTALFREE| | |
| 249 CYG_MEMPOOL_STAT_MAXFREE, stat ); | |
| 250 | |
| 251 if ( stat.arenasize > 0 ) | |
| 252 ret.arena = stat.arenasize; | |
| 253 | |
| 254 if ( stat.freeblocks > 0 ) | |
| 255 ret.ordblks = stat.freeblocks; | |
| 256 | |
| 257 if ( stat.totalallocated > 0 ) | |
| 258 ret.uordblks = stat.totalallocated; | |
| 259 | |
| 260 if ( stat.totalfree > 0 ) | |
| 261 ret.fordblks = stat.totalfree; | |
| 262 | |
| 263 if ( stat.maxfree > 0 ) | |
| 264 ret.maxfree = stat.maxfree; | |
| 265 | |
| 266 CYG_REPORT_RETURN(); | |
| 267 return ret; | |
| 268 } // mallinfo() | |
| 269 | |
| 270 #endif // ifdef CYGPKG_MEMALLOC_MALLOC_ALLOCATORS | |
| 271 | |
| 272 // EOF malloc.cxx |
