huge_test.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <stdlib.h>
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #ifndef GC_IGNORE_WARN
  5. /* Ignore misleading "Out of Memory!" warning (which is printed on */
  6. /* every GC_MALLOC call below) by defining this macro before "gc.h" */
  7. /* inclusion. */
  8. # define GC_IGNORE_WARN
  9. #endif
  10. #ifndef GC_MAXIMUM_HEAP_SIZE
  11. # define GC_MAXIMUM_HEAP_SIZE 100 * 1024 * 1024
  12. # define GC_INITIAL_HEAP_SIZE GC_MAXIMUM_HEAP_SIZE / 20
  13. /* Otherwise heap expansion aborts when deallocating large block. */
  14. /* That's OK. We test this corner case mostly to make sure that */
  15. /* it fails predictably. */
  16. #endif
  17. #ifndef GC_ATTR_ALLOC_SIZE
  18. /* Omit alloc_size attribute to avoid compiler warnings about */
  19. /* exceeding maximum object size when values close to GC_SWORD_MAX */
  20. /* are passed to GC_MALLOC. */
  21. # define GC_ATTR_ALLOC_SIZE(argnum) /* empty */
  22. #endif
  23. #include "gc.h"
  24. /*
  25. * Check that very large allocation requests fail. "Success" would usually
  26. * indicate that the size was somehow converted to a negative
  27. * number. Clients shouldn't do this, but we should fail in the
  28. * expected manner.
  29. */
  30. #define CHECK_ALLOC_FAILED(r, sz_str) \
  31. do { \
  32. if (NULL != (r)) { \
  33. fprintf(stderr, \
  34. "Size " sz_str " allocation unexpectedly succeeded\n"); \
  35. exit(1); \
  36. } \
  37. } while (0)
  38. #define GC_WORD_MAX ((GC_word)-1)
  39. #define GC_SWORD_MAX ((GC_signed_word)(GC_WORD_MAX >> 1))
  40. int main(void)
  41. {
  42. GC_INIT();
  43. CHECK_ALLOC_FAILED(GC_MALLOC(GC_SWORD_MAX - 1024), "SWORD_MAX-1024");
  44. CHECK_ALLOC_FAILED(GC_MALLOC(GC_SWORD_MAX), "SWORD_MAX");
  45. CHECK_ALLOC_FAILED(GC_MALLOC((GC_word)GC_SWORD_MAX + 1), "SWORD_MAX+1");
  46. CHECK_ALLOC_FAILED(GC_MALLOC((GC_word)GC_SWORD_MAX + 1024),
  47. "SWORD_MAX+1024");
  48. CHECK_ALLOC_FAILED(GC_MALLOC(GC_WORD_MAX - 1024), "WORD_MAX-1024");
  49. CHECK_ALLOC_FAILED(GC_MALLOC(GC_WORD_MAX - 16), "WORD_MAX-16");
  50. CHECK_ALLOC_FAILED(GC_MALLOC(GC_WORD_MAX - 8), "WORD_MAX-8");
  51. CHECK_ALLOC_FAILED(GC_MALLOC(GC_WORD_MAX - 4), "WORD_MAX-4");
  52. CHECK_ALLOC_FAILED(GC_MALLOC(GC_WORD_MAX), "WORD_MAX");
  53. return 0;
  54. }