From bd672a558342bff6d36ada4ea2ba9ec4ffc9a2a9 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Mon, 20 Aug 2018 12:30:09 +0300 Subject: [PATCH] mdbx: backport - add mdbx_limits_xyz() (squashed). Change-Id: I56c79704c59386a0c4d84b001020484c23925e6c --- mdbx.h | 7 ++++++- src/mdbx.c | 45 +++++++++++++++++++++++++++++++++++++-------- test/main.cc | 6 +++--- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/mdbx.h b/mdbx.h index 276c8ea6..2d15cc24 100644 --- a/mdbx.h +++ b/mdbx.h @@ -900,7 +900,7 @@ LIBMDBX_API int mdbx_env_set_maxdbs(MDBX_env *env, MDBX_dbi dbs); * * Returns The maximum size of a key we can write. */ LIBMDBX_API int mdbx_env_get_maxkeysize(MDBX_env *env); -LIBMDBX_API int mdbx_get_maxkeysize(size_t pagesize); +LIBMDBX_API int mdbx_get_maxkeysize(intptr_t pagesize); /* Set application information associated with the MDBX_env. * @@ -1673,6 +1673,11 @@ LIBMDBX_API int mdbx_is_dirty(const MDBX_txn *txn, const void *ptr); LIBMDBX_API int mdbx_dbi_sequence(MDBX_txn *txn, MDBX_dbi dbi, uint64_t *result, uint64_t increment); +LIBMDBX_API int mdbx_limits_pgsize_min(void); +LIBMDBX_API int mdbx_limits_pgsize_max(void); +LIBMDBX_API intptr_t mdbx_limits_dbsize_min(intptr_t pagesize); +LIBMDBX_API intptr_t mdbx_limits_dbsize_max(intptr_t pagesize); + /*----------------------------------------------------------------------------*/ /* attribute support functions for Nexenta */ typedef uint_fast64_t mdbx_attr_t; diff --git a/src/mdbx.c b/src/mdbx.c index 95e74ca4..d5a369c9 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -4897,16 +4897,15 @@ int __cold mdbx_env_get_maxkeysize(MDBX_env *env) { #define mdbx_maxgc_ov1page(pagesize) \ (((pagesize)-PAGEHDRSZ) / sizeof(pgno_t) - 1) -int mdbx_get_maxkeysize(size_t pagesize) { - if (pagesize == 0) - pagesize = mdbx_syspagesize(); - - intptr_t nodemax = mdbx_nodemax(pagesize); - if (nodemax < 0) +int mdbx_get_maxkeysize(intptr_t pagesize) { + if (pagesize < 1) + pagesize = (intptr_t)mdbx_syspagesize(); + else if (unlikely(pagesize < (intptr_t)MIN_PAGESIZE || + pagesize > (intptr_t)MAX_PAGESIZE || + !mdbx_is_power2((size_t)pagesize))) return -MDBX_EINVAL; - intptr_t maxkey = mdbx_maxkey(nodemax); - return (maxkey > 0 && maxkey < INT_MAX) ? (int)maxkey : -MDBX_EINVAL; + return mdbx_maxkey(mdbx_nodemax(pagesize)); } static void __cold mdbx_setup_pagesize(MDBX_env *env, const size_t pagesize) { @@ -12233,6 +12232,36 @@ int mdbx_dbi_sequence(MDBX_txn *txn, MDBX_dbi dbi, uint64_t *result, return MDBX_SUCCESS; } +/*----------------------------------------------------------------------------*/ + +__cold int mdbx_limits_pgsize_min(void) { return MIN_PAGESIZE; } + +__cold int mdbx_limits_pgsize_max(void) { return MAX_PAGESIZE; } + +__cold intptr_t mdbx_limits_dbsize_min(intptr_t pagesize) { + if (pagesize < 1) + pagesize = (intptr_t)mdbx_syspagesize(); + else if (unlikely(pagesize < (intptr_t)MIN_PAGESIZE || + pagesize > (intptr_t)MAX_PAGESIZE || + !mdbx_is_power2((size_t)pagesize))) + return -MDBX_EINVAL; + + return MIN_PAGENO * pagesize; +} + +__cold intptr_t mdbx_limits_dbsize_max(intptr_t pagesize) { + if (pagesize < 1) + pagesize = (intptr_t)mdbx_syspagesize(); + else if (unlikely(pagesize < (intptr_t)MIN_PAGESIZE || + pagesize > (intptr_t)MAX_PAGESIZE || + !mdbx_is_power2((size_t)pagesize))) + return -MDBX_EINVAL; + + const uint64_t limit = MAX_PAGENO * (uint64_t)pagesize; + return (limit < (intptr_t)MAX_MAPSIZE) ? (intptr_t)limit + : (intptr_t)MAX_PAGESIZE; +} + /*----------------------------------------------------------------------------*/ /* attribute support functions for Nexenta */ diff --git a/test/main.cc b/test/main.cc index e6e0e177..d417480d 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -195,8 +195,8 @@ int main(int argc, char *const argv[]) { } if (config::parse_option( argc, argv, narg, "keylen.max", params.keylen_max, config::no_scale, - 0, std::min(mdbx_get_maxkeysize(0), (int)UINT16_MAX))) { - + 0, + std::min((unsigned)mdbx_get_maxkeysize(0), (unsigned)UINT16_MAX))) { if (params.keylen_min > params.keylen_max) params.keylen_min = params.keylen_max; continue;