source: rtems/cpukit/libcsupport/src/realloc.c @ bd5984de

4.104.115
Last change on this file since bd5984de was bd5984de, checked in by Joel Sherrill <joel.sherrill@…>, on 09/17/08 at 18:37:55

2008-09-17 Joel Sherrill <joel.sherrill@…>

  • libcsupport/src/free.c, libcsupport/src/malloc.c, libcsupport/src/malloc_initialize.c, libcsupport/src/malloc_p.h, libcsupport/src/malloc_sbrk_helpers.c, libcsupport/src/malloc_statistics_helpers.c, libcsupport/src/malloc_walk.c, libcsupport/src/mallocfreespace.c, libcsupport/src/mallocinfo.c, libcsupport/src/realloc.c, libcsupport/src/rtems_memalign.c, sapi/include/confdefs.h, score/inline/rtems/score/thread.inl: Add support for optionally having a unified work area. In other words, the RTEMS Workspace and C Program Heap are the same pool of memory.
  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[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
23void *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
Note: See TracBrowser for help on using the repository browser.