mdbx: добавление cursor_reset() и cursor_drown().

This commit is contained in:
Леонид Юрьев (Leonid Yuriev) 2025-03-06 17:00:21 +03:00
parent 333069e7a8
commit 1299653457
3 changed files with 32 additions and 16 deletions

View File

@ -12,8 +12,7 @@ MDBX_cursor *mdbx_cursor_create(void *context) {
couple->outer.signature = cur_signature_ready4dispose; couple->outer.signature = cur_signature_ready4dispose;
couple->outer.next = &couple->outer; couple->outer.next = &couple->outer;
couple->userctx = context; couple->userctx = context;
couple->outer.top_and_flags = z_poor_mark; cursor_reset(couple);
couple->inner.cursor.top_and_flags = z_poor_mark | z_inner;
VALGRIND_MAKE_MEM_DEFINED(&couple->outer.backup, sizeof(couple->outer.backup)); VALGRIND_MAKE_MEM_DEFINED(&couple->outer.backup, sizeof(couple->outer.backup));
VALGRIND_MAKE_MEM_DEFINED(&couple->outer.tree, sizeof(couple->outer.tree)); VALGRIND_MAKE_MEM_DEFINED(&couple->outer.tree, sizeof(couple->outer.tree));
VALGRIND_MAKE_MEM_DEFINED(&couple->outer.clc, sizeof(couple->outer.clc)); VALGRIND_MAKE_MEM_DEFINED(&couple->outer.clc, sizeof(couple->outer.clc));
@ -31,13 +30,15 @@ int mdbx_cursor_reset(MDBX_cursor *mc) {
if (unlikely(!mc)) if (unlikely(!mc))
return LOG_IFERR(MDBX_EINVAL); return LOG_IFERR(MDBX_EINVAL);
if (unlikely(mc->signature != cur_signature_ready4dispose && mc->signature != cur_signature_live)) if (likely(mc->signature == cur_signature_live)) {
return LOG_IFERR(MDBX_EBADSIGN); cursor_reset((cursor_couple_t *)mc);
cursor_couple_t *couple = (cursor_couple_t *)mc;
couple->outer.top_and_flags = z_poor_mark;
couple->inner.cursor.top_and_flags = z_poor_mark | z_inner;
return MDBX_SUCCESS; return MDBX_SUCCESS;
}
if (likely(mc->signature == cur_signature_ready4dispose))
return MDBX_SUCCESS;
return LOG_IFERR(MDBX_EBADSIGN);
} }
int mdbx_cursor_bind(MDBX_txn *txn, MDBX_cursor *mc, MDBX_dbi dbi) { int mdbx_cursor_bind(MDBX_txn *txn, MDBX_cursor *mc, MDBX_dbi dbi) {
@ -113,7 +114,7 @@ int mdbx_cursor_unbind(MDBX_cursor *mc) {
} }
mc->next = mc; mc->next = mc;
} }
be_poor(mc); cursor_drown((cursor_couple_t *)mc);
mc->signature = cur_signature_ready4dispose; mc->signature = cur_signature_ready4dispose;
return MDBX_SUCCESS; return MDBX_SUCCESS;
} }
@ -222,10 +223,9 @@ int mdbx_txn_release_all_cursors_ex(const MDBX_txn *txn, bool unbind, size_t *co
ENSURE(nullptr, mc->signature == cur_signature_live && (mc->next != mc) && !mc->backup); ENSURE(nullptr, mc->signature == cur_signature_live && (mc->next != mc) && !mc->backup);
txn->cursors[i] = mc->next; txn->cursors[i] = mc->next;
mc->next = mc; mc->next = mc;
if (unbind) {
be_poor(mc);
mc->signature = cur_signature_ready4dispose; mc->signature = cur_signature_ready4dispose;
} else { cursor_drown((cursor_couple_t *)mc);
if (!unbind) {
mc->signature = 0; mc->signature = 0;
osal_free(mc); osal_free(mc);
} }

View File

@ -216,7 +216,6 @@ int cursor_shadow(MDBX_cursor *cursor, MDBX_txn *nested, const size_t dbi) {
MDBX_cursor *cursor_eot(MDBX_cursor *cursor, MDBX_txn *txn) { MDBX_cursor *cursor_eot(MDBX_cursor *cursor, MDBX_txn *txn) {
MDBX_cursor *const next = cursor->next; MDBX_cursor *const next = cursor->next;
cursor->next = cursor;
const unsigned stage = cursor->signature; const unsigned stage = cursor->signature;
MDBX_cursor *const shadow = cursor->backup; MDBX_cursor *const shadow = cursor->backup;
ENSURE(txn->env, stage == cur_signature_live || (stage == cur_signature_wait4eot && shadow)); ENSURE(txn->env, stage == cur_signature_live || (stage == cur_signature_wait4eot && shadow));
@ -224,7 +223,7 @@ MDBX_cursor *cursor_eot(MDBX_cursor *cursor, MDBX_txn *txn) {
if (shadow) { if (shadow) {
subcur_t *subcur = cursor->subcur; subcur_t *subcur = cursor->subcur;
tASSERT(txn, txn->parent != nullptr && shadow->txn == txn->parent); tASSERT(txn, txn->parent != nullptr && shadow->txn == txn->parent);
/* Zap: Using uninitialized memory '*mc->backup'. */ /* Zap: Using uninitialized memory '*subcur->backup'. */
MDBX_SUPPRESS_GOOFY_MSVC_ANALYZER(6001); MDBX_SUPPRESS_GOOFY_MSVC_ANALYZER(6001);
ENSURE(txn->env, shadow->signature == cur_signature_live); ENSURE(txn->env, shadow->signature == cur_signature_live);
tASSERT(txn, subcur == shadow->subcur); tASSERT(txn, subcur == shadow->subcur);
@ -250,7 +249,8 @@ MDBX_cursor *cursor_eot(MDBX_cursor *cursor, MDBX_txn *txn) {
osal_free(shadow); osal_free(shadow);
} else { } else {
ENSURE(cursor->txn->env, stage == cur_signature_live); ENSURE(cursor->txn->env, stage == cur_signature_live);
be_poor(cursor); cursor_drown((cursor_couple_t *)cursor);
cursor->next = cursor;
cursor->signature = cur_signature_ready4dispose /* Cursor may be reused */; cursor->signature = cur_signature_ready4dispose /* Cursor may be reused */;
} }
return next; return next;

View File

@ -355,3 +355,19 @@ MDBX_MAYBE_UNUSED static inline void cursor_inner_refresh(const MDBX_cursor *mc,
} }
MDBX_MAYBE_UNUSED MDBX_INTERNAL bool cursor_is_tracked(const MDBX_cursor *mc); MDBX_MAYBE_UNUSED MDBX_INTERNAL bool cursor_is_tracked(const MDBX_cursor *mc);
static inline void cursor_reset(cursor_couple_t *couple) {
couple->outer.top_and_flags = z_fresh_mark;
couple->inner.cursor.top_and_flags = z_fresh_mark | z_inner;
}
static inline void cursor_drown(cursor_couple_t *couple) {
couple->outer.top_and_flags = z_poor_mark;
couple->inner.cursor.top_and_flags = z_poor_mark | z_inner;
couple->outer.txn = nullptr;
couple->inner.cursor.txn = nullptr;
couple->outer.tree = nullptr;
/* сохраняем clc-указатель, так он используется для вычисления dbi в mdbx_cursor_renew(). */
couple->outer.dbi_state = nullptr;
couple->inner.cursor.dbi_state = nullptr;
}