[543fe820] | 1 | /* |
---|
| 2 | * calloc() |
---|
| 3 | * |
---|
| 4 | * COPYRIGHT (c) 1989-2007. |
---|
| 5 | * On-Line Applications Research Corporation (OAR). |
---|
| 6 | * |
---|
| 7 | * The license and distribution terms for this file may be |
---|
| 8 | * found in the file LICENSE in this distribution or at |
---|
| 9 | * http://www.rtems.com/license/LICENSE. |
---|
| 10 | * |
---|
| 11 | * $Id$ |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #if HAVE_CONFIG_H |
---|
| 15 | #include "config.h" |
---|
| 16 | #endif |
---|
| 17 | |
---|
| 18 | #ifdef RTEMS_NEWLIB |
---|
| 19 | #include "malloc_p.h" |
---|
| 20 | #include <stdlib.h> |
---|
| 21 | #include <errno.h> |
---|
| 22 | |
---|
| 23 | void *realloc( |
---|
| 24 | void *ptr, |
---|
| 25 | size_t size |
---|
| 26 | ) |
---|
| 27 | { |
---|
| 28 | size_t old_size; |
---|
| 29 | char *new_area; |
---|
| 30 | size_t resize; |
---|
| 31 | |
---|
| 32 | MSBUMP(realloc_calls, 1); |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | * Do not attempt to allocate memory if in a critical section or ISR. |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | if (_System_state_Is_up(_System_state_Get())) { |
---|
| 39 | if (_Thread_Dispatch_disable_level > 0) |
---|
| 40 | return (void *) 0; |
---|
| 41 | |
---|
| 42 | if (_ISR_Nest_level > 0) |
---|
| 43 | return (void *) 0; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /* |
---|
| 47 | * Continue with realloc(). |
---|
| 48 | */ |
---|
| 49 | if ( !ptr ) |
---|
| 50 | return malloc( size ); |
---|
| 51 | |
---|
| 52 | if ( !size ) { |
---|
| 53 | free( ptr ); |
---|
| 54 | return (void *) 0; |
---|
| 55 | } |
---|
| 56 | |
---|
[bd5984de] | 57 | if ( !_Protected_heap_Get_block_size(RTEMS_Malloc_Heap, ptr, &old_size) ) { |
---|
[e0a66c15] | 58 | errno = EINVAL; |
---|
| 59 | return (void *) 0; |
---|
| 60 | } |
---|
| 61 | |
---|
[543fe820] | 62 | /* |
---|
| 63 | * If block boundary integrity checking is enabled, then |
---|
| 64 | * we need to account for the boundary memory again. |
---|
| 65 | */ |
---|
| 66 | resize = size; |
---|
| 67 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
| 68 | if (rtems_malloc_boundary_helpers) |
---|
| 69 | resize += (*rtems_malloc_boundary_helpers->overhead)(); |
---|
| 70 | #endif |
---|
| 71 | |
---|
[bd5984de] | 72 | if ( _Protected_heap_Resize_block( RTEMS_Malloc_Heap, ptr, resize ) ) { |
---|
[543fe820] | 73 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
| 74 | /* |
---|
| 75 | * Successful resize. Update the boundary on the same block. |
---|
| 76 | */ |
---|
| 77 | if (rtems_malloc_boundary_helpers) |
---|
| 78 | (*rtems_malloc_boundary_helpers->at_realloc)(ptr, resize); |
---|
| 79 | #endif |
---|
| 80 | return ptr; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /* |
---|
| 84 | * There used to be a free on this error case but it is wrong to |
---|
| 85 | * free the memory per OpenGroup Single UNIX Specification V2 |
---|
| 86 | * and the C Standard. |
---|
| 87 | */ |
---|
| 88 | |
---|
| 89 | new_area = malloc( size ); |
---|
| 90 | |
---|
| 91 | MSBUMP(malloc_calls, -1); /* subtract off the malloc */ |
---|
| 92 | |
---|
| 93 | if ( !new_area ) { |
---|
| 94 | return (void *) 0; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | memcpy( new_area, ptr, (size < old_size) ? size : old_size ); |
---|
| 98 | free( ptr ); |
---|
| 99 | |
---|
| 100 | return new_area; |
---|
| 101 | |
---|
| 102 | } |
---|
| 103 | #endif |
---|