Skip to content

Commit ee06adb

Browse files
committed
mdb_txn_begin() cleanup
1 parent cc2a50a commit ee06adb

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

libraries/liblmdb/mdb.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,7 +2717,6 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
27172717
if (env->me_flags & MDB_RDONLY & ~flags) /* write txn in RDONLY env */
27182718
return EACCES;
27192719

2720-
size = tsize = sizeof(MDB_txn);
27212720
if (parent) {
27222721
/* Nested transactions: Max 1 child, write txns only, no writemap */
27232722
flags |= parent->mt_flags;
@@ -2727,40 +2726,31 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
27272726
return (parent->mt_flags & MDB_TXN_RDONLY) ? EINVAL : MDB_BAD_TXN;
27282727
}
27292728
/* Child txns save MDB_pgstate and use own copy of cursors */
2730-
size = tsize = sizeof(MDB_ntxn);
2731-
size += env->me_maxdbs * sizeof(MDB_cursor *);
2732-
} else if (!(flags & MDB_RDONLY)) {
2729+
size = env->me_maxdbs * (sizeof(MDB_db)+sizeof(MDB_cursor *)+1);
2730+
size += tsize = sizeof(MDB_ntxn);
2731+
} else if (flags & MDB_RDONLY) {
2732+
size = env->me_maxdbs * (sizeof(MDB_db)+1);
2733+
size += tsize = sizeof(MDB_txn);
2734+
} else {
27332735
/* Reuse preallocated write txn. However, do not touch it until
27342736
* mdb_txn_renew0() succeeds, since it currently may be active.
27352737
*/
27362738
txn = env->me_txn0;
27372739
goto renew;
27382740
}
2739-
size += env->me_maxdbs * (sizeof(MDB_db)+1);
2740-
27412741
if ((txn = calloc(1, size)) == NULL) {
27422742
DPRINTF(("calloc: %s", strerror(errno)));
27432743
return ENOMEM;
27442744
}
27452745
txn->mt_dbs = (MDB_db *) ((char *)txn + tsize);
2746-
if (flags & MDB_RDONLY) {
2747-
txn->mt_dbflags = (unsigned char *)(txn->mt_dbs + env->me_maxdbs);
2748-
txn->mt_dbiseqs = env->me_dbiseqs;
2749-
} else {
2750-
txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
2751-
if (parent) {
2752-
txn->mt_dbiseqs = parent->mt_dbiseqs;
2753-
txn->mt_dbflags = (unsigned char *)(txn->mt_cursors + env->me_maxdbs);
2754-
} else {
2755-
txn->mt_dbiseqs = (unsigned int *)(txn->mt_cursors + env->me_maxdbs);
2756-
txn->mt_dbflags = (unsigned char *)(txn->mt_dbiseqs + env->me_maxdbs);
2757-
}
2758-
}
2746+
txn->mt_dbflags = (unsigned char *)txn + size - env->me_maxdbs;
27592747
txn->mt_flags = flags;
27602748
txn->mt_env = env;
27612749

27622750
if (parent) {
27632751
unsigned int i;
2752+
txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
2753+
txn->mt_dbiseqs = parent->mt_dbiseqs;
27642754
txn->mt_u.dirty_list = malloc(sizeof(MDB_ID2)*MDB_IDL_UM_SIZE);
27652755
if (!txn->mt_u.dirty_list ||
27662756
!(txn->mt_free_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX)))
@@ -2797,15 +2787,16 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
27972787
rc = mdb_cursor_shadow(parent, txn);
27982788
if (rc)
27992789
mdb_txn_reset0(txn, "beginchild-fail");
2800-
} else {
2790+
} else { /* MDB_RDONLY */
2791+
txn->mt_dbiseqs = env->me_dbiseqs;
28012792
renew:
28022793
rc = mdb_txn_renew0(txn);
28032794
}
28042795
if (rc) {
28052796
if (txn != env->me_txn0)
28062797
free(txn);
28072798
} else {
2808-
txn->mt_flags |= flags; /* for txn==me_txn0, no effect otherwise */
2799+
txn->mt_flags |= flags; /* could not change txn=me_txn0 earlier */
28092800
*ret = txn;
28102801
DPRINTF(("begin txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
28112802
txn->mt_txnid, (flags & MDB_RDONLY) ? 'r' : 'w',

0 commit comments

Comments
 (0)