From 0b02efdf01500d477ba313b28accbf180d609361 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Tue, 28 May 2019 15:14:20 +0200 Subject: [PATCH] pool/thread: introduce simpler way to grow thread pool --- src/util-pool-thread.c | 54 ++++++++++++++++++++++++++++++++++++++++++ src/util-pool-thread.h | 6 +++++ 2 files changed, 60 insertions(+) diff --git a/src/util-pool-thread.c b/src/util-pool-thread.c index e531e8935c..438f95aa7d 100644 --- a/src/util-pool-thread.c +++ b/src/util-pool-thread.c @@ -86,6 +86,60 @@ error: return NULL; } +/** \brief expand pool by one for a new thread + * \retval -1 or pool thread id + */ +int PoolThreadExpand(PoolThread *pt) +{ + if (pt == NULL || pt->array == NULL || pt->size == 0) { + SCLogError(SC_ERR_POOL_INIT, "pool grow failed"); + return -1; + } + + size_t newsize = pt->size + 1; + SCLogDebug("newsize %"PRIuMAX, (uintmax_t)newsize); + + void *ptmp = SCRealloc(pt->array, (newsize * sizeof(PoolThreadElement))); + if (ptmp == NULL) { + SCFree(pt->array); + pt->array = NULL; + SCLogError(SC_ERR_POOL_INIT, "pool grow failed"); + return -1; + } + pt->array = ptmp; + pt->size = newsize; + + /* copy settings from first thread that registered the pool */ + Pool settings; + memset(&settings, 0x0, sizeof(settings)); + PoolThreadElement *e = &pt->array[0]; + SCMutexLock(&e->lock); + settings.max_buckets = e->pool->max_buckets; + settings.preallocated = e->pool->preallocated; + settings.elt_size = e->pool->elt_size; + settings.Alloc = e->pool->Alloc; + settings.Init = e->pool->Init; + settings.InitData = e->pool->InitData; + settings.Cleanup = e->pool->Cleanup; + settings.Free = e->pool->Free; + SCMutexUnlock(&e->lock); + + e = &pt->array[newsize - 1]; + memset(e, 0x00, sizeof(*e)); + SCMutexInit(&e->lock, NULL); + SCMutexLock(&e->lock); + e->pool = PoolInit(settings.max_buckets, settings.preallocated, + settings.elt_size, settings.Alloc, settings.Init, settings.InitData, + settings.Cleanup, settings.Free); + SCMutexUnlock(&e->lock); + if (e->pool == NULL) { + SCLogError(SC_ERR_POOL_INIT, "pool grow failed"); + return -1; + } + + return (int)(newsize - 1); +} + /** * */ diff --git a/src/util-pool-thread.h b/src/util-pool-thread.h index d43d4e8c9f..26d1a5a18e 100644 --- a/src/util-pool-thread.h +++ b/src/util-pool-thread.h @@ -70,6 +70,12 @@ PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, u * \retval r id of new entry on succes, -1 on error */ int PoolThreadGrow(PoolThread *pt, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int (*Init)(void *, void *), void *InitData, void (*Cleanup)(void *), void (*Free)(void *)); +/** \brief grow a thread pool by one + * \note copies settings from initial PoolThreadInit() call + * \param pt thread pool to grow + * \retval r id of new entry on succes, -1 on error */ +int PoolThreadExpand(PoolThread *pt); + /** \brief destroy the thread pool * \note wrapper around PoolFree() * \param pt thread pool */