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