home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / realloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-19  |  1.7 KB  |  64 lines

  1. /* 
  2.  * realloc.c --
  3.  *
  4.  *    Source code for the "realloc" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/realloc.c,v 1.2 88/07/29 17:04:22 ouster Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include <bstring.h>
  21. #include "stdlib.h"
  22.  
  23. /*
  24.  *----------------------------------------------------------------------
  25.  *
  26.  * realloc --
  27.  *
  28.  *    Change the size of the block referenced by ptr to "size",
  29.  *    possibly moving the block to a larger storage area.
  30.  *
  31.  * Results:
  32.  *    The return value is a pointer to the new area of memory.
  33.  *    The contents of this block will be unchanged up to the
  34.  *    lesserof the new and old sizes.
  35.  *
  36.  * Side effects:
  37.  *    The old block of memory may be released.
  38.  *
  39.  *----------------------------------------------------------------------
  40.  */
  41.  
  42. char *
  43. realloc(ptr, newSize)
  44.     char      *ptr;        /* Ptr to currently allocated block.  If
  45.                  * it's 0, then this procedure behaves
  46.                  * identically to malloc. */
  47.     unsigned int newSize;    /* Size of block after it is extended */
  48. {
  49.     unsigned int curSize;
  50.     char *newPtr;
  51.  
  52.     if (ptr == 0) {
  53.     return malloc(newSize);
  54.     }
  55.     curSize = Mem_Size(ptr);
  56.     if (newSize <= curSize) {
  57.     return ptr;
  58.     }
  59.     newPtr = malloc(newSize);
  60.     bcopy(ptr, newPtr, (int) curSize);
  61.     free(ptr);
  62.     return(newPtr);
  63. }
  64.