diff options
author | Ivan Maidanski <ivmai@mail.ru> | 2011-09-13 10:58:36 +0400 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2011-09-30 17:35:15 +0400 |
commit | 0b30ed5b081184a3f5624096dbcb02e97003eeee (patch) | |
tree | a5d8c8f994987d45212c882662a5b4a17d321655 /tests | |
parent | 460a105c70c957f1d99ba570301960dbc097df05 (diff) | |
download | bdwgc-0b30ed5b081184a3f5624096dbcb02e97003eeee.tar.gz |
ENABLE_DISCLAIM: Make stylistic fixes in many places and add many FIXME.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/disclaim_bench.c | 133 | ||||
-rw-r--r-- | tests/disclaim_test.c | 109 | ||||
-rw-r--r-- | tests/tests.am | 1 |
3 files changed, 143 insertions, 100 deletions
diff --git a/tests/disclaim_bench.c b/tests/disclaim_bench.c index df67bf4f..ea7ca094 100644 --- a/tests/disclaim_bench.c +++ b/tests/disclaim_bench.c @@ -1,10 +1,27 @@ -#include "gc_disclaim.h" -#include "atomic_ops.h" +/* + * Copyright (c) 2011 by Hewlett-Packard Company. All rights reserved. + * + * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED + * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. + * + * Permission is hereby granted to use or copy this program + * for any purpose, provided the above notices are retained on all copies. + * Permission to modify the code and to distribute modified code is granted, + * provided the above notices are retained, and a notice that the code was + * modified is included with the above copyright notice. + * + */ + #include <assert.h> #include <stdlib.h> #include <stdio.h> #include <string.h> -#include <time.h> + +#include <time.h> // FIXME: It would be good not to use timing API by + // default (is it is not quite portable). + +#include "atomic_ops.h" +#include "gc_disclaim.h" static AO_t free_count = 0; @@ -16,11 +33,10 @@ struct testobj_s { void testobj_finalize(void *obj, void *carg) { -#define obj ((testobj_t)obj) AO_fetch_and_add1((AO_t *)carg); - assert(obj->i++ == 109); -#undef obj + assert(((testobj_t)obj)->i++ == 109); } + static struct GC_finalizer_closure fclos = { testobj_finalize, &free_count @@ -30,28 +46,33 @@ testobj_t testobj_new(int model) { testobj_t obj; switch (model) { - case 0: - obj = GC_malloc(sizeof(struct testobj_s)); - GC_register_finalizer_no_order(obj, testobj_finalize, &free_count, - NULL, NULL); - break; - case 1: - obj = GC_finalized_malloc(sizeof(struct testobj_s), &fclos); - break; - case 2: - obj = GC_malloc(sizeof(struct testobj_s)); - break; - default: - abort(); + case 0: + obj = GC_MALLOC(sizeof(struct testobj_s)); + GC_register_finalizer_no_order(obj, testobj_finalize, &free_count, + NULL, NULL); + break; + case 1: + obj = GC_finalized_malloc(sizeof(struct testobj_s), &fclos); + break; + case 2: + obj = GC_MALLOC(sizeof(struct testobj_s)); + break; + default: + exit(-1); } assert(obj->i == 0 && obj->keep_link == NULL); obj->i = 109; return obj; } - #define ALLOC_CNT (4*1024*1024) -#define KEEP_CNT ( 32*1024) +#define KEEP_CNT (32*1024) + +static char const *model_str[3] = { + "regular finalization", + "finalize on reclaim", + "no finalization" +}; int main(int argc, char **argv) { @@ -59,57 +80,55 @@ int main(int argc, char **argv) int model; testobj_t *keep_arr; double t; - static char const *model_str[3] = { - "regular finalization", - "finalize on reclaim", - "no finalization" - }; - GC_init(); + GC_INIT(); GC_init_finalized_malloc(); - /* Seed with time for distict usage patters over repeated runs. */ - srand48(time(NULL)); + /* Seed with time for distict usage patterns over repeated runs. */ + srand48(time(NULL)); // FIXME: not available on some targets - keep_arr = GC_malloc(sizeof(void *)*KEEP_CNT); + keep_arr = GC_MALLOC(sizeof(void *)*KEEP_CNT); if (argc == 1) { - char *buf = GC_malloc(strlen(argv[0]) + 3); - printf("\t\t\tfin. ratio time/s time/fin.\n"); - for (i = 0; i < 3; ++i) { - int st; - sprintf(buf, "%s %d", argv[0], i); - st = system(buf); - if (st != 0) - return st; - } - return 0; + char *buf = GC_MALLOC(strlen(argv[0]) + 3); + printf("\t\t\tfin. ratio time/s time/fin.\n"); + for (i = 0; i < 3; ++i) { + int st; + sprintf(buf, "%s %d", argv[0], i); // FIXME: Use snprintf + //FIXME: snprintf not available on WinCE + st = system(buf); // FIXME: is this available on all targets? + if (st != 0) + return st; + } + return 0; } if (argc == 2 && strcmp(argv[1], "--help") == 0) { - fprintf(stderr, - "Usage: %s FINALIZATION_MODEL\n" - "\t0 -- original finalization\n" - "\t1 -- finalization on reclaim\n" - "\t2 -- no finalization\n", argv[0]); - return 1; + fprintf(stderr, + "Usage: %s FINALIZATION_MODEL\n" + "\t0 -- original finalization\n" + "\t1 -- finalization on reclaim\n" + "\t2 -- no finalization\n", argv[0]); + return 1; } model = atoi(argv[1]); if (model < 0 || model > 2) - exit(2); - t = -clock(); + exit(2); + t = -clock(); // FIXME: not available on some targets? + // FIXME: don't use '-' on probably unsigned type for (i = 0; i < ALLOC_CNT; ++i) { - int k = lrand48() % KEEP_CNT; - keep_arr[k] = testobj_new(model); + int k = lrand48() % KEEP_CNT; // FIXME: not available on some targets + keep_arr[k] = testobj_new(model); } + GC_gcollect(); - t += clock(); - t /= CLOCKS_PER_SEC; + + t += clock(); // FIXME: not available on some targets? + t /= CLOCKS_PER_SEC; // FIXME: not available on some targets if (model < 2) - printf("%20s: %12.4lf %12lg %12lg\n", model_str[model], - free_count/(double)ALLOC_CNT, t, t/free_count); + printf("%20s: %12.4lf %12lg %12lg\n", model_str[model], + free_count/(double)ALLOC_CNT, t, t/free_count); else - printf("%20s: 0 %12lg N/A\n", - model_str[model], t); - + printf("%20s: 0 %12lg N/A\n", // FIXME: Use \t + model_str[model], t); return 0; } diff --git a/tests/disclaim_test.c b/tests/disclaim_test.c index 6e005ad7..00fad7c6 100644 --- a/tests/disclaim_test.c +++ b/tests/disclaim_test.c @@ -1,12 +1,27 @@ -/* Test that objects reachable from an object allocated with - * GC_malloc_with_finalizer is not reclaimable before the finalizer - * is called. */ +/* + * Copyright (c) 2011 by Hewlett-Packard Company. All rights reserved. + * + * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED + * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. + * + * Permission is hereby granted to use or copy this program + * for any purpose, provided the above notices are retained on all copies. + * Permission to modify the code and to distribute modified code is granted, + * provided the above notices are retained, and a notice that the code was + * modified is included with the above copyright notice. + * + */ + +/* Test that objects reachable from an object allocated with */ +/* GC_malloc_with_finalizer is not reclaimable before the finalizer */ +/* is called. */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <assert.h> -#include <gc_disclaim.h> + +#include "gc_disclaim.h" typedef struct pair_s *pair_t; @@ -24,7 +39,9 @@ pair_dct(void *obj, void *cd) int checksum; /* Check that obj and its car and cdr are not trashed. */ - //printf("Destruct %p = (%p, %p)\n", p, p->car, p->cdr); +# ifdef DEBUG_DISCLAIM_DESTRUCT + printf("Destruct %p = (%p, %p)\n", p, p->car, p->cdr); +# endif assert(GC_base(obj)); assert(p->is_valid); assert(!p->car || p->car->is_valid); @@ -46,12 +63,15 @@ pair_new(pair_t car, pair_t cdr) { pair_t p; static struct GC_finalizer_closure fc = { pair_dct, NULL }; + p = GC_finalized_malloc(sizeof(struct pair_s), &fc); p->is_valid = 1; p->checksum = 782 + (car? car->checksum : 0) + (cdr? cdr->checksum : 0); p->car = car; p->cdr = cdr; - //printf("Construct %p = (%p, %p)\n", p, p->car, p->cdr); +# ifdef DEBUG_DISCLAIM_DESTRUCT + printf("Construct %p = (%p, %p)\n", p, p->car, p->cdr); +# endif return p; } @@ -59,14 +79,14 @@ void pair_check_rec(pair_t p) { while (p) { - int checksum = 782; - if (p->car) checksum += p->car->checksum; - if (p->cdr) checksum += p->cdr->checksum; - assert(p->checksum == checksum); - if (rand() % 2) - p = p->car; - else - p = p->cdr; + int checksum = 782; + if (p->car) checksum += p->car->checksum; + if (p->cdr) checksum += p->cdr->checksum; + assert(p->checksum == checksum); + if (rand() % 2) + p = p->car; + else + p = p->cdr; } } @@ -90,52 +110,57 @@ void *test(void *data) pair_t pop[POP_SIZE]; memset(pop, 0, sizeof(pop)); for (i = 0; i < MUTATE_CNT; ++i) { - int t = rand() % POP_SIZE; - switch (rand() % (i > GROW_LIMIT? 5 : 3)) { - case 0: case 3: - if (pop[t]) - pop[t] = pop[t]->car; - break; - case 1: case 4: - if (pop[t]) - pop[t] = pop[t]->cdr; - break; - case 2: - pop[t] = pair_new(pop[rand() % POP_SIZE], - pop[rand() % POP_SIZE]); - break; - } - if (rand() % 8 == 1) - pair_check_rec(pop[rand() % POP_SIZE]); + int t = rand() % POP_SIZE; + switch (rand() % (i > GROW_LIMIT? 5 : 3)) { + case 0: case 3: + if (pop[t]) + pop[t] = pop[t]->car; + break; + case 1: case 4: + if (pop[t]) + pop[t] = pop[t]->cdr; + break; + case 2: + pop[t] = pair_new(pop[rand() % POP_SIZE], + pop[rand() % POP_SIZE]); + break; + } + if (rand() % 8 == 1) + pair_check_rec(pop[rand() % POP_SIZE]); } return 0; } -int main() +int main(void) { #if THREAD_CNT > 1 pthread_t th[THREAD_CNT]; int i; #endif - GC_init(); + GC_INIT(); GC_init_finalized_malloc(); + #if THREAD_CNT > 1 printf("Threaded disclaim test.\n"); for (i = 0; i < THREAD_CNT; ++i) { - int err = pthread_create(&th[i], NULL, test, NULL); - if (err) { - fprintf(stderr, "Failed to create thread # %d: %s\n", i, - strerror(err)); - exit(1); - } + // FIXME: this is not available on Win32 without pthreads + // FIXME: Should GC_ suffix be used? + int err = pthread_create(&th[i], NULL, test, NULL); + if (err) { + fprintf(stderr, "Failed to create thread # %d: %s\n", i, + strerror(err)); + exit(1); + } + } + for (i = 0; i < THREAD_CNT; ++i) { + // FIXME: Should GC_ suffix be used? + // FIXME: Check error code. + pthread_join(th[i], NULL); } - for (i = 0; i < THREAD_CNT; ++i) - pthread_join(th[i], NULL); #else printf("Unthreaded disclaim test.\n"); test(NULL); #endif return 0; } - diff --git a/tests/tests.am b/tests/tests.am index f7985c9a..9685ae67 100644 --- a/tests/tests.am +++ b/tests/tests.am @@ -97,7 +97,6 @@ TESTS += disclaim_test check_PROGRAMS += disclaim_test disclaim_test_SOURCES = tests/disclaim_test.c disclaim_test_LDADD = $(test_ldadd) - TESTS += disclaim_bench check_PROGRAMS += disclaim_bench disclaim_bench_SOURCES = tests/disclaim_bench.c |