home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_10 / 1010051a < prev    next >
Text File  |  1992-08-12  |  1KB  |  53 lines

  1. /*  Listing 4
  2.  
  3.   This module provides malloc and calloc shell
  4.   functions to enable test code to force malloc and
  5.   calloc to fail. The test code should call either
  6.   SetCalloc or SetMalloc to specify the number of
  7.   times calloc and malloc should succeed before
  8.   failing.
  9. */
  10. #include <stddef.h>
  11. #include <stdlib.h>
  12.  
  13. static int  callocPass;
  14. static int  mallocPass;
  15.  
  16. /* set calloc counter */
  17. void    SetCalloc( int passes )
  18. {
  19.   callocPass = passes;
  20. }
  21.  
  22. /* set malloc counter */
  23. void    SetMalloc( int passes )
  24. {
  25.   mallocPass = passes;
  26. }
  27.  
  28. /* count down to 0 and then fail to allocate memory */
  29. void *testCalloc(size_t numElems,size_t elemSize)
  30. {
  31.   #undef calloc
  32.   if ( callocPass > 0 )
  33.   {
  34.      --callocPass;
  35.      return calloc( numElems, elemSize );
  36.   }
  37.   else
  38.      return NULL;
  39. }
  40.  
  41. /* count down to 0 and then fail to allocate memory */
  42. void    *testMalloc( size_t numElems )
  43. {
  44.   #undef malloc
  45.   if ( mallocPass > 0 )
  46.   {
  47.      --mallocPass;
  48.      return malloc( numElems );
  49.   }
  50.   else
  51.      return NULL;
  52. }
  53.