/** @defgroup internal MDB Internals
* @{
*/
-/** @defgroup compat Windows Compatibility Macros
+/** @defgroup compat Compatibility Macros
* A bunch of macros to minimize the amount of platform-specific ifdefs
* needed throughout the rest of the code. When the features this library
* needs are similar enough to POSIX to be hidden in a one-or-two line
* replacement, this macro approach is used.
* @{
*/
+
+ /** Wrapper around __func__, which is a C99 feature */
+#if __STDC_VERSION__ >= 199901L
+# define mdb_func_ __func__
+#elif __GNUC__ >= 2 || _MSC_VER >= 1300
+# define mdb_func_ __FUNCTION__
+#else
+/* If a debug message says <mdb_unknown>(), update the #if statements above */
+# define mdb_func_ "<mdb_unknown>"
+#endif
+
#ifdef _WIN32
#define MDB_USE_HASH 1
#define MDB_PIDLOCK 0
*/
# define DPRINTF(args) ((void) ((mdb_debug) && DPRINTF0 args))
# define DPRINTF0(fmt, ...) \
- fprintf(stderr, "%s:%d " fmt "\n", __func__, __LINE__, __VA_ARGS__)
+ fprintf(stderr, "%s:%d " fmt "\n", mdb_func_, __LINE__, __VA_ARGS__)
#else
# define DPRINTF(args) ((void) 0)
#endif
}
#if MDB_DEBUG
+/** Return the page number of \b mp which may be sub-page, for debug output */
+static pgno_t
+mdb_dbg_pgno(MDB_page *mp)
+{
+ pgno_t ret;
+ COPY_PGNO(ret, mp->mp_pgno);
+ return ret;
+}
+
/** Display a key in hexadecimal and return the address of the result.
* @param[in] key the key to display
* @param[in] buf the buffer to write into. Should always be #DKBUF.
DKBUF;
nkeys = NUMKEYS(mp);
- fprintf(stderr, "Page %"Z"u numkeys %d\n", mp->mp_pgno, nkeys);
+ fprintf(stderr, "Page %"Z"u numkeys %d\n", mdb_dbg_pgno(mp), nkeys);
for (i=0; i<nkeys; i++) {
node = NODEPTR(mp, i);
key.mv_size = node->mn_ksize;
off = sz - psize;
}
if ((ret = malloc(sz)) != NULL) {
+ VGMEMP_ALLOC(env, ret, sz);
if (!(env->me_flags & MDB_NOMEMINIT)) {
memset((char *)ret + off, 0, psize);
ret->mp_pad = 0;
}
- VGMEMP_ALLOC(env, ret, sz);
}
return ret;
}
unsigned int i;
MDB_env *env;
- assert(txn != NULL);
- assert(txn->mt_env != NULL);
+ if (txn == NULL || txn->mt_env == NULL)
+ return EINVAL;
if (txn->mt_child) {
rc = mdb_txn_commit(txn->mt_child);
int r2;
#endif
- assert(txn != NULL);
- assert(txn->mt_env != NULL);
-
toggle = txn->mt_txnid & 1;
DPRINTF(("writing meta page %d for root page %"Z"u",
toggle, txn->mt_dbs[MAIN_DBI].md_root));
#define CHANGELESS (MDB_FIXEDMAP|MDB_NOSUBDIR|MDB_RDONLY|MDB_WRITEMAP| \
MDB_NOTLS|MDB_NOLOCK|MDB_NORDAHEAD)
+#if VALID_FLAGS & PERSISTENT_FLAGS & (CHANGEABLE|CHANGELESS)
+# error "Persistent DB flags & env flags overlap, but both go in mm_flags"
+#endif
+
int
mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
{
nkeys = NUMKEYS(mp);
-#if MDB_DEBUG
- {
- pgno_t pgno;
- COPY_PGNO(pgno, mp->mp_pgno);
DPRINTF(("searching %u keys in %s %spage %"Z"u",
nkeys, IS_LEAF(mp) ? "leaf" : "branch", IS_SUBP(mp) ? "sub-" : "",
- pgno));
- }
-#endif
+ mdb_dbg_pgno(mp)));
low = IS_LEAF(mp) ? 0 : 1;
high = nkeys - 1;
DDBI(mc), (void *) mc));
if (mc->mc_snum >= CURSOR_STACK) {
- assert(mc->mc_snum < CURSOR_STACK);
+ mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
return MDB_CURSOR_FULL;
}
p = (MDB_page *)(env->me_map + env->me_psize * pgno);
} else {
DPRINTF(("page %"Z"u not found", pgno));
- assert(p != NULL);
+ txn->mt_flags |= MDB_TXN_ERROR;
return MDB_PAGE_NOTFOUND;
}
if (!IS_LEAF(mp)) {
DPRINTF(("internal error, index points to a %02X page!?",
mp->mp_flags));
+ mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
return MDB_CORRUPTED;
}
int exact = 0;
DKBUF;
- assert(key);
- assert(data);
+ if (key == NULL || data == NULL)
+ return EINVAL;
+
DPRINTF(("===> get db %u key [%s]", dbi, DKEY(key)));
if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
}
}
- DPRINTF(("cursor_next: top page is %"Z"u in cursor %p", mp->mp_pgno, (void *) mc));
+ DPRINTF(("cursor_next: top page is %"Z"u in cursor %p",
+ mdb_dbg_pgno(mp), (void *) mc));
if (mc->mc_flags & C_DEL)
goto skip;
skip:
DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
- mp->mp_pgno, NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
+ mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
if (IS_LEAF2(mp)) {
key->mv_size = mc->mc_db->md_pad;
}
}
- DPRINTF(("cursor_prev: top page is %"Z"u in cursor %p", mp->mp_pgno, (void *) mc));
+ DPRINTF(("cursor_prev: top page is %"Z"u in cursor %p",
+ mdb_dbg_pgno(mp), (void *) mc));
if (mc->mc_ki[mc->mc_top] == 0) {
DPUTS("=====> move to prev sibling page");
mc->mc_flags &= ~C_EOF;
DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
- mp->mp_pgno, NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
+ mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
if (IS_LEAF2(mp)) {
key->mv_size = mc->mc_db->md_pad;
int exact = 0;
int (*mfunc)(MDB_cursor *mc, MDB_val *key, MDB_val *data);
- assert(mc);
+ if (mc == NULL)
+ return EINVAL;
if (mc->mc_txn->mt_flags & MDB_TXN_ERROR)
return MDB_BAD_TXN;
* it. mp: new (sub-)page. offset: growth in page
* size. xdata: node data with new page or DB.
*/
- unsigned i, offset = 0;
+ ssize_t i, offset = 0;
mp = fp = xdata.mv_data = env->me_pbuf;
mp->mp_pgno = mc->mc_pg[mc->mc_top]->mp_pgno;
fp = olddata.mv_data;
switch (flags) {
default:
+ i = -(ssize_t)SIZELEFT(fp);
if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
- offset = EVEN(NODESIZE + sizeof(indx_t) +
- data->mv_size);
- break;
- }
- offset = fp->mp_pad;
- if (SIZELEFT(fp) < offset) {
+ offset = i += (ssize_t) EVEN(
+ sizeof(indx_t) + NODESIZE + data->mv_size);
+ } else {
+ i += offset = fp->mp_pad;
offset *= 4; /* space for 4 more */
- break;
}
- /* FALLTHRU: Big enough MDB_DUPFIXED sub-page */
+ if (i > 0)
+ break;
+ /* FALLTHRU: Sub-page is big enough */
case MDB_CURRENT:
fp->mp_flags |= P_DIRTY;
COPY_PGNO(fp->mp_pgno, mp->mp_pgno);
} else {
memcpy((char *)mp + mp->mp_upper, (char *)fp + fp->mp_upper,
olddata.mv_size - fp->mp_upper);
- for (i=0; i<NUMKEYS(fp); i++)
+ for (i = NUMKEYS(fp); --i >= 0; )
mp->mp_ptrs[i] = fp->mp_ptrs[i] + offset;
}
}
DPRINTF(("add to %s %spage %"Z"u index %i, data size %"Z"u key size %"Z"u [%s]",
IS_LEAF(mp) ? "leaf" : "branch",
IS_SUBP(mp) ? "sub-" : "",
- mp->mp_pgno, indx, data ? data->mv_size : 0,
+ mdb_dbg_pgno(mp), indx, data ? data->mv_size : 0,
key ? key->mv_size : 0, key ? DKEY(key) : "null"));
if (IS_LEAF2(mp)) {
full:
DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
- mp->mp_pgno, NUMKEYS(mp)));
+ mdb_dbg_pgno(mp), NUMKEYS(mp)));
DPRINTF(("upper-lower = %u - %u = %"Z"d", mp->mp_upper,mp->mp_lower,room));
DPRINTF(("node size = %"Z"u", node_size));
+ mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
return MDB_PAGE_FULL;
}
MDB_node *node;
char *base;
-#if MDB_DEBUG
- {
- pgno_t pgno;
- COPY_PGNO(pgno, mp->mp_pgno);
DPRINTF(("delete node %u on %s page %"Z"u", indx,
- IS_LEAF(mp) ? "leaf" : "branch", pgno));
- }
-#endif
+ IS_LEAF(mp) ? "leaf" : "branch", mdb_dbg_pgno(mp)));
assert(indx < NUMKEYS(mp));
if (IS_LEAF2(mp)) {
MDB_cursor mn;
minkeys = 1 + (IS_BRANCH(mc->mc_pg[mc->mc_top]));
-#if MDB_DEBUG
- {
- pgno_t pgno;
- COPY_PGNO(pgno, mc->mc_pg[mc->mc_top]->mp_pgno);
DPRINTF(("rebalancing %s page %"Z"u (has %u keys, %.1f%% full)",
IS_LEAF(mc->mc_pg[mc->mc_top]) ? "leaf" : "branch",
- pgno, NUMKEYS(mc->mc_pg[mc->mc_top]),
+ mdb_dbg_pgno(mc->mc_pg[mc->mc_top]), NUMKEYS(mc->mc_pg[mc->mc_top]),
(float)PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) / 10));
- }
-#endif
if (PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) >= FILL_THRESHOLD &&
NUMKEYS(mc->mc_pg[mc->mc_top]) >= minkeys) {
-#if MDB_DEBUG
- pgno_t pgno;
- COPY_PGNO(pgno, mc->mc_pg[mc->mc_top]->mp_pgno);
DPRINTF(("no need to rebalance page %"Z"u, above fill threshold",
- pgno));
-#endif
+ mdb_dbg_pgno(mc->mc_pg[mc->mc_top])));
return MDB_SUCCESS;
}
if (rc != MDB_SUCCESS)
mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
else {
- MDB_cursor *m2;
+ MDB_cursor *m2, *m3;
MDB_dbi dbi = mc->mc_dbi;
mp = mc->mc_pg[mc->mc_top];
/* Adjust other cursors pointing to mp */
for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
- if (m2 == mc || m2->mc_snum < mc->mc_snum)
+ m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
+ if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
continue;
- if (!(m2->mc_flags & C_INITIALIZED))
+ if (m3 == mc || m3->mc_snum < mc->mc_snum)
continue;
- if (m2->mc_pg[mc->mc_top] == mp) {
- if (m2->mc_ki[mc->mc_top] >= ki) {
- m2->mc_flags |= C_DEL;
- if (m2->mc_ki[mc->mc_top] > ki)
- m2->mc_ki[mc->mc_top]--;
+ if (m3->mc_pg[mc->mc_top] == mp) {
+ if (m3->mc_ki[mc->mc_top] >= ki) {
+ m3->mc_flags |= C_DEL;
+ if (m3->mc_ki[mc->mc_top] > ki)
+ m3->mc_ki[mc->mc_top]--;
}
- if (m2->mc_ki[mc->mc_top] >= nkeys)
- mdb_cursor_sibling(m2, 1);
+ if (m3->mc_ki[mc->mc_top] >= nkeys)
+ mdb_cursor_sibling(m3, 1);
}
}
mc->mc_flags |= C_DEL;
int rc, exact;
DKBUF;
- assert(key != NULL);
+ if (key == NULL)
+ return EINVAL;
DPRINTF(("====> delete db %u key [%s]", dbi, DKEY(key)));
MDB_cursor mc;
MDB_xcursor mx;
- assert(key != NULL);
- assert(data != NULL);
+ if (key == NULL || data == NULL)
+ return EINVAL;
if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
mr = env->me_txns->mti_readers;
for (i=0; i<rdrs; i++) {
if (mr[i].mr_pid) {
- size_t tid;
- tid = mr[i].mr_tid;
- if (mr[i].mr_txnid == (txnid_t)-1) {
- sprintf(buf, "%10d %"Z"x -\n", mr[i].mr_pid, tid);
- } else {
- sprintf(buf, "%10d %"Z"x %"Z"u\n", mr[i].mr_pid, tid, mr[i].mr_txnid);
- }
+ txnid_t txnid = mr[i].mr_txnid;
+ sprintf(buf, txnid == (txnid_t)-1 ?
+ "%10d %"Z"x -\n" : "%10d %"Z"x %"Z"u\n",
+ (int)mr[i].mr_pid, (size_t)mr[i].mr_tid, txnid);
if (first) {
first = 0;
rc = func(" pid thread txnid\n", ctx);