From 8c4a45e384accf92b1b6d748530e8d46f7de40d6 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 20 Apr 2024 11:10:30 -0400 Subject: refactor: Overhaul C libraries and fix builds --- lib/Net.Compression/vnlib_compress/CMakeLists.txt | 464 +++++++-------------- lib/Net.Compression/vnlib_compress/Taskfile.yaml | 57 +-- lib/Net.Compression/vnlib_compress/compression.h | 16 +- .../vnlib_compress/feature_brotli.c | 6 +- lib/Net.Compression/vnlib_compress/util.h | 56 ++- 5 files changed, 206 insertions(+), 393 deletions(-) (limited to 'lib/Net.Compression') diff --git a/lib/Net.Compression/vnlib_compress/CMakeLists.txt b/lib/Net.Compression/vnlib_compress/CMakeLists.txt index f963c41..c3eb723 100644 --- a/lib/Net.Compression/vnlib_compress/CMakeLists.txt +++ b/lib/Net.Compression/vnlib_compress/CMakeLists.txt @@ -1,95 +1,181 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.10) project(vnlib_compress C) - -#export header files to the main project -file(GLOB COMP_HEADERS *.h) - -#Add indepednent source files to the project -set(VNLIB_COMPRESS_SOURCES compression.c) +set(CMAKE_PROJECT_NAME "vnlib_compress") #set options for enable botli and zlib option(ENABLE_BROTLI "Enable brotli compression" ON) option(ENABLE_ZLIB "Enable zlib compression" ON) option(ENABLE_RPMALLOC "Enable local source code vnlib_rpmalloc memory allocator" OFF) +option(COMPRESS_BUILD_SHARED "Produces a shared library instead of a static library" ON) +option(USE_STATIC_RUNTIME "Use the static runtime library" OFF) +set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The build configuration type") + +string(TOLOWER ${CMAKE_BUILD_TYPE} build_type) +message(STATUS "Build type is '${build_type}'") -set(THIRD_PARTY_DIR ./third-party) +#export all header files to the main project +file(GLOB COMP_HEADERS *.h) +set(VNLIB_COMPRESS_SOURCES + compression.c +) + +include(FetchContent) + +############################### +# +# DOWNLOAD DEPENDENCIES +# +############################### -#add feature specific source files to the project if(ENABLE_BROTLI) + + message(STATUS "Downloading brotli compression as a local dependency") + + set(BROTLI_BUNDLED_MODE OFF) #explicitly set bundled mode to off + set(BROTLI_EMSCRIPTEN OFF) #disable emscripten support + set(BUILD_SHARED_LIBS OFF) #disable shared library building, as only static is needed + + FetchContent_Declare( + lib_brotli + GIT_REPOSITORY https://github.com/google/brotli.git + GIT_TAG 1b3a5ccb6e7b9384b741437532f4dae0730c61f2 + GIT_PROGRESS TRUE + ) + + FetchContent_GetProperties(lib_brotli) + FetchContent_MakeAvailable(lib_brotli) + + #add include directories for brotli + include_directories(${lib_brotli_SOURCE_DIR}/c/include) + + #add the brotli source files to the project list(APPEND VNLIB_COMPRESS_SOURCES feature_brotli.c) - #define the brotli feature macro to enable zlib support - add_definitions(-DVNLIB_COMPRESSOR_BROTLI_ENABLED) + add_compile_definitions(VNLIB_COMPRESSOR_BROTLI_ENABLED) endif() if(ENABLE_ZLIB) + + #by default the cloudlfare fork should build a static lib. It will be large + message(STATUS "Downloading ZLIB as a local dependency") + + set(SKIP_INSTALL_ALL ON) #do not install zlib files + + #add zlib source files to the project + FetchContent_Declare( + lib_cf_zlib + GIT_REPOSITORY https://github.com/cloudflare/zlib.git + GIT_TAG 92530568d2c128b4432467b76a3b54d93d6350bd + GIT_PROGRESS TRUE + ) + + #Get project properties + FetchContent_GetProperties(lib_cf_zlib) + FetchContent_MakeAvailable(lib_cf_zlib) + + #add include directories for zlib + include_directories(${lib_cf_zlib_SOURCE_DIR}) + + #enable the feature code for zlib and add the source files list(APPEND VNLIB_COMPRESS_SOURCES feature_zlib.c) - #define the zlib feature macro to enable zlib support - add_definitions(-DVNLIB_COMPRESSOR_ZLIB_ENABLED) + add_compile_definitions(VNLIB_COMPRESSOR_ZLIB_ENABLED) endif() -#create my shared library -add_library(${CMAKE_PROJECT_NAME} SHARED ${VNLIB_COMPRESS_SOURCES} ${COMP_HEADERS}) -#also create static library -add_library(${CMAKE_PROJECT_NAME}_static STATIC ${VNLIB_COMPRESS_SOURCES} ${COMP_HEADERS}) +#Add support for rpmalloc memmory allocator +if(ENABLE_RPMALLOC) -#if on unix lib will be appended, so we can adjust -if(UNIX) - set_target_properties(${CMAKE_PROJECT_NAME} ${CMAKE_PROJECT_NAME}_static PROPERTIES OUTPUT_NAME vn_compress) + #enable greedy mode for rpmalloc + set(ENABLE_GREEDY ON) + + add_subdirectory( + ../../Utils.Memory/vnlib_rpmalloc/ + ${CMAKE_CURRENT_BINARY_DIR}/vnlib_rpmalloc/ + ) + + add_compile_definitions(VNLIB_CUSTOM_MALLOC_ENABLE) endif() #Setup the compiler options -set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) #enable position independent code (for shared libraries with exports) + +############################### +# +# CONFIGURE LIBRARY BUILD +# +############################### + +if(COMPRESS_BUILD_SHARED) + set(TARGET_NAME ${CMAKE_PROJECT_NAME}) + add_library(${TARGET_NAME} SHARED ${VNLIB_COMPRESS_SOURCES} ${COMP_HEADERS}) +else() + set(TARGET_NAME ${CMAKE_PROJECT_NAME}_static) + add_library(${TARGET_NAME} STATIC ${VNLIB_COMPRESS_SOURCES} ${COMP_HEADERS}) +endif() -#enable position independent code (for shared libraries with exports) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) +target_compile_features(${TARGET_NAME} PRIVATE c_std_90) #force compiler to use c90 standard for library + +#if on unix lib will be appended, so we can adjust +if(UNIX) + set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME vn_compress) +endif() #since were buildiing in tree, set the export defintiions -add_definitions(-DVNLIB_EXPORTING) +target_compile_definitions(${TARGET_NAME} PRIVATE VNLIB_EXPORTING) + +if(ENABLE_BROTLI) + #link the encoder-only library to the main project + target_link_libraries(${TARGET_NAME} PRIVATE brotlienc) +endif() + +if(ENABLE_ZLIB) + #workaround cloudlfare fork bug. SSE is always enabled with x64 on Windows + if(MSVC) + target_compile_definitions(zlib PRIVATE HAS_SSE42 PRIVATE HAS_SSE2) + endif() + + target_link_libraries(${TARGET_NAME} PRIVATE zlib) +endif() -message(STATUS "Build type is '${CMAKE_BUILD_TYPE}'") +#link rpmalloc to the main project +if(ENABLE_RPMALLOC) + target_link_libraries(${TARGET_NAME} PRIVATE vnlib_rpmalloc_static) + add_dependencies(${TARGET_NAME} vnlib_rpmalloc_static) -#include checks for zlib and brotli -include(CheckTypeSize) -include(CheckFunctionExists) -include(CheckIncludeFile) -include(CheckCCompilerFlag) -enable_testing() + #Include the nativeheap api header + target_include_directories(${TARGET_NAME} PRIVATE ../../Utils.Memory/vnlib_rpmalloc/) +endif() #setup flags for windows compilation if(MSVC) - - #global windows cl flags - add_compile_options( + target_compile_options( + ${TARGET_NAME} + PRIVATE + /Qspectre /sdl /TC /GS - - $<$:/FC> - $<$:/showIncludes> - ) - #only target our project - target_compile_options( - ${CMAKE_PROJECT_NAME} - PRIVATE + #disable warnings for struct padding and spectre mitigation when WX is enabled + $<$:/wd5045> + $<$:/wd4820> #for debug configs $<$:/options:strict> - #disable warnings for struct padding and spectre mitigation wuen WX is enabled - $<$:/wd5045> - $<$:/wd4820> + $<$:/FC> #full path in diagnostics $<$:/Wall> - $<$:/WX> #warnings as errors (only for our project) - $<$:/Zi> - $<$:/Zo> + $<$:/WX> #warnings as errors (only for our project) + $<$:/Zi> #enable debug info + $<$:/showIncludes> ) #set build macros - add_compile_definitions( + target_compile_definitions( + ${TARGET_NAME} + PRIVATE + $<$:DEBUG> $<$:RELEASE> ) @@ -97,215 +183,33 @@ if(MSVC) #configure gcc flags elseif(CMAKE_COMPILER_IS_GNUCC) - add_compile_options( - -Wextra - -fstack-protector - - $<$:-g> - $<$:-Og> - $<$:-Wall> - $<$:-Werror> - ) - - #only target our project - target_compile_options( - ${CMAKE_PROJECT_NAME} + target_compile_options( + ${TARGET_NAME} PRIVATE - $<$:-Wall> - $<$:-pedantic> - ) -endif() - -#check for brotli encoding only feature enablement -if(ENABLE_BROTLI) - - #macros for brotli - set(BROTLI_DEFINITIONS) - - set(LIBM_LIBRARY) - check_function_exists(log2 LOG2_RES) - - if(NOT LOG2_RES) - - set(orig_req_libs "${CMAKE_REQUIRED_LIBRARIES}") - set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};m") - - check_function_exists(log2 LOG2_LIBM_RES) - - if(LOG2_LIBM_RES) - set(LIBM_LIBRARY "m") - list(APPEND BROTLI_DEFINITIONS -DBROTLI_HAVE_LOG2=1) - else() - list(APPEND BROTLI_DEFINITIONS -DBROTLI_HAVE_LOG2=0) - endif() - - set(CMAKE_REQUIRED_LIBRARIES "${orig_req_libs}") - unset(LOG2_LIBM_RES) - unset(orig_req_libs) - else() - list(APPEND BROTLI_DEFINITIONS -DBROTLI_HAVE_LOG2=1) - endif() - unset(LOG2_RES) - - #target definitions from brotli cmakelists - if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") - list(APPEND BROTLI_DEFINITIONS -DOS_LINUX) - elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") - list(APPEND BROTLI_DEFINITIONS -DOS_FREEBSD) - elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - list(APPEND BROTLI_DEFINITIONS -DOS_MACOSX) - set(CMAKE_MACOS_RPATH TRUE) - endif() - - #add the include directory for brotli so we can include the header files - include_directories(${THIRD_PARTY_DIR}/brotli/c/include) - - #get common sources - file(GLOB BROTLI_SOURCES ${THIRD_PARTY_DIR}/brotli/c/common/*.c) - - #we need to add the brotli encoder source files to the project - file(GLOB BROTLI_ENC_SOURCES ${THIRD_PARTY_DIR}/brotli/c/enc/*.c) - - #add brotli as a static library to link later - add_library(lib_brotli STATIC ${BROTLI_SOURCES} ${BROTLI_ENC_SOURCES}) - - target_link_libraries(${CMAKE_PROJECT_NAME} lib_brotli) - - #add the definitions to the brotli project only - target_compile_definitions(lib_brotli PRIVATE ${BROTLI_DEFINITIONS}) - -endif() - -#check for zlib feature enablement, supports madler and cloudflare forks -if(ENABLE_ZLIB) - - #add the include directory for zlib so we can include the header files - include_directories(${THIRD_PARTY_DIR}/zlib) - - set(ZLIB_DEFINITIONS) - set(Z_C_FLAGS) - - #we only need to add the zlib deflate source files to the project - set(ZLIB_SOURCES - ${THIRD_PARTY_DIR}/zlib/deflate.c - ${THIRD_PARTY_DIR}/zlib/adler32.c - ${THIRD_PARTY_DIR}/zlib/crc32.c - ${THIRD_PARTY_DIR}/zlib/zutil.c - ${THIRD_PARTY_DIR}/zlib/trees.c + -Wextra + -fstack-protector ) - - check_type_size(off64_t OFF64_T) - check_function_exists(fseeko HAVE_FSEEKO) - - if(HAVE_OFF64_T) - list(APPEND ZLIB_DEFINITIONS -D_LARGEFILE64_SOURCE=1) - endif() - #add fseeko if we have it - if(NOT HAVE_FSEEKO) - list(APPEND ZLIB_DEFINITIONS -DNO_FSEEKO) - endif() - - if(MSVC) - - set(CMAKE_DEBUG_POSTFIX "d") - - list(APPEND ZLIB_DEFINITIONS - -D_CRT_SECURE_NO_DEPRECATE - -D_CRT_NONSTDC_NO_DEPRECATE + #enable debug compiler options + if(build_type STREQUAL "debug") + target_compile_options( + ${TARGET_NAME} + PRIVATE + + -g #enable debugger info + -Og #disable optimizations + -Wall #enable all warnings + -Werror #treat warnings as errors + -pedantic #enable pedantic mode ) - - #NOTE - #During CI a pre-compiled library will be built. - #We cannot depend on users having the the same instructions as the build machine - #So some optimizations are disabled for the pre-compiled library - - if(NOT CI_PRECOMPILE) - - #setup avx compiler support on Windows - check_c_compiler_flag(/arch:AVX HAS_AVX) - if (HAS_AVX) - list(APPEND Z_C_FLAGS /arch:AVX) - list(APPEND ZLIB_DEFINITIONS -DHAS_AVX) - endif() - - endif() - - #All x64 machines have SSE2, so we can use it as - #and the Windows compiler will automatically use it - #so we only need to set the definition - list(APPEND Z_C_FLAGS /arch:SSE2) - list(APPEND ZLIB_DEFINITIONS -DHAS_SSE2 -DHAS_SSE42) - - elseif(UNIX) - - #for cloudflare intrinsic detections - check_c_compiler_flag(-march=armv8-a+crc ARM_CRC) - check_c_compiler_flag(-msse2 HAS_SSE2) - check_c_compiler_flag(-mssse3 HAS_SSSE3) - check_c_compiler_flag(-msse4.2 HAS_SSE42) - check_c_compiler_flag(-mpclmul HAS_PCLMUL) - - if(ARM_CRC) - list(APPEND Z_C_FLAGS -march=armv8-a+crc) - - if(EXISTS "{THIRD_PARTY_DIR}/zlib/adler32_simd.c") - list(APPEND ZLIB_DEFINITIONS -DADLER32_SIMD_NEON) - list(APPEND ZLIB_SOURCES ${THIRD_PARTY_DIR}/zlib/adler32_simd.c) - endif() - - else() - if(HAS_SSE2) - list(APPEND Z_C_FLAGS -msse2) - list(APPEND ZLIB_DEFINITIONS -DHAS_SSE2) - #excluding inflate specific optimizations - endif() - if(HAS_SSSE3) - list(APPEND Z_C_FLAGS -mssse3) - - #add cloudflare intrinsic optimizations, may not be present if using non-cloudflare fork - if(EXISTS "${THIRD_PARTY_DIR}/zlib/adler32_simd.c") - list(APPEND ZLIB_DEFINITIONS -DHAS_SSSE3 -DADLER32_SIMD_SSSE3) - list(APPEND ZLIB_SOURCES ${THIRD_PARTY_DIR}/zlib/adler32_simd.c) - endif() - - endif() - if(HAS_SSE42) - list(APPEND Z_C_FLAGS -msse4.2) - list(APPEND ZLIB_DEFINITIONS -DHAS_SSE42) - endif() - if(HAS_PCLMUL) - list(APPEND Z_C_FLAGS -mpclmul) - - #add cloudflare intrinsic optimizations for PCMLONGMUL crc32, may not be present if using non-cloudflare fork - if(EXISTS "${THIRD_PARTY_DIR}/zlib/crc32_simd.c") - list(APPEND ZLIB_DEFINITIONS -DHAS_PCLMUL) - list(APPEND ZLIB_SOURCES ${THIRD_PARTY_DIR}/zlib/crc32_simd.c) - endif() - - endif() - endif() - endif() - - - - #add zlib as a library to link later - add_library(lib_deflate STATIC ${ZLIB_SOURCES}) + target_compile_definitions(${TARGET_NAME} PRIVATE DEBUG) - if(MSVC) - #allways targeting x64 machines - set_target_properties(lib_deflate PROPERTIES STATIC_LIBRARY_FLAGS "/machine:x64") endif() - #add the definitions to the zlib project only - target_compile_definitions(lib_deflate PRIVATE ${ZLIB_DEFINITIONS}) - - #only target zlib project with compiler flags - target_compile_options(lib_deflate PRIVATE ${Z_C_FLAGS}) - - target_link_libraries(${CMAKE_PROJECT_NAME} lib_deflate) +else() + message(FATAL_ERROR "Unsupported compiler, sorry. Submit an issue for your platform and I'll work on it :)") endif() if(NATIVE_HEAP_LIB_PATH) @@ -314,63 +218,7 @@ if(NATIVE_HEAP_LIB_PATH) include_directories(${NATIVE_HEAP_INCLUDES}) #If manual heap linking is enabled, we need to link the native heap library - target_link_libraries(${CMAKE_PROJECT_NAME} ${NATIVE_HEAP_LIB_PATH}) - - #Disable rpmalloc if we are linking a custom native heap - set(ENABLE_RPMALLOC OFF) - - #add defintion to enable custom malloc heap overrides - target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE VNLIB_CUSTOM_MALLOC_ENABLE) - -endif() - -#Add support for rpmalloc memmory allocator -if(ENABLE_RPMALLOC) - - #Include the nativeheap api header - include_directories(../../Utils.Memory/NativeHeapApi/src) - - if(MSVC) + target_link_libraries(${TARGET_NAME} PRIVATE ${NATIVE_HEAP_LIB_PATH}) + target_compile_definitions(${TARGET_NAME} PRIVATE VNLIB_CUSTOM_MALLOC_ENABLE) #configure src - #find the rpmalloc static library for windows builds - find_library(VNLIB_RPMALLOC_DEBUG_LIB - NAMES vnlib_rpmalloc_static - PATHS ../../Utils.Memory/vnlib_rpmalloc/build/Debug - ) - - find_library(VNLIB_RPMALLOC_RELEASE_LIB - NAMES vnlib_rpmalloc_static - PATHS ../../Utils.Memory/vnlib_rpmalloc/build/Release - ) - - #target static libraries for all configs - target_link_libraries(${CMAKE_PROJECT_NAME} - $<$:${VNLIB_RPMALLOC_DEBUG_LIB}> - #set release for all release configs - $<$:${VNLIB_RPMALLOC_RELEASE_LIB}> - $<$:${VNLIB_RPMALLOC_RELEASE_LIB}> - $<$:${VNLIB_RPMALLOC_RELEASE_LIB}> - ) - - elseif(UNIX) - - #find the rpmalloc library for unix builds - find_library(VNLIB_RPMALLOC_LIB - NAMES libvn_rpmalloc - PATHS ../../Utils.Memory/vnlib_rpmalloc/build - ) - - #add the rpmalloc library to the project - target_link_libraries(${CMAKE_PROJECT_NAME} ${VNLIB_RPMALLOC_LIB}) - - else() - - #failure - message(FATAL_ERROR "Unsupported platform configuration for rpmalloc") - - endif() - - #add defintion to enable custom malloc heap overrides - target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE VNLIB_CUSTOM_MALLOC_ENABLE) - -endif() +endif() \ No newline at end of file diff --git a/lib/Net.Compression/vnlib_compress/Taskfile.yaml b/lib/Net.Compression/vnlib_compress/Taskfile.yaml index fff756d..4ddc464 100644 --- a/lib/Net.Compression/vnlib_compress/Taskfile.yaml +++ b/lib/Net.Compression/vnlib_compress/Taskfile.yaml @@ -9,50 +9,21 @@ version: '3' vars: - THIRD_PARTY_DIR: './third-party' PROJECT_NAME: 'vnlib_compress' - ZLIB_GIT_REPO: 'https://github.com/cloudflare/zlib.git' - BROTLI_GIT_REPO: 'https://github.com/google/brotli.git' tasks: default: cmds: - - cmd: echo "Building vnlib_compress" + - cmd: echo "Building {{.PROJECT_NAME}}" silent: true - - #make third-party dir before cloning libs - - cmd: powershell -Command "mkdir '{{.THIRD_PARTY_DIR}}' -Force" - platforms: [windows] - ignore_error: true - - cmd: mkdir -p '{{.THIRD_PARTY_DIR}}' - platforms: [linux, darwin] - ignore_error: true - - - task: zlib - - task: brotli - - #invoke cmake for build - - cmake -B./build {{.CMAKE_ARGS}} - - #build for platform - - cmake --build build/ --config Release - + - cmake -Bbuild/ -DCMAKE_BUILD_TYPE=Release {{.CMAKE_ARGS}} + - cmake --build build/ --config Release #called by ci pipline to build the winx64 project build: cmds: - #make third-party dir before cloning libs - - cmd: powershell -Command "mkdir '{{.THIRD_PARTY_DIR}}' -Force" - platforms: [windows] - ignore_error: true - - cmd: mkdir -p '{{.THIRD_PARTY_DIR}}' - platforms: [linux, darwin] - ignore_error: true - - task: zlib - - task: brotli - #the CI pipline may have issues reading modules if the third-party dir is not cleaned every time a build runs, only an issue after build - defer: { task: clean-third-party } @@ -85,31 +56,15 @@ tasks: clean: ignore_error: true cmds: - - for: [ bin/, build/, third-party/ ] + - for: [ bin/, build/ ] cmd: powershell Remove-Item -Recurse '{{.ITEM}}' -Force - - #update or install the cloudflare fork of zlib library - zlib: - internal: true - status: - - cd {{.THIRD_PARTY_DIR}} && git clone {{.ZLIB_GIT_REPO}} - cmds: - - cd {{.THIRD_PARTY_DIR}}/zlib && git pull - - #update or install the google brotli library - brotli: - internal: true - status: - - cd {{.THIRD_PARTY_DIR}} && git clone {{.BROTLI_GIT_REPO}} - cmds: - - cd {{.THIRD_PARTY_DIR}}/brotli && git pull clean-third-party: internal: false ignore_error: true cmds: - - cmd: powershell rm -Recurse -Force '{{.THIRD_PARTY_DIR}}' + - cmd: powershell rm -Recurse -Force 'build/_deps/' platforms: [windows] - - cmd: rm -rf {{.THIRD_PARTY_DIR}} + - cmd: rm -rf 'build/_deps/' platforms: [linux, darwin] \ No newline at end of file diff --git a/lib/Net.Compression/vnlib_compress/compression.h b/lib/Net.Compression/vnlib_compress/compression.h index a6a6104..3d03145 100644 --- a/lib/Net.Compression/vnlib_compress/compression.h +++ b/lib/Net.Compression/vnlib_compress/compression.h @@ -44,31 +44,31 @@ #define _IS_WINDOWS #endif -//Set api export calling convention (allow used to override) +/*Set api export calling convention(allow used to override)*/ #ifndef VNLIB_COMPRESS_CC #ifdef _IS_WINDOWS - //STD for importing to other languages such as .NET + /*STD for importing to other languages such as.NET*/ #define VNLIB_COMPRESS_CC __stdcall #else #define VNLIB_COMPRESS_CC #endif -#endif // !VNLIB_CC +#endif /* !VNLIB_CC */ -#ifndef VNLIB_COMPRESS_EXPORT //Allow users to disable the export/impoty macro if using source code directly +#ifndef VNLIB_COMPRESS_EXPORT /*Allow users to disable the export/impoty macro if using source code directly*/ #ifdef VNLIB_COMPRESS_EXPORTING #ifdef _IS_WINDOWS #define VNLIB_COMPRESS_EXPORT __declspec(dllexport) #else #define VNLIB_COMPRESS_EXPORT __attribute__((visibility("default"))) - #endif // IS_WINDOWS + #endif /* IS_WINDOWS */ #else #ifdef _IS_WINDOWS #define VNLIB_COMPRESS_EXPORT __declspec(dllimport) #else #define VNLIB_COMPRESS_EXPORT - #endif // IS_WINDOWS - #endif // !VNLIB_EXPORTING -#endif // !VNLIB_EXPORT + #endif /* IS_WINDOWS */ + #endif /* !VNLIB_EXPORTING */ +#endif /* !VNLIB_EXPORT */ #ifndef _In_ #define _In_ diff --git a/lib/Net.Compression/vnlib_compress/feature_brotli.c b/lib/Net.Compression/vnlib_compress/feature_brotli.c index d5ba141..361c61a 100644 --- a/lib/Net.Compression/vnlib_compress/feature_brotli.c +++ b/lib/Net.Compression/vnlib_compress/feature_brotli.c @@ -223,14 +223,14 @@ int BrCompressBlock(const CompressorState* state, CompressionOperation* operatio int64_t BrGetCompressedSize(const CompressorState* state, uint64_t length, int32_t flush) { - (void)flush; + size_t size; + + (void)sizeof(flush); /* * When the flush flag is set, the caller is requesting the * entire size of the compressed data, which can include metadata */ - size_t size; - validateCompState(state) if (length <= 0) diff --git a/lib/Net.Compression/vnlib_compress/util.h b/lib/Net.Compression/vnlib_compress/util.h index c29de4b..292e3bf 100644 --- a/lib/Net.Compression/vnlib_compress/util.h +++ b/lib/Net.Compression/vnlib_compress/util.h @@ -30,6 +30,8 @@ * will be enabled when heapapi.h is included. */ #ifdef VNLIB_CUSTOM_MALLOC_ENABLE + /* Since static linking ie snabled, heapapi must have extern symbol not dllimport */ + #define VNLIB_HEAP_API extern #include #endif @@ -37,32 +39,28 @@ #define IS_WINDOWS #endif -/* If not Windows, define inline */ -#ifndef IS_WINDOWS - #ifndef inline - #define inline __inline__ - #endif // !inline -#endif // !IS_WINDOWS - -/* If not Windows, define inline */ -#ifndef IS_WINDOWS - #ifndef inline - #define inline __inline__ - #endif // !inline -#endif // !IS_WINDOWS - -//NULL +#if defined(IS_WINDOWS) || defined(inline) || defined(__clang__) + #define _cp_fn_inline inline +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 allows usage of inline keyword */ + #define _cp_fn_inline inline +#elif defined(__GNUC__) || defined(__GNUG__) + #define _cp_fn_inline __inline__ +#else + #define _cp_fn_inline + #pragma message("Warning: No inline keyword defined for this compiler") +#endif + #ifndef NULL #define NULL ((void*)0) -#endif // !NULL +#endif /* !NULL */ #ifndef TRUE #define TRUE 1 -#endif // !TRUE +#endif /* !TRUE */ #ifndef FALSE #define FALSE 0 -#endif // !FALSE +#endif /* !FALSE */ /* * Add debug runtime assertions @@ -76,6 +74,9 @@ #define CHECK_NULL_PTR(ptr) if(!ptr) return ERR_INVALID_PTR; #ifdef NATIVE_HEAP_API /* Defined in the NativeHeapApi */ + + #include + /* * Add overrides for malloc, calloc, and free that use * the nativeheap api to allocate memory @@ -84,23 +85,32 @@ * api consistency. */ - static inline void* vnmalloc(size_t num, size_t size) + static _cp_fn_inline void* vnmalloc(size_t num, size_t size) { return heapAlloc(heapGetSharedHeapHandle(), num, size, FALSE); } - static inline void* vncalloc(size_t num, size_t size) + static _cp_fn_inline void* vncalloc(size_t num, size_t size) { return heapAlloc(heapGetSharedHeapHandle(), num, size, TRUE); } - static inline void vnfree(void* ptr) + static _cp_fn_inline void vnfree(void* ptr) { + #ifdef DEBUG + ERRNO result; result = heapFree(heapGetSharedHeapHandle(), ptr); - //track failed free results + /* track failed free results */ assert(result > 0); + + #else + + heapFree(heapGetSharedHeapHandle(), ptr); + + #endif + } #else @@ -125,6 +135,6 @@ */ #define vncalloc(num, size) calloc(num, size) -#endif // NATIVE_HEAP_API +#endif /* NATIVE_HEAP_API */ #endif /* !UTIL_H_ */ \ No newline at end of file -- cgit