home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / postscript / part01 / source / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-27  |  1.3 KB  |  63 lines

  1. /*
  2.  * Copyright (C) Rutherford Appleton Laboratory 1987
  3.  * 
  4.  * This source may be copied, distributed, altered or used, but not sold for profit
  5.  * or incorporated into a product except under licence from the author.
  6.  * It is not in the public domain.
  7.  * This notice should remain in the source unaltered, and any changes to the source
  8.  * made by persons other than the author should be marked as such.
  9.  * 
  10.  *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  11.  */
  12. #define THRESHOLD 4096
  13. #define NULL 0
  14.  
  15. static char *FreeSpace = NULL, *sbrk ();
  16. static int Left = 0;
  17.  
  18. char *malloc (size) unsigned size;
  19.  {
  20.      char *res;
  21.      
  22.      if (FreeSpace == NULL)
  23.          FreeSpace = sbrk (0);
  24.      
  25.      size = (size + 3) & ~3;
  26.      res = FreeSpace;
  27.      if (size > Left)
  28.       {
  29.           int chunk;
  30.  
  31.           if (sbrk (chunk = size > THRESHOLD ? size : THRESHOLD) == (char *) -1)
  32.               return NULL;
  33.           Left += chunk;
  34.       }
  35.      Left -= size;         
  36.      FreeSpace += size;
  37.      
  38.      return res;
  39.  }
  40.  
  41. int free (block) char *block;
  42.  {
  43.  }
  44.  
  45. char *realloc (b, size) char *b; unsigned size;
  46.  {
  47.      char *block = malloc (size);
  48.      
  49.      while (--size > 0)
  50.          *block++ = *b++;
  51.      return block;
  52.  }
  53.  
  54. char *calloc (size1, size2) unsigned size1, size2;
  55.  {
  56.      unsigned total = size1 * size2;
  57.      char *res = malloc (total), *p = res;
  58.      
  59.      while (--total > 0)
  60.          *p++ = 0;
  61.      return res;
  62.  }
  63.