int art_tree_map_init(struct datastore *ds, struct ds_context *ctx) { int errors = 0; char *error_string; /* calculate a required pool size */ if (ctx->psize < PMEMOBJ_MIN_POOL) ctx->psize = PMEMOBJ_MIN_POOL; if (!ctx->fileio) { if (access(ctx->filename, F_OK) != 0) { error_string = "pmemobj_create"; ctx->pop = pmemobj_create(ctx->filename, POBJ_LAYOUT_NAME(arttree_tx), ctx->psize, ctx->fmode); ctx->newpool = 1; } else { error_string = "pmemobj_open"; ctx->pop = pmemobj_open(ctx->filename, POBJ_LAYOUT_NAME(arttree_tx)); } if (ctx->pop == NULL) { perror(error_string); errors++; } } else { int flags = O_CREAT | O_RDWR | O_SYNC; /* Create a file if it does not exist. */ if ((ctx->fd = open(ctx->filename, flags, ctx->fmode)) < 0) { perror(ctx->filename); errors++; } /* allocate the pmem */ if ((errno = posix_fallocate(ctx->fd, 0, ctx->psize)) != 0) { perror("posix_fallocate"); errors++; } } if (!errors) { pmemobj_ds_set_priv(ds, ctx); } else { if (ctx->fileio) { if (ctx->fd >= 0) { close(ctx->fd); } } else { if (ctx->pop) { pmemobj_close(ctx->pop); } } } return errors; }
/* * pmemlog_create -- pool create wrapper */ PMEMlogpool * pmemlog_create(const char *path, size_t poolsize, mode_t mode) { return (PMEMlogpool *)pmemobj_create(path, POBJ_LAYOUT_NAME(obj_pmemlog_minimal), poolsize, mode); }
int main(int argc, char *argv[]) { if (argc < 3 || argc > 4) { printf("usage: %s hashmap_tx|hashmap_atomic|ctree|btree|rbtree" " file-name [<seed>]\n", argv[0]); return 1; } const struct map_ops *ops = NULL; const char *path = argv[2]; const char *type = argv[1]; if (strcmp(type, "hashmap_tx") == 0) { ops = MAP_HASHMAP_TX; } else if (strcmp(type, "hashmap_atomic") == 0) { ops = MAP_HASHMAP_ATOMIC; } else if (strcmp(type, "ctree") == 0) { ops = MAP_CTREE; } else if (strcmp(type, "btree") == 0) { ops = MAP_BTREE; } else if (strcmp(type, "rbtree") == 0) { ops = MAP_RBTREE; } else { fprintf(stderr, "invalid hasmap type -- '%s'\n", type); return 1; } if (access(path, F_OK) != 0) { pop = pmemobj_create(path, POBJ_LAYOUT_NAME(map), PM_HASHSET_POOL_SIZE, S_IRUSR | S_IWUSR); if (pop == NULL) { fprintf(stderr, "failed to create pool: %s\n", pmemobj_errormsg()); return 1; } struct hashmap_args args; if (argc > 3) args.seed = atoi(argv[3]); else args.seed = time(NULL); srand(args.seed); mapc = map_ctx_init(ops, pop); if (!mapc) { pmemobj_close(pop); perror("map_ctx_init"); return 1; } root = POBJ_ROOT(pop, struct root); printf("seed: %u\n", args.seed); map_new(mapc, &D_RW(root)->map, &args); map = D_RO(root)->map; } else {
int main(int argc, char *argv[]) { START(argc, argv, "heap_interrupt"); UT_COMPILE_ERROR_ON(POBJ_LAYOUT_TYPES_NUM(heap_interrupt) != 0); if (argc != 4) UT_FATAL("usage: %s file [cmd: c/o] [scenario]", argv[0]); const char *path = argv[1]; PMEMobjpool *pop = NULL; int exists = argv[2][0] == 'o'; int scenario = atoi(argv[3]); if (!exists) { if ((pop = pmemobj_create(path, POBJ_LAYOUT_NAME(heap_interrupt), 0, S_IWUSR | S_IRUSR)) == NULL) { UT_FATAL("failed to create pool\n"); } scenarios[scenario].create(pop); /* if we get here, something is wrong with function mocking */ UT_ASSERT(0); } else { if ((pop = pmemobj_open(path, POBJ_LAYOUT_NAME(heap_interrupt))) == NULL) { UT_FATAL("failed to open pool\n"); } scenarios[scenario].verify(pop); } pmemobj_close(pop); DONE(NULL); }
/* * pmemlog_open -- pool open wrapper */ PMEMlogpool * pmemlog_open(const char *path) { return (PMEMlogpool *)pmemobj_open(path, POBJ_LAYOUT_NAME(obj_pmemlog_minimal)); }
int main(int argc, char *argv[]) { unsigned int res = 0; PMEMobjpool *pop; const char *path; START(argc, argv, "ex_linkedlist"); if (argc != 2) { UT_FATAL("usage: %s file-name", argv[0]); } path = argv[1]; if (access(path, F_OK) != 0) { if ((pop = pmemobj_create(path, POBJ_LAYOUT_NAME(list), PMEMOBJ_MIN_POOL, 0666)) == NULL) { UT_FATAL("!pmemobj_create: %s", path); } } else { if ((pop = pmemobj_open(path, POBJ_LAYOUT_NAME(list))) == NULL) { UT_FATAL("!pmemobj_open: %s", path); } } TOID(struct base) base = POBJ_ROOT(pop, struct base); TOID(struct tqueuehead) tqhead = D_RO(base)->tqueue; TOID(struct slisthead) slhead = D_RO(base)->slist; TX_BEGIN(pop) { tqhead = TX_NEW(struct tqueuehead); slhead = TX_NEW(struct slisthead); } TX_ONABORT { abort(); } TX_END init_tqueue(pop, tqhead); init_slist(pop, slhead); int i = 0; TOID(struct tqnode) tqelement; POBJ_TAILQ_FOREACH(tqelement, tqhead, tnd) { if (D_RO(tqelement)->data != expectedResTQ[i]) { res = 1; break; } i++; } PRINT_RES(res, tail queue); i = 0; res = 0; TOID(struct snode) slelement; POBJ_SLIST_FOREACH(slelement, slhead, snd) { if (D_RO(slelement)->data != expectedResSL[i]) { res = 1; break; } i++; } PRINT_RES(res, singly linked list); pmemobj_close(pop); DONE(NULL); }
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); }