From ad1459df725f2bbcd289bb5fcec4220d40d5a670 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sat, 30 May 2026 07:36:15 +0200 Subject: [PATCH] threads: add threadvars allocator and free func Mostly meant to help unittests and fuzz targets alloc the storage correctly. --- src/Makefile.am | 1 + src/threadvars.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/threadvars.h | 15 +++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/threadvars.c diff --git a/src/Makefile.am b/src/Makefile.am index 9856854d42..43f0ad1a10 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1009,6 +1009,7 @@ libsuricata_c_a_SOURCES = \ thread-callbacks.c \ thread-storage.c \ threads.c \ + threadvars.c \ tm-modules.c \ tm-queuehandlers.c \ tm-queues.c \ diff --git a/src/threadvars.c b/src/threadvars.c new file mode 100644 index 0000000000..880d4e65de --- /dev/null +++ b/src/threadvars.c @@ -0,0 +1,47 @@ +/* Copyright (C) 2026 Open Information Security Foundation + * + * You can copy, redistribute or modify this Program under the terms of + * the GNU General Public License version 2 as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include "suricata-common.h" +#include "threadvars.h" +#include "thread-storage.h" + +/** + * \brief Allocate a new ThreadVars structure. + * + * \retval NULL if allocation failed. + * \retval Pointer to newly allocated ThreadVars structure. + */ +ThreadVars *ThreadVarsAlloc(void) +{ + ThreadVars *tv = SCCalloc(1, sizeof(ThreadVars) + SCThreadStorageSize()); + if (tv == NULL) + return NULL; + SC_ATOMIC_INIT(tv->flags); + return tv; +} + +/** + * \brief Free a ThreadVars structure. + * + * \param tv Pointer to ThreadVars structure to be freed. + */ +void ThreadVarsFree(ThreadVars *tv) +{ + if (tv == NULL) + return; + SCFree(tv); +} diff --git a/src/threadvars.h b/src/threadvars.h index b6d3aac9be..b1f1132f76 100644 --- a/src/threadvars.h +++ b/src/threadvars.h @@ -143,4 +143,19 @@ typedef struct ThreadVars_ { #define THREAD_SET_PRIORITY 0x02 /** Real time priority */ #define THREAD_SET_AFFTYPE 0x04 /** Priority and affinity */ +/** + * \brief Allocate a new ThreadVars structure. + * + * \retval NULL if allocation failed. + * \retval Pointer to newly allocated ThreadVars structure. + */ +ThreadVars *ThreadVarsAlloc(void); + +/** + * \brief Free a ThreadVars structure. + * + * \param tv Pointer to ThreadVars structure to be freed. + */ +void ThreadVarsFree(ThreadVars *tv); + #endif /* SURICATA_THREADVARS_H */