static void test(mps_ap_t leafap, mps_ap_t exactap, mps_ap_t weakap, mps_ap_t bogusap) { mps_word_t *weaktable; mps_word_t *exacttable; mps_word_t *preserve[TABLE_SLOTS]; /* preserves objects in the weak */ /* table by referring to them */ unsigned long i, j; void *p; exacttable = alloc_table(TABLE_SLOTS, exactap); weaktable = alloc_table(TABLE_SLOTS, weakap); table_link(exacttable, weaktable); /* Leave bogusap between reserve and commit for the duration */ die(mps_reserve(&p, bogusap, 64), "Reserve bogus"); for(i = 0; i < TABLE_SLOTS; ++i) { mps_word_t *string; if(rnd() < P_A_HALF) { string = alloc_string("iamalive", leafap); preserve[i] = string; } else { string = alloc_string("iamdead", leafap); preserve[i] = 0; } set_table_slot(weaktable, i, string); string = alloc_string("iamexact", leafap); set_table_slot(exacttable, i, string); } for(j = 0; j < ITERATIONS; ++j) { for(i = 0; i < TABLE_SLOTS; ++i) { mps_word_t *string; string = alloc_string("spong", leafap); } } for(i = 0; i < TABLE_SLOTS; ++i) { if(preserve[i] == 0) { if(table_slot(weaktable, i)) { fprintf(stdout, "Strongly unreachable weak table entry found, " "slot %lu.\n", i); } else { if(table_slot(exacttable, i) != 0) { fprintf(stdout, "Weak table entry deleted, but corresponding " "exact table entry not deleted, slot %lu.\n", i); } } } } (void)mps_commit(bogusap, p, 64); puts("A okay\n"); }
static void test(mps_arena_t arena, mps_ap_t leafap, mps_ap_t exactap, mps_ap_t weakap, mps_ap_t bogusap) { mps_word_t *weaktable; mps_word_t *exacttable; mps_word_t *preserve[TABLE_SLOTS]; /* preserves objects in the weak */ /* table by referring to them */ size_t i, j; void *p; exacttable = alloc_table(TABLE_SLOTS, exactap); weaktable = alloc_table(TABLE_SLOTS, weakap); table_link(exacttable, weaktable); /* Leave bogusap between reserve and commit for the duration */ die(mps_reserve(&p, bogusap, 64), "Reserve bogus"); for(i = 0; i < TABLE_SLOTS; ++i) { mps_word_t *string; /* Ensure that the first and last entries in the table are * preserved, so that we don't get false positives due to the * local variables 'weak_table' and 'string' keeping these entries * alive (see job003436). */ if (rnd() % 2 == 0 || i == 0 || i + 1 == TABLE_SLOTS) { string = alloc_string("iamalive", leafap); preserve[i] = string; } else { string = alloc_string("iamdead", leafap); preserve[i] = 0; } set_table_slot(weaktable, i, string); string = alloc_string("iamexact", leafap); set_table_slot(exacttable, i, string); } for(j = 0; j < ITERATIONS; ++j) { for(i = 0; i < TABLE_SLOTS; ++i) { (void)alloc_string("spong", leafap); } } die(mps_arena_collect(arena), "mps_arena_collect"); mps_arena_release(arena); for(i = 0; i < TABLE_SLOTS; ++i) { if (preserve[i] == 0) { if (table_slot(weaktable, i)) { error("Strongly unreachable weak table entry found, " "slot %"PRIuLONGEST".\n", (ulongest_t)i); } else { if (table_slot(exacttable, i) != 0) { error("Weak table entry deleted, but corresponding " "exact table entry not deleted, slot %"PRIuLONGEST".\n", (ulongest_t)i); } } } } (void)mps_commit(bogusap, p, 64); }