Skip to content

Commit

Permalink
Added : XXH_NAMESPACE, for namespace emulation in C
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyan4973 committed Jun 28, 2015
1 parent 962fd0a commit cab8663
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
16 changes: 6 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ else
EXT =
endif

.PHONY: clean all

default: xxhsum

Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions xxhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions xxhsum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit cab8663

Please sign in to comment.