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 | |
---|
57 | /* |
---|
58 | * If block boundary integrity checking is enabled, then |
---|
59 | * we need to account for the boundary memory again. |
---|
60 | */ |
---|
61 | resize = size; |
---|
62 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
63 | if (rtems_malloc_boundary_helpers) |
---|
64 | resize += (*rtems_malloc_boundary_helpers->overhead)(); |
---|
65 | #endif |
---|
66 | |
---|
67 | if ( _Protected_heap_Resize_block( &RTEMS_Malloc_Heap, ptr, resize ) ) { |
---|
68 | #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS) |
---|
69 | /* |
---|
70 | * Successful resize. Update the boundary on the same block. |
---|
71 | */ |
---|
72 | if (rtems_malloc_boundary_helpers) |
---|
73 | (*rtems_malloc_boundary_helpers->at_realloc)(ptr, resize); |
---|
74 | #endif |
---|
75 | return ptr; |
---|
76 | } |
---|
77 | |
---|
78 | /* |
---|
79 | * There used to be a free on this error case but it is wrong to |
---|
80 | * free the memory per OpenGroup Single UNIX Specification V2 |
---|
81 | * and the C Standard. |
---|
82 | */ |
---|
83 | |
---|
84 | new_area = malloc( size ); |
---|
85 | |
---|
86 | MSBUMP(malloc_calls, -1); /* subtract off the malloc */ |
---|
87 | |
---|
88 | if ( !new_area ) { |
---|
89 | return (void *) 0; |
---|
90 | } |
---|
91 | |
---|
92 | if ( !_Protected_heap_Get_block_size(&RTEMS_Malloc_Heap, ptr, &old_size) ) { |
---|
93 | errno = EINVAL; |
---|
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 |
---|