/* * NROOT -- Create the root of a new tree. * * Parameters: * t: tree * * Returns: * RET_ERROR, RET_SUCCESS */ static int nroot(BTREE *t) { PAGE *meta, *root; pgno_t npg; if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { mpool_put(t->bt_mp, meta, 0); return (RET_SUCCESS); } if (errno != EINVAL) /* It's OK to not exist. */ return (RET_ERROR); errno = 0; if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) return (RET_ERROR); if ((root = mpool_new(t->bt_mp, &npg)) == NULL) return (RET_ERROR); if (npg != P_ROOT) return (RET_ERROR); root->pgno = npg; root->prevpg = root->nextpg = P_INVALID; root->lower = BTDATAOFF; root->upper = t->bt_psize; root->flags = P_BLEAF; memset(meta, 0, t->bt_psize); mpool_put(t->bt_mp, meta, MPOOL_DIRTY); mpool_put(t->bt_mp, root, MPOOL_DIRTY); return (RET_SUCCESS); }
/* * __bt_new -- * Get a new page, preferably from the freelist. * * Parameters: * t: tree * npg: storage for page number. * * Returns: * Pointer to a page, NULL on error. */ PAGE * __bt_new(BTREE *t, pgno_t *npg) { PAGE *h; if (t->bt_free != P_INVALID && (h = mpool_get(t->bt_mp, t->bt_free, 0)) != NULL) { *npg = t->bt_free; t->bt_free = h->nextpg; F_SET(t, B_METADIRTY); return (h); } return (mpool_new(t->bt_mp, npg, MPOOL_PAGE_NEXT)); }
void test_mpool() { struct mpool* m = mpool_new(1024); int i; void* last_p = NULL; for (i=0; i<1024/4; ++i) { void *p = mpool_alloc(m, 4); if (last_p) { assert((p-last_p) == 4); } last_p = p; } void* p = mpool_alloc(m, 1024); p = mpool_alloc(m, 1025); p = mpool_alloc(m, 1020); p = mpool_alloc(m, 10240); mpool_dump(m); mpool_delete(m); }
void * __mpool_new__44bsd(MPOOL *mp, pgno_t *pgnoaddr) { return (mpool_new(mp, pgnoaddr, MPOOL_PAGE_NEXT)); }