From c616689d9ada98a21fab6e7143b2f014c92d021d Mon Sep 17 00:00:00 2001 From: Hallvard Furuseth Date: Wed, 8 Apr 2015 21:52:05 +0200 Subject: [PATCH] Cleanup MDB_env.me_txn0. More fallout from 4d02c741b120786df1b87ee9ed49c1d3f9bc7522. --- libraries/liblmdb/mdb.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c index 2444d23..1e0579c 100644 --- a/libraries/liblmdb/mdb.c +++ b/libraries/liblmdb/mdb.c @@ -2581,6 +2581,7 @@ mdb_txn_renew0(MDB_txn *txn) } txn->mt_dbxs = env->me_dbxs; /* mostly static anyway */ } else { + /* Not yet touching txn == env->me_txn0, it may be active */ if (ti) { LOCK_MUTEX_W(env); @@ -2664,7 +2665,7 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret) { MDB_txn *txn; MDB_ntxn *ntxn; - int rc, size, tsize = sizeof(MDB_txn); + int rc, size, tsize; flags &= MDB_TXN_BEGIN_FLAGS; flags |= env->me_flags & MDB_WRITEMAP; @@ -2675,6 +2676,8 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret) } if (env->me_flags & MDB_RDONLY & ~flags) /* write txn in RDONLY env */ return EACCES; + + size = tsize = sizeof(MDB_txn); if (parent) { /* Nested transactions: Max 1 child, write txns only, no writemap */ flags |= parent->mt_flags; @@ -2683,16 +2686,15 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret) { return (parent->mt_flags & MDB_TXN_RDONLY) ? EINVAL : MDB_BAD_TXN; } - tsize = sizeof(MDB_ntxn); - } - size = tsize; - if (!(flags & MDB_RDONLY)) { - if (!parent) { - txn = env->me_txn0; /* just reuse preallocated write txn */ - goto ok; - } - /* child txns use own copy of cursors */ + /* Child txns save MDB_pgstate and use own copy of cursors */ + size = tsize = sizeof(MDB_ntxn); size += env->me_maxdbs * sizeof(MDB_cursor *); + } else if (!(flags & MDB_RDONLY)) { + /* Reuse preallocated write txn. However, do not touch it until + * mdb_txn_renew0() succeeds, since it currently may be active. + */ + txn = env->me_txn0; + goto renew; } size += env->me_maxdbs * (sizeof(MDB_db)+1); @@ -2717,7 +2719,6 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret) txn->mt_flags = flags; txn->mt_env = env; -ok: if (parent) { unsigned int i; txn->mt_u.dirty_list = malloc(sizeof(MDB_ID2)*MDB_IDL_UM_SIZE); @@ -2757,6 +2758,7 @@ ok: if (rc) mdb_txn_reset0(txn, "beginchild-fail"); } else { +renew: rc = mdb_txn_renew0(txn); } if (rc) { @@ -4684,15 +4686,13 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode if (rc) goto leave; } - if (!((flags & MDB_RDONLY) || - (env->me_pbuf = calloc(1, env->me_psize)))) - rc = ENOMEM; if (!(flags & MDB_RDONLY)) { MDB_txn *txn; int tsize = sizeof(MDB_txn), size = tsize + env->me_maxdbs * (sizeof(MDB_db)+sizeof(MDB_cursor *)+sizeof(unsigned int)+1); - txn = calloc(1, size); - if (txn) { + if ((env->me_pbuf = calloc(1, env->me_psize)) && + (txn = calloc(1, size))) + { txn->mt_dbs = (MDB_db *)((char *)txn + tsize); txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs); txn->mt_dbiseqs = (unsigned int *)(txn->mt_cursors + env->me_maxdbs);