This commit is contained in:
Samuel Marks 2025-12-16 23:14:06 -08:00 committed by GitHub
commit d95b2a7557
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 192 additions and 85 deletions

View File

@ -19,7 +19,9 @@ option(BUILD_EXAMPLE_MODULES "Build example modules" OFF)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
project("valkey") project("valkey")
add_compile_options(-Wundef) if (NOT WIN32)
add_compile_options(-Wundef)
endif (NOT WIN32)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)

View File

@ -99,7 +99,9 @@ macro (valkey_build_and_install_bin target sources ld_flags libs link_name)
endif () endif ()
# Enable all warnings + fail on warning # Enable all warnings + fail on warning
target_compile_options(${target} PRIVATE -Werror -Wall) if (NOT WIN32)
target_compile_options(${target} PRIVATE -Werror -Wall)
endif (NOT WIN32)
# Install cli tool and create a redis symbolic link # Install cli tool and create a redis symbolic link
valkey_install_bin(${target}) valkey_install_bin(${target})
@ -245,14 +247,15 @@ elseif (UNIX)
add_valkey_server_linker_option("-lm") add_valkey_server_linker_option("-lm")
endif () endif ()
if (VALKEY_DEBUG_BUILD) if (VALKEY_DEBUG_BUILD AND NOT WIN32)
# Debug build, use enable "-fno-omit-frame-pointer" # Debug build, use enable "-fno-omit-frame-pointer"
add_valkey_server_compiler_options("-fno-omit-frame-pointer") add_valkey_server_compiler_options("-fno-omit-frame-pointer")
endif () endif ()
# Check for Atomic # Check for Atomic
check_include_files(stdatomic.h HAVE_C11_ATOMIC) check_include_files(stdatomic.h HAVE_C11_ATOMIC)
if (HAVE_C11_ATOMIC) if (WIN32)
elseif (HAVE_C11_ATOMIC)
add_valkey_server_compiler_options("-std=gnu11") add_valkey_server_compiler_options("-std=gnu11")
else () else ()
add_valkey_server_compiler_options("-std=c99") add_valkey_server_compiler_options("-std=c99")
@ -291,7 +294,9 @@ if (USE_JEMALLOC)
endif () endif ()
# Common compiler flags # Common compiler flags
add_valkey_server_compiler_options("-pedantic") if (NOT WIN32)
add_valkey_server_compiler_options("-pedantic")
endif ()
# ---------------------------------------------------- # ----------------------------------------------------
# Build options (allocator, tls, rdma et al) - end # Build options (allocator, tls, rdma et al) - end

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7.0) cmake_minimum_required(VERSION 3.10)
macro(getDefinedVersion name) macro(getDefinedVersion name)
set(VERSION_REGEX "^#define LIBVALKEY_${name} (.+)$") set(VERSION_REGEX "^#define LIBVALKEY_${name} (.+)$")

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7.0) cmake_minimum_required(VERSION 3.10)
project(examples LANGUAGES C) project(examples LANGUAGES C)
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)

View File

@ -47,6 +47,12 @@ typedef SSIZE_T ssize_t;
#endif #endif
#endif #endif
#ifdef _MSC_VER
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#else
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#endif /* _MSC_VER */
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
@ -55,34 +61,34 @@ typedef char *sds;
/* Note: sdshdr5 is never used, we just access the flags byte directly. /* Note: sdshdr5 is never used, we just access the flags byte directly.
* However is here to document the layout of type 5 SDS strings. */ * However is here to document the layout of type 5 SDS strings. */
struct __attribute__((__packed__)) sdshdr5 { PACK(struct sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr8 { PACK(struct sdshdr8 {
uint8_t len; /* used */ uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */ uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr16 { PACK(struct sdshdr16 {
uint16_t len; /* used */ uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */ uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr32 { PACK(struct sdshdr32 {
uint32_t len; /* used */ uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */ uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr64 { PACK(struct sdshdr64 {
uint64_t len; /* used */ uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */ uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
#define SDS_TYPE_5 0 #define SDS_TYPE_5 0
#define SDS_TYPE_8 1 #define SDS_TYPE_8 1

View File

@ -96,6 +96,10 @@ __inline int c99_snprintf(char *str, size_t size, const char *format, ...) {
#define random rand #define random rand
#endif #endif
#ifndef SSIZE_MAX
#define SSIZE_MAX MAXSSIZE_T
#endif /* !SSIZE_MAX */
#endif /* _WIN32 */ #endif /* _WIN32 */
#endif /* VALKEY_WIN32_H */ #endif /* VALKEY_WIN32_H */

View File

@ -105,8 +105,11 @@
#define _DEFAULT_SOURCE /* For fchmod() */ #define _DEFAULT_SOURCE /* For fchmod() */
#define _BSD_SOURCE /* For fchmod() */ #define _BSD_SOURCE /* For fchmod() */
#ifdef _MSC_VER
#else
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>

View File

@ -29,6 +29,8 @@
#define LUA_BITOP_VERSION "1.0.2" #define LUA_BITOP_VERSION "1.0.2"
#define LUA_LIB #define LUA_LIB
#include <stdint.h>
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"

View File

@ -15,7 +15,34 @@ valkey_build_and_install_bin(valkey-server "${VALKEY_SERVER_SRCS}" "${VALKEY_SER
"redis-server") "redis-server")
add_dependencies(valkey-server generate_commands_def) add_dependencies(valkey-server generate_commands_def)
add_dependencies(valkey-server generate_fmtargs_h) add_dependencies(valkey-server generate_fmtargs_h)
add_dependencies(valkey-server release_header) if (WIN32)
execute_process(COMMAND git log -1 --format=%H
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_SHA1
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND git status --porcelain
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_PORCELAIN
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if (GIT_PORCELAIN)
set(GIT_DIRTY 0)
else ()
set(GIT_DIRTY 1)
endif ()
if (NOT DEFINED CMAKE_SYSTEM_NAME)
set(CMAKE_SYSTEM_NAME "${CMAKE_HOST_SYSTEM_NAME}")
endif (NOT DEFINED CMAKE_SYSTEM_NAME)
string(TIMESTAMP now "%s" UTC)
set(BUILD_ID "${CMAKE_SYSTEM_NAME}_${now}")
set(BUILD_ID_RAW "${BUILD_ID}") # is this right?
configure_file(
"${CMAKE_SOURCE_DIR}/src/release.h.in"
"${CMAKE_SOURCE_DIR}/src/release.h" @ONLY
)
else ()
add_dependencies(valkey-server release_header)
endif ()
if (VALKEY_RELEASE_BUILD) if (VALKEY_RELEASE_BUILD)
# Enable LTO for Release build # Enable LTO for Release build

View File

@ -31,6 +31,8 @@
#include "fmacros.h" #include "fmacros.h"
#include <sys/types.h> #include <sys/types.h>
#ifdef _MSC_VER
#else
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/un.h> #include <sys/un.h>
@ -39,13 +41,14 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <netdb.h>
#include <grp.h>
#endif
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <netdb.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <grp.h>
#include "anet.h" #include "anet.h"
#include "config.h" #include "config.h"

View File

@ -51,6 +51,11 @@
#undef ip_len #undef ip_len
#endif #endif
#ifdef _MSC_VER
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#endif
int anetTcpNonBlockConnect(char *err, const char *addr, int port); int anetTcpNonBlockConnect(char *err, const char *addr, int port);
int anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port, const char *source_addr, int mptcp); int anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port, const char *source_addr, int mptcp);
int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len, int flags); int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len, int flags);

View File

@ -38,9 +38,15 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include "sds.h" #include "sds.h"
#include <unistd.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#ifdef _MSC_VER
#else
#include <unistd.h>
#endif
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/err.h> #include <openssl/err.h>

View File

@ -30,7 +30,10 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
#ifdef _MSC_VER
#else
#include <sys/param.h> #include <sys/param.h>
#endif
#ifdef __APPLE__ #ifdef __APPLE__
#include <fcntl.h> // for fcntl(fd, F_FULLFSYNC) #include <fcntl.h> // for fcntl(fd, F_FULLFSYNC)
@ -216,7 +219,18 @@ void setproctitle(const char *fmt, ...);
#include <sys/types.h> /* This will likely define BYTE_ORDER */ #include <sys/types.h> /* This will likely define BYTE_ORDER */
#ifndef BYTE_ORDER #ifndef BYTE_ORDER
#if (BSD >= 199103) #if defined(MSC_VER)
/*#include <winsock2.h>
#include <sys/param.h>*/
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\x1" == 0x1)
#ifdef IS_BIG_ENDIAN
#define BIG_ENDIAN
#define BYTE_ORDER BIG_ENDIAN
#else
#define LITTLE_ENDIAN
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#elif (BSD >= 199103)
#include <machine/endian.h> #include <machine/endian.h>
#else #else
#if defined(linux) || defined(__linux__) #if defined(linux) || defined(__linux__)
@ -238,7 +252,6 @@ void setproctitle(const char *fmt, ...);
defined(__hp9000s300) || defined(__hp9000s700) || defined(BIT_ZERO_ON_LEFT) || defined(m68k) || \ defined(__hp9000s300) || defined(__hp9000s700) || defined(BIT_ZERO_ON_LEFT) || defined(m68k) || \
defined(__sparc) || (defined(__APPLE__) && defined(__POWERPC__)) defined(__sparc) || (defined(__APPLE__) && defined(__POWERPC__))
#define BYTE_ORDER BIG_ENDIAN #define BYTE_ORDER BIG_ENDIAN
#endif
#endif /* linux */ #endif /* linux */
#endif /* BSD */ #endif /* BSD */
#endif /* BYTE_ORDER */ #endif /* BYTE_ORDER */

View File

@ -1,6 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#ifndef _MSC_VER
#include <strings.h> #include <strings.h>
#endif
#if defined(__i386__) || defined(__X86_64__) #if defined(__i386__) || defined(__X86_64__)
#include <immintrin.h> #include <immintrin.h>
#endif #endif

View File

@ -13,7 +13,10 @@
#include "fmacros.h" #include "fmacros.h"
#include <stdint.h> #include <stdint.h>
#ifdef _MSC_VER
#else
#include <unistd.h> #include <unistd.h>
#endif
/* A counter in micro-seconds. The 'monotime' type is provided for variables /* A counter in micro-seconds. The 'monotime' type is provided for variables
* holding a monotonic time. This will help distinguish & document that the * holding a monotonic time. This will help distinguish & document that the

41
src/release.h.in Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2025, Valkey authors
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef VALKEY_RELEASE_H
#define VALKEY_RELEASE_H
#include "version.h"
#define REDIS_GIT_SHA1 "@GIT_SHA1@"
#define REDIS_GIT_DIRTY @GIT_DIRTY@
#define REDIS_BUILD_ID "@BUILD_ID@"
#define REDIS_BUILD_ID_RAW "@BUILD_ID_RAW@"
#endif /* !VALKEY_RELEASE_H */

View File

@ -32,6 +32,12 @@
#ifndef __SDS_H #ifndef __SDS_H
#define __SDS_H #define __SDS_H
#ifdef _MSC_VER
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#else
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#endif /* _MSC_VER */
#define SDS_MAX_PREALLOC (1024 * 1024) #define SDS_MAX_PREALLOC (1024 * 1024)
extern const char *SDS_NOINIT; extern const char *SDS_NOINIT;
@ -50,34 +56,34 @@ typedef const char *const_sds;
/* Note: sdshdr5 is never used, we just access the flags byte directly. /* Note: sdshdr5 is never used, we just access the flags byte directly.
* However is here to document the layout of type 5 SDS strings. */ * However is here to document the layout of type 5 SDS strings. */
struct __attribute__((__packed__)) sdshdr5 { PACK(struct sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr8 { PACK(struct sdshdr8 {
uint8_t len; /* used */ uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */ uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr16 { PACK(struct sdshdr16 {
uint16_t len; /* used */ uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */ uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr32 { PACK(struct sdshdr32 {
uint32_t len; /* used */ uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */ uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
struct __attribute__((__packed__)) sdshdr64 { PACK(struct sdshdr64 {
uint64_t len; /* used */ uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */ uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; });
#define SDS_TYPE_5 0 #define SDS_TYPE_5 0
#define SDS_TYPE_8 1 #define SDS_TYPE_8 1
@ -234,11 +240,11 @@ sds sdscatprintf(sds s, const char *fmt, ...);
sds sdscatfmt(sds s, char const *fmt, ...); sds sdscatfmt(sds s, char const *fmt, ...);
sds sdstrim(sds s, const char *cset); sds sdstrim(sds s, const char *cset);
void sdssubstr(sds s, size_t start, size_t len); void sdssubstr(sds s, size_t start, size_t len);
void sdsrange(sds s, ssize_t start, ssize_t end); void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end);
void sdsupdatelen(sds s); void sdsupdatelen(sds s);
void sdsclear(sds s); void sdsclear(sds s);
int sdscmp(const_sds s1, const_sds s2); int sdscmp(const_sds s1, const_sds s2);
sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count); sds *sdssplitlen(const char *s, ptrdiff_t len, const char *sep, int seplen, int *count);
void sdsfreesplitres(sds *tokens, int count); void sdsfreesplitres(sds *tokens, int count);
void sdstolower(sds s); void sdstolower(sds s);
void sdstoupper(sds s); void sdstoupper(sds s);
@ -263,7 +269,7 @@ int sdsHdrSize(char type);
char sdsReqType(size_t string_size); char sdsReqType(size_t string_size);
sds sdsMakeRoomFor(sds s, size_t addlen); sds sdsMakeRoomFor(sds s, size_t addlen);
sds sdsMakeRoomForNonGreedy(sds s, size_t addlen); sds sdsMakeRoomForNonGreedy(sds s, size_t addlen);
void sdsIncrLen(sds s, ssize_t incr); void sdsIncrLen(sds s, ptrdiff_t incr);
sds sdsRemoveFreeSpace(sds s, int would_regrow); sds sdsRemoveFreeSpace(sds s, int would_regrow);
sds sdsResize(sds s, size_t size, int would_regrow); sds sdsResize(sds s, size_t size, int would_regrow);
size_t sdsAllocSize(const_sds s); size_t sdsAllocSize(const_sds s);

View File

@ -32,7 +32,9 @@
#include "fmacros.h" #include "fmacros.h"
#include <sys/types.h> #include <sys/types.h>
#ifndef _MSC_VER
#include <unistd.h> #include <unistd.h>
#endif
/** This is an API to invoke callback on a list of threads using a user defined signal handler. /** This is an API to invoke callback on a list of threads using a user defined signal handler.
* NOTE: This is API is only supported only in linux systems. * NOTE: This is API is only supported only in linux systems.

View File

@ -1,33 +1,3 @@
/* zmalloc - total amount of allocated memory aware version of malloc()
*
* Copyright (c) 2009-2010, Redis Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ZMALLOC_H #ifndef __ZMALLOC_H
#define __ZMALLOC_H #define __ZMALLOC_H
@ -65,12 +35,12 @@
#endif #endif
/* On native libc implementations, we should still do our best to provide a /* On native libc implementations, we should still do our best to provide a
* HAVE_MALLOC_SIZE capability. This can be set explicitly as well: * HAVE_MALLOC_SIZE capability.
* *
* NO_MALLOC_USABLE_SIZE disables it on all platforms, even if they are * NO_MALLOC_USABLE_SIZE disables it on all platforms, even if they are
* known to support it. * known to support it.
* USE_MALLOC_USABLE_SIZE forces use of malloc_usable_size() regardless * USE_MALLOC_USABLE_SIZE forces use of malloc_usable_size() regardless
* of platform. * of platform.
*/ */
#ifndef ZMALLOC_LIB #ifndef ZMALLOC_LIB
#define ZMALLOC_LIB "libc" #define ZMALLOC_LIB "libc"
@ -103,15 +73,24 @@
/* The zcalloc symbol is a symbol name already used by zlib, which is defining /* The zcalloc symbol is a symbol name already used by zlib, which is defining
* other names using the "z" prefix specific to zlib. In practice, linking * other names using the "z" prefix specific to zlib. In practice, linking
* valkey with a static openssl, which itself might depend on a static libz * valkey with a static openssl, which itself might depend on a static libz
* will result in link time error rejecting multiple symbol definitions. */ * will result in a link time error rejecting multiple symbol definitions.
#define zmalloc valkey_malloc */
#define zcalloc valkey_calloc #define zmalloc valkey_malloc
#define zrealloc valkey_realloc #define zcalloc valkey_calloc
#define zfree valkey_free #define zrealloc valkey_realloc
#define zfree valkey_free
/* On MSVC, the GNU __attribute__ syntax is not supported.
* We define it as a variadic macro that discards its arguments.
*/
#ifdef _MSC_VER
#define __attribute__(...) /* nothing */
#endif
/* 'noinline' attribute is intended to prevent the `-Wstringop-overread` warning /* 'noinline' attribute is intended to prevent the `-Wstringop-overread` warning
* when using gcc-12 later with LTO enabled. It may be removed once the * when using gcc-12 later with LTO enabled. It may be removed once the bug
* bug[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503] is fixed. */ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 is fixed.
*/
__attribute__((malloc, alloc_size(1), noinline)) void *zmalloc(size_t size); __attribute__((malloc, alloc_size(1), noinline)) void *zmalloc(size_t size);
__attribute__((malloc, alloc_size(1), noinline)) void *zcalloc(size_t size); __attribute__((malloc, alloc_size(1), noinline)) void *zcalloc(size_t size);
__attribute__((malloc, alloc_size(1, 2), noinline)) void *zcalloc_num(size_t num, size_t size); __attribute__((malloc, alloc_size(1, 2), noinline)) void *zcalloc_num(size_t num, size_t size);
@ -151,15 +130,13 @@ size_t zmalloc_usable_size(void *ptr);
* obtained from z[*]_usable() family functions, there is no need for this step. */ * obtained from z[*]_usable() family functions, there is no need for this step. */
#define zmalloc_usable_size(p) zmalloc_size(p) #define zmalloc_usable_size(p) zmalloc_size(p)
/* derived from https://github.com/systemd/systemd/pull/25688 /* Derived from https://github.com/systemd/systemd/pull/25688
* We use zmalloc_usable_size() everywhere to use memory blocks, but that is an abuse since the * We use zmalloc_usable_size() everywhere to use memory blocks, but that is an
* malloc_usable_size() isn't meant for this kind of use, it is for diagnostics only. That is also why the * abuse of malloc_usable_size(). The extend_to_usable function is a dummy
* behavior is flaky when built with _FORTIFY_SOURCE, the compiler can sense that we reach outside * allocator function that notifies the compiler of updated pointer size.
* the allocated block and SIGABRT. * The function simply returns the pointer as is; its sole purpose is to carry
* We use a dummy allocator function to tell the compiler that the new size of ptr is newsize. * the alloc_size attribute.
* The implementation returns the pointer as is; the only reason for its existence is as a conduit for the */
* alloc_size attribute. This cannot be a static inline because gcc then loses the attributes on the function.
* See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 */
__attribute__((alloc_size(2), noinline)) void *extend_to_usable(void *ptr, size_t size); __attribute__((alloc_size(2), noinline)) void *extend_to_usable(void *ptr, size_t size);
#endif #endif