extzstd 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -14
  3. data/contrib/zstd/CHANGELOG +114 -56
  4. data/contrib/zstd/CONTRIBUTING.md +14 -0
  5. data/contrib/zstd/Makefile +37 -31
  6. data/contrib/zstd/README.md +6 -0
  7. data/contrib/zstd/appveyor.yml +4 -1
  8. data/contrib/zstd/lib/Makefile +231 -134
  9. data/contrib/zstd/lib/README.md +28 -0
  10. data/contrib/zstd/lib/common/bitstream.h +24 -15
  11. data/contrib/zstd/lib/common/compiler.h +116 -3
  12. data/contrib/zstd/lib/common/cpu.h +0 -2
  13. data/contrib/zstd/lib/common/debug.h +11 -18
  14. data/contrib/zstd/lib/common/entropy_common.c +188 -42
  15. data/contrib/zstd/lib/common/error_private.c +1 -0
  16. data/contrib/zstd/lib/common/error_private.h +1 -1
  17. data/contrib/zstd/lib/common/fse.h +38 -11
  18. data/contrib/zstd/lib/common/fse_decompress.c +123 -16
  19. data/contrib/zstd/lib/common/huf.h +26 -5
  20. data/contrib/zstd/lib/common/mem.h +66 -93
  21. data/contrib/zstd/lib/common/pool.c +22 -16
  22. data/contrib/zstd/lib/common/pool.h +1 -1
  23. data/contrib/zstd/lib/common/threading.c +6 -5
  24. data/contrib/zstd/lib/common/xxhash.c +18 -56
  25. data/contrib/zstd/lib/common/xxhash.h +1 -1
  26. data/contrib/zstd/lib/common/zstd_common.c +9 -9
  27. data/contrib/zstd/lib/common/zstd_deps.h +111 -0
  28. data/contrib/zstd/lib/common/zstd_errors.h +1 -0
  29. data/contrib/zstd/lib/common/zstd_internal.h +89 -58
  30. data/contrib/zstd/lib/compress/fse_compress.c +30 -23
  31. data/contrib/zstd/lib/compress/hist.c +26 -28
  32. data/contrib/zstd/lib/compress/hist.h +1 -1
  33. data/contrib/zstd/lib/compress/huf_compress.c +210 -95
  34. data/contrib/zstd/lib/compress/zstd_compress.c +1339 -409
  35. data/contrib/zstd/lib/compress/zstd_compress_internal.h +119 -41
  36. data/contrib/zstd/lib/compress/zstd_compress_literals.c +4 -4
  37. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +17 -3
  38. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +23 -19
  39. data/contrib/zstd/lib/compress/zstd_cwksp.h +60 -24
  40. data/contrib/zstd/lib/compress/zstd_double_fast.c +22 -22
  41. data/contrib/zstd/lib/compress/zstd_fast.c +19 -19
  42. data/contrib/zstd/lib/compress/zstd_lazy.c +351 -77
  43. data/contrib/zstd/lib/compress/zstd_lazy.h +20 -0
  44. data/contrib/zstd/lib/compress/zstd_ldm.c +59 -18
  45. data/contrib/zstd/lib/compress/zstd_ldm.h +6 -0
  46. data/contrib/zstd/lib/compress/zstd_opt.c +190 -45
  47. data/contrib/zstd/lib/compress/zstdmt_compress.c +74 -406
  48. data/contrib/zstd/lib/compress/zstdmt_compress.h +26 -108
  49. data/contrib/zstd/lib/decompress/huf_decompress.c +302 -200
  50. data/contrib/zstd/lib/decompress/zstd_ddict.c +8 -8
  51. data/contrib/zstd/lib/decompress/zstd_ddict.h +1 -1
  52. data/contrib/zstd/lib/decompress/zstd_decompress.c +125 -80
  53. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +145 -37
  54. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +5 -2
  55. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +11 -10
  56. data/contrib/zstd/lib/dictBuilder/cover.c +29 -20
  57. data/contrib/zstd/lib/dictBuilder/cover.h +1 -1
  58. data/contrib/zstd/lib/dictBuilder/fastcover.c +20 -19
  59. data/contrib/zstd/lib/dictBuilder/zdict.c +15 -16
  60. data/contrib/zstd/lib/dictBuilder/zdict.h +1 -1
  61. data/contrib/zstd/lib/legacy/zstd_v01.c +5 -1
  62. data/contrib/zstd/lib/legacy/zstd_v02.c +5 -1
  63. data/contrib/zstd/lib/legacy/zstd_v03.c +5 -1
  64. data/contrib/zstd/lib/legacy/zstd_v04.c +6 -2
  65. data/contrib/zstd/lib/legacy/zstd_v05.c +5 -1
  66. data/contrib/zstd/lib/legacy/zstd_v06.c +5 -1
  67. data/contrib/zstd/lib/legacy/zstd_v07.c +5 -1
  68. data/contrib/zstd/lib/libzstd.pc.in +3 -3
  69. data/contrib/zstd/lib/zstd.h +348 -47
  70. data/ext/extzstd.c +6 -0
  71. data/ext/extzstd.h +6 -0
  72. data/gemstub.rb +3 -21
  73. data/lib/extzstd.rb +0 -2
  74. data/lib/extzstd/version.rb +6 -1
  75. data/test/test_basic.rb +0 -5
  76. metadata +5 -4
@@ -10,9 +10,9 @@
10
10
 
11
11
 
12
12
  /* ====== Dependencies ======= */
13
- #include <stddef.h> /* size_t */
13
+ #include "zstd_deps.h" /* size_t */
14
14
  #include "debug.h" /* assert */
15
- #include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
15
+ #include "zstd_internal.h" /* ZSTD_customMalloc, ZSTD_customFree */
16
16
  #include "pool.h"
17
17
 
18
18
  /* ====== Compiler specifics ====== */
@@ -105,6 +105,10 @@ static void* POOL_thread(void* opaque) {
105
105
  assert(0); /* Unreachable */
106
106
  }
107
107
 
108
+ POOL_ctx* ZSTD_createThreadPool(size_t numThreads) {
109
+ return POOL_create (numThreads, 0);
110
+ }
111
+
108
112
  POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
109
113
  return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
110
114
  }
@@ -115,14 +119,14 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
115
119
  /* Check parameters */
116
120
  if (!numThreads) { return NULL; }
117
121
  /* Allocate the context and zero initialize */
118
- ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem);
122
+ ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem);
119
123
  if (!ctx) { return NULL; }
120
124
  /* Initialize the job queue.
121
125
  * It needs one extra space since one space is wasted to differentiate
122
126
  * empty and full queues.
123
127
  */
124
128
  ctx->queueSize = queueSize + 1;
125
- ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem);
129
+ ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem);
126
130
  ctx->queueHead = 0;
127
131
  ctx->queueTail = 0;
128
132
  ctx->numThreadsBusy = 0;
@@ -136,7 +140,7 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
136
140
  }
137
141
  ctx->shutdown = 0;
138
142
  /* Allocate space for the thread handles */
139
- ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
143
+ ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
140
144
  ctx->threadCapacity = 0;
141
145
  ctx->customMem = customMem;
142
146
  /* Check for errors */
@@ -179,12 +183,14 @@ void POOL_free(POOL_ctx *ctx) {
179
183
  ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
180
184
  ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
181
185
  ZSTD_pthread_cond_destroy(&ctx->queuePopCond);
182
- ZSTD_free(ctx->queue, ctx->customMem);
183
- ZSTD_free(ctx->threads, ctx->customMem);
184
- ZSTD_free(ctx, ctx->customMem);
186
+ ZSTD_customFree(ctx->queue, ctx->customMem);
187
+ ZSTD_customFree(ctx->threads, ctx->customMem);
188
+ ZSTD_customFree(ctx, ctx->customMem);
185
189
  }
186
190
 
187
-
191
+ void ZSTD_freeThreadPool (ZSTD_threadPool* pool) {
192
+ POOL_free (pool);
193
+ }
188
194
 
189
195
  size_t POOL_sizeof(POOL_ctx *ctx) {
190
196
  if (ctx==NULL) return 0; /* supports sizeof NULL */
@@ -203,11 +209,11 @@ static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads)
203
209
  return 0;
204
210
  }
205
211
  /* numThreads > threadCapacity */
206
- { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
212
+ { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
207
213
  if (!threadPool) return 1;
208
214
  /* replace existing thread pool */
209
- memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
210
- ZSTD_free(ctx->threads, ctx->customMem);
215
+ ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
216
+ ZSTD_customFree(ctx->threads, ctx->customMem);
211
217
  ctx->threads = threadPool;
212
218
  /* Initialize additional threads */
213
219
  { size_t threadId;
@@ -301,7 +307,7 @@ int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque)
301
307
  struct POOL_ctx_s {
302
308
  int dummy;
303
309
  };
304
- static POOL_ctx g_ctx;
310
+ static POOL_ctx g_poolCtx;
305
311
 
306
312
  POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
307
313
  return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
@@ -311,11 +317,11 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customM
311
317
  (void)numThreads;
312
318
  (void)queueSize;
313
319
  (void)customMem;
314
- return &g_ctx;
320
+ return &g_poolCtx;
315
321
  }
316
322
 
317
323
  void POOL_free(POOL_ctx* ctx) {
318
- assert(!ctx || ctx == &g_ctx);
324
+ assert(!ctx || ctx == &g_poolCtx);
319
325
  (void)ctx;
320
326
  }
321
327
 
@@ -337,7 +343,7 @@ int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) {
337
343
 
338
344
  size_t POOL_sizeof(POOL_ctx* ctx) {
339
345
  if (ctx==NULL) return 0; /* supports sizeof NULL */
340
- assert(ctx == &g_ctx);
346
+ assert(ctx == &g_poolCtx);
341
347
  return sizeof(*ctx);
342
348
  }
343
349
 
@@ -16,7 +16,7 @@ extern "C" {
16
16
  #endif
17
17
 
18
18
 
19
- #include <stddef.h> /* size_t */
19
+ #include "zstd_deps.h"
20
20
  #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
21
21
  #include "../zstd.h"
22
22
 
@@ -78,11 +78,12 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
78
78
 
79
79
  #if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
80
80
 
81
- #include <stdlib.h>
81
+ #define ZSTD_DEPS_NEED_MALLOC
82
+ #include "zstd_deps.h"
82
83
 
83
84
  int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
84
85
  {
85
- *mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
86
+ *mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
86
87
  if (!*mutex)
87
88
  return 1;
88
89
  return pthread_mutex_init(*mutex, attr);
@@ -94,14 +95,14 @@ int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
94
95
  return 0;
95
96
  {
96
97
  int const ret = pthread_mutex_destroy(*mutex);
97
- free(*mutex);
98
+ ZSTD_free(*mutex);
98
99
  return ret;
99
100
  }
100
101
  }
101
102
 
102
103
  int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
103
104
  {
104
- *cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
105
+ *cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
105
106
  if (!*cond)
106
107
  return 1;
107
108
  return pthread_cond_init(*cond, attr);
@@ -113,7 +114,7 @@ int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
113
114
  return 0;
114
115
  {
115
116
  int const ret = pthread_cond_destroy(*cond);
116
- free(*cond);
117
+ ZSTD_free(*cond);
117
118
  return ret;
118
119
  }
119
120
  }
@@ -77,14 +77,12 @@
77
77
  * Includes & Memory related functions
78
78
  ***************************************/
79
79
  /* Modify the local functions below should you wish to use some other memory routines */
80
- /* for malloc(), free() */
81
- #include <stdlib.h>
82
- #include <stddef.h> /* size_t */
83
- static void* XXH_malloc(size_t s) { return malloc(s); }
84
- static void XXH_free (void* p) { free(p); }
85
- /* for memcpy() */
86
- #include <string.h>
87
- static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
80
+ /* for ZSTD_malloc(), ZSTD_free() */
81
+ #define ZSTD_DEPS_NEED_MALLOC
82
+ #include "zstd_deps.h" /* size_t, ZSTD_malloc, ZSTD_free, ZSTD_memcpy */
83
+ static void* XXH_malloc(size_t s) { return ZSTD_malloc(s); }
84
+ static void XXH_free (void* p) { ZSTD_free(p); }
85
+ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return ZSTD_memcpy(dest,src,size); }
88
86
 
89
87
  #ifndef XXH_STATIC_LINKING_ONLY
90
88
  # define XXH_STATIC_LINKING_ONLY
@@ -95,49 +93,13 @@ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcp
95
93
  /* *************************************
96
94
  * Compiler Specific Options
97
95
  ***************************************/
98
- #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
99
- # define INLINE_KEYWORD inline
100
- #else
101
- # define INLINE_KEYWORD
102
- #endif
103
-
104
- #if defined(__GNUC__) || defined(__ICCARM__)
105
- # define FORCE_INLINE_ATTR __attribute__((always_inline))
106
- #elif defined(_MSC_VER)
107
- # define FORCE_INLINE_ATTR __forceinline
108
- #else
109
- # define FORCE_INLINE_ATTR
110
- #endif
111
-
112
- #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
113
-
114
-
115
- #ifdef _MSC_VER
116
- # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
117
- #endif
96
+ #include "compiler.h"
118
97
 
119
98
 
120
99
  /* *************************************
121
100
  * Basic Types
122
101
  ***************************************/
123
- #ifndef MEM_MODULE
124
- # define MEM_MODULE
125
- # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
126
- # include <stdint.h>
127
- typedef uint8_t BYTE;
128
- typedef uint16_t U16;
129
- typedef uint32_t U32;
130
- typedef int32_t S32;
131
- typedef uint64_t U64;
132
- # else
133
- typedef unsigned char BYTE;
134
- typedef unsigned short U16;
135
- typedef unsigned int U32;
136
- typedef signed int S32;
137
- typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */
138
- # endif
139
- #endif
140
-
102
+ #include "mem.h" /* BYTE, U32, U64, size_t */
141
103
 
142
104
  #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
143
105
 
@@ -163,14 +125,14 @@ static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
163
125
  static U32 XXH_read32(const void* memPtr)
164
126
  {
165
127
  U32 val;
166
- memcpy(&val, memPtr, sizeof(val));
128
+ ZSTD_memcpy(&val, memPtr, sizeof(val));
167
129
  return val;
168
130
  }
169
131
 
170
132
  static U64 XXH_read64(const void* memPtr)
171
133
  {
172
134
  U64 val;
173
- memcpy(&val, memPtr, sizeof(val));
135
+ ZSTD_memcpy(&val, memPtr, sizeof(val));
174
136
  return val;
175
137
  }
176
138
 
@@ -307,12 +269,12 @@ XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
307
269
  ****************************/
308
270
  XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState)
309
271
  {
310
- memcpy(dstState, srcState, sizeof(*dstState));
272
+ ZSTD_memcpy(dstState, srcState, sizeof(*dstState));
311
273
  }
312
274
 
313
275
  XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)
314
276
  {
315
- memcpy(dstState, srcState, sizeof(*dstState));
277
+ ZSTD_memcpy(dstState, srcState, sizeof(*dstState));
316
278
  }
317
279
 
318
280
 
@@ -554,12 +516,12 @@ XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
554
516
  XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)
555
517
  {
556
518
  XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
557
- memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
519
+ ZSTD_memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
558
520
  state.v1 = seed + PRIME32_1 + PRIME32_2;
559
521
  state.v2 = seed + PRIME32_2;
560
522
  state.v3 = seed + 0;
561
523
  state.v4 = seed - PRIME32_1;
562
- memcpy(statePtr, &state, sizeof(state));
524
+ ZSTD_memcpy(statePtr, &state, sizeof(state));
563
525
  return XXH_OK;
564
526
  }
565
527
 
@@ -567,12 +529,12 @@ XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int s
567
529
  XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
568
530
  {
569
531
  XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
570
- memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
532
+ ZSTD_memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
571
533
  state.v1 = seed + PRIME64_1 + PRIME64_2;
572
534
  state.v2 = seed + PRIME64_2;
573
535
  state.v3 = seed + 0;
574
536
  state.v4 = seed - PRIME64_1;
575
- memcpy(statePtr, &state, sizeof(state));
537
+ ZSTD_memcpy(statePtr, &state, sizeof(state));
576
538
  return XXH_OK;
577
539
  }
578
540
 
@@ -843,14 +805,14 @@ XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t
843
805
  {
844
806
  XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
845
807
  if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
846
- memcpy(dst, &hash, sizeof(*dst));
808
+ ZSTD_memcpy(dst, &hash, sizeof(*dst));
847
809
  }
848
810
 
849
811
  XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
850
812
  {
851
813
  XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
852
814
  if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
853
- memcpy(dst, &hash, sizeof(*dst));
815
+ ZSTD_memcpy(dst, &hash, sizeof(*dst));
854
816
  }
855
817
 
856
818
  XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
@@ -55,7 +55,7 @@ extern "C" {
55
55
  /* ****************************
56
56
  * Definitions
57
57
  ******************************/
58
- #include <stddef.h> /* size_t */
58
+ #include "zstd_deps.h"
59
59
  typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
60
60
 
61
61
 
@@ -13,8 +13,8 @@
13
13
  /*-*************************************
14
14
  * Dependencies
15
15
  ***************************************/
16
- #include <stdlib.h> /* malloc, calloc, free */
17
- #include <string.h> /* memset */
16
+ #define ZSTD_DEPS_NEED_MALLOC
17
+ #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
18
18
  #include "error_private.h"
19
19
  #include "zstd_internal.h"
20
20
 
@@ -53,31 +53,31 @@ const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString
53
53
  /*=**************************************************************
54
54
  * Custom allocator
55
55
  ****************************************************************/
56
- void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
56
+ void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
57
57
  {
58
58
  if (customMem.customAlloc)
59
59
  return customMem.customAlloc(customMem.opaque, size);
60
- return malloc(size);
60
+ return ZSTD_malloc(size);
61
61
  }
62
62
 
63
- void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
63
+ void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
64
64
  {
65
65
  if (customMem.customAlloc) {
66
66
  /* calloc implemented as malloc+memset;
67
67
  * not as efficient as calloc, but next best guess for custom malloc */
68
68
  void* const ptr = customMem.customAlloc(customMem.opaque, size);
69
- memset(ptr, 0, size);
69
+ ZSTD_memset(ptr, 0, size);
70
70
  return ptr;
71
71
  }
72
- return calloc(1, size);
72
+ return ZSTD_calloc(1, size);
73
73
  }
74
74
 
75
- void ZSTD_free(void* ptr, ZSTD_customMem customMem)
75
+ void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
76
76
  {
77
77
  if (ptr!=NULL) {
78
78
  if (customMem.customFree)
79
79
  customMem.customFree(customMem.opaque, ptr);
80
80
  else
81
- free(ptr);
81
+ ZSTD_free(ptr);
82
82
  }
83
83
  }
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright (c) 2016-2020, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ /* This file provides common libc dependencies that zstd requires.
12
+ * The purpose is to allow replacing this file with a custom implementation
13
+ * to compile zstd without libc support.
14
+ */
15
+
16
+ /* Need:
17
+ * NULL
18
+ * INT_MAX
19
+ * UINT_MAX
20
+ * ZSTD_memcpy()
21
+ * ZSTD_memset()
22
+ * ZSTD_memmove()
23
+ */
24
+ #ifndef ZSTD_DEPS_COMMON
25
+ #define ZSTD_DEPS_COMMON
26
+
27
+ #include <limits.h>
28
+ #include <stddef.h>
29
+ #include <string.h>
30
+
31
+ #if defined(__GNUC__) && __GNUC__ >= 4
32
+ # define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
33
+ # define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
34
+ # define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
35
+ #else
36
+ # define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
37
+ # define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
38
+ # define ZSTD_memset(p,v,l) memset((p),(v),(l))
39
+ #endif
40
+
41
+ #endif /* ZSTD_DEPS_COMMON */
42
+
43
+ /* Need:
44
+ * ZSTD_malloc()
45
+ * ZSTD_free()
46
+ * ZSTD_calloc()
47
+ */
48
+ #ifdef ZSTD_DEPS_NEED_MALLOC
49
+ #ifndef ZSTD_DEPS_MALLOC
50
+ #define ZSTD_DEPS_MALLOC
51
+
52
+ #include <stdlib.h>
53
+
54
+ #define ZSTD_malloc(s) malloc(s)
55
+ #define ZSTD_calloc(n,s) calloc((n), (s))
56
+ #define ZSTD_free(p) free((p))
57
+
58
+ #endif /* ZSTD_DEPS_MALLOC */
59
+ #endif /* ZSTD_DEPS_NEED_MALLOC */
60
+
61
+ /*
62
+ * Provides 64-bit math support.
63
+ * Need:
64
+ * U64 ZSTD_div64(U64 dividend, U32 divisor)
65
+ */
66
+ #ifdef ZSTD_DEPS_NEED_MATH64
67
+ #ifndef ZSTD_DEPS_MATH64
68
+ #define ZSTD_DEPS_MATH64
69
+
70
+ #define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
71
+
72
+ #endif /* ZSTD_DEPS_MATH64 */
73
+ #endif /* ZSTD_DEPS_NEED_MATH64 */
74
+
75
+ /* Need:
76
+ * assert()
77
+ */
78
+ #ifdef ZSTD_DEPS_NEED_ASSERT
79
+ #ifndef ZSTD_DEPS_ASSERT
80
+ #define ZSTD_DEPS_ASSERT
81
+
82
+ #include <assert.h>
83
+
84
+ #endif /* ZSTD_DEPS_ASSERT */
85
+ #endif /* ZSTD_DEPS_NEED_ASSERT */
86
+
87
+ /* Need:
88
+ * ZSTD_DEBUG_PRINT()
89
+ */
90
+ #ifdef ZSTD_DEPS_NEED_IO
91
+ #ifndef ZSTD_DEPS_IO
92
+ #define ZSTD_DEPS_IO
93
+
94
+ #include <stdio.h>
95
+ #define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
96
+
97
+ #endif /* ZSTD_DEPS_IO */
98
+ #endif /* ZSTD_DEPS_NEED_IO */
99
+
100
+ /* Only requested when <stdint.h> is known to be present.
101
+ * Need:
102
+ * intptr_t
103
+ */
104
+ #ifdef ZSTD_DEPS_NEED_STDINT
105
+ #ifndef ZSTD_DEPS_STDINT
106
+ #define ZSTD_DEPS_STDINT
107
+
108
+ #include <stdint.h>
109
+
110
+ #endif /* ZSTD_DEPS_STDINT */
111
+ #endif /* ZSTD_DEPS_NEED_STDINT */