• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.1
/src/util-pool-thread.c
1
/* Copyright (C) 2013 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \defgroup utilpool Pool
20
 *
21
 * @{
22
 */
23

24
/**
25
 * \file
26
 *
27
 * \author Victor Julien <victor@inliniac.net>
28
 *
29
 * Pool utility functions
30
 */
31

32
#include "suricata-common.h"
33
#include "util-pool.h"
34
#include "util-pool-thread.h"
35
#include "util-unittest.h"
36
#include "util-debug.h"
37
#include "util-validate.h"
38

39
/**
40
 *  \brief per thread Pool, initialization function
41
 *  \param thread number of threads this is for. Can start with 1 and be expanded.
42
 *  Other params are as for PoolInit()
43
 */
44
PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size,
45
        uint32_t elt_size,  void *(*Alloc)(void), int (*Init)(void *, void *),
46
        void *InitData,  void (*Cleanup)(void *), void (*Free)(void *))
47
{
2✔
48
    sc_errno = SC_OK;
2✔
49

50
    if (threads <= 0) {
2✔
51
        SCLogDebug("error");
×
52
        sc_errno = SC_EINVAL;
×
53
        return NULL;
×
54
    }
×
55

56
    PoolThread *pt = SCCalloc(1, sizeof(*pt));
2✔
57
    if (unlikely(pt == NULL)) {
2✔
58
        SCLogDebug("memory alloc error");
×
59
        sc_errno = SC_ENOMEM;
×
60
        goto error;
×
61
    }
×
62

63
    SCLogDebug("size %d", threads);
2✔
64
    pt->array = SCMalloc(threads * sizeof(PoolThreadElement));
2✔
65
    if (pt->array == NULL) {
2✔
66
        SCLogDebug("memory alloc error");
×
67
        sc_errno = SC_ENOMEM;
×
68
        goto error;
×
69
    }
×
70
    pt->size = threads;
2✔
71

72
    for (int i = 0; i < threads; i++) {
4✔
73
        PoolThreadElement *e = &pt->array[i];
2✔
74

75
        SCMutexInit(&e->lock, NULL);
2✔
76
        SCMutexLock(&e->lock);
2✔
77
//        SCLogDebug("size %u prealloc_size %u elt_size %u Alloc %p Init %p InitData %p Cleanup %p Free %p",
78
//                size, prealloc_size, elt_size,
79
//                Alloc, Init, InitData, Cleanup, Free);
80
        e->pool = PoolInit(size, prealloc_size, elt_size, Alloc, Init, InitData, Cleanup, Free);
2✔
81
        SCMutexUnlock(&e->lock);
2✔
82
        if (e->pool == NULL) {
2✔
83
            SCLogDebug("error");
×
84
            goto error;
×
85
        }
×
86
    }
2✔
87

88
    return pt;
2✔
89
error:
×
90
    if (pt != NULL)
×
91
        PoolThreadFree(pt);
×
92
    return NULL;
×
93
}
2✔
94

95
/** \brief expand pool by one for a new thread
96
 *  \retval -1 or pool thread id
97
 */
98
int PoolThreadExpand(PoolThread *pt)
UNCOV
99
{
×
UNCOV
100
    if (pt == NULL || pt->array == NULL || pt->size == 0) {
×
101
        SCLogError("pool grow failed");
×
102
        return -1;
×
103
    }
×
104

UNCOV
105
    size_t newsize = pt->size + 1;
×
UNCOV
106
    SCLogDebug("newsize %"PRIuMAX, (uintmax_t)newsize);
×
107

UNCOV
108
    void *ptmp = SCRealloc(pt->array, (newsize * sizeof(PoolThreadElement)));
×
UNCOV
109
    if (ptmp == NULL) {
×
110
        SCFree(pt->array);
×
111
        pt->array = NULL;
×
112
        SCLogError("pool grow failed");
×
113
        return -1;
×
114
    }
×
UNCOV
115
    pt->array = ptmp;
×
UNCOV
116
    pt->size = newsize;
×
117

118
    /* copy settings from first thread that registered the pool */
UNCOV
119
    Pool settings;
×
UNCOV
120
    memset(&settings, 0x0, sizeof(settings));
×
UNCOV
121
    PoolThreadElement *e = &pt->array[0];
×
UNCOV
122
    SCMutexLock(&e->lock);
×
UNCOV
123
    settings.max_buckets = e->pool->max_buckets;
×
UNCOV
124
    settings.preallocated = e->pool->preallocated;
×
UNCOV
125
    settings.elt_size = e->pool->elt_size;
×
UNCOV
126
    settings.Alloc = e->pool->Alloc;
×
UNCOV
127
    settings.Init = e->pool->Init;
×
UNCOV
128
    settings.InitData = e->pool->InitData;
×
UNCOV
129
    settings.Cleanup = e->pool->Cleanup;
×
UNCOV
130
    settings.Free = e->pool->Free;
×
UNCOV
131
    SCMutexUnlock(&e->lock);
×
132

UNCOV
133
    e = &pt->array[newsize - 1];
×
UNCOV
134
    memset(e, 0x00, sizeof(*e));
×
UNCOV
135
    SCMutexInit(&e->lock, NULL);
×
UNCOV
136
    SCMutexLock(&e->lock);
×
UNCOV
137
    e->pool = PoolInit(settings.max_buckets, settings.preallocated,
×
UNCOV
138
            settings.elt_size, settings.Alloc, settings.Init, settings.InitData,
×
UNCOV
139
            settings.Cleanup, settings.Free);
×
UNCOV
140
    SCMutexUnlock(&e->lock);
×
UNCOV
141
    if (e->pool == NULL) {
×
142
        SCLogError("pool grow failed");
×
143
        return -1;
×
144
    }
×
145

UNCOV
146
    return (int)(newsize - 1);
×
UNCOV
147
}
×
148

149
int PoolThreadSize(PoolThread *pt)
150
{
×
151
    if (pt == NULL)
×
152
        return -1;
×
153
    return (int)pt->size;
×
154
}
×
155

156
void PoolThreadFree(PoolThread *pt)
UNCOV
157
{
×
UNCOV
158
    if (pt == NULL)
×
159
        return;
×
160

UNCOV
161
    if (pt->array != NULL) {
×
UNCOV
162
        for (int i = 0; i < (int)pt->size; i++) {
×
UNCOV
163
            PoolThreadElement *e = &pt->array[i];
×
UNCOV
164
            SCMutexLock(&e->lock);
×
UNCOV
165
            PoolFree(e->pool);
×
UNCOV
166
            SCMutexUnlock(&e->lock);
×
UNCOV
167
            SCMutexDestroy(&e->lock);
×
UNCOV
168
        }
×
UNCOV
169
        SCFree(pt->array);
×
UNCOV
170
    }
×
UNCOV
171
    SCFree(pt);
×
UNCOV
172
}
×
173

174
void *PoolThreadGetById(PoolThread *pt, uint16_t id)
175
{
367,576✔
176
    void *data = NULL;
367,576✔
177

178
    if (pt == NULL || id >= pt->size)
367,576✔
179
        return NULL;
×
180

181
    PoolThreadElement *e = &pt->array[id];
367,576✔
182
    SCMutexLock(&e->lock);
367,576✔
183
    data = PoolGet(e->pool);
367,576✔
184
    SCMutexUnlock(&e->lock);
367,576✔
185
    if (data) {
367,576✔
186
        PoolThreadId *did = data;
367,576✔
187
        *did = id;
367,576✔
188
    }
367,576✔
189

190
    return data;
367,576✔
191
}
367,576✔
192

193
void PoolThreadReturn(PoolThread *pt, void *data)
UNCOV
194
{
×
UNCOV
195
    PoolThreadId *id = data;
×
196

UNCOV
197
    if (pt == NULL || *id >= pt->size)
×
198
        return;
×
199

UNCOV
200
    SCLogDebug("returning to id %u", *id);
×
201

UNCOV
202
    PoolThreadElement *e = &pt->array[*id];
×
UNCOV
203
    SCMutexLock(&e->lock);
×
UNCOV
204
    PoolReturn(e->pool, data);
×
UNCOV
205
    SCMutexUnlock(&e->lock);
×
UNCOV
206
}
×
207

208
void PoolThreadLock(PoolThread *pt, PoolThreadId id)
209
{
5,741✔
210
    DEBUG_VALIDATE_BUG_ON(pt == NULL || id >= pt->size);
5,741✔
211
    PoolThreadElement *e = &pt->array[id];
5,741✔
212
    SCMutexLock(&e->lock);
5,741✔
213
}
5,741✔
214

215
void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data)
216
{
367,424✔
217
    DEBUG_VALIDATE_BUG_ON(pt == NULL || id >= pt->size);
367,424✔
218
    PoolThreadElement *e = &pt->array[id];
367,424✔
219
    PoolReturn(e->pool, data);
367,424✔
220
}
367,424✔
221

222
void PoolThreadUnlock(PoolThread *pt, PoolThreadId id)
223
{
5,741✔
224
    DEBUG_VALIDATE_BUG_ON(pt == NULL || id >= pt->size);
5,741✔
225
    PoolThreadElement *e = &pt->array[id];
5,741✔
226
    SCMutexUnlock(&e->lock);
5,741✔
227
}
5,741✔
228

229
#ifdef UNITTESTS
230
struct PoolThreadTestData {
231
    PoolThreadId res;
232
    int abc;
233
};
234

235
static void *PoolThreadTestAlloc(void)
236
{
237
    void *data = SCMalloc(sizeof(struct PoolThreadTestData));
238
    return data;
239
}
240

241
static
242
int PoolThreadTestInit(void *data, void *allocdata)
243
{
244
    if (!data)
245
        return 0;
246

247
    memset(data,0x00,sizeof(allocdata));
248
    struct PoolThreadTestData *pdata = data;
249
    pdata->abc = *(int *)allocdata;
250
    return 1;
251
}
252

253
static
254
void PoolThreadTestFree(void *data)
255
{
256
}
257

258
static int PoolThreadTestInit01(void)
259
{
260
    PoolThread *pt = PoolThreadInit(4, /* threads */
261
                                    10, 5, 10, PoolThreadTestAlloc,
262
                                    NULL, NULL, NULL, NULL);
263
    FAIL_IF(pt == NULL);
264
    PoolThreadFree(pt);
265
    PASS;
266
}
267

268
static int PoolThreadTestInit02(void)
269
{
270
    int i = 123;
271

272
    PoolThread *pt = PoolThreadInit(4, /* threads */
273
                                    10, 5, 10,
274
                                    PoolThreadTestAlloc, PoolThreadTestInit,
275
                                    &i, PoolThreadTestFree, NULL);
276
    FAIL_IF(pt == NULL);
277
    PoolThreadFree(pt);
278
    PASS;
279
}
280

281
static int PoolThreadTestGet01(void)
282
{
283
    PoolThread *pt = PoolThreadInit(4, /* threads */
284
                                    10, 5, 10, PoolThreadTestAlloc,
285
                                    NULL, NULL, NULL, NULL);
286
    FAIL_IF(pt == NULL);
287

288
    void *data = PoolThreadGetById(pt, 3);
289
    FAIL_IF_NULL(data);
290

291
    struct PoolThreadTestData *pdata = data;
292
    FAIL_IF(pdata->res != 3);
293

294
    PoolThreadFree(pt);
295
    PASS;
296
}
297

298
static int PoolThreadTestGet02(void)
299
{
300
    int i = 123;
301

302
    PoolThread *pt = PoolThreadInit(4, /* threads */
303
                                    10, 5, 10, PoolThreadTestAlloc,
304
                                    PoolThreadTestInit, &i, PoolThreadTestFree, NULL);
305
    FAIL_IF_NULL(pt);
306

307
    void *data = PoolThreadGetById(pt, 3);
308
    FAIL_IF_NULL(data);
309

310
    struct PoolThreadTestData *pdata = data;
311
    FAIL_IF_NOT (pdata->res == 3);
312

313
    FAIL_IF_NOT (pdata->abc == 123);
314

315
    PoolThreadFree(pt);
316
    PASS;
317
}
318

319
static int PoolThreadTestReturn01(void)
320
{
321
    int i = 123;
322

323
    PoolThread *pt = PoolThreadInit(4, /* threads */
324
                                    10, 5, 10, PoolThreadTestAlloc,
325
                                    PoolThreadTestInit, &i, PoolThreadTestFree, NULL);
326
    FAIL_IF_NULL(pt);
327

328
    void *data = PoolThreadGetById(pt, 3);
329
    FAIL_IF_NULL(data);
330

331
    struct PoolThreadTestData *pdata = data;
332
    FAIL_IF_NOT (pdata->res == 3);
333

334
    FAIL_IF_NOT (pdata->abc == 123);
335

336
    FAIL_IF_NOT (pt->array[3].pool->outstanding == 1);
337

338
    PoolThreadReturn(pt, data);
339

340
    FAIL_IF_NOT (pt->array[3].pool->outstanding == 0);
341

342
    PoolThreadFree(pt);
343
    PASS;
344
}
345

346
static int PoolThreadTestGrow01(void)
347
{
348
    PoolThread *pt = PoolThreadInit(4, /* threads */
349
                                    10, 5, 10, PoolThreadTestAlloc,
350
                                    NULL, NULL, NULL, NULL);
351
    FAIL_IF_NULL(pt);
352
    FAIL_IF(PoolThreadExpand(pt) < 0);
353

354
    PoolThreadFree(pt);
355
    PASS;
356
}
357

358
static int PoolThreadTestGrow02(void)
359
{
360
    int i = 123;
361

362
    PoolThread *pt = PoolThreadInit(4, /* threads */
363
                                    10, 5, 10, PoolThreadTestAlloc,
364
                                    PoolThreadTestInit, &i, PoolThreadTestFree, NULL);
365
    FAIL_IF_NULL(pt);
366
    FAIL_IF(PoolThreadExpand(pt) < 0);
367

368
    PoolThreadFree(pt);
369
    PASS;
370
}
371

372
static int PoolThreadTestGrow03(void)
373
{
374
    int i = 123;
375

376
    PoolThread *pt = PoolThreadInit(4, /* threads */
377
                                    10, 5, 10, PoolThreadTestAlloc,
378
                                    PoolThreadTestInit, &i, PoolThreadTestFree, NULL);
379
    FAIL_IF_NULL(pt);
380
    FAIL_IF(PoolThreadExpand(pt) < 0);
381

382
    void *data = PoolThreadGetById(pt, 4);
383
    FAIL_IF_NULL(data);
384

385
    struct PoolThreadTestData *pdata = data;
386
    FAIL_IF_NOT(pdata->res == 4);
387

388
    FAIL_IF_NOT(pdata->abc == 123);
389

390
    FAIL_IF_NOT(pt->array[4].pool->outstanding == 1);
391

392
    PoolThreadReturn(pt, data);
393

394
    FAIL_IF_NOT(pt->array[4].pool->outstanding == 0);
395

396
    PoolThreadFree(pt);
397
    PASS;
398
}
399

400
#endif
401

402
void PoolThreadRegisterTests(void)
UNCOV
403
{
×
404
#ifdef UNITTESTS
405
    UtRegisterTest("PoolThreadTestInit01", PoolThreadTestInit01);
406
    UtRegisterTest("PoolThreadTestInit02", PoolThreadTestInit02);
407

408
    UtRegisterTest("PoolThreadTestGet01", PoolThreadTestGet01);
409
    UtRegisterTest("PoolThreadTestGet02", PoolThreadTestGet02);
410

411
    UtRegisterTest("PoolThreadTestReturn01", PoolThreadTestReturn01);
412

413
    UtRegisterTest("PoolThreadTestGrow01", PoolThreadTestGrow01);
414
    UtRegisterTest("PoolThreadTestGrow02", PoolThreadTestGrow02);
415
    UtRegisterTest("PoolThreadTestGrow03", PoolThreadTestGrow03);
416
#endif
UNCOV
417
}
×
418

419
/**
420
 * @}
421
 */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc