Changeset 11290355 in rtems for c/src/lib/libc
- Timestamp:
- 09/29/95 17:19:16 (27 years ago)
- Branches:
- 4.10, 4.11, 4.8, 4.9, 5, master
- Children:
- 1ceface
- Parents:
- 1039ae4
- Location:
- c/src/lib/libc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
c/src/lib/libc/error.c
r1039ae4 r11290355 40 40 * if ((fd = open(pathname, O_RDNLY)) < 0) 41 41 * { 42 * rtems_error( FLOSS_ERROR_ERRNO, "open of '%s' failed", pathname);42 * rtems_error(RTEMS_ERROR_ERRNO, "open of '%s' failed", pathname); 43 43 * goto failed; 44 44 * } -
c/src/lib/libc/malloc.c
r1039ae4 r11290355 15 15 16 16 #include <rtems.h> 17 #ifdef RTEMS_LIBC18 #include <memory.h>19 #endif20 17 #include "libcsupport.h" 21 18 #ifdef RTEMS_NEWLIB … … 30 27 #include <string.h> 31 28 32 /*33 * XXX: Do we really need to duplicate these? It appears that they34 * only cause typing problems.35 */36 37 #if 038 void *malloc(size_t);39 void *calloc(size_t, size_t);40 void *realloc(void *, size_t);41 void free(void *);42 void *sbrk(size_t);43 #endif44 45 29 rtems_id RTEMS_Malloc_Heap; 46 30 size_t RTEMS_Malloc_Sbrk_amount; 31 32 #ifdef RTEMS_DEBUG 33 #define MALLOC_STATS 34 #endif 35 36 #ifdef MALLOC_STATS 37 #define MSBUMP(f,n) malloc_stats.f += (n) 38 39 struct { 40 unsigned32 space_available; /* current size of malloc area */ 41 unsigned32 malloc_calls; /* # calls to malloc */ 42 unsigned32 free_calls; 43 unsigned32 realloc_calls; 44 unsigned32 calloc_calls; 45 unsigned32 max_depth; /* most ever malloc'd at 1 time */ 46 unsigned64 lifetime_allocated; 47 unsigned64 lifetime_freed; 48 } malloc_stats; 49 50 #else /* No malloc_stats */ 51 #define MSBUMP(f,n) 52 #endif 47 53 48 54 void RTEMS_Malloc_Initialize( … … 78 84 u32_address = (u32_address + CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1); 79 85 80 /*81 * Adjust the length by whatever we aligned by82 86 /* 87 * adjust the length by whatever we aligned by 88 */ 83 89 84 90 length -= u32_address - old_address; … … 98 104 starting_address, 99 105 length, 100 8, /* XXX : use CPU dependent RTEMS constant */106 CPU_ALIGNMENT, 101 107 RTEMS_DEFAULT_ATTRIBUTES, 102 108 &RTEMS_Malloc_Heap … … 104 110 if ( status != RTEMS_SUCCESSFUL ) 105 111 rtems_fatal_error_occurred( status ); 112 113 #ifdef MALLOC_STATS 114 /* zero all the stats */ 115 (void) memset(&malloc_stats, 0, sizeof(malloc_stats)); 116 #endif 117 118 MSBUMP(space_available, length); 106 119 } 107 120 … … 115 128 rtems_unsigned32 sbrk_amount; 116 129 rtems_status_code status; 130 131 MSBUMP(malloc_calls, 1); 117 132 118 133 if ( !size ) … … 150 165 return (void *) 0; 151 166 152 /*153 fprintf(stderr, "Extended the C heap starting at 0x%x for %d bytes\n",154 (unsigned32)starting_address, the_size);155 */156 157 167 status = rtems_region_extend( 158 168 RTEMS_Malloc_Heap, … … 162 172 if ( status != RTEMS_SUCCESSFUL ) { 163 173 sbrk(-the_size); 164 return(FALSE);165 174 errno = ENOMEM; 166 175 return (void *) 0; 167 176 } 177 178 MSBUMP(space_available, the_size); 179 168 180 status = rtems_region_get_segment( 169 181 RTEMS_Malloc_Heap, … … 179 191 } 180 192 193 #ifdef MALLOC_STATS 194 if (return_this) 195 { 196 unsigned32 current_depth; 197 MSBUMP(lifetime_allocated, size); 198 current_depth = malloc_stats.lifetime_allocated - malloc_stats.lifetime_freed; 199 if (current_depth > malloc_stats.max_depth) 200 malloc_stats.max_depth = current_depth; 201 } 202 #endif 203 181 204 return return_this; 182 205 } … … 189 212 register char *cptr; 190 213 int length; 214 215 MSBUMP(calloc_calls, 1); 191 216 192 217 length = nelem * elsize; … … 207 232 char *new_area; 208 233 234 MSBUMP(realloc_calls, 1); 235 209 236 if ( !ptr ) 210 237 return malloc( size ); 211 238 212 239 if ( !size ) { 240 free( ptr ); 241 return (void *) 0; 242 } 243 244 new_area = malloc( size ); 245 if ( !new_area ) { 213 246 free( ptr ); 214 247 return (void *) 0; … … 221 254 } 222 255 223 new_area = malloc( size );224 if ( !new_area ) {225 free( ptr );226 return (void *) 0;227 }228 229 256 memcpy( new_area, ptr, (size < old_size) ? size : old_size ); 230 257 free( ptr ); … … 240 267 rtems_status_code status; 241 268 269 MSBUMP(free_calls, 1); 270 242 271 if ( !ptr ) 243 272 return; 244 273 274 #ifdef MALLOC_STATS 275 { 276 unsigned32 size; 277 status = rtems_region_get_segment_size( RTEMS_Malloc_Heap, ptr, &size ); 278 if ( status == RTEMS_SUCCESSFUL ) { 279 MSBUMP(lifetime_freed, size); 280 } 281 } 282 #endif 283 245 284 status = rtems_region_return_segment( RTEMS_Malloc_Heap, ptr ); 246 285 if ( status != RTEMS_SUCCESSFUL ) { … … 250 289 } 251 290 291 #ifdef MALLOC_STATS 292 /* 293 * Dump the malloc statistics 294 * May be called via atexit() (installable by our bsp) or 295 * at any time by user 296 */ 297 298 void malloc_dump(void) 299 { 300 unsigned32 allocated = malloc_stats.lifetime_allocated - malloc_stats.lifetime_freed; 301 302 printf("Malloc stats\n"); 303 printf(" avail:%uk allocated:%uk (%d%%) max:%uk (%d%%) lifetime:%Luk freed:%Luk\n", 304 (unsigned int) malloc_stats.space_available / 1024, 305 (unsigned int) allocated / 1024, 306 /* avoid float! */ 307 (allocated * 100) / malloc_stats.space_available, 308 (unsigned int) malloc_stats.max_depth / 1024, 309 (malloc_stats.max_depth * 100) / malloc_stats.space_available, 310 (unsigned long long) malloc_stats.lifetime_allocated / 1024, 311 (unsigned long long) malloc_stats.lifetime_freed / 1024); 312 printf(" Call counts: malloc:%d free:%d realloc:%d calloc:%d\n", 313 malloc_stats.malloc_calls, 314 malloc_stats.free_calls, 315 malloc_stats.realloc_calls, 316 malloc_stats.calloc_calls); 317 } 318 #endif 319 252 320 /* 253 321 * "Reentrant" versions of the above routines implemented above.
Note: See TracChangeset
for help on using the changeset viewer.