int main(int argc, char **argv) { int nthreads; if (1 == argc) { nthreads = DEFAULT_NTHREADS; } else if (2 == argc) { nthreads = atoi(argv[1]); if (nthreads < 1 || nthreads > MAX_NTHREADS) { fprintf(stderr, "Invalid # of threads argument\n"); exit(1); } } else { fprintf(stderr, "Usage: %s [# of threads]\n", argv[0]); exit(1); } printf("Performing %d reversals of %d element lists in %d threads\n", N_REVERSALS, LIST_LENGTH, nthreads); AO_malloc_enable_mmap(); /* Test various corner cases. */ AO_free(NULL); AO_free(AO_malloc(0)); # ifdef HAVE_MMAP AO_free(AO_malloc(CHUNK_SIZE - (sizeof(AO_t)-1))); /* large alloc */ # endif run_parallel(nthreads, run_one_test, dummy_test, "AO_malloc/AO_free"); return 0; }
void * run_one_test(void * arg) { ln * x = make_list(1, LIST_LENGTH); int i; char *p = (char *)AO_malloc(LARGE_OBJ_SIZE); char *q; char a = 'a' + ((int)((AO_PTRDIFF_T)arg) * 2) % ('z' - 'a' + 1); char b = a + 1; if (0 == p) { # ifdef HAVE_MMAP fprintf(stderr, "AO_malloc(%d) failed\n", LARGE_OBJ_SIZE); abort(); # else fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n", LARGE_OBJ_SIZE); # endif } else { p[0] = p[LARGE_OBJ_SIZE/2] = p[LARGE_OBJ_SIZE-1] = a; q = (char *)AO_malloc(LARGE_OBJ_SIZE); if (q == 0) { fprintf(stderr, "Out of memory\n"); /* Normal for more than about 10 threads without mmap? */ exit(2); } q[0] = q[LARGE_OBJ_SIZE/2] = q[LARGE_OBJ_SIZE-1] = b; if (p[0] != a || p[LARGE_OBJ_SIZE/2] != a || p[LARGE_OBJ_SIZE-1] != a) { fprintf(stderr, "First large allocation smashed\n"); abort(); } AO_free(p); if (q[0] != b || q[LARGE_OBJ_SIZE/2] != b || q[LARGE_OBJ_SIZE-1] != b) { fprintf(stderr, "Second large allocation smashed\n"); abort(); } AO_free(q); } # ifdef DEBUG_RUN_ONE_TEST x = reverse(x, 0); print_list(x); x = reverse(x, 0); print_list(x); # endif for (i = 0; i < N_REVERSALS; ++i) { x = reverse(x, 0); } check_list(x, 1, LIST_LENGTH); free_list(x); return NULL; }
void * run_one_test(void * arg) { ln * x = make_list(1, LIST_LENGTH); int i; char *p = AO_malloc(LARGE_OBJ_SIZE); char *q; if (0 == p) { # ifdef HAVE_MMAP fprintf(stderr, "AO_malloc(%d) failed\n", LARGE_OBJ_SIZE); abort(); # else fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n", LARGE_OBJ_SIZE); # endif } else { p[0] = p[LARGE_OBJ_SIZE/2] = p[LARGE_OBJ_SIZE-1] = 'a'; q = AO_malloc(LARGE_OBJ_SIZE); if (q == 0) { fprintf(stderr, "Out of memory\n"); /* Normal for more than about 10 threads without mmap? */ exit(2); } q[0] = q[LARGE_OBJ_SIZE/2] = q[LARGE_OBJ_SIZE-1] = 'b'; if (p[0] != 'a' || p[LARGE_OBJ_SIZE/2] != 'a' || p[LARGE_OBJ_SIZE-1] != 'a') { fprintf(stderr, "First large allocation smashed\n"); abort(); } AO_free(p); if (q[0] != 'b' || q[LARGE_OBJ_SIZE/2] != 'b' || q[LARGE_OBJ_SIZE-1] != 'b') { fprintf(stderr, "Second large allocation smashed\n"); abort(); } AO_free(q); } # ifdef DEBUG_RUN_ONE_TEST x = reverse(x, 0); print_list(x); x = reverse(x, 0); print_list(x); # endif for (i = 0; i < N_REVERSALS; ++i) { x = reverse(x, 0); } check_list(x, 1, LIST_LENGTH); return arg; /* use arg to suppress compiler warning */ }
ln *cons(int d, ln *tail) { static size_t extra = 0; size_t my_extra = extra; ln *result; int * extras; unsigned i; if (my_extra > 100) extra = my_extra = 0; else ++extra; result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra); if (result == 0) { fprintf(stderr, "Out of memory\n"); /* Normal for more than about 10 threads without mmap? */ exit(2); } result -> data = d; result -> next = tail; extras = (int *)(result+1); for (i = 0; i < my_extra; ++i) extras[i] = 42; return result; }
ln *cons(int d, ln *tail) { # ifdef AO_HAVE_fetch_and_add1 static volatile AO_t extra = 0; size_t my_extra = (size_t)AO_fetch_and_add1(&extra) % 101; # else static size_t extra = 0; /* data race in extra is OK */ size_t my_extra = (extra++) % 101; # endif ln *result; int * extras; unsigned i; result = (ln *)AO_malloc(sizeof(ln) + sizeof(int)*my_extra); if (result == 0) { fprintf(stderr, "Out of memory\n"); /* Normal for more than about 10 threads without mmap? */ exit(2); } result -> data = d; result -> next = tail; extras = (int *)(result+1); for (i = 0; i < my_extra; ++i) extras[i] = 42; return result; }
void * run_one_test(void * arg) { ln * x = make_list(1, LIST_LENGTH); int i; char *p = AO_malloc(LARGE_OBJ_SIZE); char *q; if (0 == p) { fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n", LARGE_OBJ_SIZE); AO_free(p); } else { p[0] = p[LARGE_OBJ_SIZE/2] = p[LARGE_OBJ_SIZE-1] = 'a'; q = AO_malloc(LARGE_OBJ_SIZE); q[0] = q[LARGE_OBJ_SIZE/2] = q[LARGE_OBJ_SIZE-1] = 'b'; if (p[0] != 'a' || p[LARGE_OBJ_SIZE/2] != 'a' || p[LARGE_OBJ_SIZE-1] != 'a') { fprintf(stderr, "First large allocation smashed\n"); abort(); } AO_free(p); if (q[0] != 'b' || q[LARGE_OBJ_SIZE/2] != 'b' || q[LARGE_OBJ_SIZE-1] != 'b') { fprintf(stderr, "Second large allocation smashed\n"); abort(); } AO_free(q); } # ifdef DEBUG_RUN_ONE_TEST x = reverse(x, 0); print_list(x); x = reverse(x, 0); print_list(x); # endif for (i = 0; i < N_REVERSALS; ++i) { x = reverse(x, 0); } check_list(x, 1, LIST_LENGTH); return 0; }