OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [malloc/] [realloc.c] - Blame information for rev 1325

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
 * libc/stdlib/malloc/realloc.c -- realloc function
3
 *
4
 *  Copyright (C) 2002  NEC Corporation
5
 *  Copyright (C) 2002  Miles Bader <miles@gnu.org>
6
 *
7
 * This file is subject to the terms and conditions of the GNU Lesser
8
 * General Public License.  See the file COPYING.LIB in the main
9
 * directory of this archive for more details.
10
 *
11
 * Written by Miles Bader <miles@gnu.org>
12
 */
13
 
14
#include <stdlib.h>
15
#include <string.h>
16
#include <errno.h>
17
 
18
#include "malloc.h"
19
#include "heap.h"
20
 
21
 
22
void *
23
realloc (void *mem, size_t new_size)
24
{
25
  size_t size;
26
  char *base_mem;
27
 
28
  /* Check for special cases.  */
29
  if (! mem)
30
    return malloc (new_size);
31
  if (! new_size)
32
    {
33
      free (mem);
34
      return malloc (new_size);
35
    }
36
 
37
  /* Normal realloc.  */
38
 
39
  base_mem = MALLOC_BASE (mem);
40
  size = MALLOC_SIZE (mem);
41
 
42
  /* Include extra space to record the size of the allocated block.
43
     Also make sure that we're dealing in a multiple of the heap
44
     allocation unit (SIZE is already guaranteed to be so).*/
45
  new_size = HEAP_ADJUST_SIZE (new_size + MALLOC_HEADER_SIZE);
46
 
47
  MALLOC_DEBUG (1, "realloc: 0x%lx, %d (base = 0x%lx, total_size = %d)",
48
                (long)mem, new_size, (long)base_mem, size);
49
 
50
  if (new_size > size)
51
    /* Grow the block.  */
52
    {
53
      size_t extra = new_size - size;
54
 
55
      __heap_lock (&__malloc_heap);
56
      extra = __heap_alloc_at (&__malloc_heap, base_mem + size, extra);
57
      __heap_unlock (&__malloc_heap);
58
 
59
      if (extra)
60
        /* Record the changed size.  */
61
        MALLOC_SET_SIZE (base_mem, size + extra);
62
      else
63
        /* Our attempts to extend MEM in place failed, just
64
           allocate-and-copy.  */
65
        {
66
          void *new_mem = malloc (new_size - MALLOC_HEADER_SIZE);
67
          if (new_mem)
68
            {
69
              memcpy (new_mem, mem, size - MALLOC_HEADER_SIZE);
70
              free (mem);
71
            }
72
          mem = new_mem;
73
        }
74
    }
75
  else if (new_size + MALLOC_REALLOC_MIN_FREE_SIZE <= size)
76
    /* Shrink the block.  */
77
    {
78
      __heap_lock (&__malloc_heap);
79
      __heap_free (&__malloc_heap, base_mem + new_size, size - new_size);
80
      __heap_unlock (&__malloc_heap);
81
      MALLOC_SET_SIZE (base_mem, new_size);
82
    }
83
 
84
  if (mem)
85
    MALLOC_DEBUG (-1, "realloc: returning 0x%lx (base:0x%lx, total_size:%d)",
86
                  (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
87
  else
88
    MALLOC_DEBUG (-1, "realloc: returning 0");
89
 
90
  return mem;
91
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.