source: rtems/cpukit/libcsupport/src/realloc.c @ 01557b0

4.115
Last change on this file since 01557b0 was 01557b0, checked in by Sebastian Huber <sebastian.huber@…>, on 11/27/14 at 10:44:48

libcsupport: Delete malloc statistics

Use the heap handler statistics instead. Add heap walk option to MALLOC
shell command.

close #1367

  • Property mode set to 100644
File size: 1.6 KB
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.org/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#include <string.h>
26
27#include <rtems/score/sysstate.h>
28#include <rtems/score/objectimpl.h>
29
30void *realloc(
31  void *ptr,
32  size_t size
33)
34{
35  uintptr_t old_size;
36  char    *new_area;
37
38  /*
39   *  Do not attempt to allocate memory if in a critical section or ISR.
40   */
41
42  if (_System_state_Is_up(_System_state_Get())) {
43    if (!_Thread_Dispatch_is_enabled())
44      return (void *) 0;
45  }
46
47  /*
48   * Continue with realloc().
49   */
50  if ( !ptr )
51    return malloc( size );
52
53  if ( !size ) {
54    free( ptr );
55    return (void *) 0;
56  }
57
58  if ( !_Protected_heap_Get_block_size(RTEMS_Malloc_Heap, ptr, &old_size) ) {
59    errno = EINVAL;
60    return (void *) 0;
61  }
62
63  /*
64   *  Now resize it.
65   */
66  if ( _Protected_heap_Resize_block( RTEMS_Malloc_Heap, ptr, size ) ) {
67    return ptr;
68  }
69
70  /*
71   *  There used to be a free on this error case but it is wrong to
72   *  free the memory per OpenGroup Single UNIX Specification V2
73   *  and the C Standard.
74   */
75
76  new_area = malloc( size );
77
78  if ( !new_area ) {
79    return (void *) 0;
80  }
81
82  memcpy( new_area, ptr, (size < old_size) ? size : old_size );
83  free( ptr );
84
85  return new_area;
86
87}
88#endif
Note: See TracBrowser for help on using the repository browser.