Skip to content

Commit ef7e3f4

Browse files
authored
Merge pull request #1006 from Cyan4973/memX_redirect
memxxx() redirect
2 parents e4e33f1 + eaffecc commit ef7e3f4

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ The following macros can be set at compilation time to modify `libxxhash`'s beha
141141
But one-shot hashing (like `XXH32()`) or streaming using statically allocated states
142142
still work as expected.
143143
This build flag is useful for embedded environments without dynamic allocation.
144+
- `XXH_memcpy`, `XXH_memset`, `XXH_memcmp` : redirect `memcpy()`, `memset()` and `memcmp()` to some user-selected symbol at compile time.
145+
Redirecting all 3 removes the need to include `<string.h>` standard library.
144146
- `XXH_DEBUGLEVEL` : When set to any value >= 1, enables `assert()` statements.
145147
This (slightly) slows down execution, but may help finding bugs during debugging sessions.
146148

xxhash.h

+31-14
Original file line numberDiff line numberDiff line change
@@ -2394,16 +2394,35 @@ static void XXH_free(void* p) { free(p); }
23942394

23952395
#endif /* XXH_NO_STDLIB */
23962396

2397-
#include <string.h>
2397+
#ifndef XXH_memcpy
2398+
/*!
2399+
* @internal
2400+
* @brief XXH_memcpy() macro can be redirected at compile time
2401+
*/
2402+
# include <string.h>
2403+
# define XXH_memcpy memcpy
2404+
#endif
23982405

2406+
#ifndef XXH_memset
23992407
/*!
24002408
* @internal
2401-
* @brief Modify this function to use a different routine than memcpy().
2409+
* @brief XXH_memset() macro can be redirected at compile time
24022410
*/
2403-
static void* XXH_memcpy(void* dest, const void* src, size_t size)
2404-
{
2405-
return memcpy(dest,src,size);
2406-
}
2411+
# include <string.h>
2412+
# define XXH_memset memset
2413+
#endif
2414+
2415+
#ifndef XXH_memcmp
2416+
/*!
2417+
* @internal
2418+
* @brief XXH_memcmp() macro can be redirected at compile time
2419+
* Note: only needed by XXH128.
2420+
*/
2421+
# include <string.h>
2422+
# define XXH_memcmp memcmp
2423+
#endif
2424+
2425+
24072426

24082427
#include <limits.h> /* ULLONG_MAX */
24092428

@@ -3224,7 +3243,7 @@ XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t
32243243
XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed)
32253244
{
32263245
XXH_ASSERT(statePtr != NULL);
3227-
memset(statePtr, 0, sizeof(*statePtr));
3246+
XXH_memset(statePtr, 0, sizeof(*statePtr));
32283247
XXH32_initAccs(statePtr->acc, seed);
32293248
return XXH_OK;
32303249
}
@@ -3721,7 +3740,7 @@ XXH_PUBLIC_API void XXH64_copyState(XXH_NOESCAPE XXH64_state_t* dstState, const
37213740
XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH_NOESCAPE XXH64_state_t* statePtr, XXH64_hash_t seed)
37223741
{
37233742
XXH_ASSERT(statePtr != NULL);
3724-
memset(statePtr, 0, sizeof(*statePtr));
3743+
XXH_memset(statePtr, 0, sizeof(*statePtr));
37253744
XXH64_initAccs(statePtr->acc, seed);
37263745
return XXH_OK;
37273746
}
@@ -6409,7 +6428,7 @@ XXH3_reset_internal(XXH3_state_t* statePtr,
64096428
XXH_ASSERT(offsetof(XXH3_state_t, nbStripesPerBlock) > initStart);
64106429
XXH_ASSERT(statePtr != NULL);
64116430
/* set members from bufferedSize to nbStripesPerBlock (excluded) to 0 */
6412-
memset((char*)statePtr + initStart, 0, initLength);
6431+
XXH_memset((char*)statePtr + initStart, 0, initLength);
64136432
statePtr->acc[0] = XXH_PRIME32_3;
64146433
statePtr->acc[1] = XXH_PRIME64_1;
64156434
statePtr->acc[2] = XXH_PRIME64_2;
@@ -7183,14 +7202,12 @@ XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest (XXH_NOESCAPE const XXH3_state_
71837202
#endif /* !XXH_NO_STREAM */
71847203
/* 128-bit utility functions */
71857204

7186-
#include <string.h> /* memcmp, memcpy */
7187-
71887205
/* return : 1 is equal, 0 if different */
71897206
/*! @ingroup XXH3_family */
71907207
XXH_PUBLIC_API int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2)
71917208
{
71927209
/* note : XXH128_hash_t is compact, it has no padding byte */
7193-
return !(memcmp(&h1, &h2, sizeof(h1)));
7210+
return !(XXH_memcmp(&h1, &h2, sizeof(h1)));
71947211
}
71957212

71967213
/* This prototype is compatible with stdlib's qsort().
@@ -7274,7 +7291,7 @@ XXH3_generateSecret(XXH_NOESCAPE void* secretBuffer, size_t secretSize, XXH_NOES
72747291
{ size_t pos = 0;
72757292
while (pos < secretSize) {
72767293
size_t const toCopy = XXH_MIN((secretSize - pos), customSeedSize);
7277-
memcpy((char*)secretBuffer + pos, customSeed, toCopy);
7294+
XXH_memcpy((char*)secretBuffer + pos, customSeed, toCopy);
72787295
pos += toCopy;
72797296
} }
72807297

@@ -7299,7 +7316,7 @@ XXH3_generateSecret_fromSeed(XXH_NOESCAPE void* secretBuffer, XXH64_hash_t seed)
72997316
XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
73007317
XXH3_initCustomSecret(secret, seed);
73017318
XXH_ASSERT(secretBuffer != NULL);
7302-
memcpy(secretBuffer, secret, XXH_SECRET_DEFAULT_SIZE);
7319+
XXH_memcpy(secretBuffer, secret, XXH_SECRET_DEFAULT_SIZE);
73037320
}
73047321

73057322

0 commit comments

Comments
 (0)