static void test_ctree_insert() { struct ctree *t = ctree_new(); UT_ASSERT(t != NULL); Rcounter_malloc = TEST_INSERT; UT_ASSERT(ctree_is_empty(t)); /* leaf Malloc fail */ UT_ASSERT(ctree_insert(t, TEST_VAL_A, 0) != 0); /* all OK root */ UT_ASSERT(ctree_insert(t, TEST_VAL_B, 0) == 0); /* insert +2 mallocs */ /* accessor Malloc fail */ UT_ASSERT(ctree_insert(t, TEST_VAL_A, 0) != 0); /* insert duplicate */ UT_ASSERT(ctree_insert(t, TEST_VAL_B, 0) != 0); /* all OK second */ UT_ASSERT(ctree_insert(t, TEST_VAL_A, 0) == 0); UT_ASSERT(!ctree_is_empty(t)); ctree_delete(t); }
static void test_ctree_remove() { struct ctree *t = ctree_new(); UT_ASSERT(t != NULL); Rcounter_malloc = TEST_REMOVE; /* remove from empty tree */ UT_ASSERT(ctree_remove(t, TEST_VAL_A, 0) == 0); /* insert 2 valid values */ UT_ASSERT(ctree_insert(t, TEST_VAL_A, 0) == 0); UT_ASSERT(ctree_insert(t, TEST_VAL_B, 0) == 0); /* fail to remove equal greater */ UT_ASSERT(ctree_remove(t, TEST_VAL_C, 0) == 0); /* remove accessor */ UT_ASSERT(ctree_remove(t, TEST_VAL_A, 1) == TEST_VAL_A); /* remove root */ UT_ASSERT(ctree_remove(t, TEST_VAL_B, 1) == TEST_VAL_B); ctree_delete(t); }
static void test_ctree_new_delete_empty() { struct ctree *t = NULL; Rcounter_malloc = TEST_NEW_DELETE; /* t Malloc fail */ t = ctree_new(); UT_ASSERT(t == NULL); /* all OK and delete */ t = ctree_new(); UT_ASSERT(t != NULL); ctree_delete(t); }
/* * bucket_new -- allocates and initializes bucket instance */ struct bucket * bucket_new(size_t unit_size, int unit_max) { ASSERT(unit_size > 0); struct bucket *b = Malloc(sizeof (*b)); if (b == NULL) goto error_bucket_malloc; b->tree = ctree_new(); if (b->tree == NULL) goto error_tree_new; if ((errno = pthread_mutex_init(&b->lock, NULL)) != 0) { ERR("!pthread_mutex_init"); goto error_mutex_init; } b->unit_size = unit_size; b->unit_max = unit_max; if (bucket_is_small(b)) { b->bitmap_nallocs = RUNSIZE / unit_size; ASSERT(b->bitmap_nallocs <= RUN_BITMAP_SIZE); int unused_bits = RUN_BITMAP_SIZE - b->bitmap_nallocs; int unused_values = unused_bits / BITS_PER_VALUE; b->bitmap_nval = MAX_BITMAP_VALUES - unused_values; unused_bits -= (unused_values * BITS_PER_VALUE); ASSERT(unused_bits >= 0); b->bitmap_lastval = unused_bits ? (((1ULL << unused_bits) - 1ULL) << (BITS_PER_VALUE - unused_bits)) : 0; } else { b->bitmap_nval = 0; b->bitmap_lastval = 0; b->bitmap_nallocs = 0; } return b; error_mutex_init: ctree_delete(b->tree); error_tree_new: Free(b); error_bucket_malloc: return NULL; }
/* * obj_init -- initialization of obj * * Called by constructor. */ void obj_init(void) { LOG(3, NULL); COMPILE_ERROR_ON(sizeof (struct pmemobjpool) != 8192); #ifdef USE_COW_ENV char *env = getenv("PMEMOBJ_COW"); if (env) Open_cow = atoi(env); #endif pools_ht = cuckoo_new(); if (pools_ht == NULL) FATAL("!cuckoo_new"); pools_tree = ctree_new(); if (pools_tree == NULL) FATAL("!ctree_new"); }
/* * bucket_tree_create -- (internal) creates a new tree-based container */ static struct block_container * bucket_tree_create(size_t unit_size) { struct block_container_ctree *bc = Malloc(sizeof(*bc)); if (bc == NULL) goto error_container_malloc; bc->super.type = CONTAINER_CTREE; bc->super.unit_size = unit_size; bc->tree = ctree_new(); if (bc->tree == NULL) goto error_ctree_new; return &bc->super; error_ctree_new: Free(bc); error_container_malloc: return NULL; }
static void test_ctree_find() { struct ctree *t = ctree_new(); UT_ASSERT(t != NULL); /* search empty tree */ uint64_t k = TEST_VAL_A; UT_ASSERT(ctree_find_le(t, &k) == 0); /* insert 2 valid elements */ UT_ASSERT(ctree_insert(t, TEST_VAL_A, TEST_VAL_A) == 0); UT_ASSERT(ctree_insert(t, TEST_VAL_B, TEST_VAL_B) == 0); /* search for values */ k = 0; UT_ASSERT(ctree_find_le(t, &k) == 0); k = TEST_VAL_A; UT_ASSERT(ctree_find_le(t, &k) == TEST_VAL_A); k = TEST_VAL_B; UT_ASSERT(ctree_find_le(t, &k) == TEST_VAL_B); ctree_delete(t); }