diff --git a/Makefile b/Makefile index b233bf6b..11db9d7a 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ else EXT = endif +.PHONY: clean all default: xxhsum @@ -61,29 +62,24 @@ test32: clean xxhsum32 @echo ---- test 32-bits ---- ./xxhsum32 -bi1 xxhash.c -armtest: +armtest: clean @echo ---- test ARM compilation ---- - $(MAKE) clean $(MAKE) xxhsum CC=arm-linux-gnueabi-gcc MOREFLAGS="-Werror" -clangtest: +clangtest: clean @echo ---- test clang compilation ---- - $(MAKE) clean $(MAKE) all CC=clang MOREFLAGS="-Werror -Wconversion -Wno-sign-conversion" -gpptest: +gpptest: clean @echo ---- test g++ compilation ---- - $(MAKE) clean $(MAKE) all CC=g++ CFLAGS="-O3 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror" -sanitize: +sanitize: clean @echo ---- check undefined behavior - sanitize ---- - $(MAKE) clean $(MAKE) test CC=clang MOREFLAGS="-g -fsanitize=undefined" -staticAnalyze: +staticAnalyze: clean @echo ---- static analyzer - scan-build ---- - $(MAKE) clean scan-build --status-bugs -v $(MAKE) all MOREFLAGS=-g test-all: test test32 armtest clangtest gpptest sanitize staticAnalyze diff --git a/xxhash.h b/xxhash.h index 0df5ac1b..c60aa615 100644 --- a/xxhash.h +++ b/xxhash.h @@ -78,6 +78,39 @@ extern "C" { typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; +/***************************** +* Namespace Emulation +*****************************/ +/* Motivations : + +If you need to include xxHash into your library, +but wish to avoid xxHash symbols to be present on your library interface +in an effort to avoid potential name collision if another library also includes xxHash, + +you can use XXH_NAMESPACE, which will automatically prefix any symbol from xxHash +with the value of XXH_NAMESPACE (so avoid to keep it NULL, and avoid numeric values). + +Note that no change is required within the calling program : +it can still call xxHash functions using their regular name. +They will be automatically translated by this header. +*/ +#ifdef XXH_NAMESPACE +# define XXH_CAT(A,B) A##B +# define XXH_NAME2(A,B) XXH_CAT(A,B) +# define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32) +# define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64) +# define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState) +# define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState) +# define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState) +# define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState) +# define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset) +# define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset) +# define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update) +# define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update) +# define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest) +# define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) +#endif + /***************************** * Simple Hash Functions diff --git a/xxhsum.c b/xxhsum.c index 72cca5d3..f9cd9b83 100644 --- a/xxhsum.c +++ b/xxhsum.c @@ -179,25 +179,28 @@ static int BMK_GetMilliSpan( int nTimeStart ) } -static size_t BMK_findMaxMem(U64 requestedMem) +static size_t BMK_findMaxMem(U64 requiredMem) { - size_t step = (64 MB); - size_t allocatedMemory; + size_t step = 64 MB; BYTE* testmem=NULL; - requestedMem += 3*step; - requestedMem -= (size_t)requestedMem & (step-1); - if (requestedMem > MAX_MEM) requestedMem = MAX_MEM; - allocatedMemory = (size_t)requestedMem; + requiredMem = (((requiredMem >> 26) + 1) << 26); + requiredMem += 2*step; + if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; while (!testmem) { - allocatedMemory -= step; - testmem = (BYTE*) malloc((size_t)allocatedMemory); + if (requiredMem > step) requiredMem -= step; + else requiredMem >>= 1; + testmem = (BYTE*) malloc ((size_t)requiredMem); } free (testmem); - return (size_t) (allocatedMemory - step); + /* keep some space available */ + if (requiredMem > step) requiredMem -= step; + else requiredMem >>= 1; + + return (size_t)requiredMem; } @@ -225,9 +228,10 @@ static void BMK_benchMem(const void* buffer, size_t bufferSize) { int iterationNb; double fastestH = 100000000.; - U32 hashResult; + U32 hashResult = 0; DISPLAY("\r%79s\r", ""); /* Clean display line */ + if (g_nbIterations<1) g_nbIterations=1; for (iterationNb = 1; iterationNb <= g_nbIterations; iterationNb++) { int nbHashes = 0; @@ -345,7 +349,7 @@ static int BMK_benchFile(char** fileNamesTable, int nbFiles) /* Check file existence */ inFileName = fileNamesTable[fileIdx++]; inFile = fopen( inFileName, "rb" ); - if (inFile==NULL) + if ((inFile==NULL) || (inFileName==NULL)) { DISPLAY( "Pb opening %s\n", inFileName); return 11;