summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYann Collet <Cyan4973@users.noreply.github.com>2020-11-11 15:43:17 -0800
committerGitHub <noreply@github.com>2020-11-11 15:43:17 -0800
commit9061a0a93582854cbddb64cb048ae527a0c220af (patch)
tree1ebee340d4343f2eabbbd102dde2fd7297d54dc2
parentf9631e44a83b3b1278d6d3405acca20ecc8ad4c5 (diff)
parent86f5e9d0f4a561dfefb3a773ee4198236bf6e202 (diff)
Merge pull request #946 from lz4/customMem
proposal for LZ4_USER_MEMORY_FUNCTIONS
-rw-r--r--.travis.yml6
-rw-r--r--lib/README.md4
-rw-r--r--lib/lz4.c21
-rw-r--r--lib/lz4frame.c2
-rw-r--r--lib/lz4hc.c15
-rw-r--r--tests/Makefile9
-rw-r--r--tests/fullbench.c8
7 files changed, 48 insertions, 17 deletions
diff --git a/.travis.yml b/.travis.yml
index 2281394..f201d52 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -31,11 +31,15 @@ matrix:
script:
- CC=clang MOREFLAGS=-fsanitize=address make -C tests test-frametest test-fuzzer
- - name: Custom LZ4_DISTANCE_MAX ; Build lz4-wlib (CLI linked to dynamic library)
+ - name: Custom LZ4_DISTANCE_MAX ; lz4-wlib (CLI linked to dynamic library); LZ4_USER_MEMORY_FUNCTIONS
script:
- MOREFLAGS=-DLZ4_DISTANCE_MAX=8000 make check
- make clean
- make -C programs lz4-wlib
+ - make clean
+ - make -C tests fullbench-wmalloc # test LZ4_USER_MEMORY_FUNCTIONS
+ - make clean
+ - CC="c++ -Wno-deprecated" make -C tests fullbench-wmalloc # stricter function signature check
- name: (Precise) g++ and clang CMake test
dist: precise
diff --git a/lib/README.md b/lib/README.md
index 318106d..e2af868 100644
--- a/lib/README.md
+++ b/lib/README.md
@@ -69,6 +69,10 @@ The following build macro can be selected to adjust source code behavior at comp
This build macro offers another project-specific method
by defining `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files.
+- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to <stdlib>'s `malloc`, `calloc` and `free`
+ by user-defined functions, which must be called `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`.
+ User functions must be available at link time.
+
- `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths
by using bitcount instructions, generally implemented as fast single instructions in many cpus.
In case the target cpus doesn't support it, or compiler intrinsic doesn't work, or feature bad performance,
diff --git a/lib/lz4.c b/lib/lz4.c
index 71bdce1..5fe3433 100644
--- a/lib/lz4.c
+++ b/lib/lz4.c
@@ -188,10 +188,23 @@
/*-************************************
* Memory routines
**************************************/
-#include <stdlib.h> /* malloc, calloc, free */
-#define ALLOC(s) malloc(s)
-#define ALLOC_AND_ZERO(s) calloc(1,s)
-#define FREEMEM(p) free(p)
+#ifdef LZ4_USER_MEMORY_FUNCTIONS
+/* memory management functions can be customized by user project.
+ * Below functions must exist somewhere in the Project
+ * and be available at link time */
+void* LZ4_malloc(size_t s);
+void* LZ4_calloc(size_t n, size_t s);
+void LZ4_free(void* p);
+# define ALLOC(s) LZ4_malloc(s)
+# define ALLOC_AND_ZERO(s) LZ4_calloc(1,s)
+# define FREEMEM(p) LZ4_free(p)
+#else
+# include <stdlib.h> /* malloc, calloc, free */
+# define ALLOC(s) malloc(s)
+# define ALLOC_AND_ZERO(s) calloc(1,s)
+# define FREEMEM(p) free(p)
+#endif
+
#include <string.h> /* memset, memcpy */
#define MEM_INIT(p,v,s) memset((p),(v),(s))
diff --git a/lib/lz4frame.c b/lib/lz4frame.c
index e02b76c..ec02c92 100644
--- a/lib/lz4frame.c
+++ b/lib/lz4frame.c
@@ -71,8 +71,8 @@
* towards another library or solution of their choice
* by modifying below section.
*/
-#include <stdlib.h> /* malloc, calloc, free */
#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */
+# include <stdlib.h> /* malloc, calloc, free */
# define ALLOC(s) malloc(s)
# define ALLOC_AND_ZERO(s) calloc(1,(s))
# define FREEMEM(p) free(p)
diff --git a/lib/lz4hc.c b/lib/lz4hc.c
index 286ff68..8c6063f 100644
--- a/lib/lz4hc.c
+++ b/lib/lz4hc.c
@@ -93,7 +93,7 @@ static U32 LZ4HC_hashPtr(const void* ptr) { return HASH_FUNCTION(LZ4_read32(ptr)
**************************************/
static void LZ4HC_clearTables (LZ4HC_CCtx_internal* hc4)
{
- MEM_INIT((void*)hc4->hashTable, 0, sizeof(hc4->hashTable));
+ MEM_INIT(hc4->hashTable, 0, sizeof(hc4->hashTable));
MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable));
}
@@ -983,11 +983,10 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s
/* allocation */
LZ4_streamHC_t* LZ4_createStreamHC(void)
{
- LZ4_streamHC_t* const state = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t));
- if (LZ4_initStreamHC(state, sizeof(*state)) == NULL) {
- free(state);
- return NULL;
- }
+ LZ4_streamHC_t* const state =
+ (LZ4_streamHC_t*)ALLOC_AND_ZERO(sizeof(LZ4_streamHC_t));
+ if (state == NULL) return NULL;
+ LZ4_setCompressionLevel(state, LZ4HC_CLEVEL_DEFAULT);
return state;
}
@@ -1326,7 +1325,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx,
int retval = 0;
#define TRAILING_LITERALS 3
#ifdef LZ4HC_HEAPMODE
- LZ4HC_optimal_t* const opt = (LZ4HC_optimal_t*)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS));
+ LZ4HC_optimal_t* const opt = (LZ4HC_optimal_t*)ALLOC(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS));
#else
LZ4HC_optimal_t opt[LZ4_OPT_NUM + TRAILING_LITERALS]; /* ~64 KB, which is a bit large for stack... */
#endif
@@ -1609,7 +1608,7 @@ if (limit == fillOutput) {
}
_return_label:
#ifdef LZ4HC_HEAPMODE
- free(opt);
+ FREEMEM(opt);
#endif
return retval;
}
diff --git a/tests/Makefile b/tests/Makefile
index 43c9651..476849e 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -53,6 +53,7 @@ TEST_FILES := COPYING
FUZZER_TIME := -T90s
NB_LOOPS ?= -i1
+.PHONY: default
default: all
all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial
@@ -89,6 +90,10 @@ fullbench-dll: fullbench.c $(LZ4DIR)/xxhash.c
$(MAKE) -C $(LZ4DIR) liblz4
$(CC) $(FLAGS) $^ -o $@$(EXT) -DLZ4_DLL_IMPORT=1 $(LZ4DIR)/dll/$(LIBLZ4).dll
+# test LZ4_USER_MEMORY_FUNCTIONS
+fullbench-wmalloc: CPPFLAGS += -DLZ4_USER_MEMORY_FUNCTIONS
+fullbench-wmalloc: fullbench
+
fuzzer : lz4.o lz4hc.o xxhash.o fuzzer.c
$(CC) $(FLAGS) $^ -o $@$(EXT)
@@ -107,6 +112,7 @@ checkFrame : lz4frame.o lz4.o lz4hc.o xxhash.o checkFrame.c
decompress-partial: lz4.o decompress-partial.c
$(CC) $(FLAGS) $^ -o $@$(EXT)
+.PHONY: clean
clean:
@$(MAKE) -C $(LZ4DIR) $@ > $(VOID)
@$(MAKE) -C $(PRGDIR) $@ > $(VOID)
@@ -170,9 +176,6 @@ test32: test
test-amalgamation: lz4_all.o
-lz4_all.o: lz4_all.c
- $(CC) $(CFLAGS) $(CPPFLAGS) -c $^ -o $@
-
lz4_all.c: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c
$(CAT) $^ > $@
diff --git a/tests/fullbench.c b/tests/fullbench.c
index 4ec30ce..cb9b684 100644
--- a/tests/fullbench.c
+++ b/tests/fullbench.c
@@ -156,6 +156,14 @@ static size_t BMK_findMaxMem(U64 requiredMem)
/*********************************************************
+* Memory management, to test LZ4_USER_MEMORY_FUNCTIONS
+*********************************************************/
+void* LZ4_malloc(size_t s) { return malloc(s); }
+void* LZ4_calloc(size_t n, size_t s) { return calloc(n,s); }
+void LZ4_free(void* p) { free(p); }
+
+
+/*********************************************************
* Benchmark function
*********************************************************/
static LZ4_stream_t LZ4_stream;