source: rtems/cpukit/libcsupport/src/realloc.c @ 543fe820

4.104.114.9
Last change on this file since 543fe820 was 543fe820, checked in by Joel Sherrill <joel.sherrill@…>, on Dec 18, 2007 at 8:36:40 PM

2007-12-18 Joel Sherrill <joel.sherrill@…>

  • libcsupport/Makefile.am, libcsupport/preinstall.am, libcsupport/src/malloc.c, libcsupport/src/mallocinfo.c, libmisc/Makefile.am, libmisc/shell/main_mallocinfo.c, libmisc/shell/shellconfig.h: Split malloc.c into multiple files with one function per file. Also split out statistics into a separate file which can be plugged in dynamically. Right now, it is always in. I suspect that splitting the file removed more code than leaving statistics in. I tinkered with malloc information command in the shell. I resurrected the malloc arena code as malloc boundary. This code is now compiled all the time even though it does not appear to work.
  • libcsupport/include/rtems/malloc.h, libcsupport/src/_calloc_r.c, libcsupport/src/_free_r.c, libcsupport/src/_malloc_r.c, libcsupport/src/_realloc_r.c, libcsupport/src/calloc.c, libcsupport/src/free.c, libcsupport/src/malloc_boundary.c, libcsupport/src/malloc_get_statistics.c, libcsupport/src/malloc_initialize.c, libcsupport/src/malloc_p.h, libcsupport/src/malloc_report_statistics.c, libcsupport/src/malloc_report_statistics_plugin.c, libcsupport/src/malloc_statistics_helpers.c, libcsupport/src/malloc_walk.c, libcsupport/src/realloc.c, libmisc/shell/main_perioduse.c: New files.
  • 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  /*
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
Note: See TracBrowser for help on using the repository browser.