From b47badb3ee84e7a638f6414cf4aa681e84297849 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 14 Oct 2018 11:14:14 +0300 Subject: [PATCH] mdbx-windows: rework mdbx_memalign_alloc()/mdbx_memalign_free() to avoid dependency from CRT. 11 of 17 for https://github.com/leo-yuriev/libmdbx/issues/43 Change-Id: Id1a76f88588251cab9a93aa9753021b30159b09a --- src/osal.c | 9 +++++---- src/osal.h | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/osal.c b/src/osal.c index 3a652466..c6ae5d8f 100644 --- a/src/osal.c +++ b/src/osal.c @@ -262,8 +262,9 @@ int mdbx_asprintf(char **strp, const char *fmt, ...) { #ifndef mdbx_memalign_alloc int mdbx_memalign_alloc(size_t alignment, size_t bytes, void **result) { -#if _MSC_VER - *result = _aligned_malloc(bytes, alignment); +#if defined(_WIN32) || defined(_WIN64) + (void)alignment; + *result = VirtualAlloc(NULL, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); return *result ? MDBX_SUCCESS : MDBX_ENOMEM /* ERROR_OUTOFMEMORY */; #elif __GLIBC_PREREQ(2, 16) || __STDC_VERSION__ >= 201112L *result = memalign(alignment, bytes); @@ -279,8 +280,8 @@ int mdbx_memalign_alloc(size_t alignment, size_t bytes, void **result) { #ifndef mdbx_memalign_free void mdbx_memalign_free(void *ptr) { -#if _MSC_VER - _aligned_free(ptr); +#if defined(_WIN32) || defined(_WIN64) + VirtualFree(ptr, 0, MEM_RELEASE); #else mdbx_free(ptr); #endif diff --git a/src/osal.h b/src/osal.h index e5c48f7c..8a2abc3b 100644 --- a/src/osal.h +++ b/src/osal.h @@ -439,8 +439,12 @@ static __inline int mdbx_get_errno(void) { return rc; } +#ifndef mdbx_memalign_alloc int mdbx_memalign_alloc(size_t alignment, size_t bytes, void **result); +#endif +#ifndef mdbx_memalign_free void mdbx_memalign_free(void *ptr); +#endif int mdbx_condmutex_init(mdbx_condmutex_t *condmutex); int mdbx_condmutex_lock(mdbx_condmutex_t *condmutex);