From b681b59434972fd77cbffa4ac6854298392be0ae 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: Sun, 5 Jan 2025 20:57:13 +0300 Subject: [PATCH] =?UTF-8?q?mdbx:=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3/=D0=B2=D1=8B=D1=87=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20`txn=5Fbasal=5Fcreate/destroy(?= =?UTF-8?q?)`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api-env.c | 33 +++------------------------------ src/env.c | 7 +------ src/proto.h | 2 ++ src/txn.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/api-env.c b/src/api-env.c index dfd7ca6f..cc747b80 100644 --- a/src/api-env.c +++ b/src/api-env.c @@ -482,39 +482,12 @@ __cold int mdbx_env_openW(MDBX_env *env, const wchar_t *pathname, MDBX_env_flags } if ((flags & MDBX_RDONLY) == 0) { - MDBX_txn *txn = nullptr; - const intptr_t bitmap_bytes = -#if MDBX_ENABLE_DBI_SPARSE - ceil_powerof2(env->max_dbi, CHAR_BIT * sizeof(txn->dbi_sparse[0])) / CHAR_BIT; -#else - 0; -#endif /* MDBX_ENABLE_DBI_SPARSE */ - const size_t base = sizeof(MDBX_txn) + sizeof(cursor_couple_t); - const size_t size = base + bitmap_bytes + - env->max_dbi * (sizeof(txn->dbs[0]) + sizeof(txn->cursors[0]) + sizeof(txn->dbi_seqs[0]) + - sizeof(txn->dbi_state[0])); - - txn = osal_calloc(1, size); - if (unlikely(!txn)) { - rc = MDBX_ENOMEM; - goto bailout; - } - txn->dbs = ptr_disp(txn, base); - txn->cursors = ptr_disp(txn->dbs, env->max_dbi * sizeof(txn->dbs[0])); - txn->dbi_seqs = ptr_disp(txn->cursors, env->max_dbi * sizeof(txn->cursors[0])); - txn->dbi_state = ptr_disp(txn, size - env->max_dbi * sizeof(txn->dbi_state[0])); -#if MDBX_ENABLE_DBI_SPARSE - txn->dbi_sparse = ptr_disp(txn->dbi_state, -bitmap_bytes); -#endif /* MDBX_ENABLE_DBI_SPARSE */ - txn->env = env; - txn->flags = MDBX_TXN_FINISHED; - env->basal_txn = txn; - txn->tw.retired_pages = pnl_alloc(MDBX_PNL_INITIAL); - txn->tw.repnl = pnl_alloc(MDBX_PNL_INITIAL); - if (unlikely(!txn->tw.retired_pages || !txn->tw.repnl)) { + env->basal_txn = txn_basal_create(env->max_dbi); + if (unlikely(!env->basal_txn)) { rc = MDBX_ENOMEM; goto bailout; } + env->basal_txn->env = env; env_options_adjust_defaults(env); } diff --git a/src/env.c b/src/env.c index 58ee0346..99b5d895 100644 --- a/src/env.c +++ b/src/env.c @@ -588,12 +588,7 @@ __cold int env_close(MDBX_env *env, bool resurrect_after_fork) { env->pathname.buffer = nullptr; } if (env->basal_txn) { - dpl_free(env->basal_txn); - txl_free(env->basal_txn->tw.gc.retxl); - pnl_free(env->basal_txn->tw.retired_pages); - pnl_free(env->basal_txn->tw.spilled.list); - pnl_free(env->basal_txn->tw.repnl); - osal_free(env->basal_txn); + txn_basal_destroy(env->basal_txn); env->basal_txn = nullptr; } } diff --git a/src/proto.h b/src/proto.h index 857a6794..5c58587e 100644 --- a/src/proto.h +++ b/src/proto.h @@ -70,6 +70,8 @@ MDBX_INTERNAL int txn_end(MDBX_txn *txn, unsigned mode); MDBX_INTERNAL int txn_write(MDBX_txn *txn, iov_ctx_t *ctx); MDBX_INTERNAL void txn_take_gcprof(const MDBX_txn *txn, MDBX_commit_latency *latency); MDBX_INTERNAL void txn_merge(MDBX_txn *const parent, MDBX_txn *const txn, const size_t parent_retired_len); +MDBX_INTERNAL MDBX_txn *txn_basal_create(const size_t max_dbi); +MDBX_INTERNAL void txn_basal_destroy(MDBX_txn *txn); /* env.c */ MDBX_INTERNAL int env_open(MDBX_env *env, mdbx_mode_t mode); diff --git a/src/txn.c b/src/txn.c index 478f4d07..d6932b96 100644 --- a/src/txn.c +++ b/src/txn.c @@ -1080,3 +1080,47 @@ int txn_unpark(MDBX_txn *txn) { int err = txn_end(txn, TXN_END_OUSTED | TXN_END_RESET | TXN_END_UPDATE); return err ? err : MDBX_OUSTED; } + +MDBX_txn *txn_basal_create(const size_t max_dbi) { + MDBX_txn *txn = nullptr; + const intptr_t bitmap_bytes = +#if MDBX_ENABLE_DBI_SPARSE + ceil_powerof2(max_dbi, CHAR_BIT * sizeof(txn->dbi_sparse[0])) / CHAR_BIT; +#else + 0; +#endif /* MDBX_ENABLE_DBI_SPARSE */ + const size_t base = sizeof(MDBX_txn) + /* GC cursor */ sizeof(cursor_couple_t); + const size_t size = + base + bitmap_bytes + + max_dbi * (sizeof(txn->dbs[0]) + sizeof(txn->cursors[0]) + sizeof(txn->dbi_seqs[0]) + sizeof(txn->dbi_state[0])); + + txn = osal_calloc(1, size); + if (unlikely(!txn)) + return txn; + + txn->dbs = ptr_disp(txn, base); + txn->cursors = ptr_disp(txn->dbs, max_dbi * sizeof(txn->dbs[0])); + txn->dbi_seqs = ptr_disp(txn->cursors, max_dbi * sizeof(txn->cursors[0])); + txn->dbi_state = ptr_disp(txn, size - max_dbi * sizeof(txn->dbi_state[0])); +#if MDBX_ENABLE_DBI_SPARSE + txn->dbi_sparse = ptr_disp(txn->dbi_state, -bitmap_bytes); +#endif /* MDBX_ENABLE_DBI_SPARSE */ + txn->flags = MDBX_TXN_FINISHED; + txn->tw.retired_pages = pnl_alloc(MDBX_PNL_INITIAL); + txn->tw.repnl = pnl_alloc(MDBX_PNL_INITIAL); + if (unlikely(!txn->tw.retired_pages || !txn->tw.repnl)) { + txn_basal_destroy(txn); + txn = nullptr; + } + + return txn; +} + +void txn_basal_destroy(MDBX_txn *txn) { + dpl_free(txn); + txl_free(txn->tw.gc.retxl); + pnl_free(txn->tw.retired_pages); + pnl_free(txn->tw.spilled.list); + pnl_free(txn->tw.repnl); + osal_free(txn); +}