@@ -350,15 +350,16 @@ typedef struct {
350
350
int totalCCtx ;
351
351
int availCCtx ;
352
352
ZSTD_customMem cMem ;
353
- ZSTD_CCtx * cctx [ 1 ]; /* variable size */
353
+ ZSTD_CCtx * * cctxs ;
354
354
} ZSTDMT_CCtxPool ;
355
355
356
- /* note : all CCtx borrowed from the pool should be released back to the pool _before_ freeing the pool */
356
+ /* note : all CCtx borrowed from the pool must be reverted back to the pool _before_ freeing the pool */
357
357
static void ZSTDMT_freeCCtxPool (ZSTDMT_CCtxPool * pool )
358
358
{
359
359
int cid ;
360
360
for (cid = 0 ; cid < pool -> totalCCtx ; cid ++ )
361
- ZSTD_freeCCtx (pool -> cctx [cid ]); /* note : compatible with free on NULL */
361
+ ZSTD_freeCCtx (pool -> cctxs [cid ]); /* note : compatible with free on NULL */
362
+ ZSTD_customFree (pool -> cctxs , pool -> cMem );
362
363
ZSTD_pthread_mutex_destroy (& pool -> poolMutex );
363
364
ZSTD_customFree (pool , pool -> cMem );
364
365
}
@@ -373,14 +374,19 @@ static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(int nbWorkers,
373
374
assert (nbWorkers > 0 );
374
375
if (!cctxPool ) return NULL ;
375
376
if (ZSTD_pthread_mutex_init (& cctxPool -> poolMutex , NULL )) {
377
+ ZSTDMT_freeCCtxPool (cctxPool );
378
+ return NULL ;
379
+ }
380
+ cctxPool -> totalCCtx = nbWorkers ;
381
+ cctxPool -> cctxs = (ZSTD_CCtx * * )ZSTD_customCalloc (nbWorkers * sizeof (ZSTD_CCtx * ), cMem );
382
+ if (!cctxPool -> cctxs ) {
376
383
ZSTD_customFree (cctxPool , cMem );
377
384
return NULL ;
378
385
}
379
386
cctxPool -> cMem = cMem ;
380
- cctxPool -> totalCCtx = nbWorkers ;
387
+ cctxPool -> cctxs [0 ] = ZSTD_createCCtx_advanced (cMem );
388
+ if (!cctxPool -> cctxs [0 ]) { ZSTDMT_freeCCtxPool (cctxPool ); return NULL ; }
381
389
cctxPool -> availCCtx = 1 ; /* at least one cctx for single-thread mode */
382
- cctxPool -> cctx [0 ] = ZSTD_createCCtx_advanced (cMem );
383
- if (!cctxPool -> cctx [0 ]) { ZSTDMT_freeCCtxPool (cctxPool ); return NULL ; }
384
390
DEBUGLOG (3 , "cctxPool created, with %u workers" , nbWorkers );
385
391
return cctxPool ;
386
392
}
@@ -404,14 +410,15 @@ static size_t ZSTDMT_sizeof_CCtxPool(ZSTDMT_CCtxPool* cctxPool)
404
410
{ unsigned const nbWorkers = cctxPool -> totalCCtx ;
405
411
size_t const poolSize = sizeof (* cctxPool )
406
412
+ (nbWorkers - 1 ) * sizeof (ZSTD_CCtx * );
407
- unsigned u ;
413
+ size_t const arraySize = cctxPool -> totalCCtx * sizeof ( ZSTD_CCtx * ) ;
408
414
size_t totalCCtxSize = 0 ;
415
+ unsigned u ;
409
416
for (u = 0 ; u < nbWorkers ; u ++ ) {
410
- totalCCtxSize += ZSTD_sizeof_CCtx (cctxPool -> cctx [u ]);
417
+ totalCCtxSize += ZSTD_sizeof_CCtx (cctxPool -> cctxs [u ]);
411
418
}
412
419
ZSTD_pthread_mutex_unlock (& cctxPool -> poolMutex );
413
420
assert (nbWorkers > 0 );
414
- return poolSize + totalCCtxSize ;
421
+ return poolSize + arraySize + totalCCtxSize ;
415
422
}
416
423
}
417
424
@@ -421,7 +428,7 @@ static ZSTD_CCtx* ZSTDMT_getCCtx(ZSTDMT_CCtxPool* cctxPool)
421
428
ZSTD_pthread_mutex_lock (& cctxPool -> poolMutex );
422
429
if (cctxPool -> availCCtx ) {
423
430
cctxPool -> availCCtx -- ;
424
- { ZSTD_CCtx * const cctx = cctxPool -> cctx [cctxPool -> availCCtx ];
431
+ { ZSTD_CCtx * const cctx = cctxPool -> cctxs [cctxPool -> availCCtx ];
425
432
ZSTD_pthread_mutex_unlock (& cctxPool -> poolMutex );
426
433
return cctx ;
427
434
} }
@@ -435,7 +442,7 @@ static void ZSTDMT_releaseCCtx(ZSTDMT_CCtxPool* pool, ZSTD_CCtx* cctx)
435
442
if (cctx == NULL ) return ; /* compatibility with release on NULL */
436
443
ZSTD_pthread_mutex_lock (& pool -> poolMutex );
437
444
if (pool -> availCCtx < pool -> totalCCtx )
438
- pool -> cctx [pool -> availCCtx ++ ] = cctx ;
445
+ pool -> cctxs [pool -> availCCtx ++ ] = cctx ;
439
446
else {
440
447
/* pool overflow : should not happen, since totalCCtx==nbWorkers */
441
448
DEBUGLOG (4 , "CCtx pool overflow : free cctx" );
0 commit comments