From 12996534578771ea590efb321c0d1145b13cf9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=AE=D1=80=D1=8C?= =?UTF-8?q?=D0=B5=D0=B2=20=28Leonid=20Yuriev=29?= Date: Thu, 6 Mar 2025 17:00:21 +0300 Subject: [PATCH] =?UTF-8?q?mdbx:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20`cursor=5Freset()`=20=D0=B8=20`curso?= =?UTF-8?q?r=5Fdrown()`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api-cursor.c | 26 +++++++++++++------------- src/cursor.c | 6 +++--- src/cursor.h | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/api-cursor.c b/src/api-cursor.c index 05c98b4b..ca6c2b18 100644 --- a/src/api-cursor.c +++ b/src/api-cursor.c @@ -12,8 +12,7 @@ MDBX_cursor *mdbx_cursor_create(void *context) { couple->outer.signature = cur_signature_ready4dispose; couple->outer.next = &couple->outer; couple->userctx = context; - couple->outer.top_and_flags = z_poor_mark; - couple->inner.cursor.top_and_flags = z_poor_mark | z_inner; + cursor_reset(couple); 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.clc, sizeof(couple->outer.clc)); @@ -31,13 +30,15 @@ int mdbx_cursor_reset(MDBX_cursor *mc) { if (unlikely(!mc)) return LOG_IFERR(MDBX_EINVAL); - if (unlikely(mc->signature != cur_signature_ready4dispose && mc->signature != cur_signature_live)) - return LOG_IFERR(MDBX_EBADSIGN); + if (likely(mc->signature == cur_signature_live)) { + cursor_reset((cursor_couple_t *)mc); + return MDBX_SUCCESS; + } - 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; + 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) { @@ -113,7 +114,7 @@ int mdbx_cursor_unbind(MDBX_cursor *mc) { } mc->next = mc; } - be_poor(mc); + cursor_drown((cursor_couple_t *)mc); mc->signature = cur_signature_ready4dispose; 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); txn->cursors[i] = mc->next; mc->next = mc; - if (unbind) { - be_poor(mc); - mc->signature = cur_signature_ready4dispose; - } else { + mc->signature = cur_signature_ready4dispose; + cursor_drown((cursor_couple_t *)mc); + if (!unbind) { mc->signature = 0; osal_free(mc); } diff --git a/src/cursor.c b/src/cursor.c index 1fd8cbc1..d8199748 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -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 *const next = cursor->next; - cursor->next = cursor; const unsigned stage = cursor->signature; MDBX_cursor *const shadow = cursor->backup; 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) { subcur_t *subcur = cursor->subcur; 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); ENSURE(txn->env, shadow->signature == cur_signature_live); tASSERT(txn, subcur == shadow->subcur); @@ -250,7 +249,8 @@ MDBX_cursor *cursor_eot(MDBX_cursor *cursor, MDBX_txn *txn) { osal_free(shadow); } else { 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 */; } return next; diff --git a/src/cursor.h b/src/cursor.h index e53e91db..c8854faf 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -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); + +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; +}