int search_element(struct ds_context *ctx) { PMEMobjpool *pop; TOID(var_string) value; int errors = 0; if (ctx == NULL) { errors++; } else if (ctx->pop == NULL) { errors++; } if (!errors) { pop = ctx->pop; printf("search key [%s]: ", (char *)ctx->key); value = art_search(pop, ctx->key, ctx->key_len); if (TOID_IS_NULL(value)) { printf("not found\n"); } else { printf("value [%s]\n", D_RO(value)->s); } } return errors; }
static int dump_art_node_callback(void *data, const unsigned char *key, uint32_t key_len, const unsigned char *val, uint32_t val_len) { cb_data *cbd; const art_node *an; TOID(art_node4) an4; TOID(art_node16) an16; TOID(art_node48) an48; TOID(art_node256) an256; TOID(art_leaf) al; TOID(art_node_u) child; TOID(var_string) oid_key; TOID(var_string) oid_value; if (data != NULL) { cbd = (cb_data *)data; switch (D_RO(cbd->node)->art_node_type) { case NODE4: an4 = D_RO(cbd->node)->u.an4; an = &(D_RO(an4)->n); child = D_RO(an4)->children[cbd->child_idx]; if (!TOID_IS_NULL(child)) { print_node_info("node4", cbd->node.oid.off, an); printf("N%lx -> N%lx [label=\"%c\"];\n", cbd->node.oid.off, child.oid.off, D_RO(an4)->keys[cbd->child_idx]); } break; case NODE16: an16 = D_RO(cbd->node)->u.an16; an = &(D_RO(an16)->n); child = D_RO(an16)->children[cbd->child_idx]; if (!TOID_IS_NULL(child)) { print_node_info("node16", cbd->node.oid.off, an); printf("N%lx -> N%lx [label=\"%c\"];\n", cbd->node.oid.off, child.oid.off, D_RO(an16)->keys[cbd->child_idx]); } break; case NODE48: an48 = D_RO(cbd->node)->u.an48; an = &(D_RO(an48)->n); child = D_RO(an48)->children[cbd->child_idx]; if (!TOID_IS_NULL(child)) { print_node_info("node48", cbd->node.oid.off, an); printf("N%lx -> N%lx [label=\"%c\"];\n", cbd->node.oid.off, child.oid.off, D_RO(an48)->keys[cbd->child_idx]); } break; case NODE256: an256 = D_RO(cbd->node)->u.an256; an = &(D_RO(an256)->n); child = D_RO(an256)->children[cbd->child_idx]; if (!TOID_IS_NULL(child)) { print_node_info("node256", cbd->node.oid.off, an); printf("N%lx -> N%lx [label=\"0x%x\"];\n", cbd->node.oid.off, child.oid.off, (char)((cbd->child_idx) & 0xff)); } break; case art_leaf_t: al = D_RO(cbd->node)->u.al; oid_key = D_RO(al)->key; oid_value = D_RO(al)->value; printf("N%lx [shape=box," "label=\"leaf at\\n0x%lx\"];\n", cbd->node.oid.off, cbd->node.oid.off); printf("N%lx [shape=box," "label=\"key at 0x%lx: %s\"];\n", oid_key.oid.off, oid_key.oid.off, D_RO(oid_key)->s); printf("N%lx [shape=box," "label=\"value at 0x%lx: %s\"];\n", oid_value.oid.off, oid_value.oid.off, D_RO(oid_value)->s); printf("N%lx -> N%lx;\n", cbd->node.oid.off, oid_key.oid.off); printf("N%lx -> N%lx;\n", cbd->node.oid.off, oid_value.oid.off); break; default: break; } } else { printf("leaf: key len %d = [%s], value len %d = [%s]\n", key_len, key, val_len, val); } return 0; }
int main(int argc, char *argv[]) { START(argc, argv, "obj_constructor"); if (argc != 2) UT_FATAL("usage: %s file-name", argv[0]); const char *path = argv[1]; PMEMobjpool *pop = NULL; int ret; TOID(struct root) root; TOID(struct node) node; if ((pop = pmemobj_create(path, POBJ_LAYOUT_NAME(constr), 0, S_IWUSR | S_IRUSR)) == NULL) UT_FATAL("!pmemobj_create: %s", path); /* * Allocate memory until OOM, so we can check later if the alloc * cancellation didn't damage the heap in any way. */ int allocs = 0; while (pmemobj_alloc(pop, NULL, sizeof (struct node), 1, NULL, NULL) == 0) allocs++; UT_ASSERTne(allocs, 0); PMEMoid oid; PMEMoid next; POBJ_FOREACH_SAFE(pop, oid, next) pmemobj_free(&oid); errno = 0; root.oid = pmemobj_root_construct(pop, sizeof (struct root), root_constr_cancel, NULL); UT_ASSERT(TOID_IS_NULL(root)); UT_ASSERTeq(errno, ECANCELED); errno = 0; ret = pmemobj_alloc(pop, NULL, sizeof (struct node), 1, node_constr_cancel, NULL); UT_ASSERTeq(ret, -1); UT_ASSERTeq(errno, ECANCELED); /* the same number of allocations should be possible. */ while (pmemobj_alloc(pop, NULL, sizeof (struct node), 1, NULL, NULL) == 0) allocs--; UT_ASSERTeq(allocs, 0); POBJ_FOREACH_SAFE(pop, oid, next) pmemobj_free(&oid); root.oid = pmemobj_root_construct(pop, sizeof (struct root), NULL, NULL); UT_ASSERT(!TOID_IS_NULL(root)); errno = 0; node.oid = pmemobj_list_insert_new(pop, offsetof(struct node, next), &D_RW(root)->list, OID_NULL, 0, sizeof (struct node), 1, node_constr_cancel, NULL); UT_ASSERT(TOID_IS_NULL(node)); UT_ASSERTeq(errno, ECANCELED); pmemobj_close(pop); DONE(NULL); }
#define TEST_WCS_1 L"Test string 3" #define TEST_WCS_2 L"Test string 4" #define TEST_STR_EMPTY "" #define TEST_WCS_EMPTY L"" /* * do_strdup -- duplicate a string to not allocated toid using pmemobj_strdup */ static void do_strdup(PMEMobjpool *pop) { TOID(char) str = TOID_NULL(char); TOID(wchar_t) wcs = TOID_NULL(wchar_t); pmemobj_strdup(pop, &str.oid, TEST_STR_1, TYPE_SIMPLE); pmemobj_wcsdup(pop, &wcs.oid, TEST_WCS_1, TYPE_SIMPLE); UT_ASSERT(!TOID_IS_NULL(str)); UT_ASSERT(!TOID_IS_NULL(wcs)); UT_ASSERTeq(strcmp(D_RO(str), TEST_STR_1), 0); UT_ASSERTeq(wcscmp(D_RO(wcs), TEST_WCS_1), 0); } /* * do_strdup_null -- duplicate a NULL string to not allocated toid */ static void do_strdup_null(PMEMobjpool *pop) { TOID(char) str = TOID_NULL(char); TOID(wchar_t) wcs = TOID_NULL(wchar_t); pmemobj_strdup(pop, &str.oid, NULL, TYPE_NULL); pmemobj_wcsdup(pop, &wcs.oid, NULL, TYPE_NULL);