mdbx: big-bang (initial).

- OS Abstraction Layer;
 - Windows Support.
 - preparation for more changes.

Change-Id: I53772eda9091ba361cbc9a28656190ea0d4c5cee
This commit is contained in:
Leo Yuriev 2017-03-16 18:09:27 +03:00
parent 95e606606a
commit 0f49ed6e53
33 changed files with 3678 additions and 2498 deletions

3
.clang-format Normal file
View File

@ -0,0 +1,3 @@
BasedOnStyle: LLVM
Standard: Cpp11
ReflowComments: true

27
AUTHORS Normal file
View File

@ -0,0 +1,27 @@
Contributors
============
Alexey Naumov <alexey.naumov@gmail.com>
Chris Mikkelson <cmikk@qwest.net>
Claude Brisson <claude.brisson@gmail.com>
David Barbour <dmbarbour@gmail.com>
David Wilson <dw@botanicus.net>
Hallvard Furuseth <hallvard@openldap.org>, <h.b.furuseth@usit.uio.no>
Heiko Becker <heirecka@exherbo.org>
Howard Chu <hyc@openldap.org>, <hyc@symas.com>
Ignacio Casal Quinteiro <ignacio.casal@nice-software.com>
Jean-Christophe DUBOIS <jcd@tribudubois.net>
John Hewson <john@jahewson.com>
Kurt Zeilenga <kurt.zeilenga@isode.com>
Leonid Yuriev <leo@yuriev.ru>, <lyuryev@ptsecurity.com>
Lorenz Bauer <lmb@cloudflare.com>
Luke Yeager <lyeager@nvidia.com>
Martin Hedenfalk <martin@bzero.se>
Ondrej Kuznik <ondrej.kuznik@acision.com>
Orivej Desh <orivej@gmx.fr>
Oskari Timperi <oskari.timperi@iki.fi>
Pavel Medvedev <pmedvedev@gmail.com>
Philipp Storz <philipp.storz@bareos.com>
Quanah Gibson-Mount <quanah@openldap.org>
Salvador Ortiz <sog@msg.com.mx>
Sebastien Launay <sebastien@slaunay.fr>

View File

@ -23,9 +23,9 @@ mandir ?= $(prefix)/man
suffix ?=
CC ?= gcc
XCFLAGS ?= -DNDEBUG=1 -DMDB_DEBUG=0
CFLAGS ?= -O2 -g3 -Wall -Werror -Wextra -ffunction-sections -fPIC
CFLAGS += -std=gnu99 -pthread $(XCFLAGS)
XCFLAGS ?= -DNDEBUG=1 -DMDB_DEBUG=0 -DMDBX_EXPORTS=1
CFLAGS ?= -O2 -g3 -Wall -Werror -Wextra -ffunction-sections -fPIC -fvisibility=hidden
CFLAGS += -D_GNU_SOURCE=1 -std=gnu99 -pthread $(XCFLAGS)
COVER ?= -coverage -fprofile-arcs -ftest-coverage -O0
# LY: for ability to built with modern glibc,
@ -44,7 +44,7 @@ MANPAGES := mdbx_stat.1 mdbx_copy.1 mdbx_dump.1 mdbx_load.1
TESTS := test0 test1 test2 test3 test4 test5 test6 test_bench \
test_yota1 test_yota2
SRC_MDBX := $(add_prefix src/, mdbx.c mdbx.h reopen.h barriers.h midl.h)
MDBX_SRC := mdbx.h $(addprefix src/, mdbx.c osal.c lck-posix.c defs.h bits.h osal.h midl.h)
.PHONY: mdbx all install clean check tests coverage
@ -80,22 +80,28 @@ check: tests
&& echo "*** LMDB-TEST-6" && ./test6 && ./mdbx_chk -v tmp.db \
&& echo "*** LMDB-TESTs - all done"
mdbx.o: $(SRC_MDBX) Makefile
mdbx.o: $(MDBX_SRC) Makefile
$(CC) $(CFLAGS) -c src/mdbx.c -o $@
libmdbx.a: mdbx.o
$(AR) rs $@ $^
osal.o: $(MDBX_SRC) Makefile
$(CC) $(CFLAGS) -c src/osal.c -o $@
libmdbx.so: mdbx.o
lck-posix.o: $(MDBX_SRC) Makefile
$(CC) $(CFLAGS) -c src/lck-posix.c -o $@
libmdbx.a: mdbx.o osal.o lck-posix.o
$(AR) rs $@ $?
libmdbx.so: mdbx.o osal.o lck-posix.o
$(CC) $(CFLAGS) $(LDFLAGS) -save-temps -pthread -shared $(LDOPS) -o $@ $^
mdbx_%: src/mdbx_%.c mdbx.o
mdbx_%: src/tools/mdbx_%.c libmdbx.a
$(CC) $(CFLAGS) $(LDFLAGS) $(LDOPS) -o $@ $^
test%: test/test%.c mdbx.o
test%: test/test%.c libmdbx.a
$(CC) $(CFLAGS) $(LDFLAGS) -Isrc -o $@ $^
gcov-mdbx.o: $(SRC_MDBX) Makefile
gcov-mdbx.o: $(MDBX_SRC) Makefile
$(CC) $(CFLAGS) $(COVER) -c src/mdbx.c -o $@
# Seem this useless :(

View File

@ -1,5 +1,17 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*
* ---
*
* This code is derived from "LMDB engine" written by
* Howard Chu (Symas Corporation), which itself derived from btree.c
@ -35,48 +47,140 @@
*/
#pragma once
/* *INDENT-OFF* */
/* clang-format off */
#ifndef _MDBX_H_
#define _MDBX_H_
#define MDBX_MODE_ENABLED 1
#ifndef __has_attribute
# define __has_attribute(x) (0)
#endif
#ifndef __dll_export
# if defined(_WIN32) || defined(__CYGWIN__)
# if defined(__GNUC__) || __has_attribute(dllexport)
# define __dll_export __attribute__((dllexport))
# elif defined(_MSC_VER)
# define __dll_export __declspec(dllexport)
# else
# define __dll_export
# endif
# elif defined(__GNUC__) || __has_attribute(visibility)
# define __dll_export __attribute__((visibility("default")))
# else
# define __dll_export
# endif
#endif /* __dll_export */
#ifndef __dll_import
# if defined(_WIN32) || defined(__CYGWIN__)
# if defined(__GNUC__) || __has_attribute(dllimport)
# define __dll_import __attribute__((dllimport))
# elif defined(_MSC_VER)
# define __dll_import __declspec(dllimport)
# else
# define __dll_import
# endif
# else
# define __dll_import
# endif
#endif /* __dll_import */
#if defined(LIBMDBX_EXPORTS)
# define LIBMDBX_API __dll_export
#elif defined(MDBX_IMPORTS)
# define LIBMDBX_API __dll_import
#else
# define LIBMDBX_API
#endif /* LIBMDBX_API */
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4514) /* 'xyz': unreferenced inline function \
has been removed */
#pragma warning(disable : 4710) /* 'xyz': function not inlined */
#pragma warning(disable : 4711) /* function 'xyz' selected for \
automatic inline expansion */
#pragma warning(disable : 4061) /* enumerator 'abc' in switch of enum \
'xyz' is not explicitly handled by a case \
label */
#pragma warning(disable : 4201) /* nonstandard extension used : \
nameless struct / union */
#pragma warning(disable : 4127) /* conditional expression is constant \
*/
#pragma warning(push, 1)
#pragma warning(disable : 4530) /* C++ exception handler used, but \
unwind semantics are not enabled. Specify \
/EHsc */
#pragma warning(disable : 4577) /* 'noexcept' used with no exception \
handling mode specified; termination on \
exception is not guaranteed. Specify /EHsc \
*/
#endif /* _MSC_VER (warnings) */
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#if defined(_WIN32) || defined(_WIN64)
# include <windows.h>
# include <winnt.h>
typedef unsigned mode_t;
typedef HANDLE mdbx_filehandle_t;
typedef DWORD mdbx_pid_t;
typedef DWORD mdbx_tid_t;
#else
# include <pthread.h> /* for pthread_t */
# include <sys/uio.h> /* for truct iovec */
# include <sys/types.h> /* for pid_t */
# define HAVE_STRUCT_IOVEC 1
typedef int mdbx_filehandle_t;
typedef pid_t mdbx_pid_t;
typedef pthread_t mdbx_tid_t;
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
/* *INDENT-ON* */
/* clang-format on */
#ifdef __cplusplus
extern "C" {
#endif
/** Library major version */
#define MDB_VERSION_MAJOR 0
#define MDBX_VERSION_MAJOR 0
/** Library minor version */
#define MDB_VERSION_MINOR 9
#define MDBX_VERSION_MINOR 2
/** Library patch version */
#define MDB_VERSION_PATCH 19
#define MDBX_VERSION_PATCH 0
/** Combine args a,b,c into a single integer for easy version comparisons */
#define MDB_VERINT(a, b, c) (((a) << 24) | ((b) << 16) | (c))
/** The full library version as a single integer */
#define MDB_VERSION_FULL \
MDB_VERINT(MDB_VERSION_MAJOR, MDB_VERSION_MINOR, MDB_VERSION_PATCH)
#define MDBX_VERSION_FULL \
MDB_VERINT(MDBX_VERSION_MAJOR, MDBX_VERSION_MINOR, MDBX_VERSION_PATCH)
/* The release date of this library version */
#define MDB_VERSION_DATE "DEVEL"
#define MDBX_VERSION_DATE "DEVEL"
/* A stringifier for the version info */
#define MDB_VERSTR(a, b, c, d) \
#define MDBX_VERSTR(a, b, c, d) \
"MDBX " #a "." #b "." #c ": (" d ", https://github.com/ReOpen/libmdbx)"
/* A helper for the stringifier macro */
#define MDB_VERFOO(a, b, c, d) MDB_VERSTR(a, b, c, d)
#define MDBX_VERFOO(a, b, c, d) MDBX_VERSTR(a, b, c, d)
/* The full library version as a C string */
#define MDB_VERSION_STRING \
MDB_VERFOO(MDB_VERSION_MAJOR, MDB_VERSION_MINOR, MDB_VERSION_PATCH, \
MDB_VERSION_DATE)
#define MDBX_VERSION_STRING \
MDBX_VERFOO(MDBX_VERSION_MAJOR, MDBX_VERSION_MINOR, MDBX_VERSION_PATCH, \
MDBX_VERSION_DATE)
/* Opaque structure for a database environment.
*
@ -109,6 +213,14 @@ typedef struct MDB_cursor MDB_cursor;
* The same applies to data sizes in databases with the MDB_DUPSORT flag.
* Other data items can in theory be from 0 to 0xffffffff bytes long.
*/
#ifndef HAVE_STRUCT_IOVEC
struct iovec {
void *iov_base;
size_t iov_len;
};
#define HAVE_STRUCT_IOVEC
#endif /* HAVE_STRUCT_IOVEC */
typedef struct iovec MDB_val;
#define mv_size iov_len
#define mv_data iov_base
@ -117,87 +229,85 @@ typedef struct iovec MDB_val;
typedef int(MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
/* Environment Flags */
/* mmap at a fixed address (experimental) */
#define MDB_FIXEDMAP 0x01
/* no environment directory */
#define MDB_NOSUBDIR 0x4000
#define MDB_NOSUBDIR 0x4000u
/* don't fsync after commit */
#define MDB_NOSYNC 0x10000
#define MDB_NOSYNC 0x10000u
/* read only */
#define MDB_RDONLY 0x20000
#define MDB_RDONLY 0x20000u
/* don't fsync metapage after commit */
#define MDB_NOMETASYNC 0x40000
#define MDB_NOMETASYNC 0x40000u
/* use writable mmap */
#define MDB_WRITEMAP 0x80000
#define MDB_WRITEMAP 0x80000u
/* use asynchronous msync when MDB_WRITEMAP is used */
#define MDB_MAPASYNC 0x100000
#define MDB_MAPASYNC 0x100000u
/* tie reader locktable slots to MDB_txn objects instead of to threads */
#define MDB_NOTLS 0x200000
#define MDB_NOTLS 0x200000u
/* don't do any locking, caller must manage their own locks
* WARNING: libmdbx don't support this mode. */
#define MDB_NOLOCK__UNSUPPORTED 0x400000
#define MDB_NOLOCK__UNSUPPORTED 0x400000u
/* don't do readahead */
#define MDB_NORDAHEAD 0x800000
#define MDB_NORDAHEAD 0x800000u
/* don't initialize malloc'd memory before writing to datafile */
#define MDB_NOMEMINIT 0x1000000
#define MDB_NOMEMINIT 0x1000000u
#if MDBX_MODE_ENABLED
/* aim to coalesce FreeDB records */
#define MDBX_COALESCE 0x2000000
#define MDBX_COALESCE 0x2000000u
/* LIFO policy for reclaiming FreeDB records */
#define MDBX_LIFORECLAIM 0x4000000
#define MDBX_LIFORECLAIM 0x4000000u
#endif /* MDBX_MODE_ENABLED */
/* make a steady-sync only on close and explicit env-sync */
#define MDBX_UTTERLY_NOSYNC (MDB_NOSYNC | MDB_MAPASYNC)
/* debuging option, fill/perturb released pages */
#define MDBX_PAGEPERTURB 0x8000000
#define MDBX_PAGEPERTURB 0x8000000u
/* Database Flags */
/* use reverse string keys */
#define MDB_REVERSEKEY 0x02
#define MDB_REVERSEKEY 0x02u
/* use sorted duplicates */
#define MDB_DUPSORT 0x04
#define MDB_DUPSORT 0x04u
/* numeric keys in native byte order, either unsigned int or mdbx_size_t.
* (lmdb expects 32-bit int <= size_t <= 32/64-bit mdbx_size_t.)
* The keys must all be of the same size. */
#define MDB_INTEGERKEY 0x08
#define MDB_INTEGERKEY 0x08u
/* with MDB_DUPSORT, sorted dup items have fixed size */
#define MDB_DUPFIXED 0x10
#define MDB_DUPFIXED 0x10u
/* with MDB_DUPSORT, dups are MDB_INTEGERKEY-style integers */
#define MDB_INTEGERDUP 0x20
#define MDB_INTEGERDUP 0x20u
/* with MDB_DUPSORT, use reverse string dups */
#define MDB_REVERSEDUP 0x40
#define MDB_REVERSEDUP 0x40u
/* create DB if not already existing */
#define MDB_CREATE 0x40000
#define MDB_CREATE 0x40000u
/* Write Flags */
/* For put: Don't write if the key already exists. */
#define MDB_NOOVERWRITE 0x10
#define MDB_NOOVERWRITE 0x10u
/* Only for MDB_DUPSORT
* For put: don't write if the key and data pair already exist.
* For mdbx_cursor_del: remove all duplicate data items.
*/
#define MDB_NODUPDATA 0x20
#define MDB_NODUPDATA 0x20u
/* For mdbx_cursor_put: overwrite the current key/data pair
* MDBX allows this flag for mdbx_put() for explicit overwrite/update without
* insertion. */
#define MDB_CURRENT 0x40
#define MDB_CURRENT 0x40u
/* For put: Just reserve space for data, don't copy it. Return a
* pointer to the reserved space.
*/
#define MDB_RESERVE 0x10000
#define MDB_RESERVE 0x10000u
/* Data is being appended, don't split full pages. */
#define MDB_APPEND 0x20000
#define MDB_APPEND 0x20000u
/* Duplicate data is being appended, don't split full pages. */
#define MDB_APPENDDUP 0x40000
#define MDB_APPENDDUP 0x40000u
/* Store multiple data items in one call. Only for MDB_DUPFIXED. */
#define MDB_MULTIPLE 0x80000
#define MDB_MULTIPLE 0x80000u
/* Copy Flags */
/* Compacting copy: Omit free space from copy, and renumber all
* pages sequentially. */
#define MDB_CP_COMPACT 1
#define MDB_CP_COMPACT 1u
/* Cursor Get operations.
*
@ -235,8 +345,8 @@ typedef enum MDB_cursor_op {
MDB_PREV_NODUP, /* Position at last data item of previous key */
MDB_SET, /* Position at specified key */
MDB_SET_KEY, /* Position at specified key, return key + data */
MDB_SET_RANGE, /* Position at first key greater than or equal to specified
key. */
MDB_SET_RANGE, /* Position at first key greater than or equal to specified
key. */
MDB_PREV_MULTIPLE /* Position at previous page and return key and up to
a page of duplicate data items.
Only for MDB_DUPFIXED */
@ -280,7 +390,8 @@ typedef enum MDB_cursor_op {
#define MDB_MAP_RESIZED (-30785)
/* Operation and DB incompatible, or DB type changed. This can mean:
* - The operation expects an MDB_DUPSORT / MDB_DUPFIXED database.
* - Opening a named DB when the unnamed DB has MDB_DUPSORT/MDB_INTEGERKEY.
* - Opening a named DB when the unnamed DB has
*MDB_DUPSORT/MDB_INTEGERKEY.
* - Accessing a data record as a database, or vice versa.
* - The database was dropped and recreated with different flags.
*/
@ -307,7 +418,6 @@ typedef enum MDB_cursor_op {
* - ABI version mismatch (rare case); */
#define MDBX_EBADSIGN (-30420)
/* Statistics for a database in the environment */
typedef struct MDBX_stat {
unsigned ms_psize; /* Size of a database page.
@ -325,12 +435,12 @@ typedef struct MDBX_envinfo {
void *me_mapaddr; /* Address of map, if fixed */
size_t me_mapsize; /* Size of the data memory map */
size_t me_last_pgno; /* ID of the last used page */
size_t me_last_txnid; /* ID of the last committed transaction */
uint64_t me_last_txnid; /* ID of the last committed transaction */
unsigned me_maxreaders; /* max reader slots in the environment */
unsigned me_numreaders; /* max reader slots used in the environment */
size_t me_tail_txnid; /* ID of the last reader transaction */
size_t me_meta1_txnid, me_meta1_sign;
size_t me_meta2_txnid, me_meta2_sign;
uint64_t me_tail_txnid; /* ID of the last reader transaction */
uint64_t me_meta1_txnid, me_meta1_sign;
uint64_t me_meta2_txnid, me_meta2_sign;
} MDBX_envinfo;
/* Return the LMDB library version information.
@ -343,7 +453,7 @@ typedef struct MDBX_envinfo {
* here
* Returns "version string" The library version as a string
*/
const char *mdbx_version(int *major, int *minor, int *patch);
LIBMDBX_API const char *mdbx_version(int *major, int *minor, int *patch);
/* Return a string describing a given error code.
*
@ -355,8 +465,8 @@ const char *mdbx_version(int *major, int *minor, int *patch);
* [in] err The error code
* Returns "error message" The description of the error
*/
const char *mdbx_strerror(int errnum);
const char *mdbx_strerror_r(int errnum, char *buf, size_t buflen);
LIBMDBX_API const char *mdbx_strerror(int errnum);
LIBMDBX_API const char *mdbx_strerror_r(int errnum, char *buf, size_t buflen);
/* Create an LMDB environment handle.
*
@ -370,7 +480,7 @@ const char *mdbx_strerror_r(int errnum, char *buf, size_t buflen);
* [out] env The address where the new handle will be stored
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_create(MDB_env **env);
LIBMDBX_API int mdbx_env_create(MDB_env **env);
/* Open an environment handle.
*
@ -383,19 +493,6 @@ int mdbx_env_create(MDB_env **env);
* must be set to 0 or by bitwise OR'ing together one or more of the
* values described here.
* Flags set by mdbx_env_set_flags() are also used.
* - MDB_FIXEDMAP
* use a fixed address for the mmap region. This flag must be specified
* when creating the environment, and is stored persistently in the
*environment.
* If successful, the memory map will always reside at the same
*virtual address
* and pointers used to reference data items in the database will
*be constant
* across multiple invocations. This option may not always work,
*depending on
* how the operating system has allocated memory to shared
*libraries and other uses.
* The feature is highly experimental.
* - MDB_NOSUBDIR
* By default, LMDB creates its environment in a directory whose
* pathname is given in \b path, and creates its data and lock
@ -558,9 +655,10 @@ int mdbx_env_create(MDB_env **env);
*files.
* - EAGAIN - the environment was locked by another process.
*/
int mdbx_env_open(MDB_env *env, const char *path, unsigned flags, mode_t mode);
int mdbx_env_open_ex(MDB_env *env, const char *path, unsigned flags,
mode_t mode, int *exclusive);
LIBMDBX_API int mdbx_env_open(MDB_env *env, const char *path, unsigned flags,
mode_t mode);
LIBMDBX_API int mdbx_env_open_ex(MDB_env *env, const char *path, unsigned flags,
mode_t mode, int *exclusive);
/* Copy an LMDB environment to the specified path.
*
@ -576,7 +674,7 @@ int mdbx_env_open_ex(MDB_env *env, const char *path, unsigned flags,
* empty.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_copy(MDB_env *env, const char *path);
LIBMDBX_API int mdbx_env_copy(MDB_env *env, const char *path);
/* Copy an LMDB environment to the specified file descriptor.
*
@ -591,7 +689,7 @@ int mdbx_env_copy(MDB_env *env, const char *path);
* have already been opened for Write access.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_copyfd(MDB_env *env, int fd);
LIBMDBX_API int mdbx_env_copyfd(MDB_env *env, mdbx_filehandle_t fd);
/* Copy an LMDB environment to the specified path, with options.
*
@ -616,7 +714,7 @@ int mdbx_env_copyfd(MDB_env *env, int fd);
*leak.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_copy2(MDB_env *env, const char *path, unsigned flags);
LIBMDBX_API int mdbx_env_copy2(MDB_env *env, const char *path, unsigned flags);
/* Copy an LMDB environment to the specified file descriptor,
* with options.
@ -635,7 +733,8 @@ int mdbx_env_copy2(MDB_env *env, const char *path, unsigned flags);
* See mdbx_env_copy2() for options.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_copyfd2(MDB_env *env, int fd, unsigned flags);
LIBMDBX_API int mdbx_env_copyfd2(MDB_env *env, mdbx_filehandle_t fd,
unsigned flags);
/* Return statistics about the LMDB environment.
*
@ -643,7 +742,7 @@ int mdbx_env_copyfd2(MDB_env *env, int fd, unsigned flags);
* [out] stat The address of an MDB_stat structure
* where the statistics will be copied
*/
int mdbx_env_stat(MDB_env *env, MDBX_stat *stat, size_t bytes);
LIBMDBX_API int mdbx_env_stat(MDB_env *env, MDBX_stat *stat, size_t bytes);
/* Return information about the LMDB environment.
*
@ -651,7 +750,7 @@ int mdbx_env_stat(MDB_env *env, MDBX_stat *stat, size_t bytes);
* [out] stat The address of an MDB_envinfo structure
* where the information will be copied
*/
int mdbx_env_info(MDB_env *env, MDBX_envinfo *info, size_t bytes);
LIBMDBX_API int mdbx_env_info(MDB_env *env, MDBX_envinfo *info, size_t bytes);
/* Flush the data buffers to disk.
*
@ -670,7 +769,7 @@ int mdbx_env_info(MDB_env *env, MDBX_envinfo *info, size_t bytes);
* - EINVAL - an invalid parameter was specified.
* - EIO - an error occurred during synchronization.
*/
int mdbx_env_sync(MDB_env *env, int force);
LIBMDBX_API int mdbx_env_sync(MDB_env *env, int force);
/* Close the environment and release the memory map.
*
@ -687,7 +786,7 @@ int mdbx_env_sync(MDB_env *env, int force);
* on opening next time, and transactions since the last non-weak
* checkpoint (meta-page update) will rolledback for consistency guarantee.
*/
void mdbx_env_close(MDB_env *env);
LIBMDBX_API void mdbx_env_close(MDB_env *env);
/* Set environment flags.
*
@ -701,7 +800,7 @@ void mdbx_env_close(MDB_env *env);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_env_set_flags(MDB_env *env, unsigned flags, int onoff);
LIBMDBX_API int mdbx_env_set_flags(MDB_env *env, unsigned flags, int onoff);
/* Get environment flags.
*
@ -711,7 +810,7 @@ int mdbx_env_set_flags(MDB_env *env, unsigned flags, int onoff);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_env_get_flags(MDB_env *env, unsigned *flags);
LIBMDBX_API int mdbx_env_get_flags(MDB_env *env, unsigned *flags);
/* Return the path that was used in mdbx_env_open().
*
@ -723,7 +822,7 @@ int mdbx_env_get_flags(MDB_env *env, unsigned *flags);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_env_get_path(MDB_env *env, const char **path);
LIBMDBX_API int mdbx_env_get_path(MDB_env *env, const char **path);
/* Return the filedescriptor for the given environment.
*
@ -737,7 +836,7 @@ int mdbx_env_get_path(MDB_env *env, const char **path);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_env_get_fd(MDB_env *env, int *fd);
LIBMDBX_API int mdbx_env_get_fd(MDB_env *env, mdbx_filehandle_t *fd);
/* Set the size of the memory map to use for this environment.
*
@ -772,7 +871,7 @@ int mdbx_env_get_fd(MDB_env *env, int *fd);
*has
* an active write transaction.
*/
int mdbx_env_set_mapsize(MDB_env *env, size_t size);
LIBMDBX_API int mdbx_env_set_mapsize(MDB_env *env, size_t size);
/* Set the maximum number of threads/reader slots for the environment.
*
@ -792,7 +891,7 @@ int mdbx_env_set_mapsize(MDB_env *env, size_t size);
* - EINVAL - an invalid parameter was specified, or the environment is
*already open.
*/
int mdbx_env_set_maxreaders(MDB_env *env, unsigned readers);
LIBMDBX_API int mdbx_env_set_maxreaders(MDB_env *env, unsigned readers);
/* Get the maximum number of threads/reader slots for the environment.
*
@ -802,7 +901,7 @@ int mdbx_env_set_maxreaders(MDB_env *env, unsigned readers);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_env_get_maxreaders(MDB_env *env, unsigned *readers);
LIBMDBX_API int mdbx_env_get_maxreaders(MDB_env *env, unsigned *readers);
/* Set the maximum number of named databases for the environment.
*
@ -822,7 +921,7 @@ int mdbx_env_get_maxreaders(MDB_env *env, unsigned *readers);
* - EINVAL - an invalid parameter was specified, or the environment is
*already open.
*/
int mdbx_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
LIBMDBX_API int mdbx_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
/* Get the maximum size of keys and MDB_DUPSORT data we can write.
*
@ -831,7 +930,7 @@ int mdbx_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
* [in] env An environment handle returned by mdbx_env_create()
* Returns The maximum size of a key we can write
*/
int mdbx_env_get_maxkeysize(MDB_env *env);
LIBMDBX_API int mdbx_env_get_maxkeysize(MDB_env *env);
/* Set application information associated with the MDB_env.
*
@ -839,14 +938,14 @@ int mdbx_env_get_maxkeysize(MDB_env *env);
* [in] ctx An arbitrary pointer for whatever the application needs.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_set_userctx(MDB_env *env, void *ctx);
LIBMDBX_API int mdbx_env_set_userctx(MDB_env *env, void *ctx);
/* Get the application information associated with the MDB_env.
*
* [in] env An environment handle returned by mdbx_env_create()
* Returns The pointer set by mdbx_env_set_userctx().
*/
void *mdbx_env_get_userctx(MDB_env *env);
LIBMDBX_API void *mdbx_env_get_userctx(MDB_env *env);
/* A callback function for most LMDB assert() failures,
* called before printing the message and aborting.
@ -864,7 +963,7 @@ typedef void MDB_assert_func(MDB_env *env, const char *msg,
* [in] func An MDB_assert_func function, or 0.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_set_assert(MDB_env *env, MDB_assert_func *func);
LIBMDBX_API int mdbx_env_set_assert(MDB_env *env, MDB_assert_func *func);
/* Create a transaction for use with the environment.
*
@ -898,14 +997,14 @@ int mdbx_env_set_assert(MDB_env *env, MDB_assert_func *func);
* the reader lock table is full. See mdbx_env_set_maxreaders().
* - ENOMEM - out of memory.
*/
int mdbx_txn_begin(MDB_env *env, MDB_txn *parent, unsigned flags,
MDB_txn **txn);
LIBMDBX_API int mdbx_txn_begin(MDB_env *env, MDB_txn *parent, unsigned flags,
MDB_txn **txn);
/* Returns the transaction's MDB_env
*
* [in] txn A transaction handle returned by mdbx_txn_begin()
*/
MDB_env *mdbx_txn_env(MDB_txn *txn);
LIBMDBX_API MDB_env *mdbx_txn_env(MDB_txn *txn);
/* Return the transaction's ID.
*
@ -916,7 +1015,7 @@ MDB_env *mdbx_txn_env(MDB_txn *txn);
* [in] txn A transaction handle returned by mdbx_txn_begin()
* Returns A transaction ID, valid if input is an active transaction.
*/
size_t mdbx_txn_id(MDB_txn *txn);
LIBMDBX_API size_t mdbx_txn_id(MDB_txn *txn);
/* Commit all the operations of a transaction into the database.
*
@ -940,7 +1039,7 @@ size_t mdbx_txn_id(MDB_txn *txn);
* - EIO - a low-level I/O error occurred while writing.
* - ENOMEM - out of memory.
*/
int mdbx_txn_commit(MDB_txn *txn);
LIBMDBX_API int mdbx_txn_commit(MDB_txn *txn);
/* Abandon all the operations of the transaction instead of saving
* them.
@ -959,7 +1058,7 @@ int mdbx_txn_commit(MDB_txn *txn);
*
* [in] txn A transaction handle returned by mdbx_txn_begin()
*/
int mdbx_txn_abort(MDB_txn *txn);
LIBMDBX_API int mdbx_txn_abort(MDB_txn *txn);
/* Reset a read-only transaction.
*
@ -978,7 +1077,7 @@ int mdbx_txn_abort(MDB_txn *txn);
* the database size may grow much more rapidly than otherwise.
* [in] txn A transaction handle returned by mdbx_txn_begin()
*/
int mdbx_txn_reset(MDB_txn *txn);
LIBMDBX_API int mdbx_txn_reset(MDB_txn *txn);
/* Renew a read-only transaction.
*
@ -992,7 +1091,7 @@ int mdbx_txn_reset(MDB_txn *txn);
* must be shut down.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_txn_renew(MDB_txn *txn);
LIBMDBX_API int mdbx_txn_renew(MDB_txn *txn);
/* Open a database in the environment.
* A database handle denotes the name and parameters of a database,
@ -1072,7 +1171,8 @@ int mdbx_txn_renew(MDB_txn *txn);
* - MDB_DBS_FULL - too many databases have been opened. See
*mdbx_env_set_maxdbs().
*/
int mdbx_dbi_open(MDB_txn *txn, const char *name, unsigned flags, MDB_dbi *dbi);
LIBMDBX_API int mdbx_dbi_open(MDB_txn *txn, const char *name, unsigned flags,
MDB_dbi *dbi);
/* Retrieve statistics for a database.
*
@ -1084,7 +1184,8 @@ int mdbx_dbi_open(MDB_txn *txn, const char *name, unsigned flags, MDB_dbi *dbi);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_stat(MDB_txn *txn, MDB_dbi dbi, MDBX_stat *stat, size_t bytes);
LIBMDBX_API int mdbx_stat(MDB_txn *txn, MDB_dbi dbi, MDBX_stat *stat,
size_t bytes);
/* Retrieve the DB flags for a database handle.
*
@ -1093,7 +1194,7 @@ int mdbx_stat(MDB_txn *txn, MDB_dbi dbi, MDBX_stat *stat, size_t bytes);
* [out] flags Address where the flags will be returned.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned *flags);
LIBMDBX_API int mdbx_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned *flags);
/* Close a database handle. Normally unnecessary. Use with care:
*
@ -1111,7 +1212,7 @@ int mdbx_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned *flags);
* [in] env An environment handle returned by mdbx_env_create()
* [in] dbi A database handle returned by mdbx_dbi_open()
*/
void mdbx_dbi_close(MDB_env *env, MDB_dbi dbi);
LIBMDBX_API void mdbx_dbi_close(MDB_env *env, MDB_dbi dbi);
/* Empty or delete+close a database.
*
@ -1122,7 +1223,7 @@ void mdbx_dbi_close(MDB_env *env, MDB_dbi dbi);
* environment and close the DB handle.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_drop(MDB_txn *txn, MDB_dbi dbi, int del);
LIBMDBX_API int mdbx_drop(MDB_txn *txn, MDB_dbi dbi, int del);
/* Set a custom key comparison function for a database.
*
@ -1146,7 +1247,7 @@ int mdbx_drop(MDB_txn *txn, MDB_dbi dbi, int del);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
LIBMDBX_API int mdbx_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
/* Set a custom data comparison function for a MDB_DUPSORT database.
*
@ -1174,7 +1275,7 @@ int mdbx_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
LIBMDBX_API int mdbx_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
/* Get items from a database.
*
@ -1200,7 +1301,8 @@ int mdbx_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
* - MDB_NOTFOUND - the key was not in the database.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
LIBMDBX_API int mdbx_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
MDB_val *data);
/* Store items into a database.
*
@ -1252,8 +1354,8 @@ int mdbx_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
* - EACCES - an attempt was made to write in a read-only transaction.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
unsigned flags);
LIBMDBX_API int mdbx_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
unsigned flags);
/* Delete items from a database.
*
@ -1283,7 +1385,8 @@ int mdbx_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
* - EACCES - an attempt was made to write in a read-only transaction.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
LIBMDBX_API int mdbx_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
MDB_val *data);
/* Create a cursor handle.
*
@ -1313,7 +1416,8 @@ int mdbx_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
LIBMDBX_API int mdbx_cursor_open(MDB_txn *txn, MDB_dbi dbi,
MDB_cursor **cursor);
/* Close a cursor handle.
*
@ -1321,7 +1425,7 @@ int mdbx_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
* Its transaction must still be live if it is a write-transaction.
* [in] cursor A cursor handle returned by mdbx_cursor_open()
*/
void mdbx_cursor_close(MDB_cursor *cursor);
LIBMDBX_API void mdbx_cursor_close(MDB_cursor *cursor);
/* Renew a cursor handle.
*
@ -1337,19 +1441,19 @@ void mdbx_cursor_close(MDB_cursor *cursor);
* errors are:
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
LIBMDBX_API int mdbx_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
/* Return the cursor's transaction handle.
*
* [in] cursor A cursor handle returned by mdbx_cursor_open()
*/
MDB_txn *mdbx_cursor_txn(MDB_cursor *cursor);
LIBMDBX_API MDB_txn *mdbx_cursor_txn(MDB_cursor *cursor);
/* Return the cursor's database handle.
*
* [in] cursor A cursor handle returned by mdbx_cursor_open()
*/
MDB_dbi mdbx_cursor_dbi(MDB_cursor *cursor);
LIBMDBX_API MDB_dbi mdbx_cursor_dbi(MDB_cursor *cursor);
/* Retrieve by cursor.
*
@ -1371,8 +1475,8 @@ MDB_dbi mdbx_cursor_dbi(MDB_cursor *cursor);
* - MDB_NOTFOUND - no matching key found.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
MDB_cursor_op op);
LIBMDBX_API int mdbx_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
MDB_cursor_op op);
/* Store by cursor.
*
@ -1444,8 +1548,8 @@ int mdbx_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
* - EACCES - an attempt was made to write in a read-only transaction.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
unsigned flags);
LIBMDBX_API int mdbx_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
unsigned flags);
/* Delete current key/data pair
*
@ -1461,7 +1565,7 @@ int mdbx_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
* - EACCES - an attempt was made to write in a read-only transaction.
* - EINVAL - an invalid parameter was specified.
*/
int mdbx_cursor_del(MDB_cursor *cursor, unsigned flags);
LIBMDBX_API int mdbx_cursor_del(MDB_cursor *cursor, unsigned flags);
/* Return count of duplicates for current key.
*
@ -1474,7 +1578,7 @@ int mdbx_cursor_del(MDB_cursor *cursor, unsigned flags);
* - EINVAL - cursor is not initialized, or an invalid parameter was
*specified.
*/
int mdbx_cursor_count(MDB_cursor *cursor, size_t *countp);
LIBMDBX_API int mdbx_cursor_count(MDB_cursor *cursor, size_t *countp);
/* Compare two data items according to a particular database.
*
@ -1486,7 +1590,8 @@ int mdbx_cursor_count(MDB_cursor *cursor, size_t *countp);
* [in] b The second item to compare
* Returns < 0 if a < b, 0 if a == b, > 0 if a > b
*/
int mdbx_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
LIBMDBX_API int mdbx_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a,
const MDB_val *b);
/* Compare two data items according to a particular database.
*
@ -1498,7 +1603,8 @@ int mdbx_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
* [in] b The second item to compare
* Returns < 0 if a < b, 0 if a == b, > 0 if a > b
*/
int mdbx_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
LIBMDBX_API int mdbx_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a,
const MDB_val *b);
/* A callback function used to print a message from the library.
*
@ -1515,7 +1621,7 @@ typedef int(MDB_msg_func)(const char *msg, void *ctx);
* [in] ctx Anything the message function needs
* Returns < 0 on failure, >= 0 on success.
*/
int mdbx_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
LIBMDBX_API int mdbx_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
/* Check for stale entries in the reader lock table.
*
@ -1523,11 +1629,11 @@ int mdbx_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
* [out] dead Number of stale slots that were cleared
* Returns 0 on success, non-zero on failure.
*/
int mdbx_reader_check(MDB_env *env, int *dead);
LIBMDBX_API int mdbx_reader_check(MDB_env *env, int *dead);
char *mdbx_dkey(MDB_val *key, char *buf);
LIBMDBX_API char *mdbx_dkey(MDB_val *key, char *buf);
int mdbx_env_close_ex(MDB_env *env, int dont_sync);
LIBMDBX_API int mdbx_env_close_ex(MDB_env *env, int dont_sync);
/* Set threshold to force flush the data buffers to disk,
* even of MDB_NOSYNC, MDB_NOMETASYNC and MDB_MAPASYNC flags
@ -1546,7 +1652,8 @@ int mdbx_env_close_ex(MDB_env *env, int dont_sync);
* when a synchronous flush would be made.
* Returns A non-zero error value on failure and 0 on success.
*/
int mdbx_env_set_syncbytes(MDB_env *env, size_t bytes);
LIBMDBX_API int mdbx_env_set_syncbytes(MDB_env *env, size_t bytes);
/* Returns a lag of the reading.
*
* Returns an information for estimate how much given read-only
@ -1557,7 +1664,7 @@ int mdbx_env_set_syncbytes(MDB_env *env, size_t bytes);
* Returns Number of transactions committed after the given was started for
* read, or -1 on failure.
*/
int mdbx_txn_straggler(MDB_txn *txn, int *percent);
LIBMDBX_API int mdbx_txn_straggler(MDB_txn *txn, int *percent);
/* A callback function for killing a laggard readers,
* but also could waiting ones. Called in case of MDB_MAP_FULL error.
@ -1573,8 +1680,8 @@ int mdbx_txn_straggler(MDB_txn *txn, int *percent);
* 1 on success (reader was killed),
* >1 on success (reader was SURE killed).
*/
typedef int(MDBX_oom_func)(MDB_env *env, int pid, void *thread_id, size_t txn,
unsigned gap, int retry);
typedef int(MDBX_oom_func)(MDB_env *env, int pid, mdbx_tid_t thread_id,
size_t txn, unsigned gap, int retry);
/* Set the OOM callback.
*
@ -1584,7 +1691,7 @@ typedef int(MDBX_oom_func)(MDB_env *env, int pid, void *thread_id, size_t txn,
* [in] env An environment handle returned by mdbx_env_create().
* [in] oomfunc A #MDBX_oom_func function or NULL to disable.
*/
void mdbx_env_set_oomfunc(MDB_env *env, MDBX_oom_func *oom_func);
LIBMDBX_API void mdbx_env_set_oomfunc(MDB_env *env, MDBX_oom_func *oom_func);
/* Get the current oom_func callback.
*
@ -1594,7 +1701,7 @@ void mdbx_env_set_oomfunc(MDB_env *env, MDBX_oom_func *oom_func);
* [in] env An environment handle returned by mdbx_env_create().
* Returns A #MDBX_oom_func function or NULL if disabled.
*/
MDBX_oom_func *mdbx_env_get_oomfunc(MDB_env *env);
LIBMDBX_API MDBX_oom_func *mdbx_env_get_oomfunc(MDB_env *env);
#define MDBX_DBG_ASSERT 1
#define MDBX_DBG_PRINT 2
@ -1609,48 +1716,56 @@ MDBX_oom_func *mdbx_env_get_oomfunc(MDB_env *env);
typedef void MDBX_debug_func(int type, const char *function, int line,
const char *msg, va_list args);
int mdbx_setup_debug(int flags, MDBX_debug_func *logger, long edge_txn);
LIBMDBX_API int mdbx_setup_debug(int flags, MDBX_debug_func *logger,
long edge_txn);
typedef int MDBX_pgvisitor_func(size_t pgno, unsigned pgnumber, void *ctx,
const char *dbi, const char *type, int nentries,
int payload_bytes, int header_bytes,
int unused_bytes);
int mdbx_env_pgwalk(MDB_txn *txn, MDBX_pgvisitor_func *visitor, void *ctx);
LIBMDBX_API int mdbx_env_pgwalk(MDB_txn *txn, MDBX_pgvisitor_func *visitor,
void *ctx);
typedef struct mdbx_canary { size_t x, y, z, v; } mdbx_canary;
int mdbx_canary_put(MDB_txn *txn, const mdbx_canary *canary);
size_t mdbx_canary_get(MDB_txn *txn, mdbx_canary *canary);
LIBMDBX_API int mdbx_canary_put(MDB_txn *txn, const mdbx_canary *canary);
LIBMDBX_API size_t mdbx_canary_get(MDB_txn *txn, mdbx_canary *canary);
/* Returns:
* - MDBX_RESULT_TRUE when no more data available
* or cursor not positioned;
* - MDBX_RESULT_FALSE when data available;
* - Otherwise the error code. */
int mdbx_cursor_eof(MDB_cursor *mc);
LIBMDBX_API int mdbx_cursor_eof(MDB_cursor *mc);
/* Returns: MDBX_RESULT_TRUE, MDBX_RESULT_FALSE or Error code. */
int mdbx_cursor_on_first(MDB_cursor *mc);
LIBMDBX_API int mdbx_cursor_on_first(MDB_cursor *mc);
/* Returns: MDBX_RESULT_TRUE, MDBX_RESULT_FALSE or Error code. */
int mdbx_cursor_on_last(MDB_cursor *mc);
LIBMDBX_API int mdbx_cursor_on_last(MDB_cursor *mc);
int mdbx_replace(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *new_data,
MDB_val *old_data, unsigned flags);
LIBMDBX_API int mdbx_replace(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
MDB_val *new_data, MDB_val *old_data,
unsigned flags);
/* Same as mdbx_get(), but:
* 1) if values_count is not NULL, then returns the count
* of multi-values/duplicates for a given key.
* 2) updates the key for pointing to the actual key's data inside DB. */
int mdbx_get_ex(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
int *values_count);
LIBMDBX_API int mdbx_get_ex(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
MDB_val *data, int *values_count);
int mdbx_is_dirty(const MDB_txn *txn, const void *ptr);
LIBMDBX_API int mdbx_is_dirty(const MDB_txn *txn, const void *ptr);
int mdbx_dbi_open_ex(MDB_txn *txn, const char *name, unsigned flags,
MDB_dbi *dbi, MDB_cmp_func *keycmp, MDB_cmp_func *datacmp);
LIBMDBX_API int mdbx_dbi_open_ex(MDB_txn *txn, const char *name, unsigned flags,
MDB_dbi *dbi, MDB_cmp_func *keycmp,
MDB_cmp_func *datacmp);
#ifdef __cplusplus
}
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif /* _MDBX_H_ */

View File

@ -1,156 +0,0 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2015,2016 Peter-Service R&D LLC.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/*****************************************************************************
* Properly compiler/memory/coherence barriers
* in the most portable way for libmdbx project.
*
* Feedback and comments are welcome.
* https://gist.github.com/leo-yuriev/ba186a6bf5cf3a27bae7 */
#pragma once
/* *INDENT-OFF* */
/* clang-format off */
#if defined(__mips) && defined(__linux)
/* Only MIPS has explicit cache control */
# include <asm/cachectl.h>
#endif
#if defined(__GNUC__) || defined(__clang__)
# define MDBX_INLINE __inline
#elif defined(__INTEL_COMPILER) /* LY: Intel Compiler may mimic GCC and MSC */
# include <intrin.h>
# if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
# pragma intrinsic(__mf)
# elif defined(__i386__) || defined(__x86_64__)
# pragma intrinsic(_mm_mfence)
# endif
# define MDBX_INLINE __inline
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
# include <mbarrier.h>
# define MDBX_INLINE inline
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) \
&& (defined(HP_IA64) || defined(__ia64))
# include <machine/sys/inline.h>
# define MDBX_INLINE
#elif defined(__IBMC__) && defined(__powerpc)
# include <atomic.h>
# define MDBX_INLINE
#elif defined(_AIX)
# include <builtins.h>
# include <sys/atomic_op.h>
# define MDBX_INLINE
#elif (defined(__osf__) && defined(__DECC)) || defined(__alpha)
# include <machine/builtins.h>
# include <c_asm.h>
# define MDBX_INLINE
#elif defined(__MWERKS__)
/* CodeWarrior - troubles ? */
# pragma gcc_extensions
# define MDBX_INLINE
#elif defined(__SNC__)
/* Sony PS3 - troubles ? */
# define MDBX_INLINE
#else
# define MDBX_INLINE
#endif
#if defined(__i386__) || defined(__x86_64__) \
|| defined(_M_AMD64) || defined(_M_IX86) \
|| defined(__i386) || defined(__amd64) \
|| defined(i386) || defined(__x86_64) \
|| defined(_AMD64_) || defined(_M_X64)
# define MDB_CACHE_IS_COHERENT 1
#elif defined(__hppa) || defined(__hppa__)
# define MDB_CACHE_IS_COHERENT 1
#endif
#ifndef MDB_CACHE_IS_COHERENT
# define MDB_CACHE_IS_COHERENT 0
#endif
#define MDBX_BARRIER_COMPILER 0
#define MDBX_BARRIER_MEMORY 1
static MDBX_INLINE void mdbx_barrier(int type) {
#if defined(__clang__)
__asm__ __volatile__ ("" ::: "memory");
if (type > MDBX_BARRIER_COMPILER)
# if __has_extension(c_atomic) || __has_extension(cxx_atomic)
__c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
# else
__sync_synchronize();
# endif
#elif defined(__GNUC__)
__asm__ __volatile__ ("" ::: "memory");
if (type > MDBX_BARRIER_COMPILER)
# if defined(__ATOMIC_SEQ_CST)
__atomic_thread_fence(__ATOMIC_SEQ_CST);
# else
__sync_synchronize();
# endif
#elif defined(__INTEL_COMPILER) /* LY: Intel Compiler may mimic GCC and MSC */
__memory_barrier();
if (type > MDBX_BARRIER_COMPILER)
# if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
__mf();
# elif defined(__i386__) || defined(__x86_64__)
_mm_mfence();
# else
# error "Unknown target for Intel Compiler, please report to us."
# endif
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
__compiler_barrier();
if (type > MDBX_BARRIER_COMPILER)
__machine_rw_barrier();
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) \
&& (defined(HP_IA64) || defined(__ia64))
_Asm_sched_fence(/* LY: no-arg meaning 'all expect ALU', e.g. 0x3D3D */);
if (type > MDBX_BARRIER_COMPILER)
_Asm_mf();
#elif defined(_AIX) || defined(__ppc__) || defined(__powerpc__) \
|| defined(__ppc64__) || defined(__powerpc64__)
__fence();
if (type > MDBX_BARRIER_COMPILER)
__lwsync();
#elif (defined(__osf__) && defined(__DECC)) || defined(__alpha)
__PAL_DRAINA(); /* LY: excessive ? */
__MB();
#else
# error "Could not guess the kind of compiler, please report to us."
#endif
}
#define mdbx_compiler_barrier() \
mdbx_barrier(MDBX_BARRIER_COMPILER)
#define mdbx_memory_barrier() \
mdbx_barrier(MDBX_BARRIER_MEMORY)
#define mdbx_coherent_barrier() \
mdbx_barrier(MDB_CACHE_IS_COHERENT ? MDBX_BARRIER_COMPILER : MDBX_BARRIER_MEMORY)
static MDBX_INLINE void mdbx_invalidate_cache(void *addr, int nbytes) {
mdbx_coherent_barrier();
#if defined(__mips) && defined(__linux)
/* MIPS has cache coherency issues.
* Note: for any nbytes >= on-chip cache size, entire is flushed. */
cacheflush(addr, nbytes, DCACHE);
#elif defined(_M_MRX000) || defined(_MIPS_)
# error "Sorry, cacheflush() for MIPS not implemented"
#else
/* LY: assume no mmap/dcache issues. */
(void) addr;
(void) nbytes;
#endif
}

796
src/bits.h Normal file
View File

@ -0,0 +1,796 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#pragma once
/* *INDENT-OFF* */
/* clang-format off */
#ifndef _FILE_OFFSET_BITS
# define _FILE_OFFSET_BITS 64
#endif
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
# define _CRT_SECURE_NO_WARNINGS
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4464) /* C4464: relative include path contains '..' */
#pragma warning(disable : 4710) /* C4710: 'xyz': function not inlined */
#pragma warning(disable : 4711) /* C4711: function 'xyz' selected for automatic inline expansion */
//#pragma warning(disable : 4061) /* C4061: enumerator 'abc' in switch of enum 'xyz' is not explicitly handled by a case label */
#pragma warning(disable : 4201) /* C4201: nonstandard extension used : nameless struct / union */
#pragma warning(disable : 4706) /* C4706: assignment within conditional expression */
#pragma warning(disable : 4127) /* C4127: conditional expression is constant */
#endif /* _MSC_VER (warnings) */
#include "../mdbx.h"
#include "./defs.h"
#if defined(USE_VALGRIND)
# include <valgrind/memcheck.h>
# ifndef VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE
/* LY: available since Valgrind 3.10 */
# define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# endif
#else
# define VALGRIND_CREATE_MEMPOOL(h,r,z)
# define VALGRIND_DESTROY_MEMPOOL(h)
# define VALGRIND_MEMPOOL_TRIM(h,a,s)
# define VALGRIND_MEMPOOL_ALLOC(h,a,s)
# define VALGRIND_MEMPOOL_FREE(h,a)
# define VALGRIND_MEMPOOL_CHANGE(h,a,b,s)
# define VALGRIND_MAKE_MEM_NOACCESS(a,s)
# define VALGRIND_MAKE_MEM_DEFINED(a,s)
# define VALGRIND_MAKE_MEM_UNDEFINED(a,s)
# define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a,s) (0)
# define VALGRIND_CHECK_MEM_IS_DEFINED(a,s) (0)
#endif /* USE_VALGRIND */
#ifdef __SANITIZE_ADDRESS__
# include <sanitizer/asan_interface.h>
#else
# define ASAN_POISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
# define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
#endif /* __SANITIZE_ADDRESS__ */
#include "./osal.h"
#ifndef MDB_DEBUG
# define MDB_DEBUG 0
#endif
#if MDB_DEBUG
# undef NDEBUG
#endif
#if defined(__GNUC__) && !__GNUC_PREREQ(4,2)
/* Actualy libmdbx was not tested with compilers older than GCC from RHEL6.
* But you could remove this #error and try to continue at your own risk.
* In such case please don't rise up an issues related ONLY to old compilers.
*/
# warning "libmdbx required at least GCC 4.2 compatible C/C++ compiler."
#endif
#if defined(__GLIBC__) && !__GLIBC_PREREQ(2,12)
/* Actualy libmdbx was not tested with something older than glibc 2.12 (from RHEL6).
* But you could remove this #error and try to continue at your own risk.
* In such case please don't rise up an issues related ONLY to old systems.
*/
# warning "libmdbx required at least GLIBC 2.12."
#endif
#if defined(__i386) || defined(__x86_64) || defined(_M_IX86)
# define MISALIGNED_OK 1 /* TODO */
#endif
#ifndef MISALIGNED_OK
# define MISALIGNED_OK 0
#endif /* MISALIGNED_OK */
#if (-6 & 5) || CHAR_BIT != 8 || UINT_MAX < 0xffffffff || ULONG_MAX % 0xFFFF
# error "Sanity checking failed: Two's complement, reasonably sized integer types"
#endif
/*----------------------------------------------------------------------------*/
#ifndef ARRAY_LENGTH
# ifdef __cplusplus
template <typename T, size_t N>
char (&__ArraySizeHelper(T (&array)[N]))[N];
# define ARRAY_LENGTH(array) (sizeof(::__ArraySizeHelper(array)))
# else
# define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
# endif
#endif /* ARRAY_LENGTH */
#ifndef ARRAY_END
# define ARRAY_END(array) (&array[ARRAY_LENGTH(array)])
#endif /* ARRAY_END */
#ifndef STRINGIFY
# define STRINGIFY_HELPER(x) #x
# define STRINGIFY(x) STRINGIFY_HELPER(x)
#endif /* STRINGIFY */
#ifndef offsetof
# define offsetof(type, member) __builtin_offsetof(type, member)
#endif /* offsetof */
#ifndef container_of
# define container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#endif /* container_of */
/* *INDENT-ON* */
/* clang-format on */
#define FIXME "FIXME: " __FILE__ ", " STRINGIFY(__LINE__)
/*----------------------------------------------------------------------------*/
/** handle for the DB used to track free pages. */
#define FREE_DBI 0
/** handle for the default DB. */
#define MAIN_DBI 1
/** Number of DBs in metapage (free and main) - also hardcoded elsewhere */
#define CORE_DBS 2
/** Number of meta pages - also hardcoded elsewhere */
#define NUM_METAS 2
/* A generic unsigned ID number. These were entryIDs in back-bdb.
* Preferably it should have the same size as a pointer.
*/
typedef size_t MDB_ID;
/** A page number in the database.
* Note that 64 bit page numbers are overkill, since pages themselves
* already represent 12-13 bits of addressable memory, and the OS will
* always limit applications to a maximum of 63 bits of address space.
*
* @note In the #MDB_node structure, we only store 48 bits of this value,
* which thus limits us to only 60 bits of addressable data.
*/
typedef MDB_ID pgno_t;
/** A transaction ID.
* See struct MDB_txn.mt_txnid for details.
*/
typedef MDB_ID txnid_t;
/* An IDL is an ID List, a sorted array of IDs. The first
* element of the array is a counter for how many actual
* IDs are in the list. In the original back-bdb code, IDLs are
* sorted in ascending order. For libmdb IDLs are sorted in
* descending order.
*/
typedef MDB_ID *MDB_IDL;
/* An ID2 is an ID/pointer pair.
*/
typedef struct MDB_ID2 {
MDB_ID mid; /* The ID */
void *mptr; /* The pointer */
} MDB_ID2;
/* An ID2L is an ID2 List, a sorted array of ID2s.
* The first element's \b mid member is a count of how many actual
* elements are in the array. The \b mptr member of the first element is
* unused.
* The array is sorted in ascending order by \b mid.
*/
typedef MDB_ID2 *MDB_ID2L;
/** Used for offsets within a single page.
* Since memory pages are typically 4 or 8KB in size, 12-13 bits,
* this is plenty.
*/
typedef uint16_t indx_t;
#pragma pack(push, 1)
/** The information we store in a single slot of the reader table.
* In addition to a transaction ID, we also record the process and
* thread ID that owns a slot, so that we can detect stale information,
* e.g. threads or processes that went away without cleaning up.
* @note We currently don't check for stale records. We simply re-init
* the table when we know that we're the only process opening the
* lock file.
*/
typedef struct MDB_rxbody {
/** Current Transaction ID when this transaction began, or (txnid_t)-1.
* Multiple readers that start at the same time will probably have the
* same ID here. Again, it's not important to exclude them from
* anything; all we need to know is which version of the DB they
* started from so we can avoid overwriting any data used in that
* particular version.
*/
volatile txnid_t mrb_txnid;
/** The process ID of the process owning this reader txn. */
volatile mdbx_pid_t mrb_pid;
/** The thread ID of the thread owning this txn. */
volatile mdbx_tid_t mrb_tid;
} MDB_rxbody;
/** The actual reader record, with cacheline padding. */
typedef struct MDB_reader {
union {
MDB_rxbody mrx;
/** shorthand for mrb_txnid */
#define mr_txnid mru.mrx.mrb_txnid
#define mr_pid mru.mrx.mrb_pid
#define mr_tid mru.mrx.mrb_tid
/** cache line alignment */
char pad[(sizeof(MDB_rxbody) + MDBX_CACHELINE_SIZE - 1) &
~(MDBX_CACHELINE_SIZE - 1)];
} mru;
} MDB_reader;
/** Information about a single database in the environment. */
typedef struct MDB_db {
uint32_t md_xsize; /**< also ksize for LEAF2 pages */
uint16_t md_flags; /**< @ref mdbx_dbi_open */
uint16_t md_depth; /**< depth of this tree */
pgno_t md_branch_pages; /**< number of internal pages */
pgno_t md_leaf_pages; /**< number of leaf pages */
pgno_t md_overflow_pages; /**< number of overflow pages */
size_t md_entries; /**< number of data items */
pgno_t md_root; /**< the root page of this tree */
} MDB_db;
/** Meta page content.
* A meta page is the start point for accessing a database snapshot.
* Pages 0-1 are meta pages. Transaction N writes meta page #(N % 2).
*/
typedef struct MDB_meta {
/** Stamp identifying this as an LMDB file. It must be set
* to #MDB_MAGIC. */
uint32_t mm_magic;
/** Version number of this file. Must be set to #MDB_DATA_VERSION. */
uint32_t mm_version;
size_t mm_mapsize; /**< size of mmap region */
MDB_db mm_dbs[CORE_DBS]; /**< first is free space, 2nd is main db */
/** The size of pages used in this DB */
#define mm_psize mm_dbs[FREE_DBI].md_xsize
/** Any persistent environment flags. @ref mdbx_env */
#define mm_flags mm_dbs[FREE_DBI].md_flags
/** Last used page in the datafile.
* Actually the file may be shorter if the freeDB lists the final pages.
*/
pgno_t mm_last_pg;
volatile txnid_t mm_txnid; /**< txnid that committed this page */
#define MDB_DATASIGN_NONE 0u
#define MDB_DATASIGN_WEAK 1u
volatile uint64_t mm_datasync_sign;
#define META_IS_WEAK(meta) ((meta)->mm_datasync_sign == MDB_DATASIGN_WEAK)
#define META_IS_STEADY(meta) ((meta)->mm_datasync_sign > MDB_DATASIGN_WEAK)
#if MDBX_MODE_ENABLED
volatile mdbx_canary mm_canary;
#endif
} MDB_meta;
/** Common header for all page types. The page type depends on #mp_flags.
*
* #P_BRANCH and #P_LEAF pages have unsorted '#MDB_node's at the end, with
* sorted #mp_ptrs[] entries referring to them. Exception: #P_LEAF2 pages
* omit mp_ptrs and pack sorted #MDB_DUPFIXED values after the page header.
*
* #P_OVERFLOW records occupy one or more contiguous pages where only the
* first has a page header. They hold the real data of #F_BIGDATA nodes.
*
* #P_SUBP sub-pages are small leaf "pages" with duplicate data.
* A node with flag #F_DUPDATA but not #F_SUBDATA contains a sub-page.
* (Duplicate data can also go in sub-databases, which use normal pages.)
*
* #P_META pages contain #MDB_meta, the start point of an LMDB snapshot.
*
* Each non-metapage up to #MDB_meta.%mm_last_pg is reachable exactly once
* in the snapshot: Either used by a database or listed in a freeDB record.
*/
typedef struct MDB_page {
#define mp_pgno mp_p.p_pgno
#define mp_next mp_p.p_next
union {
pgno_t p_pgno; /**< page number */
struct MDB_page *p_next; /**< for in-memory list of freed pages */
} mp_p;
uint16_t mp_leaf2_ksize; /**< key size if this is a LEAF2 page */
/** @defgroup mdbx_page Page Flags
* @ingroup internal
* Flags for the page headers.
* @{
*/
#define P_BRANCH 0x01 /**< branch page */
#define P_LEAF 0x02 /**< leaf page */
#define P_OVERFLOW 0x04 /**< overflow page */
#define P_META 0x08 /**< meta page */
#define P_DIRTY 0x10 /**< dirty page, also set for #P_SUBP pages */
#define P_LEAF2 0x20 /**< for #MDB_DUPFIXED records */
#define P_SUBP 0x40 /**< for #MDB_DUPSORT sub-pages */
#define P_LOOSE 0x4000 /**< page was dirtied then freed, can be reused */
#define P_KEEP 0x8000 /**< leave this page alone during spill */
/** @} */
uint16_t mp_flags; /**< @ref mdbx_page */
#define mp_lower mp_pb.pb.pb_lower
#define mp_upper mp_pb.pb.pb_upper
#define mp_pages mp_pb.pb_pages
union {
struct {
indx_t pb_lower; /**< lower bound of free space */
indx_t pb_upper; /**< upper bound of free space */
} pb;
uint32_t pb_pages; /**< number of overflow pages */
} mp_pb;
indx_t mp_ptrs[1]; /**< dynamic size */
} MDB_page;
/** Size of the page header, excluding dynamic data at the end */
#define PAGEHDRSZ ((unsigned)offsetof(MDB_page, mp_ptrs))
/** Buffer for a stack-allocated meta page.
* The members define size and alignment, and silence type
* aliasing warnings. They are not used directly; that could
* mean incorrectly using several union members in parallel.
*/
typedef union MDB_metabuf {
MDB_page mb_page;
struct {
char mm_pad[PAGEHDRSZ];
MDB_meta mm_meta;
} mb_metabuf;
} MDB_metabuf;
/* The header for the reader table (a memory-mapped lock file). */
typedef struct MDBX_lockinfo {
/* Stamp identifying this as an LMDB file. It must be set to MDB_MAGIC. */
uint64_t mti_magic;
/* Format of this lock file. Must be set to MDB_LOCK_FORMAT. */
uint64_t mti_format;
/* The ID of the last transaction committed to the database.
* This is recorded here only for convenience; the value can always
* be determined by reading the main database meta pages. */
volatile txnid_t mti_txnid;
#ifdef MDBX_OSAL_LOCK
MDBX_OSAL_LOCK mti_wmutex;
#endif
/* The number of slots that have been used in the reader table.
* This always records the maximum count, it is not decremented
* when readers release their slots. */
__cache_aligned volatile unsigned mti_numreaders;
#ifdef MDBX_OSAL_LOCK
/* Mutex protecting access to this table. */
MDBX_OSAL_LOCK mti_rmutex;
#endif
MDB_reader mti_readers[1];
} MDBX_lockinfo;
#pragma pack(pop)
/** Auxiliary DB info.
* The information here is mostly static/read-only. There is
* only a single copy of this record in the environment.
*/
typedef struct MDB_dbx {
MDB_val md_name; /**< name of the database */
MDB_cmp_func *md_cmp; /**< function for comparing keys */
MDB_cmp_func *md_dcmp; /**< function for comparing data items */
} MDB_dbx;
#if MDBX_MODE_ENABLED
#define MDBX_MODE_SALT 0
#else
#error !?
#endif
/** A database transaction.
* Every operation requires a transaction handle.
*/
struct MDB_txn {
#define MDBX_MT_SIGNATURE (0x93D53A31 ^ MDBX_MODE_SALT)
unsigned mt_signature;
MDB_txn *mt_parent; /**< parent of a nested txn */
/** Nested txn under this txn, set together with flag #MDB_TXN_HAS_CHILD */
MDB_txn *mt_child;
pgno_t mt_next_pgno; /**< next unallocated page */
/** The ID of this transaction. IDs are integers incrementing from 1.
* Only committed write transactions increment the ID. If a transaction
* aborts, the ID may be re-used by the next writer.
*/
txnid_t mt_txnid;
MDB_env *mt_env; /**< the DB environment */
/** The list of reclaimed txns from freeDB */
MDB_IDL mt_lifo_reclaimed;
/** The list of pages that became unused during this transaction.
*/
MDB_IDL mt_free_pgs;
/** The list of loose pages that became unused and may be reused
* in this transaction, linked through #NEXT_LOOSE_PAGE(page).
*/
MDB_page *mt_loose_pgs;
/** Number of loose pages (#mt_loose_pgs) */
int mt_loose_count;
/** The sorted list of dirty pages we temporarily wrote to disk
* because the dirty list was full. page numbers in here are
* shifted left by 1, deleted slots have the LSB set.
*/
MDB_IDL mt_spill_pgs;
union {
/** For write txns: Modified pages. Sorted when not MDB_WRITEMAP. */
MDB_ID2L dirty_list;
/** For read txns: This thread/txn's reader table slot, or NULL. */
MDB_reader *reader;
} mt_u;
/** Array of records for each DB known in the environment. */
MDB_dbx *mt_dbxs;
/** Array of MDB_db records for each known DB */
MDB_db *mt_dbs;
/** Array of sequence numbers for each DB handle */
unsigned *mt_dbiseqs;
/** @defgroup mt_dbflag Transaction DB Flags
* @ingroup internal
* @{
*/
#define DB_DIRTY 0x01 /**< DB was written in this txn */
#define DB_STALE 0x02 /**< Named-DB record is older than txnID */
#define DB_NEW 0x04 /**< Named-DB handle opened in this txn */
#define DB_VALID 0x08 /**< DB handle is valid, see also #MDB_VALID */
#define DB_USRVALID 0x10 /**< As #DB_VALID, but not set for #FREE_DBI */
#define DB_DUPDATA 0x20 /**< DB is #MDB_DUPSORT data */
/** @} */
/** In write txns, array of cursors for each DB */
MDB_cursor **mt_cursors;
/** Array of flags for each DB */
unsigned char *mt_dbflags;
/** Number of DB records in use, or 0 when the txn is finished.
* This number only ever increments until the txn finishes; we
* don't decrement it when individual DB handles are closed.
*/
MDB_dbi mt_numdbs;
/** @defgroup mdbx_txn Transaction Flags
* @ingroup internal
* @{
*/
/** #mdbx_txn_begin() flags */
#define MDB_TXN_BEGIN_FLAGS (MDB_NOMETASYNC | MDB_NOSYNC | MDB_RDONLY)
#define MDB_TXN_NOMETASYNC \
MDB_NOMETASYNC /**< don't sync meta for this txn on commit */
#define MDB_TXN_NOSYNC MDB_NOSYNC /**< don't sync this txn on commit */
#define MDB_TXN_RDONLY MDB_RDONLY /**< read-only transaction */
/* internal txn flags */
#define MDB_TXN_WRITEMAP \
MDB_WRITEMAP /**< copy of #MDB_env flag in writers \
*/
#define MDB_TXN_FINISHED 0x01 /**< txn is finished or never began */
#define MDB_TXN_ERROR 0x02 /**< txn is unusable after an error */
#define MDB_TXN_DIRTY 0x04 /**< must write, even if dirty list is empty */
#define MDB_TXN_SPILLS 0x08 /**< txn or a parent has spilled pages */
#define MDB_TXN_HAS_CHILD 0x10 /**< txn has an #MDB_txn.%mt_child */
/** most operations on the txn are currently illegal */
#define MDB_TXN_BLOCKED (MDB_TXN_FINISHED | MDB_TXN_ERROR | MDB_TXN_HAS_CHILD)
/** @} */
unsigned mt_flags; /**< @ref mdbx_txn */
/** #dirty_list room: Array size - \#dirty pages visible to this txn.
* Includes ancestor txns' dirty pages not hidden by other txns'
* dirty/spilled pages. Thus commit(nested txn) has room to merge
* dirty_list into mt_parent after freeing hidden mt_parent pages.
*/
unsigned mt_dirty_room;
#if MDBX_MODE_ENABLED
mdbx_canary mt_canary;
#endif
};
/** Enough space for 2^32 nodes with minimum of 2 keys per node. I.e., plenty.
* At 4 keys per node, enough for 2^64 nodes, so there's probably no need to
* raise this on a 64 bit machine.
*/
#define CURSOR_STACK 32
struct MDB_xcursor;
/** Cursors are used for all DB operations.
* A cursor holds a path of (page pointer, key index) from the DB
* root to a position in the DB, plus other state. #MDB_DUPSORT
* cursors include an xcursor to the current data item. Write txns
* track their cursors and keep them up to date when data moves.
* Exception: An xcursor's pointer to a #P_SUBP page can be stale.
* (A node with #F_DUPDATA but no #F_SUBDATA contains a subpage).
*/
struct MDB_cursor {
#define MDBX_MC_SIGNATURE (0xFE05D5B1 ^ MDBX_MODE_SALT)
#define MDBX_MC_READY4CLOSE (0x2817A047 ^ MDBX_MODE_SALT)
#define MDBX_MC_WAIT4EOT (0x90E297A7 ^ MDBX_MODE_SALT)
unsigned mc_signature;
/** Next cursor on this DB in this txn */
MDB_cursor *mc_next;
/** Backup of the original cursor if this cursor is a shadow */
MDB_cursor *mc_backup;
/** Context used for databases with #MDB_DUPSORT, otherwise NULL */
struct MDB_xcursor *mc_xcursor;
/** The transaction that owns this cursor */
MDB_txn *mc_txn;
/** The database handle this cursor operates on */
MDB_dbi mc_dbi;
/** The database record for this cursor */
MDB_db *mc_db;
/** The database auxiliary record for this cursor */
MDB_dbx *mc_dbx;
/** The @ref mt_dbflag for this database */
uint8_t *mc_dbflag;
uint16_t mc_snum; /**< number of pushed pages */
uint16_t mc_top; /**< index of top page, normally mc_snum-1 */
/** @defgroup mdbx_cursor Cursor Flags
* @ingroup internal
* Cursor state flags.
* @{
*/
#define C_INITIALIZED 0x01 /**< cursor has been initialized and is valid */
#define C_EOF 0x02 /**< No more data */
#define C_SUB 0x04 /**< Cursor is a sub-cursor */
#define C_DEL 0x08 /**< last op was a cursor_del */
#define C_UNTRACK 0x40 /**< Un-track cursor when closing */
#define C_RECLAIMING 0x80 /**< FreeDB lookup is prohibited */
/** @} */
unsigned mc_flags; /**< @ref mdbx_cursor */
MDB_page *mc_pg[CURSOR_STACK]; /**< stack of pushed pages */
indx_t mc_ki[CURSOR_STACK]; /**< stack of page indices */
};
/** Context for sorted-dup records.
* We could have gone to a fully recursive design, with arbitrarily
* deep nesting of sub-databases. But for now we only handle these
* levels - main DB, optional sub-DB, sorted-duplicate DB.
*/
typedef struct MDB_xcursor {
/** A sub-cursor for traversing the Dup DB */
MDB_cursor mx_cursor;
/** The database record for this Dup DB */
MDB_db mx_db;
/** The auxiliary DB record for this Dup DB */
MDB_dbx mx_dbx;
/** The @ref mt_dbflag for this Dup DB */
unsigned char mx_dbflag;
} MDB_xcursor;
/** Check if there is an inited xcursor, so #XCURSOR_REFRESH() is proper */
#define XCURSOR_INITED(mc) \
((mc)->mc_xcursor && ((mc)->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
/** Update sub-page pointer, if any, in \b mc->mc_xcursor. Needed
* when the node which contains the sub-page may have moved. Called
* with \b mp = mc->mc_pg[mc->mc_top], \b ki = mc->mc_ki[mc->mc_top].
*/
#define XCURSOR_REFRESH(mc, mp, ki) \
do { \
MDB_page *xr_pg = (mp); \
MDB_node *xr_node = NODEPTR(xr_pg, ki); \
if ((xr_node->mn_flags & (F_DUPDATA | F_SUBDATA)) == F_DUPDATA) \
(mc)->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(xr_node); \
} while (0)
/** State of FreeDB old pages, stored in the MDB_env */
typedef struct MDB_pgstate {
pgno_t *mf_pghead; /**< Reclaimed freeDB pages, or NULL before use */
txnid_t mf_pglast; /**< ID of last used record, or 0 if !mf_pghead */
} MDB_pgstate;
#define MDBX_LOCKINFO_WHOLE_SIZE \
((sizeof(MDBX_lockinfo) + MDBX_CACHELINE_SIZE - 1) & \
~((size_t)MDBX_CACHELINE_SIZE - 1))
/** Lockfile format signature: version, features and field layout */
#define MDB_LOCK_FORMAT \
(((uint64_t)(MDBX_OSAL_LOCK_SIGN) << 32) + \
((MDBX_LOCKINFO_WHOLE_SIZE + MDBX_CACHELINE_SIZE - 1) << 16) + \
(MDB_LOCK_VERSION) /* Flags which describe functionality */)
/** The database environment. */
struct MDB_env {
#define MDBX_ME_SIGNATURE (0x9A899641 ^ MDBX_MODE_SALT)
unsigned me_signature;
mdbx_filehandle_t me_fd; /**< The main data file */
mdbx_filehandle_t me_lfd; /**< The lock file */
/** Failed to update the meta page. Probably an I/O error. */
#define MDB_FATAL_ERROR 0x80000000U
/** Some fields are initialized. */
#define MDB_ENV_ACTIVE 0x20000000U
/** me_txkey is set */
#define MDB_ENV_TXKEY 0x10000000U
uint32_t me_flags; /**< @ref mdbx_env */
unsigned me_psize; /**< DB page size, inited from me_os_psize */
unsigned me_os_psize; /**< OS page size, from mdbx_syspagesize() */
unsigned me_maxreaders; /**< size of the reader table */
/** Max #MDBX_lockinfo.mti_numreaders of interest to #mdbx_env_close() */
unsigned me_close_readers;
MDB_dbi me_numdbs; /**< number of DBs opened */
MDB_dbi me_maxdbs; /**< size of the DB table */
mdbx_pid_t me_pid; /**< process ID of this env */
char *me_path; /**< path to the DB files */
char *me_map; /**< the memory map of the data file */
MDBX_lockinfo *me_txns; /**< the memory map of the lock file, never NULL */
void *me_pbuf; /**< scratch area for DUPSORT put() */
MDB_txn *me_txn; /**< current write transaction */
MDB_txn *me_txn0; /**< prealloc'd write transaction */
size_t me_mapsize; /**< size of the data memory map */
pgno_t me_maxpg; /**< me_mapsize / me_psize */
MDB_dbx *me_dbxs; /**< array of static DB info */
uint16_t *me_dbflags; /**< array of flags from MDB_db.md_flags */
unsigned *me_dbiseqs; /**< array of dbi sequence numbers */
mdbx_thread_key_t me_txkey; /**< thread-key for readers */
txnid_t me_pgoldest; /**< ID of oldest reader last time we looked */
MDB_pgstate me_pgstate; /**< state of old pages from freeDB */
#define me_pglast me_pgstate.mf_pglast
#define me_pghead me_pgstate.mf_pghead
MDB_page *me_dpages; /**< list of malloc'd blocks for re-use */
/** IDL of pages that became unused in a write txn */
MDB_IDL me_free_pgs;
/** ID2L of pages written during a write txn. Length MDB_IDL_UM_SIZE. */
MDB_ID2L me_dirty_list;
/** Max number of freelist items that can fit in a single overflow page */
unsigned me_maxfree_1pg;
/** Max size of a node on a page */
unsigned me_nodemax;
unsigned me_maxkey_limit; /**< max size of a key */
int me_live_reader; /**< have liveness lock in reader table */
void *me_userctx; /**< User-settable context */
#if MDB_DEBUG
MDB_assert_func *me_assert_func; /**< Callback for assertion failures */
#endif
uint64_t me_sync_pending; /**< Total dirty/commited bytes since the last
mdbx_env_sync() */
uint64_t
me_sync_threshold; /**< Treshold of above to force synchronous flush */
#if MDBX_MODE_ENABLED
MDBX_oom_func *me_oom_func; /**< Callback for kicking laggard readers */
#endif
#ifdef USE_VALGRIND
int me_valgrind_handle;
#endif
};
/** Nested transaction */
typedef struct MDB_ntxn {
MDB_txn mnt_txn; /**< the transaction */
MDB_pgstate mnt_pgstate; /**< parent transaction's saved freestate */
} MDB_ntxn;
/*----------------------------------------------------------------------------*/
extern int mdbx_runtime_flags;
extern MDBX_debug_func *mdbx_debug_logger;
extern txnid_t mdbx_debug_edge;
void mdbx_debug_log(int type, const char *function, int line, const char *fmt,
...)
#if defined(__GNUC__) || __has_attribute(format)
__attribute__((format(printf, 4, 5)))
#endif
;
void mdbx_panic(const char *fmt, ...)
#if defined(__GNUC__) || __has_attribute(format)
__attribute__((format(printf, 1, 2)))
#endif
;
#if MDB_DEBUG
#define mdbx_assert_enabled() unlikely(mdbx_runtime_flags &MDBX_DBG_ASSERT)
#define mdbx_audit_enabled() unlikely(mdbx_runtime_flags &MDBX_DBG_AUDIT)
#define mdbx_debug_enabled(type) \
unlikely(mdbx_runtime_flags &(type & (MDBX_DBG_TRACE | MDBX_DBG_EXTRA)))
#else
#ifndef NDEBUG
#define mdbx_debug_enabled(type) (1)
#else
#define mdbx_debug_enabled(type) (0)
#endif
#define mdbx_audit_enabled() (0)
#define mdbx_assert_enabled() (0)
#endif /* MDB_DEBUG */
#define mdbx_print(fmt, ...) \
mdbx_debug_log(MDBX_DBG_PRINT, NULL, 0, fmt, ##__VA_ARGS__)
#define mdbx_debug(fmt, ...) \
do { \
if (mdbx_debug_enabled(MDBX_DBG_TRACE)) \
mdbx_debug_log(MDBX_DBG_TRACE, __FUNCTION__, __LINE__, fmt "\n", \
##__VA_ARGS__); \
} while (0)
#define mdbx_debug_print(fmt, ...) \
do { \
if (mdbx_debug_enabled(MDBX_DBG_TRACE)) \
mdbx_debug_log(MDBX_DBG_TRACE, NULL, 0, fmt, ##__VA_ARGS__); \
} while (0)
#define mdbx_debug_extra(fmt, ...) \
do { \
if (mdbx_debug_enabled(MDBX_DBG_EXTRA)) \
mdbx_debug_log(MDBX_DBG_EXTRA, __FUNCTION__, __LINE__, fmt, \
##__VA_ARGS__); \
} while (0)
#define mdbx_debug_extra_print(fmt, ...) \
do { \
if (mdbx_debug_enabled(MDBX_DBG_EXTRA)) \
mdbx_debug_log(MDBX_DBG_EXTRA, NULL, 0, fmt, ##__VA_ARGS__); \
} while (0)
#define mdbx_ensure_msg(env, expr, msg) \
do { \
if (unlikely(!(expr))) \
mdbx_assert_fail(env, msg, __FUNCTION__, __LINE__); \
} while (0)
#define mdbx_ensure(env, expr) mdbx_ensure_msg(env, expr, #expr)
/* assert(3) variant in environment context */
#define mdbx_assert(env, expr) \
do { \
if (mdbx_assert_enabled()) \
mdbx_ensure(env, expr); \
} while (0)
/* assert(3) variant in cursor context */
#define mdbx_cassert(mc, expr) mdbx_assert((mc)->mc_txn->mt_env, expr)
/* assert(3) variant in transaction context */
#define mdbx_tassert(txn, expr) mdbx_assert((txn)->mt_env, expr)
/*----------------------------------------------------------------------------*/
int mdbx_reader_check0(MDB_env *env, int rlocked, int *dead);
#define METAPAGE_1(env) (&((MDB_metabuf *)(env)->me_map)->mb_metabuf.mm_meta)
#define METAPAGE_2(env) \
(&((MDB_metabuf *)((env)->me_map + env->me_psize))->mb_metabuf.mm_meta)
static __inline MDB_meta *mdbx_meta_head_w(MDB_env *env) {
MDB_meta *a = METAPAGE_1(env);
MDB_meta *b = METAPAGE_2(env);
txnid_t head_txnid = env->me_txns->mti_txnid;
mdbx_assert(env, a->mm_txnid != b->mm_txnid || head_txnid == 0);
if (a->mm_txnid == head_txnid)
return a;
if (likely(b->mm_txnid == head_txnid))
return b;
mdbx_debug("me_txns->mti_txnid not match meta-pages");
mdbx_assert(env, head_txnid == a->mm_txnid || head_txnid == b->mm_txnid);
env->me_flags |= MDB_FATAL_ERROR;
return a;
}
void mdbx_rthc_dtor(void *rthc);
void mdbx_rthc_lock(void);
void mdbx_rthc_unlock(void);
int mdbx_rthc_alloc(mdbx_thread_key_t *key, MDB_reader *begin, MDB_reader *end);
void mdbx_rthc_remove(mdbx_thread_key_t key);
void mdbx_rthc_cleanup(void);

302
src/defs.h Normal file
View File

@ -0,0 +1,302 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#pragma once
/* *INDENT-OFF* */
/* clang-format off */
#ifndef __GNUC_PREREQ
# if defined(__GNUC__) && defined(__GNUC_MINOR__)
# define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
# else
# define __GNUC_PREREQ(maj, min) (0)
# endif
#endif /* __GNUC_PREREQ */
#ifndef __CLANG_PREREQ
# ifdef __clang__
# define __CLANG_PREREQ(maj,min) \
((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
# else
# define __CLANG_PREREQ(maj,min) (0)
# endif
#endif /* __CLANG_PREREQ */
#ifndef __GLIBC_PREREQ
# if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
# define __GLIBC_PREREQ(maj, min) \
((__GLIBC__ << 16) + __GLIBC_MINOR__ >= ((maj) << 16) + (min))
# else
# define __GLIBC_PREREQ(maj, min) (0)
# endif
#endif /* __GLIBC_PREREQ */
#ifndef __has_attribute
# define __has_attribute(x) (0)
#endif
#ifndef __has_feature
# define __has_feature(x) (0)
#endif
#ifndef __has_extension
# define __has_extension(x) (0)
#endif
#ifndef __has_builtin
# define __has_builtin(x) (0)
#endif
#if __has_feature(thread_sanitizer)
# define __SANITIZE_THREAD__ 1
#endif
#if __has_feature(address_sanitizer)
# define __SANITIZE_ADDRESS__ 1
#endif
/*----------------------------------------------------------------------------*/
#ifndef __extern_C
# ifdef __cplusplus
# define __extern_C extern "C"
# else
# define __extern_C
# endif
#endif /* __extern_C */
#ifndef __cplusplus
# ifndef bool
# define bool _Bool
# endif
# ifndef true
# define true (1)
# endif
# ifndef false
# define false (0)
# endif
#endif
#if !defined(nullptr) && !defined(__cplusplus) || (__cplusplus < 201103L && !defined(_MSC_VER))
# define nullptr NULL
#endif
/*----------------------------------------------------------------------------*/
#if !defined(__thread) && (defined(_MSC_VER) || defined(__DMC__))
# define __thread __declspec(thread)
#endif /* __thread */
#ifndef __alwaysinline
# if defined(__GNUC__) || __has_attribute(always_inline)
# define __alwaysinline __inline __attribute__((always_inline))
# elif defined(_MSC_VER)
# define __alwaysinline __forceinline
# else
# define __alwaysinline
# endif
#endif /* __alwaysinline */
#ifndef __noinline
# if defined(__GNUC__) || __has_attribute(noinline)
# define __noinline __attribute__((noinline))
# elif defined(_MSC_VER)
# define __noinline __declspec(noinline)
# elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
# define __noinline inline
# elif !defined(__INTEL_COMPILER)
# define __noinline /* FIXME ? */
# endif
#endif /* __noinline */
#ifndef __must_check_result
# if defined(__GNUC__) || __has_attribute(warn_unused_result)
# define __must_check_result __attribute__((warn_unused_result))
# else
# define __must_check_result
# endif
#endif /* __must_check_result */
#ifndef __deprecated
# if defined(__GNUC__) || __has_attribute(deprecated)
# define __deprecated __attribute__((deprecated))
# elif defined(_MSC_VER)
# define __deprecated __declspec(deprecated)
# else
# define __deprecated
# endif
#endif /* __deprecated */
#ifndef __packed
# if defined(__GNUC__) || __has_attribute(packed)
# define __packed __attribute__((packed))
# else
# define __packed
# endif
#endif /* __packed */
#ifndef __aligned
# if defined(__GNUC__) || __has_attribute(aligned)
# define __aligned(N) __attribute__((aligned(N)))
# elif defined(_MSC_VER)
# define __aligned(N) __declspec(align(N))
# else
# define __aligned(N)
# endif
#endif /* __aligned */
#ifndef __noreturn
# if defined(__GNUC__) || __has_attribute(noreturn)
# define __noreturn __attribute__((noreturn))
# elif defined(_MSC_VER)
# define __noreturn __declspec(noreturn)
# else
# define __noreturn
# endif
#endif /* __noreturn */
#ifndef __nothrow
# if defined(__GNUC__) || __has_attribute(nothrow)
# define __nothrow __attribute__((nothrow))
# elif defined(_MSC_VER) && defined(__cplusplus)
# define __nothrow __declspec(nothrow)
# else
# define __nothrow
# endif
#endif /* __nothrow */
#ifndef __pure_function
/* Many functions have no effects except the return value and their
* return value depends only on the parameters and/or global variables.
* Such a function can be subject to common subexpression elimination
* and loop optimization just as an arithmetic operator would be.
* These functions should be declared with the attribute pure. */
# if defined(__GNUC__) || __has_attribute(pure)
# define __pure_function __attribute__((pure))
# else
# define __pure_function
# endif
#endif /* __pure_function */
#ifndef __const_function
/* Many functions do not examine any values except their arguments,
* and have no effects except the return value. Basically this is just
* slightly more strict class than the PURE attribute, since function
* is not allowed to read global memory.
*
* Note that a function that has pointer arguments and examines the
* data pointed to must not be declared const. Likewise, a function
* that calls a non-const function usually must not be const.
* It does not make sense for a const function to return void. */
# if defined(__GNUC__) || __has_attribute(const)
# define __const_function __attribute__((const))
# else
# define __const_function
# endif
#endif /* __const_function */
#ifndef __dll_hidden
# if defined(__GNUC__) || __has_attribute(visibility)
# define __hidden __attribute__((visibility("hidden")))
# else
# define __hidden
# endif
#endif /* __dll_hidden */
#ifndef __optimize
# if defined(__OPTIMIZE__)
# if defined(__clang__) && !__has_attribute(optimize)
# define __optimize(ops)
# elif defined(__GNUC__) || __has_attribute(optimize)
# define __optimize(ops) __attribute__((optimize(ops)))
# else
# define __optimize(ops)
# endif
# else
# define __optimize(ops)
# endif
#endif /* __optimize */
#ifndef __hot
# if defined(__OPTIMIZE__)
# if defined(__clang__) && !__has_attribute(hot)
/* just put frequently used functions in separate section */
# define __hot __attribute__((section("text.hot"))) __optimize("O3")
# elif defined(__GNUC__) || __has_attribute(hot)
# define __hot __attribute__((hot)) __optimize("O3")
# else
# define __hot __optimize("O3")
# endif
# else
# define __hot
# endif
#endif /* __hot */
#ifndef __cold
# if defined(__OPTIMIZE__)
# if defined(__clang__) && !__has_attribute(cold)
/* just put infrequently used functions in separate section */
# define __cold __attribute__((section("text.unlikely"))) __optimize("Os")
# elif defined(__GNUC__) || __has_attribute(cold)
# define __cold __attribute__((cold)) __optimize("Os")
# else
# define __cold __optimize("Os")
# endif
# else
# define __cold
# endif
#endif /* __cold */
#ifndef __flatten
# if defined(__OPTIMIZE__) && (defined(__GNUC__) || __has_attribute(flatten))
# define __flatten __attribute__((flatten))
# else
# define __flatten
# endif
#endif /* __flatten */
#ifndef likely
# if defined(__GNUC__) || defined(__clang__)
# define likely(cond) __builtin_expect(!!(cond), 1)
# else
# define likely(x) (x)
# endif
#endif /* likely */
#ifndef unlikely
# if defined(__GNUC__) || defined(__clang__)
# define unlikely(cond) __builtin_expect(!!(cond), 0)
# else
# define unlikely(x) (x)
# endif
#endif /* unlikely */
/*----------------------------------------------------------------------------*/
/* Wrapper around __func__, which is a C99 feature */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define mdbx_func_ __func__
#elif (defined(__GNUC__) && __GNUC__ >= 2) || defined(__clang__) || defined(_MSC_VER)
# define mdbx_func_ __FUNCTION__
#else
# define mdbx_func_ "<mdbx_unknown>"
#endif
/* *INDENT-ON* */
/* clang-format on */
#define MDBX_TETRAD(a, b, c, d) \
((uint32_t)(a) << 24 | (uint32_t)(b) << 16 | (uint32_t)(c) << 8 | \
(uint32_t)(d))

251
src/lck-posix.c Normal file
View File

@ -0,0 +1,251 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "./bits.h"
/* Some platforms define the EOWNERDEAD error code
* even though they don't support Robust Mutexes.
* Compile with -DMDB_USE_ROBUST=0. */
#ifndef MDB_USE_ROBUST
/* Howard Chu: Android currently lacks Robust Mutex support */
#if defined(EOWNERDEAD) && \
!defined(ANDROID) /* LY: glibc before 2.10 has a troubles with Robust \
Mutex too. */ \
&& __GLIBC_PREREQ(2, 10)
#define MDB_USE_ROBUST 1
#else
#define MDB_USE_ROBUST 0
#endif
#endif /* MDB_USE_ROBUST */
/*----------------------------------------------------------------------------*/
/* rthc */
static mdbx_mutex_t mdbx_rthc_mutex = PTHREAD_MUTEX_INITIALIZER;
void mdbx_rthc_lock(void) {
mdbx_ensure(NULL, pthread_mutex_lock(&mdbx_rthc_mutex) == 0);
}
void mdbx_rthc_unlock(void) {
mdbx_ensure(NULL, pthread_mutex_unlock(&mdbx_rthc_mutex) == 0);
}
/*----------------------------------------------------------------------------*/
/* lck */
static int mdbx_lck_op(mdbx_filehandle_t fd, int op, int lck, off_t offset);
static int mdbx_mutex_failed(MDB_env *env, pthread_mutex_t *mutex, int rc);
int mdbx_lck_init(MDB_env *env) {
pthread_mutexattr_t ma;
int rc = pthread_mutexattr_init(&ma);
if (rc)
return rc;
rc = pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED);
if (rc)
goto bailout;
#if MDB_USE_ROBUST
#if __GLIBC_PREREQ(2, 12)
rc = pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
#else
rc = pthread_mutexattr_setrobust_np(&ma, PTHREAD_MUTEX_ROBUST_NP);
#endif
if (rc)
goto bailout;
#endif /* MDB_USE_ROBUST */
#if _POSIX_C_SOURCE >= 199506L
rc = pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT);
if (rc == ENOTSUP)
rc = pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_NONE);
if (rc)
goto bailout;
#endif /* PTHREAD_PRIO_INHERIT */
rc = pthread_mutex_init(&env->me_txns->mti_rmutex, &ma);
if (rc)
goto bailout;
rc = pthread_mutex_init(&env->me_txns->mti_wmutex, &ma);
bailout:
pthread_mutexattr_destroy(&ma);
return rc;
}
void mdbx_lck_destroy(MDB_env *env) {
if (env->me_lfd != INVALID_HANDLE_VALUE) {
/* try get exclusive access */
if (mdbx_lck_op(env->me_lfd, F_SETLK, F_WRLCK, 0) == 0) {
/* got exclusive, drown mutexes */
int rc = pthread_mutex_destroy(&env->me_txns->mti_rmutex);
if (rc == 0)
rc = pthread_mutex_destroy(&env->me_txns->mti_wmutex);
assert(rc == 0);
(void)rc;
/* lock would be released (by kernel) while the me_lfd will be closed */
}
}
}
static int mdbx_robust_lock(MDB_env *env, pthread_mutex_t *mutex) {
int rc = pthread_mutex_lock(mutex);
if (unlikely(rc != 0))
rc = mdbx_mutex_failed(env, mutex, rc);
return rc;
}
static int mdbx_robust_unlock(MDB_env *env, pthread_mutex_t *mutex) {
int rc = pthread_mutex_unlock(mutex);
if (unlikely(rc != 0))
rc = mdbx_mutex_failed(env, mutex, rc);
return rc;
}
int mdbx_rdt_lock(MDB_env *env) {
return mdbx_robust_lock(env, &env->me_txns->mti_rmutex);
}
void mdbx_rdt_unlock(MDB_env *env) {
int rc = mdbx_robust_unlock(env, &env->me_txns->mti_rmutex);
if (unlikely(rc != 0))
mdbx_panic("%s() failed: errcode %d\n", mdbx_func_, rc);
}
int mdbx_txn_lock(MDB_env *env) {
return mdbx_robust_lock(env, &env->me_txns->mti_wmutex);
}
void mdbx_txn_unlock(MDB_env *env) {
int rc = mdbx_robust_unlock(env, &env->me_txns->mti_wmutex);
if (unlikely(rc != 0))
mdbx_panic("%s() failed: errcode %d\n", mdbx_func_, rc);
}
int mdbx_lck_seize(MDB_env *env) {
/* try exclusive access */
int rc = mdbx_lck_op(env->me_lfd, F_SETLK, F_WRLCK, 0);
if (rc == 0)
/* got exclusive */
return MDBX_RESULT_TRUE;
if (rc == EAGAIN || rc == EACCES || rc == EBUSY) {
/* get shared access */
rc = mdbx_lck_op(env->me_lfd, F_SETLKW, F_RDLCK, 0);
if (rc == 0) {
/* got shared, try exclusive again */
rc = mdbx_lck_op(env->me_lfd, F_SETLK, F_WRLCK, 0);
if (rc == 0)
/* now got exclusive */
return MDBX_RESULT_TRUE;
if (rc == EAGAIN || rc == EACCES || rc == EBUSY)
/* unable exclusive, but stay shared */
return MDBX_RESULT_FALSE;
}
}
assert(rc != MDBX_RESULT_FALSE && rc != MDBX_RESULT_TRUE);
return rc;
}
int mdbx_lck_downgrade(MDB_env *env) {
return mdbx_lck_op(env->me_lfd, F_SETLK, F_RDLCK, 0);
}
int mdbx_rpid_set(MDB_env *env) {
return mdbx_lck_op(env->me_lfd, F_SETLK, F_WRLCK, env->me_pid);
}
int mdbx_rpid_clear(MDB_env *env) {
return mdbx_lck_op(env->me_lfd, F_SETLKW, F_UNLCK, env->me_pid);
}
int mdbx_rpid_check(MDB_env *env, mdbx_pid_t pid) {
int rc = mdbx_lck_op(env->me_lfd, F_GETLK, F_WRLCK, pid);
if (rc == 0)
return MDBX_RESULT_FALSE;
if (rc < 0 && -rc == pid)
return MDBX_RESULT_TRUE;
return rc;
}
static int mdbx_lck_op(mdbx_filehandle_t fd, int op, int lck, off_t offset) {
for (;;) {
int rc;
struct flock lock_op;
memset(&lock_op, 0, sizeof(lock_op));
lock_op.l_type = lck;
lock_op.l_whence = SEEK_SET;
lock_op.l_start = offset;
lock_op.l_len = 1;
if ((rc = fcntl(fd, op, &lock_op)) == 0) {
if (op == F_GETLK && lock_op.l_type != F_UNLCK)
rc = -lock_op.l_pid;
} else if ((rc = errno) == EINTR) {
continue;
}
return rc;
}
}
static int __cold mdbx_mutex_failed(MDB_env *env, mdbx_mutex_t *mutex, int rc) {
#if MDB_USE_ROBUST
if (unlikely(rc == EOWNERDEAD)) {
int rlocked, rc2;
/* We own the mutex. Clean up after dead previous owner. */
rc = MDB_SUCCESS;
rlocked = (mutex == &env->me_txns->mti_rmutex);
if (!rlocked) {
/* Keep mtb.mti_txnid updated, otherwise next writer can
* overwrite data which latest meta page refers to.
*
* LY: Hm, how this can happen, if the mtb.mti_txnid
* is updating only at the finish of a successful commit ?
*/
MDB_meta *meta = mdbx_meta_head_w(env);
assert(env->me_txns->mti_txnid == meta->mm_txnid);
(void)meta;
/* env is hosed if the dead thread was ours */
if (env->me_txn) {
env->me_flags |= MDB_FATAL_ERROR;
env->me_txn = NULL;
rc = MDB_PANIC;
}
}
mdbx_debug("%cmutex owner died, %s", (rlocked ? 'r' : 'w'),
(rc ? "this process' env is hosed" : "recovering"));
rc2 = mdbx_reader_check0(env, rlocked, NULL);
if (rc2 == 0)
#if __GLIBC_PREREQ(2, 12)
rc2 = pthread_mutex_consistent(mutex);
#else
rc2 = pthread_mutex_consistent_np(mutex);
#endif
if (rc || (rc = rc2)) {
mdbx_debug("mutex recovery failed, %s", mdbx_strerror(rc));
pthread_mutex_unlock(mutex);
}
}
#endif /* MDB_USE_ROBUST */
if (unlikely(rc)) {
mdbx_debug("lock mutex failed, %s", mdbx_strerror(rc));
if (rc != EDEADLK) {
env->me_flags |= MDB_FATAL_ERROR;
rc = MDB_PANIC;
}
}
return rc;
}

314
src/lck-windows.c Normal file
View File

@ -0,0 +1,314 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "./bits.h"
/* PREAMBLE FOR WINDOWS:
*
* We are not concerned for performance here.
* If you are running Windows a performance could NOT be the goal.
* Otherwise please use Linux.
*
* Regards,
* LY
*/
/*----------------------------------------------------------------------------*/
/* rthc */
static CRITICAL_SECTION rthc_critical_section;
static void NTAPI tls_callback(PVOID module, DWORD reason, PVOID reserved) {
(void)module;
(void)reserved;
switch (reason) {
case DLL_PROCESS_ATTACH:
InitializeCriticalSection(&rthc_critical_section);
break;
case DLL_PROCESS_DETACH:
DeleteCriticalSection(&rthc_critical_section);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
mdbx_rthc_cleanup();
break;
}
}
void mdbx_rthc_lock(void) { EnterCriticalSection(&rthc_critical_section); }
void mdbx_rthc_unlock(void) { LeaveCriticalSection(&rthc_critical_section); }
/* *INDENT-OFF* */
/* clang-format off */
#if defined(_MSC_VER)
# pragma const_seg(push)
# pragma data_seg(push)
# ifdef _WIN64
/* kick a linker to create the TLS directory if not already done */
# pragma comment(linker, "/INCLUDE:_tls_used")
/* Force some symbol references. */
# pragma comment(linker, "/INCLUDE:mdbx_tls_callback")
/* specific const-segment for WIN64 */
# pragma const_seg(".CRT$XLB")
const
# else
/* kick a linker to create the TLS directory if not already done */
# pragma comment(linker, "/INCLUDE:__tls_used")
/* Force some symbol references. */
# pragma comment(linker, "/INCLUDE:_mdbx_tls_callback")
/* specific data-segment for WIN32 */
# pragma data_seg(".CRT$XLB")
# endif
PIMAGE_TLS_CALLBACK mdbx_tls_callback = tls_callback;
# pragma data_seg(pop)
# pragma const_seg(pop)
#elif defined(__GNUC__)
# ifdef _WIN64
const
# endif
PIMAGE_TLS_CALLBACK mdbx_tls_callback __attribute__((section(".CRT$XLB"), used))
= tls_callback;
#else
# error FIXME
#endif
/* *INDENT-ON* */
/* clang-format on */
/*----------------------------------------------------------------------------*/
#define LCK_SHARED 0
#define LCK_EXCLUSIVE LOCKFILE_EXCLUSIVE_LOCK
#define LCK_WAITFOR 0
#define LCK_DONTWAIT LOCKFILE_FAIL_IMMEDIATELY
static BOOL flock(mdbx_filehandle_t fd, DWORD flags, off_t offset,
size_t bytes) {
OVERLAPPED ov;
ov.hEvent = 0;
ov.Offset = (DWORD)offset;
ov.OffsetHigh = HIGH_DWORD(offset);
return LockFileEx(fd, flags, 0, (DWORD)bytes, HIGH_DWORD(bytes), &ov);
}
static BOOL funlock(mdbx_filehandle_t fd, off_t offset, size_t bytes) {
return UnlockFile(fd, (DWORD)offset, HIGH_DWORD(offset), (DWORD)bytes,
HIGH_DWORD(bytes));
}
/*----------------------------------------------------------------------------*/
/* global `write` lock for write-txt processing,
* exclusive locking both meta-pages) */
int mdbx_txn_lock(MDB_env *env) {
if (flock(env->me_fd, LCK_EXCLUSIVE | LCK_WAITFOR, 0, env->me_psize * 2))
return MDB_SUCCESS;
return GetLastError();
}
void mdbx_txn_unlock(MDB_env *env) {
if (!funlock(env->me_fd, 0, env->me_psize * 2))
mdbx_panic("%s failed: errcode %u", mdbx_func_, GetLastError());
}
/*----------------------------------------------------------------------------*/
/* global `read` lock for readers registration,
* exclusive locking `mti_numreaders` (second) cacheline */
#define LCK_LO_OFFSET 0
#define LCK_LO_LEN offsetof(MDBX_lockinfo, mti_numreaders)
#define LCK_UP_OFFSET LCK_LO_LEN
#define LCK_UP_LEN (MDBX_LOCKINFO_WHOLE_SIZE - LCK_UP_OFFSET)
#define LCK_LOWER LCK_LO_OFFSET, LCK_LO_LEN
#define LCK_UPPER LCK_UP_OFFSET, LCK_UP_LEN
int mdbx_rdt_lock(MDB_env *env) {
if (env->me_lfd == INVALID_HANDLE_VALUE)
return MDB_SUCCESS; /* readonly database in readonly filesystem */
/* transite from S-? (used) to S-E (locked), e.g. exlcusive lock upper-part */
if (flock(env->me_lfd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_UPPER))
return MDB_SUCCESS;
return GetLastError();
}
void mdbx_rdt_unlock(MDB_env *env) {
if (env->me_lfd != INVALID_HANDLE_VALUE) {
/* transite from S-E (locked) to S-? (used), e.g. unlock upper-part */
if (!funlock(env->me_lfd, LCK_UPPER))
mdbx_panic("%s failed: errcode %u", mdbx_func_, GetLastError());
}
}
/*----------------------------------------------------------------------------*/
/* global `initial` lock for lockfile initialization,
* exclusive/shared locking first cacheline */
/* FIXME: locking scheme/algo descritpion.
?-? = free
S-? = used
E-?
?-S
?-E = middle
S-S
S-E = locked
E-S
E-E = exclusive
*/
int mdbx_lck_init(MDB_env *env) {
(void)env;
return MDB_SUCCESS;
}
/* Seize state as exclusive (E-E and returns MDBX_RESULT_TRUE)
* or used (S-? and returns MDBX_RESULT_FALSE), otherwise returns an error */
int mdbx_lck_seize(MDB_env *env) {
/* 1) now on ?-? (free), get ?-E (middle) */
if (!flock(env->me_lfd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_UPPER))
return GetLastError() /* 2) something went wrong, give up */;
/* 3) now on ?-E (middle), try E-E (exclusive) */
if (flock(env->me_lfd, LCK_EXCLUSIVE | LCK_DONTWAIT, LCK_LOWER))
return MDBX_RESULT_TRUE; /* 4) got E-E (exclusive), done */
/* 5) still on ?-E (middle) */
int rc = GetLastError();
if (rc != ERROR_SHARING_VIOLATION && rc != ERROR_LOCK_VIOLATION) {
/* 6) something went wrong, give up */
if (!funlock(env->me_lfd, LCK_UPPER)) {
rc = GetLastError();
mdbx_panic("%s(%s) failed: errcode %u", mdbx_func_,
"?-E(middle) >> ?-?(free)", rc);
}
return rc;
}
/* 7) still on ?-E (middle), try S-E (locked) */
rc = flock(env->me_lfd, LCK_EXCLUSIVE | LCK_DONTWAIT, LCK_LOWER)
? MDBX_RESULT_FALSE
: GetLastError();
/* 8) now on S-E (locked) or still on ?-E (middle),
* transite to S-? (used) or ?-? (free) */
if (!funlock(env->me_lfd, LCK_UPPER)) {
rc = GetLastError();
mdbx_panic("%s(%s) failed: errcode %u", mdbx_func_,
"X-E(locked/middle) >> X-?(used/free)", rc);
}
/* 9) now on S-? (used, DONE) or ?-? (free, FAILURE) */
return rc;
}
/* Transite from exclusive state (E-E) to used (S-?) */
int mdbx_lck_downgrade(MDB_env *env) {
int rc;
/* 1) now at E-E (exclusive), continue transition to ?_E (middle) */
if (!funlock(env->me_lfd, LCK_LOWER)) {
rc = GetLastError();
mdbx_panic("%s(%s) failed: errcode %u", mdbx_func_,
"E-E(exclusive) >> ?-E(middle)", rc);
}
/* 2) now at ?-E (middle), transite to S-E (locked) */
if (!flock(env->me_lfd, LCK_SHARED | LCK_DONTWAIT, LCK_LOWER)) {
rc = GetLastError() /* 3) something went wrong, give up */;
return rc;
}
/* 4) got S-E (locked), continue transition to S-? (used) */
if (!funlock(env->me_lfd, LCK_UPPER)) {
rc = GetLastError();
mdbx_panic("%s(%s) failed: errcode %u", mdbx_func_,
"S-E(locked) >> S-?(used)", rc);
}
return MDB_SUCCESS /* 5) now at S-? (used), done */;
}
void mdbx_lck_destroy(MDB_env *env) {
int rc;
if (env->me_fd != INVALID_HANDLE_VALUE) {
/* explicitly unlock to avoid latency for other processes (windows kernel
* releases such locks via deferred queues) */
while (funlock(env->me_fd, 0, env->me_psize * 2))
;
rc = GetLastError();
assert(rc == ERROR_NOT_LOCKED);
(void)rc;
SetLastError(ERROR_SUCCESS);
}
if (env->me_lfd != INVALID_HANDLE_VALUE) {
/* double `unlock` for robustly remove overlapped shared/exclusive locks */
while (funlock(env->me_lfd, LCK_LOWER))
;
assert(rc == ERROR_NOT_LOCKED);
(void)rc;
SetLastError(ERROR_SUCCESS);
while (funlock(env->me_lfd, LCK_UPPER))
;
assert(rc == ERROR_NOT_LOCKED);
(void)rc;
SetLastError(ERROR_SUCCESS);
}
}
/*----------------------------------------------------------------------------*/
/* reader checking (by pid) */
int mdbx_rpid_set(MDB_env *env) {
(void)env;
return MDB_SUCCESS;
}
int mdbx_rpid_clear(MDB_env *env) {
(void)env;
return MDB_SUCCESS;
}
int mdbx_rpid_check(MDB_env *env, mdbx_pid_t pid) {
(void)env;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
int rc;
if (hProcess) {
rc = WaitForSingleObject(hProcess, 0);
CloseHandle(hProcess);
} else {
rc = GetLastError();
}
switch (rc) {
case ERROR_INVALID_PARAMETER:
/* pid seem invalid */
return MDBX_RESULT_FALSE;
case WAIT_OBJECT_0:
/* process just exited */
return MDBX_RESULT_FALSE;
case WAIT_TIMEOUT:
/* pid running */
return MDBX_RESULT_TRUE;
default:
/* failure */
return rc;
}
}

2453
src/mdbx.c

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,19 @@
/** A generic unsigned ID number. These were entryIDs in back-bdb.
* Preferably it should have the same size as a pointer.
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
typedef size_t MDB_ID;
/** An IDL is an ID List, a sorted array of IDs. The first
* element of the array is a counter for how many actual
* IDs are in the list. In the original back-bdb code, IDLs are
* sorted in ascending order. For libmdb IDLs are sorted in
* descending order.
*/
typedef MDB_ID *MDB_IDL;
/* IDL sizes - likely should be even bigger
* limiting factors: sizeof(ID), thread stack size
*/
* limiting factors: sizeof(ID), thread stack size */
#define MDB_IDL_LOGN 16 /* DB_SIZE is 2^16, UM_SIZE is 2^17 */
#define MDB_IDL_DB_SIZE (1 << MDB_IDL_LOGN)
#define MDB_IDL_UM_SIZE (1 << (MDB_IDL_LOGN + 1))
@ -27,27 +27,12 @@ typedef MDB_ID *MDB_IDL;
#define MDB_IDL_FIRST(ids) ((ids)[1])
#define MDB_IDL_LAST(ids) ((ids)[(ids)[0]])
/** Current max length of an #mdbx_midl_alloc()ed IDL */
/* Current max length of an #mdbx_midl_alloc()ed IDL */
#define MDB_IDL_ALLOCLEN(ids) ((ids)[-1])
/** Append ID to IDL. The IDL must be big enough. */
/* Append ID to IDL. The IDL must be big enough. */
#define mdbx_midl_xappend(idl, id) \
do { \
MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \
xidl[xlen] = (id); \
} while (0)
/** An ID2 is an ID/pointer pair.
*/
typedef struct MDB_ID2 {
MDB_ID mid; /**< The ID */
void *mptr; /**< The pointer */
} MDB_ID2;
/** An ID2L is an ID2 List, a sorted array of ID2s.
* The first element's \b mid member is a count of how many actual
* elements are in the array. The \b mptr member of the first element is
* unused.
* The array is sorted in ascending order by \b mid.
*/
typedef MDB_ID2 *MDB_ID2L;

625
src/osal.c Normal file
View File

@ -0,0 +1,625 @@
/* https://en.wikipedia.org/wiki/Operating_system_abstraction_layer */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "./bits.h"
#if defined(_WIN32) || defined(_WIN64)
static int waitfor2errcode(DWORD result) {
switch (result) {
case WAIT_OBJECT_0:
return MDB_SUCCESS;
case WAIT_FAILED:
return GetLastError();
case WAIT_ABANDONED:
return ERROR_ABANDONED_WAIT_0;
case WAIT_IO_COMPLETION:
return ERROR_USER_APC;
case WAIT_TIMEOUT:
return ERROR_TIMEOUT;
default:
return ERROR_UNHANDLED_ERROR;
}
}
#endif /* _WIN32 || _WIN64 */
/*----------------------------------------------------------------------------*/
#ifndef _MSC_VER
/* Prototype should match libc runtime. ISO POSIX (2003) & LSB 3.1 */
__nothrow __noreturn void __assert_fail(const char *assertion, const char *file,
unsigned line, const char *function);
#else
__extern_C __declspec(dllimport) void __cdecl _assert(char const *message,
char const *filename,
unsigned line);
#endif /* _MSC_VER */
#ifndef mdbx_assert_fail
void __cold mdbx_assert_fail(MDB_env *env, const char *msg, const char *func,
int line) {
#if MDB_DEBUG
if (env && env->me_assert_func) {
env->me_assert_func(env, msg, func, line);
return;
}
#else
(void)env;
#endif /* MDB_DEBUG */
if (mdbx_debug_logger)
mdbx_debug_log(MDBX_DBG_ASSERT, func, line, "assert: %s\n", msg);
#ifndef _MSC_VER
__assert_fail(msg, "mdbx", line, func);
#else
_assert(msg, func, line);
#endif /* _MSC_VER */
}
#endif /* mdbx_assert_fail */
__cold void mdbx_panic(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
#ifdef _MSC_VER
if (IsDebuggerPresent()) {
OutputDebugString("\r\n" FIXME "\r\n");
FatalExit(ERROR_UNHANDLED_ERROR);
}
#elif _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L || \
(__GLIBC_PREREQ(1, 0) && !__GLIBC_PREREQ(2, 10) && defined(_GNU_SOURCE))
vdprintf(STDERR_FILENO, fmt, ap);
#else
#error FIXME
#endif
va_end(ap);
abort();
}
/*----------------------------------------------------------------------------*/
#ifndef mdbx_asprintf
int mdbx_asprintf(char **strp, const char *fmt, ...) {
va_list ap, ones;
va_start(ap, fmt);
va_copy(ones, ap);
#ifdef _MSC_VER
int needed = _vscprintf(fmt, ap);
#elif defined(_BSD_SOURCE) || _XOPEN_SOURCE >= 500 || \
defined(_ISOC99_SOURCE) || _POSIX_C_SOURCE >= 200112L
int needed = vsnprintf(nullptr, 0, fmt, ap);
#else
#error FIXME
#endif
va_end(ap);
if (unlikely(needed < 0 || needed >= INT_MAX)) {
*strp = NULL;
va_end(ones);
return needed;
}
*strp = malloc(needed + 1);
if (unlikely(*strp == NULL)) {
va_end(ones);
return -ENOMEM;
}
#ifdef _MSC_VER
int actual = vsnprintf_s(*strp, needed + 1, _TRUNCATE, fmt, ones);
#elif defined(_BSD_SOURCE) || _XOPEN_SOURCE >= 500 || \
defined(_ISOC99_SOURCE) || _POSIX_C_SOURCE >= 200112L
int actual = vsnprintf(*strp, needed + 1, fmt, ones);
#else
#error FIXME
#endif
va_end(ones);
assert(actual == needed);
if (unlikely(actual < 0)) {
free(*strp);
*strp = NULL;
}
return actual;
}
#endif /* mdbx_asprintf */
#ifndef mdbx_memalign_alloc
int mdbx_memalign_alloc(size_t alignment, size_t bytes, void **result) {
#if _MSC_VER
*result = _aligned_malloc(bytes, alignment);
return *result ? MDB_SUCCESS : ERROR_OUTOFMEMORY;
#elif __GLIBC_PREREQ(2, 16) || __STDC_VERSION__ >= 201112L
*result = memalign(alignment, bytes);
return *result ? MDB_SUCCESS : errno;
#elif _POSIX_VERSION >= 200112L
*result = NULL;
return posix_memalign(result, alignment, bytes);
#else
#error FIXME
#endif
}
#endif /* mdbx_memalign_alloc */
#ifndef mdbx_memalign_free
void mdbx_memalign_free(void *ptr) {
#if _MSC_VER
_aligned_free(ptr);
#else
free(ptr);
#endif
}
#endif /* mdbx_memalign_free */
/*----------------------------------------------------------------------------*/
int mdbx_mutex_init(mdbx_mutex_t *mutex) {
#if defined(_WIN32) || defined(_WIN64)
*mutex = CreateMutex(NULL, FALSE, NULL);
return *mutex ? MDB_SUCCESS : GetLastError();
#else
return pthread_mutex_init(mutex, NULL);
#endif
}
int mdbx_mutex_destroy(mdbx_mutex_t *mutex) {
#if defined(_WIN32) || defined(_WIN64)
return CloseHandle(*mutex) ? MDB_SUCCESS : GetLastError();
#else
return pthread_mutex_destroy(mutex);
#endif
}
int mdbx_mutex_lock(mdbx_mutex_t *mutex) {
#if defined(_WIN32) || defined(_WIN64)
DWORD code = WaitForSingleObject(*mutex, INFINITE);
return waitfor2errcode(code);
#else
return pthread_mutex_lock(mutex);
#endif
}
int mdbx_mutex_unlock(mdbx_mutex_t *mutex) {
#if defined(_WIN32) || defined(_WIN64)
return ReleaseMutex(*mutex) ? MDB_SUCCESS : GetLastError();
#else
return pthread_mutex_unlock(mutex);
#endif
}
/*----------------------------------------------------------------------------*/
int mdbx_cond_init(mdbx_cond_t *cond) {
#if defined(_WIN32) || defined(_WIN64)
*cond = CreateEvent(NULL, FALSE, FALSE, NULL);
return *cond ? MDB_SUCCESS : GetLastError();
#else
return pthread_cond_init(cond, NULL);
#endif
}
#ifndef mdbx_cond_destroy
int mdbx_cond_destroy(mdbx_cond_t *cond) {
#if defined(_WIN32) || defined(_WIN64)
return CloseHandle(*cond) ? MDB_SUCCESS : GetLastError();
#else
return pthread_cond_destroy(cond);
#endif
}
#endif /* mdbx_cond_destroy */
int mdbx_cond_signal(mdbx_cond_t *cond) {
#if defined(_WIN32) || defined(_WIN64)
return SetEvent(*cond) ? MDB_SUCCESS : GetLastError();
#else
return pthread_cond_signal(cond);
#endif
}
int mdbx_cond_wait(mdbx_cond_t *cond, mdbx_mutex_t *mutex) {
#if defined(_WIN32) || defined(_WIN64)
DWORD code = SignalObjectAndWait(*mutex, *cond, INFINITE, FALSE);
if (code == WAIT_OBJECT_0)
code = WaitForSingleObject(*mutex, INFINITE);
return waitfor2errcode(code);
#else
return pthread_cond_wait(cond, mutex);
#endif
}
/*----------------------------------------------------------------------------*/
int mdbx_openfile(const char *pathname, int flags, mode_t mode,
mdbx_filehandle_t *fd) {
*fd = INVALID_HANDLE_VALUE;
#if defined(_WIN32) || defined(_WIN64)
(void)mode;
DWORD DesiredAccess;
DWORD ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
DWORD FlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
default:
return ERROR_INVALID_PARAMETER;
case O_RDONLY:
DesiredAccess = GENERIC_READ;
break;
case O_WRONLY: /* assume for mdb_env_copy() and friends output */
DesiredAccess = GENERIC_WRITE;
ShareMode = 0;
FlagsAndAttributes |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
break;
case O_RDWR:
DesiredAccess = GENERIC_READ | GENERIC_WRITE;
break;
}
DWORD CreationDisposition;
switch (flags & (O_EXCL | O_CREAT)) {
default:
return ERROR_INVALID_PARAMETER;
case 0:
CreationDisposition = OPEN_EXISTING;
break;
case O_EXCL | O_CREAT:
CreationDisposition = CREATE_NEW;
FlagsAndAttributes |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
break;
case O_CREAT:
CreationDisposition = OPEN_ALWAYS;
FlagsAndAttributes |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
break;
}
*fd = CreateFileA(pathname, DesiredAccess, ShareMode, NULL,
CreationDisposition, FlagsAndAttributes, NULL);
if (*fd == INVALID_HANDLE_VALUE)
return GetLastError();
if ((flags & O_CREAT) && GetLastError() != ERROR_ALREADY_EXISTS) {
/* set FILE_ATTRIBUTE_NOT_CONTENT_INDEXED for new file */
DWORD FileAttributes = GetFileAttributesA(pathname);
if (FileAttributes == INVALID_FILE_ATTRIBUTES ||
!SetFileAttributesA(pathname, FileAttributes |
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)) {
int rc = GetLastError();
CloseHandle(*fd);
*fd = INVALID_HANDLE_VALUE;
return rc;
}
}
#else
#ifdef O_CLOEXEC
flags |= O_CLOEXEC;
#endif
*fd = open(pathname, flags, mode);
if (*fd < 0)
return errno;
#if defined(FD_CLOEXEC) && defined(F_GETFD)
flags = fcntl(*fd, F_GETFD);
if (flags >= 0)
(void)fcntl(*fd, F_SETFD, flags | FD_CLOEXEC);
#endif
#endif
return MDB_SUCCESS;
}
int mdbx_closefile(mdbx_filehandle_t fd) {
#if defined(_WIN32) || defined(_WIN64)
return CloseHandle(fd) ? MDB_SUCCESS : GetLastError();
#else
return (close(fd) == 0) ? MDB_SUCCESS : errno;
#endif
}
int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, off_t offset) {
#if defined(_WIN32) || defined(_WIN64)
if (bytes > MAX_WRITE)
return ERROR_INVALID_PARAMETER;
OVERLAPPED ov;
ov.hEvent = 0;
ov.Offset = (DWORD)offset;
ov.OffsetHigh = HIGH_DWORD(offset);
DWORD read;
if (unlikely(!ReadFile(fd, buf, (DWORD)bytes, &read, &ov))) {
int rc = GetLastError();
if (rc == ERROR_HANDLE_EOF && read == 0 && offset == 0)
return ENOENT;
return rc;
}
return (read == bytes) ? MDB_SUCCESS : ERROR_READ_FAULT;
#else
ssize_t read = pread(fd, buf, bytes, offset);
if (likely(bytes == (size_t)read))
return MDB_SUCCESS;
if (read < 0)
return errno;
return (read == 0 && offset == 0) ? ENOENT : EIO;
#endif
}
int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes,
off_t offset) {
#if defined(_WIN32) || defined(_WIN64)
if (bytes > MAX_WRITE)
return ERROR_INVALID_PARAMETER;
OVERLAPPED ov;
ov.hEvent = 0;
ov.Offset = (DWORD)offset;
ov.OffsetHigh = HIGH_DWORD(offset);
DWORD written;
if (likely(WriteFile(fd, buf, (DWORD)bytes, &written, &ov)))
return (bytes == written) ? MDB_SUCCESS : ERROR_WRITE_FAULT;
return GetLastError();
#else
int rc;
ssize_t written;
do {
written = pwrite(fd, buf, bytes, offset);
if (likely(bytes == (size_t)written))
return MDB_SUCCESS;
rc = errno;
} while (rc == EINTR);
return (written < 0) ? rc : EIO /* Use which error code (ENOSPC)? */;
#endif
}
int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt,
off_t offset, size_t expected_written) {
#if defined(_WIN32) || defined(_WIN64)
size_t written = 0;
for (int i = 0; i > iovcnt; ++i) {
int rc = mdbx_pwrite(fd, iov[i].iov_base, iov[i].iov_len, offset);
if (unlikely(rc != MDB_SUCCESS))
return rc;
written += iov[i].iov_len;
offset += iov[i].iov_len;
}
return (expected_written == written) ? MDB_SUCCESS : ERROR_WRITE_FAULT;
#else
int rc;
ssize_t written;
do {
written = pwritev(fd, iov, iovcnt, offset);
if (likely(expected_written == (size_t)written))
return MDB_SUCCESS;
rc = errno;
} while (rc == EINTR);
return (written < 0) ? rc : EIO /* Use which error code? */;
#endif
}
int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) {
#ifdef SIGPIPE
sigset_t set, old;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
int rc = rc = pthread_sigmask(SIG_BLOCK, &set, &old);
if (rc != 0)
return rc;
#endif
const char *ptr = buf;
for (;;) {
size_t chunk = (MAX_WRITE < bytes) ? MAX_WRITE : bytes;
#if defined(_WIN32) || defined(_WIN64)
DWORD written;
if (unlikely(!WriteFile(fd, ptr, (DWORD)chunk, &written, NULL)))
return GetLastError();
#else
ssize_t written = write(fd, ptr, chunk);
if (written < 0) {
int rc = errno;
#ifdef SIGPIPE
if (rc == EPIPE) {
/* Collect the pending SIGPIPE, otherwise at least OS X
* gives it to the process on thread-exit (ITS#8504).
*/
int tmp;
sigwait(&set, &tmp);
written = 0;
continue;
}
pthread_sigmask(SIG_SETMASK, &old, NULL);
#endif
return rc;
}
#endif
if (likely(bytes == (size_t)written)) {
#ifdef SIGPIPE
pthread_sigmask(SIG_SETMASK, &old, NULL);
#endif
return MDB_SUCCESS;
}
ptr += written;
bytes -= written;
}
}
int mdbx_filesync(mdbx_filehandle_t fd, bool syncmeta) {
#if defined(_WIN32) || defined(_WIN64)
(void)syncmeta;
return FlushFileBuffers(fd) ? 0 : -1;
#elif __GLIBC_PREREQ(2, 16) || _BSD_SOURCE || _XOPEN_SOURCE || \
(__GLIBC_PREREQ(2, 8) && _POSIX_C_SOURCE >= 200112L)
#if _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500 || \
defined(_POSIX_SYNCHRONIZED_IO)
if (!syncmeta)
return (fdatasync(fd) == 0) ? MDB_SUCCESS : errno;
#endif
(void)syncmeta;
return (fsync(fd) == 0) ? MDB_SUCCESS : errno;
#else
#error FIXME
#endif
}
int mdbx_filesize(mdbx_filehandle_t fd, off_t *length) {
#if defined(_WIN32) || defined(_WIN64)
BY_HANDLE_FILE_INFORMATION info;
if (!GetFileInformationByHandle(fd, &info))
return GetLastError();
*length = info.nFileSizeLow | (uint64_t)info.nFileIndexHigh << 32;
#else
struct stat st;
if (fstat(fd, &st))
return errno;
*length = st.st_size;
#endif
return MDB_SUCCESS;
}
int mdbx_ftruncate(mdbx_filehandle_t fd, off_t length) {
#if defined(_WIN32) || defined(_WIN64)
LARGE_INTEGER li;
li.QuadPart = length;
return (SetFilePointerEx(fd, li, NULL, FILE_BEGIN) && SetEndOfFile(fd))
? MDB_SUCCESS
: GetLastError();
#else
return ftruncate(fd, length) == 0 ? MDB_SUCCESS : errno;
#endif
}
/*----------------------------------------------------------------------------*/
int mdbx_thread_key_create(mdbx_thread_key_t *key) {
#if defined(_WIN32) || defined(_WIN64)
*key = TlsAlloc();
return (*key != TLS_OUT_OF_INDEXES) ? MDB_SUCCESS : GetLastError();
#else
return pthread_key_create(key, mdbx_rthc_dtor);
#endif
}
void mdbx_thread_key_delete(mdbx_thread_key_t key) {
#if defined(_WIN32) || defined(_WIN64)
mdbx_ensure(NULL, TlsFree(key));
#else
mdbx_ensure(NULL, pthread_key_delete(key) == 0);
#endif
}
void *mdbx_thread_rthc_get(mdbx_thread_key_t key) {
#if defined(_WIN32) || defined(_WIN64)
return TlsGetValue(key);
#else
return pthread_getspecific(key);
#endif
}
void mdbx_thread_rthc_set(mdbx_thread_key_t key, const void *value) {
#if defined(_WIN32) || defined(_WIN64)
mdbx_ensure(NULL, TlsSetValue(key, (void *)value));
#else
mdbx_ensure(NULL, pthread_setspecific(key, value) == 0);
#endif
}
mdbx_tid_t mdbx_thread_self(void) {
#if defined(_WIN32) || defined(_WIN64)
return GetCurrentThreadId();
#else
return pthread_self();
#endif
}
int mdbx_thread_create(mdbx_thread_t *thread,
THREAD_RESULT(THREAD_CALL *start_routine)(void *),
void *arg) {
#if defined(_WIN32) || defined(_WIN64)
*thread = CreateThread(NULL, 0, start_routine, arg, 0, NULL);
return *thread ? MDB_SUCCESS : GetLastError();
#else
return pthread_create(thread, NULL, start_routine, arg);
#endif
}
int mdbx_thread_join(mdbx_thread_t thread) {
#if defined(_WIN32) || defined(_WIN64)
DWORD code = WaitForSingleObject(thread, INFINITE);
return waitfor2errcode(code);
#else
void *unused_retval = &unused_retval;
return pthread_join(thread, &unused_retval);
#endif
}
/*----------------------------------------------------------------------------*/
int mdbx_msync(void *addr, size_t length, int async) {
#if defined(_WIN32) || defined(_WIN64)
if (async)
return MDB_SUCCESS;
return FlushViewOfFile(addr, length) ? 0 : GetLastError();
#else
return (msync(addr, length, async ? MS_ASYNC : MS_SYNC) == 0) ? MDB_SUCCESS
: errno;
#endif
}
int mdbx_mremap_size(void **address, size_t old_size, size_t new_size) {
#if defined(_WIN32) || defined(_WIN64)
*address = MAP_FAILED;
(void)old_size;
(void)new_size;
return ERROR_NOT_SUPPORTED;
#else
*address = mremap(*address, old_size, new_size, 0, address);
return (*address != MAP_FAILED) ? MDB_SUCCESS : errno;
#endif
}
int mdbx_mmap(void **address, size_t length, int rw, mdbx_filehandle_t fd) {
#if defined(_WIN32) || defined(_WIN64)
HANDLE h = CreateFileMapping(fd, NULL, rw ? PAGE_READWRITE : PAGE_READONLY,
HIGH_DWORD(length), (DWORD)length, NULL);
if (!h)
return GetLastError();
*address = MapViewOfFileEx(h, rw ? FILE_MAP_WRITE : FILE_MAP_READ, 0, 0,
length, *address);
int rc = (*address != MAP_FAILED) ? MDB_SUCCESS : GetLastError();
CloseHandle(h);
return rc;
#else
*address = mmap(address, length, rw ? PROT_READ | PROT_WRITE : PROT_READ,
MAP_SHARED, fd, 0);
return (*address != MAP_FAILED) ? MDB_SUCCESS : errno;
#endif
}
int mdbx_munmap(void *address, size_t length) {
#if defined(_WIN32) || defined(_WIN64)
(void)length;
return UnmapViewOfFile(address) ? MDB_SUCCESS : GetLastError();
#else
return (munmap(address, length) == 0) ? MDB_SUCCESS : errno;
#endif
}
int mdbx_mlock(const void *address, size_t length) {
#if defined(_WIN32) || defined(_WIN64)
return VirtualLock((void *)address, length) ? MDB_SUCCESS : GetLastError();
#else
return (mlock(address, length) == 0) ? MDB_SUCCESS : errno;
#endif
}

423
src/osal.h Normal file
View File

@ -0,0 +1,423 @@
/* https://en.wikipedia.org/wiki/Operating_system_abstraction_layer */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#pragma once
#ifdef _MSC_VER
#pragma warning(push, 1)
#pragma warning(disable : 4530) /* C++ exception handler used, but \
unwind semantics are not enabled. Specify \
/EHsc */
#pragma warning(disable : 4577) /* 'noexcept' used with no exception \
handling mode specified; termination on \
exception is not guaranteed. Specify /EHsc \
*/
#endif /* _MSC_VER (warnings) */
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifndef _POSIX_C_SOURCE
#ifdef _POSIX_SOURCE
#define _POSIX_C_SOURCE 1
#else
#define _POSIX_C_SOURCE 0
#endif
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 0
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <winnt.h>
#define HAVE_SYS_STAT_H
#define HAVE_SYS_TYPES_H
typedef HANDLE mdbx_mutex_t;
typedef HANDLE mdbx_cond_t;
typedef HANDLE mdbx_thread_t;
typedef unsigned mdbx_thread_key_t;
typedef SSIZE_T ssize_t;
#define MAP_FAILED NULL
#define HIGH_DWORD(v) ((DWORD)((sizeof(v) > 4) ? ((uint64_t)(v) >> 32) : 0))
#define THREAD_CALL WINAPI
#define THREAD_RESULT DWORD
#else
#include <pthread.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
typedef pthread_mutex_t mdbx_mutex_t;
typedef pthread_cond_t mdbx_cond_t;
typedef pthread_t mdbx_thread_t;
typedef pthread_key_t mdbx_thread_key_t;
#define INVALID_HANDLE_VALUE (-1)
#define THREAD_CALL
#define THREAD_RESULT void *
#endif /* Platform */
#ifndef SSIZE_MAX
#define SSIZE_MAX INTPTR_MAX
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
/*----------------------------------------------------------------------------*/
#ifdef _MSC_VER
#if _MSC_FULL_VER < 190024215
#if _MSC_FULL_VER < 180040629 && defined(_M_IX86)
#error Please use Visual Studio 2015 (MSC 19.0) or newer for 32-bit target.
#else
#pragma message( \
"It is recommended to use Visual Studio 2015 (MSC 19.0) or newer.")
#endif
#endif
#include <intrin.h>
#elif __GNUC_PREREQ(4, 4) || defined(__clang__)
#if defined(__i386__) || defined(__x86_64__)
#include <cpuid.h>
#include <x86intrin.h>
#endif
#elif defined(__INTEL_COMPILER)
#include <intrin.h>
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
#include <mbarrier.h>
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) && \
(defined(HP_IA64) || defined(__ia64))
#include <machine/sys/inline.h>
#elif defined(__IBMC__) && defined(__powerpc)
#include <atomic.h>
#elif defined(_AIX)
#include <builtins.h>
#include <sys/atomic_op.h>
#elif (defined(__osf__) && defined(__DECC)) || defined(__alpha)
#include <c_asm.h>
#include <machine/builtins.h>
#elif defined(__MWERKS__)
/* CodeWarrior - troubles ? */
#pragma gcc_extensions
#elif defined(__SNC__)
/* Sony PS3 - troubles ? */
#else
#error Unknown C compiler, please use GNU C 5.x or newer
#endif /* Compiler */
/*----------------------------------------------------------------------------*/
/* Byteorder */
#if !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__) || \
!defined(__ORDER_BIG_ENDIAN__)
#if defined(HAVE_ENDIAN_H)
#include <endian.h>
#elif defined(HAVE_SYS_PARAM_H)
#include <sys/param.h> /* for endianness */
#elif defined(HAVE_NETINET_IN_H) && defined(HAVE_RESOLV_H)
#include <netinet/in.h>
#include <resolv.h> /* defines BYTE_ORDER on HPUX and Solaris */
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
#define __ORDER_LITTLE_ENDIAN__ __LITTLE_ENDIAN
#define __ORDER_BIG_ENDIAN__ __BIG_ENDIAN
#define __BYTE_ORDER__ __BYTE_ORDER
#else
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_BIG_ENDIAN__ 4321
#if defined(__LITTLE_ENDIAN__) || defined(_LITTLE_ENDIAN) || \
defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
defined(__MIPSEL__) || defined(_MIPSEL) || defined(__MIPSEL) || \
defined(__i386) || defined(__x86_64__) || defined(_M_IX86) || \
defined(_M_X64) || defined(i386) || defined(_X86_) || defined(__i386__) || \
defined(_X86_64_) || defined(_M_ARM) || defined(__e2k__)
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#elif defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN) || defined(__ARMEB__) || \
defined(__THUMBEB__) || defined(__AARCH64EB__) || defined(__MIPSEB__) || \
defined(_MIPSEB) || defined(__MIPSEB)
#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
#else
#error __BYTE_ORDER__ should be defined.
#endif
#endif
#endif
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ && \
__BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
#error Unsupported byte order.
#endif
/*----------------------------------------------------------------------------*/
/* Cache coherence */
#if defined(__i386__) || defined(__x86_64__) || defined(_M_AMD64) || \
defined(_M_IX86) || defined(__i386) || defined(__amd64) || \
defined(i386) || defined(__x86_64) || defined(_AMD64_) || defined(_M_X64)
#define MDBX_CACHE_IS_COHERENT 1
#elif defined(__hppa) || defined(__hppa__)
#define MDBX_CACHE_IS_COHERENT 1
#endif
#ifndef MDBX_CACHE_IS_COHERENT
#define MDBX_CACHE_IS_COHERENT 0
#endif
#ifndef MDBX_CACHELINE_SIZE
#if defined(SYSTEM_CACHE_ALIGNMENT_SIZE)
#define MDBX_CACHELINE_SIZE SYSTEM_CACHE_ALIGNMENT_SIZE
#elif defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
#define MDBX_CACHELINE_SIZE 128
#else
#define MDBX_CACHELINE_SIZE 64
#endif
#endif /* MDBX_CACHELINE_SIZE */
#ifndef __cache_aligned
#define __cache_aligned __aligned(MDBX_CACHELINE_SIZE)
#endif
/*----------------------------------------------------------------------------*/
/* Memory/Compiler barriers */
static __inline void mdbx_compiler_barrier(void) {
#if defined(__clang__) || defined(__GNUC__)
__asm__ __volatile__("" ::: "memory");
#elif defined(_MSC_VER)
_ReadWriteBarrier();
#elif defined(__INTEL_COMPILER) /* LY: Intel Compiler may mimic GCC and MSC */
__memory_barrier();
if (type > MDBX_BARRIER_COMPILER)
#if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
__mf();
#elif defined(__i386__) || defined(__x86_64__)
_mm_mfence();
#else
#error "Unknown target for Intel Compiler, please report to us."
#endif
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
__compiler_barrier();
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) && \
(defined(HP_IA64) || defined(__ia64))
_Asm_sched_fence(/* LY: no-arg meaning 'all expect ALU', e.g. 0x3D3D */);
#elif defined(_AIX) || defined(__ppc__) || defined(__powerpc__) || \
defined(__ppc64__) || defined(__powerpc64__)
__fence();
#else
#error "Could not guess the kind of compiler, please report to us."
#endif
}
static __inline void mdbx_memory_barrier(void) {
#if __has_extension(c_atomic) || __has_extension(cxx_atomic)
__c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
#elif defined(__ATOMIC_SEQ_CST)
__atomic_thread_fence(__ATOMIC_SEQ_CST);
#elif defined(__clang__) || defined(__GNUC__)
__sync_synchronize();
#elif defined(_MSC_VER)
MemoryBarrier();
#elif defined(__INTEL_COMPILER) /* LY: Intel Compiler may mimic GCC and MSC */
#if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
__mf();
#elif defined(__i386__) || defined(__x86_64__)
_mm_mfence();
#else
#error "Unknown target for Intel Compiler, please report to us."
#endif
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
__machine_rw_barrier();
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) && \
(defined(HP_IA64) || defined(__ia64))
_Asm_mf();
#elif defined(_AIX) || defined(__ppc__) || defined(__powerpc__) || \
defined(__ppc64__) || defined(__powerpc64__)
__lwsync();
#else
#error "Could not guess the kind of compiler, please report to us."
#endif
}
#if MDBX_CACHE_IS_COHERENT
#define mdbx_coherent_barrier() mdbx_compiler_barrier()
#else
#define mdbx_coherent_barrier() mdbx_memory_barrier()
#endif
#if defined(__mips) && defined(__linux)
/* Only MIPS has explicit cache control */
#include <asm/cachectl.h>
#endif
static __inline void mdbx_invalidate_cache(void *addr, size_t nbytes) {
mdbx_coherent_barrier();
#if defined(__mips) && defined(__linux)
/* MIPS has cache coherency issues.
* Note: for any nbytes >= on-chip cache size, entire is flushed. */
cacheflush(addr, nbytes, DCACHE);
#elif defined(_M_MRX000) || defined(_MIPS_)
#error "Sorry, cacheflush() for MIPS not implemented"
#else
/* LY: assume no relevant mmap/dcache issues. */
(void)addr;
(void)nbytes;
#endif
}
/*----------------------------------------------------------------------------*/
/* max bytes to write in one call */
#define MAX_WRITE (0x80000000U >> (sizeof(ssize_t) == 4))
/* Get the size of a memory page for the system.
* This is the basic size that the platform's memory manager uses, and is
* fundamental to the use of memory-mapped files. */
static __inline size_t mdbx_syspagesize(void) {
#if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwPageSize;
#else
return sysconf(_SC_PAGE_SIZE);
#endif
}
static __inline char *mdbx_strdup(const char *str) {
#ifdef _MSC_VER
return _strdup(str);
#else
return strdup(str);
#endif
}
int mdbx_memalign_alloc(size_t alignment, size_t bytes, void **result);
void mdbx_memalign_free(void *ptr);
int mdbx_mutex_init(mdbx_mutex_t *mutex);
int mdbx_mutex_destroy(mdbx_mutex_t *mutex);
int mdbx_mutex_lock(mdbx_mutex_t *mutex);
int mdbx_mutex_unlock(mdbx_mutex_t *mutex);
int mdbx_cond_init(mdbx_cond_t *cond);
int mdbx_cond_destroy(mdbx_cond_t *cond);
int mdbx_cond_signal(mdbx_cond_t *cond);
int mdbx_cond_wait(mdbx_cond_t *cond, mdbx_mutex_t *mutex);
int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt,
off_t offset, size_t expected_written);
int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t count, off_t offset);
int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t count,
off_t offset);
int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t count);
int mdbx_msync(void *addr, size_t length, int async);
int mdbx_thread_create(mdbx_thread_t *thread,
THREAD_RESULT(THREAD_CALL *start_routine)(void *),
void *arg);
int mdbx_thread_join(mdbx_thread_t thread);
mdbx_tid_t mdbx_thread_self(void);
int mdbx_thread_key_create(mdbx_thread_key_t *key);
void mdbx_thread_key_delete(mdbx_thread_key_t key);
void *mdbx_thread_rthc_get(mdbx_thread_key_t key);
void mdbx_thread_rthc_set(mdbx_thread_key_t key, const void *value);
int mdbx_filesync(mdbx_filehandle_t fd, bool syncmeta);
int mdbx_ftruncate(mdbx_filehandle_t fd, off_t length);
int mdbx_filesize(mdbx_filehandle_t fd, off_t *length);
int mdbx_openfile(const char *pathname, int flags, mode_t mode,
mdbx_filehandle_t *fd);
int mdbx_closefile(mdbx_filehandle_t fd);
int mdbx_mremap_size(void **address, size_t old_size, size_t new_size);
int mdbx_mmap(void **address, size_t length, int rw, mdbx_filehandle_t fd);
int mdbx_munmap(void *address, size_t length);
int mdbx_mlock(const void *address, size_t length);
static __inline mdbx_pid_t mdbx_getpid(void) {
#if defined(_WIN32) || defined(_WIN64)
return GetCurrentProcessId();
#else
return getpid();
#endif
}
/*----------------------------------------------------------------------------*/
#ifndef mdbx_assert_fail
void mdbx_assert_fail(MDB_env *env, const char *msg, const char *func,
int line);
#endif /* mdbx_assert_fail */
#if __GLIBC_PREREQ(2, 1)
#define mdbx_asprintf asprintf
#else
int mdbx_asprintf(char **strp, const char *fmt, ...);
#endif
/*----------------------------------------------------------------------------*/
#if defined(_WIN32) || defined(_WIN64)
#undef MDBX_OSAL_LOCK
#define MDBX_OSAL_LOCK_SIGN MDBX_TETRAD('f', 'l', 'c', 'k')
#else
#define MDBX_OSAL_LOCK pthread_mutex_t
#define MDBX_OSAL_LOCK_SIGN MDBX_TETRAD('P', 'T', 'M', 'X')
#endif
int mdbx_lck_init(MDB_env *env);
int mdbx_lck_seize(MDB_env *env);
int mdbx_lck_downgrade(MDB_env *env);
void mdbx_lck_destroy(MDB_env *env);
int mdbx_rdt_lock(MDB_env *env);
void mdbx_rdt_unlock(MDB_env *env);
int mdbx_txn_lock(MDB_env *env);
void mdbx_txn_unlock(MDB_env *env);
int mdbx_rpid_set(MDB_env *env);
int mdbx_rpid_clear(MDB_env *env);
int mdbx_rpid_check(MDB_env *env, mdbx_pid_t pid);

View File

@ -1,236 +0,0 @@
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2015,2016 Peter-Service R&D LLC.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#pragma once
/* *INDENT-OFF* */
/* clang-format off */
#ifndef __CLANG_PREREQ
# ifdef __clang__
# define __CLANG_PREREQ(maj,min) \
((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
# else
# define __CLANG_PREREQ(maj,min) (0)
# endif
#endif /* __CLANG_PREREQ */
#ifndef __has_attribute
# define __has_attribute(x) (0)
#endif
#if !defined(__thread) && (defined(_MSC_VER) || defined(__DMC__))
# define __thread __declspec(thread)
#endif
#ifndef __forceinline
# if defined(__GNUC__) || defined(__clang__)
# define __forceinline __inline __attribute__((always_inline))
# elif ! defined(_MSC_VER)
# define __forceinline
# endif
#endif /* __forceinline */
#ifndef __noinline
# if defined(__GNUC__) || defined(__clang__)
# define __noinline __attribute__((noinline))
# elif defined(_MSC_VER)
# define __noinline __declspec(noinline)
# endif
#endif /* __noinline */
#ifndef __must_check_result
# if defined(__GNUC__) || defined(__clang__)
# define __must_check_result __attribute__((warn_unused_result))
# else
# define __must_check_result
# endif
#endif /* __must_check_result */
#ifndef __hot
# if defined(__OPTIMIZE__) && (defined(__GNUC__) && !defined(__clang__))
# define __hot __attribute__((hot, optimize("O3")))
# elif defined(__GNUC__)
/* cland case, just put frequently used functions in separate section */
# define __hot __attribute__((section("text.hot")))
# else
# define __hot
# endif
#endif /* __hot */
#ifndef __cold
# if defined(__OPTIMIZE__) && (defined(__GNUC__) && !defined(__clang__))
# define __cold __attribute__((cold, optimize("Os")))
# elif defined(__GNUC__)
/* cland case, just put infrequently used functions in separate section */
# define __cold __attribute__((section("text.unlikely")))
# else
# define __cold
# endif
#endif /* __cold */
#ifndef __flatten
# if defined(__OPTIMIZE__) && (defined(__GNUC__) || defined(__clang__))
# define __flatten __attribute__((flatten))
# else
# define __flatten
# endif
#endif /* __flatten */
#ifndef __aligned
# if defined(__GNUC__) || defined(__clang__)
# define __aligned(N) __attribute__((aligned(N)))
# elif defined(__MSC_VER)
# define __aligned(N) __declspec(align(N))
# else
# define __aligned(N)
# endif
#endif /* __align */
#ifndef __noreturn
# if defined(__GNUC__) || defined(__clang__)
# define __noreturn __attribute__((noreturn))
# elif defined(__MSC_VER)
# define __noreturn __declspec(noreturn)
# else
# define __noreturn
# endif
#endif
#ifndef __nothrow
# if defined(__GNUC__) || defined(__clang__)
# define __nothrow __attribute__((nothrow))
# elif defined(__MSC_VER)
# define __nothrow __declspec(nothrow)
# else
# define __nothrow
# endif
#endif
#ifndef CACHELINE_SIZE
# if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
# define CACHELINE_SIZE 128
# else
# define CACHELINE_SIZE 64
# endif
#endif
#ifndef __cache_aligned
# define __cache_aligned __aligned(CACHELINE_SIZE)
#endif
#ifndef likely
# if defined(__GNUC__) || defined(__clang__)
# ifdef __cplusplus
/* LY: workaround for "pretty" boost */
static __inline __attribute__((always_inline))
bool likely(bool cond) { return __builtin_expect(cond, 1); }
# else
# define likely(cond) __builtin_expect(!!(cond), 1)
# endif
# else
# define likely(x) (x)
# endif
#endif /* likely */
#ifndef unlikely
# if defined(__GNUC__) || defined(__clang__)
# ifdef __cplusplus
/* LY: workaround for "pretty" boost */
static __inline __attribute__((always_inline))
bool unlikely(bool cond) { return __builtin_expect(cond, 0); }
# else
# define unlikely(cond) __builtin_expect(!!(cond), 0)
# endif
# else
# define unlikely(x) (x)
# endif
#endif /* unlikely */
#ifndef __extern_C
# ifdef __cplusplus
# define __extern_C extern "C"
# else
# define __extern_C
# endif
#endif
#ifndef __noop
# define __noop() do {} while (0)
#endif
/* -------------------------------------------------------------------------- */
#include <assert.h>
/* Prototype should match libc runtime. ISO POSIX (2003) & LSB 3.1 */
__extern_C void __assert_fail(
const char* assertion,
const char* file,
unsigned line,
const char* function) __nothrow __noreturn;
/* -------------------------------------------------------------------------- */
#if defined(HAVE_VALGRIND) || defined(USE_VALGRIND)
/* Get debugging help from Valgrind */
# include <valgrind/memcheck.h>
# ifndef VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE
/* LY: available since Valgrind 3.10 */
# define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# endif
#else
# define VALGRIND_CREATE_MEMPOOL(h,r,z)
# define VALGRIND_DESTROY_MEMPOOL(h)
# define VALGRIND_MEMPOOL_TRIM(h,a,s)
# define VALGRIND_MEMPOOL_ALLOC(h,a,s)
# define VALGRIND_MEMPOOL_FREE(h,a)
# define VALGRIND_MEMPOOL_CHANGE(h,a,b,s)
# define VALGRIND_MAKE_MEM_NOACCESS(a,s)
# define VALGRIND_MAKE_MEM_DEFINED(a,s)
# define VALGRIND_MAKE_MEM_UNDEFINED(a,s)
# define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(a,s)
# define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a,s) (0)
# define VALGRIND_CHECK_MEM_IS_DEFINED(a,s) (0)
#endif /* ! USE_VALGRIND */
#if defined(__has_feature)
# if __has_feature(thread_sanitizer)
# define __SANITIZE_THREAD__ 1
# endif
#endif
#ifdef __SANITIZE_THREAD__
# define ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread, noinline))
#else
# define ATTRIBUTE_NO_SANITIZE_THREAD
#endif
#if defined(__has_feature)
# if __has_feature(address_sanitizer)
# define __SANITIZE_ADDRESS__ 1
# endif
#endif
#ifdef __SANITIZE_ADDRESS__
# include <sanitizer/asan_interface.h>
# define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address, noinline))
#else
# define ASAN_POISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
# define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
# define ATTRIBUTE_NO_SANITIZE_ADDRESS
#endif /* __SANITIZE_ADDRESS__ */

View File

@ -1,24 +1,17 @@
/* mdbx_chk.c - memory-mapped database check tool */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2015,2016 Peter-Service R&D LLC.
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* This file is part of libmdbx.
*
* libmdbx is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* libmdbx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include <ctype.h>
@ -32,8 +25,8 @@
#include <time.h>
#include <unistd.h>
#include "mdbx.h"
#include "midl.h"
#include "../../mdbx.h"
#include "../midl.h"
typedef struct flagbit {
int bit;
@ -624,7 +617,7 @@ int main(int argc, char *argv[]) {
while ((i = getopt(argc, argv, "Vvqnwcds:")) != EOF) {
switch (i) {
case 'V':
printf("%s\n", MDB_VERSION_STRING);
printf("%s\n", MDBX_VERSION_STRING);
exit(EXIT_SUCCESS);
break;
case 'v':

View File

@ -1,9 +1,8 @@
/* mdbx_copy.c - memory-mapped database backup tool */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2012-2017 Howard Chu, Symas Corp.
* Copyright 2015,2016 Peter-Service R&D LLC.
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -15,10 +14,11 @@
* <http://www.OpenLDAP.org/license.html>.
*/
#include "mdbx.h"
#include "../../mdbx.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void sighandle(int sig) { (void)sig; }
@ -35,7 +35,7 @@ int main(int argc, char *argv[]) {
else if (argv[1][1] == 'c' && argv[1][2] == '\0')
cpflags |= MDB_CP_COMPACT;
else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
printf("%s\n", MDB_VERSION_STRING);
printf("%s\n", MDBX_VERSION_STRING);
exit(0);
} else
argc = 0;

View File

@ -1,9 +1,8 @@
/* mdbx_dump.c - memory-mapped database dump tool */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2011-2017 Howard Chu, Symas Corp.
* Copyright 2015,2016 Peter-Service R&D LLC.
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -15,7 +14,7 @@
* <http://www.OpenLDAP.org/license.html>.
*/
#include "mdbx.h"
#include "../../mdbx.h"
#include <ctype.h>
#include <errno.h>
#include <signal.h>
@ -178,7 +177,7 @@ int main(int argc, char *argv[]) {
while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) {
switch (i) {
case 'V':
printf("%s\n", MDB_VERSION_STRING);
printf("%s\n", MDBX_VERSION_STRING);
exit(0);
break;
case 'l':

View File

@ -1,9 +1,8 @@
/* mdbx_load.c - memory-mapped database load tool */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2011-2017 Howard Chu, Symas Corp.
* Copyright 2015,2016 Peter-Service R&D LLC.
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -15,7 +14,7 @@
* <http://www.OpenLDAP.org/license.html>.
*/
#include "mdbx.h"
#include "../../mdbx.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
@ -314,7 +313,7 @@ int main(int argc, char *argv[]) {
while ((i = getopt(argc, argv, "f:ns:NTV")) != EOF) {
switch (i) {
case 'V':
printf("%s\n", MDB_VERSION_STRING);
printf("%s\n", MDBX_VERSION_STRING);
exit(0);
break;
case 'f':
@ -365,8 +364,10 @@ int main(int argc, char *argv[]) {
if (info.me_mapsize)
mdbx_env_set_mapsize(env, info.me_mapsize);
#ifdef MDB_FIXEDMAP
if (info.me_mapaddr)
envflags |= MDB_FIXEDMAP;
#endif
rc = mdbx_env_open(env, envname, envflags, 0664);
if (rc) {

View File

@ -1,9 +1,8 @@
/* mdbx_stat.c - memory-mapped database status tool */
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2011-2017 Howard Chu, Symas Corp.
* Copyright 2015,2016 Peter-Service R&D LLC.
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
* and other libmdbx authors: please see AUTHORS file.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -15,7 +14,7 @@
* <http://www.OpenLDAP.org/license.html>.
*/
#include "mdbx.h"
#include "../../mdbx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -65,7 +64,7 @@ int main(int argc, char *argv[]) {
while ((i = getopt(argc, argv, "Vaefnrs:")) != EOF) {
switch (i) {
case 'V':
printf("%s\n", MDB_VERSION_STRING);
printf("%s\n", MDBX_VERSION_STRING);
exit(0);
break;
case 'a':

View File

@ -15,7 +15,7 @@
* <http://www.OpenLDAP.org/license.html>.
*/
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -79,7 +79,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -14,7 +14,7 @@
/* Based on mtest2.c - memory-mapped database tester/toy */
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -65,7 +65,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -17,7 +17,7 @@
/* Just like mtest.c, but using a subDB instead of the main DB */
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -16,7 +16,7 @@
*/
/* Tests for sorted duplicate DBs */
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -70,7 +70,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -16,7 +16,7 @@
*/
/* Tests for sorted duplicate DBs with fixed-size keys */
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -16,7 +16,7 @@
*/
/* Tests for sorted duplicate DBs using cursor_put */
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -70,7 +70,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -16,7 +16,7 @@
*/
/* Tests for DB splits and merges */
#include "mdbx.h"
#include "../mdbx.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -61,7 +61,7 @@ int main(int argc, char *argv[]) {
E(stat("/proc/self/exe", &exe_stat) ? errno : 0);
E(stat(DBPATH "/.", &db_stat) ? errno : 0);
env_oflags = MDB_FIXEDMAP | MDB_NOSYNC;
env_oflags = MDB_NOSYNC;
if (major(db_stat.st_dev) != major(exe_stat.st_dev)) {
/* LY: Assume running inside a CI-environment:
* 1) don't use FIXEDMAP to avoid EBUSY in case collision,

View File

@ -22,7 +22,7 @@
#include <time.h>
#include <unistd.h>
#include "mdbx.h"
#include "../mdbx.h"
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0))

View File

@ -22,7 +22,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include "mdbx.h"
#include "../mdbx.h"
#include <assert.h>
#include <limits.h>
#include <stddef.h>

View File

@ -22,7 +22,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include "mdbx.h"
#include "../mdbx.h"
#include <assert.h>
#include <limits.h>
#include <stddef.h>