Merge pull request #817 from terrelln/pool-custom-alloc

[pool] Accept custom allocators
This commit is contained in:
Yann Collet
2017-08-29 13:05:39 -07:00
committed by GitHub
7 changed files with 48 additions and 21 deletions
+2 -3
View File
@@ -48,10 +48,9 @@ typedef ZSTD_ErrorCode ERR_enum;
/*-**************************************** /*-****************************************
* Error codes handling * Error codes handling
******************************************/ ******************************************/
#ifdef ERROR
#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ #undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
#endif #define ERROR(name) ZSTD_ERROR(name)
#define ERROR(name) ((size_t)-PREFIX(name)) #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
+22 -8
View File
@@ -30,6 +30,7 @@ typedef struct POOL_job_s {
} POOL_job; } POOL_job;
struct POOL_ctx_s { struct POOL_ctx_s {
ZSTD_customMem customMem;
/* Keep track of the threads */ /* Keep track of the threads */
pthread_t *threads; pthread_t *threads;
size_t numThreads; size_t numThreads;
@@ -98,11 +99,15 @@ static void* POOL_thread(void* opaque) {
} }
POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) { POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) {
return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
}
POOL_ctx *POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
POOL_ctx *ctx; POOL_ctx *ctx;
/* Check the parameters */ /* Check the parameters */
if (!numThreads) { return NULL; } if (!numThreads) { return NULL; }
/* Allocate the context and zero initialize */ /* Allocate the context and zero initialize */
ctx = (POOL_ctx *)calloc(1, sizeof(POOL_ctx)); ctx = (POOL_ctx *)ZSTD_calloc(sizeof(POOL_ctx), customMem);
if (!ctx) { return NULL; } if (!ctx) { return NULL; }
/* Initialize the job queue. /* Initialize the job queue.
* It needs one extra space since one space is wasted to differentiate empty * It needs one extra space since one space is wasted to differentiate empty
@@ -119,8 +124,9 @@ POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) {
(void)pthread_cond_init(&ctx->queuePopCond, NULL); (void)pthread_cond_init(&ctx->queuePopCond, NULL);
ctx->shutdown = 0; ctx->shutdown = 0;
/* Allocate space for the thread handles */ /* Allocate space for the thread handles */
ctx->threads = (pthread_t*)malloc(numThreads * sizeof(pthread_t)); ctx->threads = (pthread_t*)ZSTD_malloc(numThreads * sizeof(pthread_t), customMem);
ctx->numThreads = 0; ctx->numThreads = 0;
ctx->customMem = customMem;
/* Check for errors */ /* Check for errors */
if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; }
/* Initialize the threads */ /* Initialize the threads */
@@ -160,9 +166,9 @@ void POOL_free(POOL_ctx *ctx) {
pthread_mutex_destroy(&ctx->queueMutex); pthread_mutex_destroy(&ctx->queueMutex);
pthread_cond_destroy(&ctx->queuePushCond); pthread_cond_destroy(&ctx->queuePushCond);
pthread_cond_destroy(&ctx->queuePopCond); pthread_cond_destroy(&ctx->queuePopCond);
if (ctx->queue) free(ctx->queue); ZSTD_free(ctx->queue, ctx->customMem);
if (ctx->threads) free(ctx->threads); ZSTD_free(ctx->threads, ctx->customMem);
free(ctx); ZSTD_free(ctx, ctx->customMem);
} }
size_t POOL_sizeof(POOL_ctx *ctx) { size_t POOL_sizeof(POOL_ctx *ctx) {
@@ -214,17 +220,24 @@ void POOL_add(void* ctxVoid, POOL_function function, void *opaque) {
/* We don't need any data, but if it is empty malloc() might return NULL. */ /* We don't need any data, but if it is empty malloc() might return NULL. */
struct POOL_ctx_s { struct POOL_ctx_s {
int data; int dummy;
}; };
static POOL_ctx g_ctx;
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) { POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
}
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
(void)numThreads; (void)numThreads;
(void)queueSize; (void)queueSize;
return (POOL_ctx*)malloc(sizeof(POOL_ctx)); (void)customMem;
return &g_ctx;
} }
void POOL_free(POOL_ctx* ctx) { void POOL_free(POOL_ctx* ctx) {
free(ctx); assert(ctx == &g_ctx);
(void)ctx;
} }
void POOL_add(void* ctx, POOL_function function, void* opaque) { void POOL_add(void* ctx, POOL_function function, void* opaque) {
@@ -234,6 +247,7 @@ void POOL_add(void* ctx, POOL_function function, void* opaque) {
size_t POOL_sizeof(POOL_ctx* ctx) { size_t POOL_sizeof(POOL_ctx* ctx) {
if (ctx==NULL) return 0; /* supports sizeof NULL */ if (ctx==NULL) return 0; /* supports sizeof NULL */
assert(ctx == &g_ctx);
return sizeof(*ctx); return sizeof(*ctx);
} }
+3
View File
@@ -16,6 +16,7 @@ extern "C" {
#include <stddef.h> /* size_t */ #include <stddef.h> /* size_t */
#include "zstd_internal.h" /* ZSTD_customMem */
typedef struct POOL_ctx_s POOL_ctx; typedef struct POOL_ctx_s POOL_ctx;
@@ -27,6 +28,8 @@ typedef struct POOL_ctx_s POOL_ctx;
*/ */
POOL_ctx *POOL_create(size_t numThreads, size_t queueSize); POOL_ctx *POOL_create(size_t numThreads, size_t queueSize);
POOL_ctx *POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem);
/*! POOL_free() : /*! POOL_free() :
Free a thread pool returned by POOL_create(). Free a thread pool returned by POOL_create().
*/ */
+4
View File
@@ -37,7 +37,11 @@ extern "C" {
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
#endif #endif
#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
#include <windows.h> #include <windows.h>
#undef ERROR
#define ERROR(name) ZSTD_ERROR(name)
/* mutex */ /* mutex */
#define pthread_mutex_t CRITICAL_SECTION #define pthread_mutex_t CRITICAL_SECTION
+1 -2
View File
@@ -15,8 +15,7 @@
#include <stdlib.h> /* malloc, calloc, free */ #include <stdlib.h> /* malloc, calloc, free */
#include <string.h> /* memset */ #include <string.h> /* memset */
#include "error_private.h" #include "error_private.h"
#define ZSTD_STATIC_LINKING_ONLY #include "zstd_internal.h"
#include "zstd.h"
/*-**************************************** /*-****************************************
+8
View File
@@ -29,6 +29,11 @@
#include "xxhash.h" /* XXH_reset, update, digest */ #include "xxhash.h" /* XXH_reset, update, digest */
#if defined (__cplusplus)
extern "C" {
#endif
/*-************************************* /*-*************************************
* Debug * Debug
***************************************/ ***************************************/
@@ -334,5 +339,8 @@ typedef struct {
size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
blockProperties_t* bpPtr); blockProperties_t* bpPtr);
#if defined (__cplusplus)
}
#endif
#endif /* ZSTD_CCOMMON_H_MODULE */ #endif /* ZSTD_CCOMMON_H_MODULE */
+1 -1
View File
@@ -426,7 +426,7 @@ ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbThreads, ZSTD_customMem cMem)
mtctx->allJobsCompleted = 1; mtctx->allJobsCompleted = 1;
mtctx->sectionSize = 0; mtctx->sectionSize = 0;
mtctx->overlapLog = ZSTDMT_OVERLAPLOG_DEFAULT; mtctx->overlapLog = ZSTDMT_OVERLAPLOG_DEFAULT;
mtctx->factory = POOL_create(nbThreads, 0); mtctx->factory = POOL_create_advanced(nbThreads, 0, cMem);
mtctx->jobs = ZSTDMT_allocJobsTable(&nbJobs, cMem); mtctx->jobs = ZSTDMT_allocJobsTable(&nbJobs, cMem);
mtctx->jobIDMask = nbJobs - 1; mtctx->jobIDMask = nbJobs - 1;
mtctx->bufPool = ZSTDMT_createBufferPool(nbThreads, cMem); mtctx->bufPool = ZSTDMT_createBufferPool(nbThreads, cMem);