| 1 |
|
| 2 |
#ifdef HAVE_CONFIG_H |
| 3 |
# include "config.h" |
| 4 |
#endif |
| 5 |
|
| 6 |
#ifndef GC_THREADS |
| 7 |
# define GC_THREADS |
| 8 |
#endif |
| 9 |
|
| 10 |
#undef GC_NO_THREAD_REDIRECTS |
| 11 |
#include "leak_detector.h" |
| 12 |
|
| 13 |
#ifdef GC_PTHREADS |
| 14 |
# include <errno.h> /* for EAGAIN */ |
| 15 |
# include <pthread.h> |
| 16 |
#else |
| 17 |
# ifndef WIN32_LEAN_AND_MEAN |
| 18 |
# define WIN32_LEAN_AND_MEAN 1 |
| 19 |
# endif |
| 20 |
# define NOSERVICE |
| 21 |
# include <windows.h> |
| 22 |
#endif /* !GC_PTHREADS */ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#define CHECK_OUT_OF_MEMORY(p) \ |
| 28 |
do { \ |
| 29 |
if (NULL == (p)) { \ |
| 30 |
fprintf(stderr, "Out of memory\n"); \ |
| 31 |
exit(69); \ |
| 32 |
} \ |
| 33 |
} while (0) |
| 34 |
|
| 35 |
#ifdef GC_PTHREADS |
| 36 |
void * test(void * arg) |
| 37 |
#else |
| 38 |
DWORD WINAPI test(LPVOID arg) |
| 39 |
#endif |
| 40 |
{ |
| 41 |
int *p[10]; |
| 42 |
int i; |
| 43 |
for (i = 0; i < 10; ++i) { |
| 44 |
p[i] = (int *)malloc(sizeof(int) + i); |
| 45 |
CHECK_OUT_OF_MEMORY(p[i]); |
| 46 |
} |
| 47 |
CHECK_LEAKS(); |
| 48 |
for (i = 1; i < 10; ++i) { |
| 49 |
free(p[i]); |
| 50 |
} |
| 51 |
# ifdef GC_PTHREADS |
| 52 |
return arg; |
| 53 |
# else |
| 54 |
return (DWORD)(GC_word)arg; |
| 55 |
# endif |
| 56 |
} |
| 57 |
|
| 58 |
#ifndef NTHREADS |
| 59 |
# define NTHREADS 5 |
| 60 |
#endif |
| 61 |
|
| 62 |
int main(void) { |
| 63 |
# if NTHREADS > 0 |
| 64 |
int i, n; |
| 65 |
# ifdef GC_PTHREADS |
| 66 |
pthread_t t[NTHREADS]; |
| 67 |
# else |
| 68 |
HANDLE t[NTHREADS]; |
| 69 |
DWORD thread_id; |
| 70 |
# endif |
| 71 |
int code; |
| 72 |
# endif |
| 73 |
|
| 74 |
GC_set_find_leak(1); /* for new collect versions not compiled */ |
| 75 |
/* with -DFIND_LEAK. */ |
| 76 |
GC_INIT(); |
| 77 |
|
| 78 |
# if NTHREADS > 0 |
| 79 |
for (i = 0; i < NTHREADS; ++i) { |
| 80 |
# ifdef GC_PTHREADS |
| 81 |
code = pthread_create(t + i, 0, test, 0); |
| 82 |
# else |
| 83 |
t[i] = CreateThread(NULL, 0, test, 0, 0, &thread_id); |
| 84 |
code = t[i] != NULL ? 0 : (int)GetLastError(); |
| 85 |
# endif |
| 86 |
if (code != 0) { |
| 87 |
fprintf(stderr, "Thread creation failed, errcode= %d\n", code); |
| 88 |
# ifdef GC_PTHREADS |
| 89 |
if (i > 1 && EAGAIN == code) break; |
| 90 |
# endif |
| 91 |
exit(2); |
| 92 |
} |
| 93 |
} |
| 94 |
n = i; |
| 95 |
for (i = 0; i < n; ++i) { |
| 96 |
# ifdef GC_PTHREADS |
| 97 |
code = pthread_join(t[i], 0); |
| 98 |
# else |
| 99 |
code = WaitForSingleObject(t[i], INFINITE) == WAIT_OBJECT_0 ? 0 : |
| 100 |
(int)GetLastError(); |
| 101 |
# endif |
| 102 |
if (code != 0) { |
| 103 |
fprintf(stderr, "Thread join failed, errcode= %d\n", code); |
| 104 |
exit(2); |
| 105 |
} |
| 106 |
} |
| 107 |
# endif |
| 108 |
|
| 109 |
CHECK_LEAKS(); |
| 110 |
CHECK_LEAKS(); |
| 111 |
CHECK_LEAKS(); |
| 112 |
return 0; |
| 113 |
} |