testobj_t testobj_new(int model) { testobj_t obj; switch (model) { case 0: obj = GC_MALLOC(sizeof(struct testobj_s)); if (obj != NULL) 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); } if (obj == NULL) { fprintf(stderr, "Out of memory!\n"); exit(3); } my_assert(obj->i == 0 && obj->keep_link == NULL); obj->i = 109; return obj; }
void test_misc_sizes(void) { static const struct GC_finalizer_closure fc = { misc_sizes_dct, NULL }; int i; for (i = 1; i <= 20; ++i) { /* Up to 1 MiB. */ void *p = GC_finalized_malloc((size_t)1 << i, &fc); if (p == NULL) { fprintf(stderr, "Out of memory!\n"); exit(3); } my_assert(memeq(p, 0, (size_t)1 << i)); memset(p, 0x56, (size_t)1 << i); *(unsigned char *)p = i; } }
pair_t pair_new(pair_t car, pair_t cdr) { pair_t p; static const struct GC_finalizer_closure fc = { pair_dct, NULL }; p = GC_finalized_malloc(sizeof(struct pair_s), &fc); if (p == NULL) { fprintf(stderr, "Out of memory!\n"); exit(3); } my_assert(!is_pair(p)); my_assert(memeq(p, 0, sizeof(struct pair_s))); memcpy(p->magic, pair_magic, sizeof(p->magic)); p->checksum = 782 + (car? car->checksum : 0) + (cdr? cdr->checksum : 0); p->car = car; p->cdr = cdr; # ifdef DEBUG_DISCLAIM_DESTRUCT printf("Construct %p = (%p, %p)\n", (void *)p, (void *)p->car, (void *)p->cdr); # endif return p; }