| 1 |
/* Test that overwrite error detection works reasonably. */ |
| 2 |
|
| 3 |
#ifndef GC_DEBUG |
| 4 |
# define GC_DEBUG |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "gc.h" |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#define COUNT 7000 |
| 13 |
#define SIZE 40 |
| 14 |
|
| 15 |
#define CHECK_OUT_OF_MEMORY(p) \ |
| 16 |
do { \ |
| 17 |
if (NULL == (p)) { \ |
| 18 |
fprintf(stderr, "Out of memory\n"); \ |
| 19 |
exit(69); \ |
| 20 |
} \ |
| 21 |
} while (0) |
| 22 |
|
| 23 |
char * A[COUNT]; |
| 24 |
|
| 25 |
char * volatile q; |
| 26 |
|
| 27 |
int main(void) |
| 28 |
{ |
| 29 |
int i; |
| 30 |
char *p; |
| 31 |
|
| 32 |
GC_INIT(); |
| 33 |
for (i = 0; i < COUNT; ++i) { |
| 34 |
A[i] = p = (char *)GC_MALLOC(SIZE); |
| 35 |
CHECK_OUT_OF_MEMORY(p); |
| 36 |
if (i % 3000 == 0) { |
| 37 |
q = NULL; |
| 38 |
GC_gcollect(); |
| 39 |
} else if (i % 5678 == 0) { |
| 40 |
/* Write a byte past the end of the allocated object */ |
| 41 |
/* but not beyond the last word of the object's memory. */ |
| 42 |
/* A volatile intermediate pointer variable is used to */ |
| 43 |
/* avoid a compiler complain of out-of-bounds access. */ |
| 44 |
q = &p[(SIZE + i / 2000) /* 42 */]; |
| 45 |
*q = 42; |
| 46 |
} |
| 47 |
} |
| 48 |
return 0; |
| 49 |
} |