home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / malloc-930716 / realloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-20  |  3.4 KB  |  115 lines

  1. /* realloc.c - C standard library routine.
  2.    Copyright (c) 1989, 1993  Michael J. Haertel
  3.    You may redistribute this library under the terms of the
  4.    GNU Library General Public License (version 2 or any later
  5.    version) as published by the Free Software Foundation.
  6.    THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED
  7.    WARRANTY.  IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR
  8.    WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
  9.    SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */
  10.  
  11. #include <limits.h>
  12. #include <stddef.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "malloc.h"
  16.  
  17. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  18.  
  19. /* Resize the given region to the new size, returning a pointer
  20.    to the (possibly moved) region.  This is optimized for speed;
  21.    some benchmarks seem to indicate that greater compactness is
  22.    achieved by unconditionally allocating and copying to a
  23.    new region. */
  24. void *
  25. realloc(void *ptr, size_t size)
  26. {
  27.     void *result, *previous;
  28.     int block, blocks, type;
  29.     int oldlimit;
  30.  
  31.     if (!ptr)
  32.     return malloc(size);
  33.     if (!size) {
  34.     free(ptr);
  35.     return malloc(0);
  36.     }
  37.  
  38.     block = BLOCK(ptr);
  39.  
  40.     switch (type = _heapinfo[block].busy.type) {
  41.     case 0:
  42.     /* Maybe reallocate a large block to a small fragment. */
  43.     if (size <= BLOCKSIZE / 2) {
  44.         if ((result = malloc(size)) != NULL) {
  45.             memcpy(result, ptr, size);
  46.             free(ptr);
  47.         }
  48.         return result;
  49.     }
  50.  
  51.     /* The new size is a large allocation as well; see if
  52.        we can hold it in place. */
  53.     blocks = BLOCKIFY(size);
  54.     if (blocks < _heapinfo[block].busy.info.size) {
  55.         /* The new size is smaller; return excess memory
  56.            to the free list. */
  57.         _heapinfo[block + blocks].busy.type = 0;
  58.         _heapinfo[block + blocks].busy.info.size
  59.         = _heapinfo[block].busy.info.size - blocks;
  60.         _heapinfo[block].busy.info.size = blocks;
  61.         free(ADDRESS(block + blocks));
  62.         return ptr;
  63.     } else if (blocks == _heapinfo[block].busy.info.size)
  64.         /* No size change necessary. */
  65.         return ptr;
  66.     else {
  67.         /* Won't fit, so allocate a new region that will.  Free
  68.            the old region first in case there is sufficient adjacent
  69.            free space to grow without moving. */
  70.         blocks = _heapinfo[block].busy.info.size;
  71.         /* Prevent free from actually returning memory to the system. */
  72.         oldlimit = _heaplimit;
  73.         _heaplimit = 0;
  74.         free(ptr);
  75.         _heaplimit = oldlimit;
  76.         result = malloc(size);
  77.         if (!result) {
  78.         /* Now we're really in trouble.  We have to unfree
  79.            the thing we just freed.  Unfortunately it might
  80.            have been coalesced with its neighbors. */
  81.         if (_heapindex == block)
  82.             malloc(blocks * BLOCKSIZE);
  83.         else {
  84.             previous = malloc((block - _heapindex) * BLOCKSIZE);
  85.             malloc(blocks * BLOCKSIZE);
  86.             free(previous);
  87.         }        
  88.         return NULL;
  89.         }
  90.         if (ptr != result)
  91.         memmove(result, ptr, blocks * BLOCKSIZE);
  92.         return result;
  93.     }
  94.     break;
  95.  
  96.     default:
  97.     /* Old size is a fragment; type is logarithm to base two of
  98.        the fragment size. */
  99.     if ((size > 1 << (type - 1)) && (size <= 1 << type))
  100.         /* New size is the same kind of fragment. */
  101.         return ptr;
  102.     else {
  103.         /* New size is different; allocate a new space, and copy
  104.            the lesser of the new size and the old. */
  105.         result = malloc(size);
  106.         if (!result)
  107.         return NULL;
  108.         memcpy(result, ptr, MIN(size, 1 << type));
  109.         free(ptr);
  110.         return result;
  111.     }
  112.     break;
  113.     }
  114. }
  115.