home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / VIRUS / CRCSET13.ZIP / BUFALLOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-13  |  908 b   |  40 lines

  1. /*
  2. BUFALLOC.C
  3.  
  4. Kevin Dean
  5. Fairview Mall P.O. Box 55074
  6. 1800 Sheppard Avenue East
  7. Willowdale, Ontario
  8. CANADA    M2J 5B9
  9. CompuServe ID: 76336,3114
  10.  
  11. March 24, 1991
  12.  
  13.     This module allocates a simple memory buffer whose size depends on the
  14. amount of memory available.  The size of the buffer is halved each time the
  15. allocation fails until memory is successfully allocated or the size goes below
  16. the minimum size requested.
  17.  
  18.     This code is public domain.
  19. */
  20.  
  21.  
  22. #include <stdlib.h>
  23.  
  24.  
  25. /***/
  26. /* Allocate a buffer of flexible size. */
  27. void *bufalloc(size_t *size, size_t minsize)
  28. {
  29. void *buffer;    /* Buffer allocated. */
  30. size_t bufsize;    /* Size of buffer allocated. */
  31.  
  32. /* Allocate as big a buffer as possible (at least minsize). */
  33. for (bufsize = *size; bufsize >= minsize && !(buffer = malloc(bufsize)); bufsize /= 2);
  34.  
  35. /* Save buffer size. */
  36. *size = bufsize;
  37.  
  38. return (buffer);
  39. }
  40.