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

4.104.114.9
Last change on this file since e0a66c15 was e0a66c15, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 29, 2008 at 5:28:27 PM

2008-01-29 Joel Sherrill <joel.sherrill@…>

  • libcsupport/Makefile.am, libcsupport/include/rtems/malloc.h, libcsupport/src/malloc_walk.c, libcsupport/src/posix_memalign.c, libcsupport/src/realloc.c, score/src/heapwalk.c: Add rtems_memalign as helper and as exposed nmemalign variant with few restrictions. Also turn on compilation of _Heap_Walk but make forced calls to it conditionally compiled. This should allow more flexibility to the user as to run-time checking of the heap.
  • libcsupport/src/rtems_memalign.c: New file.
  • Property mode set to 100644
File size: 2.1 KB
Line 
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
57  if ( !_Protected_heap_Get_block_size(&RTEMS_Malloc_Heap, ptr, &old_size) ) {
58    errno = EINVAL;
59    return (void *) 0;
60  }
61
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
72  if ( _Protected_heap_Resize_block( &RTEMS_Malloc_Heap, ptr, resize ) ) {
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.