4.115
Line | |
---|
1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Reallocate Memory Block |
---|
5 | * @ingroup libcsupport |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2007. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #ifdef RTEMS_NEWLIB |
---|
22 | #include "malloc_p.h" |
---|
23 | #include <stdlib.h> |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | void *realloc( |
---|
27 | void *ptr, |
---|
28 | size_t size |
---|
29 | ) |
---|
30 | { |
---|
31 | uintptr_t old_size; |
---|
32 | char *new_area; |
---|
33 | |
---|
34 | MSBUMP(realloc_calls, 1); |
---|
35 | |
---|
36 | /* |
---|
37 | * Do not attempt to allocate memory if in a critical section or ISR. |
---|
38 | */ |
---|
39 | |
---|
40 | if (_System_state_Is_up(_System_state_Get())) { |
---|
41 | if (_Thread_Dispatch_in_critical_section()) |
---|
42 | return (void *) 0; |
---|
43 | |
---|
44 | if (_ISR_Nest_level > 0) |
---|
45 | return (void *) 0; |
---|
46 | } |
---|
47 | |
---|
48 | /* |
---|
49 | * Continue with realloc(). |
---|
50 | */ |
---|
51 | if ( !ptr ) |
---|
52 | return malloc( size ); |
---|
53 | |
---|
54 | if ( !size ) { |
---|
55 | free( ptr ); |
---|
56 | return (void *) 0; |
---|
57 | } |
---|
58 | |
---|
59 | if ( !_Protected_heap_Get_block_size(RTEMS_Malloc_Heap, ptr, &old_size) ) { |
---|
60 | errno = EINVAL; |
---|
61 | return (void *) 0; |
---|
62 | } |
---|
63 | |
---|
64 | /* |
---|
65 | * Now resize it. |
---|
66 | */ |
---|
67 | if ( _Protected_heap_Resize_block( RTEMS_Malloc_Heap, ptr, size ) ) { |
---|
68 | return ptr; |
---|
69 | } |
---|
70 | |
---|
71 | /* |
---|
72 | * There used to be a free on this error case but it is wrong to |
---|
73 | * free the memory per OpenGroup Single UNIX Specification V2 |
---|
74 | * and the C Standard. |
---|
75 | */ |
---|
76 | |
---|
77 | new_area = malloc( size ); |
---|
78 | |
---|
79 | MSBUMP(malloc_calls, (uint32_t) -1); /* subtract off the malloc */ |
---|
80 | |
---|
81 | if ( !new_area ) { |
---|
82 | return (void *) 0; |
---|
83 | } |
---|
84 | |
---|
85 | memcpy( new_area, ptr, (size < old_size) ? size : old_size ); |
---|
86 | free( ptr ); |
---|
87 | |
---|
88 | return new_area; |
---|
89 | |
---|
90 | } |
---|
91 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.