home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / flex.lzh / FLEX / FLEX2 / GNU.LIB.SRC / XCALLOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-16  |  780 b   |  35 lines

  1. /*
  2.  * xcalloc -- a "safe" calloc
  3.  */
  4.  
  5. char *
  6. xcalloc (number, size)
  7.      int number, size;
  8. {
  9.   extern char *malloc ();
  10.   extern void memory_full();
  11.   register int total = number * size;
  12.   register char *ptr = malloc (total);
  13.   if (ptr != 0)
  14.     {
  15.       if (total > 100)
  16.         bzero (ptr, total);
  17.       else {
  18.         /* It's not too long, so loop, zeroing by longs.
  19.            It must be safe because malloc values are always well aligned.  */
  20.         register long *zp = (long *) ptr;
  21.         register long *zl = (long *) (ptr + total - 4);
  22.         register int i = total - 4;
  23.         while (zp < zl)
  24.           *zp++ = 0;
  25.         if (i < 0)
  26.           i = 0;
  27.         while (i < total)
  28.           ptr[i++] = 0;
  29.       }
  30.       return ptr;
  31.     }
  32.   memory_full ();
  33.   /*NOTREACHED*/
  34. }
  35.