home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / calloc.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  157 lines

  1. /***
  2. *calloc.c - allocate storage for an array from the heap
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines the calloc() function.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef WINHEAP
  12.  
  13. #include <malloc.h>
  14. #include <string.h>
  15. #include <winheap.h>
  16. #include <windows.h>
  17. #include <internal.h>
  18. #include <mtdll.h>
  19. #include <dbgint.h>
  20.  
  21. /***
  22. *void *calloc(size_t num, size_t size) - allocate storage for an array from
  23. *       the heap
  24. *
  25. *Purpose:
  26. *       Allocate a block of memory from heap big enough for an array of num
  27. *       elements of size bytes each, initialize all bytes in the block to 0
  28. *       and return a pointer to it.
  29. *
  30. *Entry:
  31. *       size_t num  - number of elements in the array
  32. *       size_t size - size of each element
  33. *
  34. *Exit:
  35. *       Success:  void pointer to allocated block
  36. *       Failure:  NULL
  37. *
  38. *Uses:
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. void * __cdecl _calloc_base (size_t num, size_t size)
  45. {
  46.     size_t  size_sbh;
  47.     void *  pvReturn;
  48.  
  49.     size_sbh = size = size * num;
  50.  
  51.  
  52.     /* round up to the nearest paragraph */
  53.     if (size <= _HEAP_MAXREQ)
  54.     {
  55.         if (size == 0)
  56.             size = 1;
  57.         size = (size + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1);
  58.     }
  59.  
  60.     for (;;)
  61.     {
  62.         pvReturn = NULL;
  63.  
  64.         if (size <= _HEAP_MAXREQ)
  65.         {
  66.             if (size_sbh <= __sbh_threshold)
  67.             {
  68.                 //  Allocate the block from the small-block heap and
  69.                 //  initialize it with zeros.
  70.                 _mlock(_HEAP_LOCK);
  71.                 pvReturn = __sbh_alloc_block(size_sbh);
  72.                 _munlock(_HEAP_LOCK);
  73.  
  74.                 if (pvReturn != NULL)
  75.                     memset(pvReturn, 0, size_sbh);
  76.             }
  77.  
  78.             if (pvReturn == NULL)
  79.                 pvReturn = HeapAlloc(_crtheap, HEAP_ZERO_MEMORY, size);
  80.         }
  81.  
  82.         if (pvReturn || _newmode == 0)
  83.             return pvReturn;
  84.  
  85.         /* call installed new handler */
  86.         if (!_callnewh(size))
  87.             return NULL;
  88.  
  89.         /* new handler was successful -- try to allocate again */
  90.     }
  91.  
  92. }
  93.  
  94. #else  /* WINHEAP */
  95.  
  96.  
  97. #include <cruntime.h>
  98. #include <heap.h>
  99. #include <malloc.h>
  100. #include <mtdll.h>
  101. #include <stddef.h>
  102. #include <dbgint.h>
  103.  
  104. /***
  105. *void *calloc(size_t num, size_t size) - allocate storage for an array from
  106. *       the heap
  107. *
  108. *Purpose:
  109. *       Allocate a block of memory from heap big enough for an array of num
  110. *       elements of size bytes each, initialize all bytes in the block to 0
  111. *       and return a pointer to it.
  112. *
  113. *Entry:
  114. *       size_t num  - number of elements in the array
  115. *       size_t size - size of each element
  116. *
  117. *Exit:
  118. *       Success:  void pointer to allocated block block
  119. *       Failure:  NULL
  120. *
  121. *Uses:
  122. *
  123. *Exceptions:
  124. *
  125. *******************************************************************************/
  126.  
  127. void * __cdecl _calloc_base (
  128.         size_t num,
  129.         size_t size
  130.         )
  131. {
  132.         void *retp;
  133.         REG1 size_t *startptr;
  134.         REG2 size_t *lastptr;
  135.  
  136.         /* try to malloc the requested space
  137.          */
  138.         retp = _malloc_base(size *= num);
  139.  
  140.         /* if malloc() succeeded, initialize the allocated space to zeros.
  141.          * note the assumptions that the size of the allocation block is an
  142.          * integral number of sizeof(size_t) bytes and that (size_t)0 is
  143.          * sizeof(size_t) bytes of 0.
  144.          */
  145.         if ( retp != NULL ) {
  146.             startptr = (size_t *)retp;
  147.             lastptr = startptr + ((size + sizeof(size_t) - 1) /
  148.             sizeof(size_t));
  149.             while ( startptr < lastptr )
  150.                 *(startptr++) = 0;
  151.         }
  152.  
  153.         return retp;
  154. }
  155.  
  156. #endif  /* WINHEAP */
  157.