storage: remove alloc callback as its unused

Remove the "Alloc" callback from the storage API, it was only being
used in tests without any real usage.
pull/15119/head
Jason Ish 4 months ago committed by Victor Julien
parent 332f47d557
commit f337cd573b

@ -573,13 +573,13 @@ static void NdpiInit(void)
SCLogDebug("Initializing nDPI plugin"); SCLogDebug("Initializing nDPI plugin");
/* Register thread storage. */ /* Register thread storage. */
thread_storage_id = ThreadStorageRegister("ndpi", sizeof(void *), NULL, ThreadStorageFree); thread_storage_id = ThreadStorageRegister("ndpi", sizeof(void *), ThreadStorageFree);
if (thread_storage_id.id < 0) { if (thread_storage_id.id < 0) {
FatalError("Failed to register nDPI thread storage"); FatalError("Failed to register nDPI thread storage");
} }
/* Register flow storage. */ /* Register flow storage. */
flow_storage_id = FlowStorageRegister("ndpi", sizeof(void *), NULL, FlowStorageFree); flow_storage_id = FlowStorageRegister("ndpi", sizeof(void *), FlowStorageFree);
if (flow_storage_id.id < 0) { if (flow_storage_id.id < 0) {
FatalError("Failed to register nDPI flow storage"); FatalError("Failed to register nDPI flow storage");
} }

@ -146,9 +146,8 @@ uint64_t ExpectationGetCounter(void)
void AppLayerExpectationSetup(void) void AppLayerExpectationSetup(void)
{ {
g_ippair_expectation_id = g_ippair_expectation_id =
IPPairStorageRegister("expectation", sizeof(void *), NULL, ExpectationListFree); IPPairStorageRegister("expectation", sizeof(void *), ExpectationListFree);
g_flow_expectation_id = g_flow_expectation_id = FlowStorageRegister("expectation", sizeof(void *), ExpectationDataFree);
FlowStorageRegister("expectation", sizeof(void *), NULL, ExpectationDataFree);
SC_ATOMIC_INIT(expectation_count); SC_ATOMIC_INIT(expectation_count);
} }

@ -53,11 +53,11 @@ void TagInitCtx(void)
{ {
SC_ATOMIC_INIT(num_tags); SC_ATOMIC_INIT(num_tags);
host_tag_id = HostStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree); host_tag_id = HostStorageRegister("tag", sizeof(void *), DetectTagDataListFree);
if (host_tag_id.id == -1) { if (host_tag_id.id == -1) {
FatalError("Can't initiate host storage for tag"); FatalError("Can't initiate host storage for tag");
} }
flow_tag_id = FlowStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree); flow_tag_id = FlowStorageRegister("tag", sizeof(void *), DetectTagDataListFree);
if (flow_tag_id.id == -1) { if (flow_tag_id.id == -1) {
FatalError("Can't initiate flow storage for tag"); FatalError("Can't initiate flow storage for tag");
} }

@ -49,8 +49,7 @@ unsigned int LiveDevStorageSize(void)
* \brief Register a LiveDevice storage * \brief Register a LiveDevice storage
* *
* \param name the name of the storage * \param name the name of the storage
* \param size integer coding the size of the stored value (sizeof(void *) is best choice here) * \param size integer coding the size of the stored value (sizeof(void *) is expected here)
* \param Alloc allocation function for the storage (can be null)
* \param Free free function for the new storage * \param Free free function for the new storage
* *
* \retval The ID of the newly register storage that will be used to access data * \retval The ID of the newly register storage that will be used to access data
@ -58,10 +57,10 @@ unsigned int LiveDevStorageSize(void)
* It has to be called once during the init of the sub system * It has to be called once during the init of the sub system
*/ */
LiveDevStorageId LiveDevStorageRegister(const char *name, const unsigned int size, LiveDevStorageId LiveDevStorageRegister(
void *(*Alloc)(unsigned int), void (*Free)(void *)) const char *name, const unsigned int size, void (*Free)(void *))
{ {
int id = StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free); int id = StorageRegister(STORAGE_DEVICE, name, size, Free);
LiveDevStorageId ldsi = { .id = id }; LiveDevStorageId ldsi = { .id = id };
return ldsi; return ldsi;
} }

@ -39,7 +39,7 @@ int LiveDevSetStorageById(LiveDevice *d, LiveDevStorageId id, void *ptr);
void LiveDevFreeStorage(LiveDevice *d); void LiveDevFreeStorage(LiveDevice *d);
LiveDevStorageId LiveDevStorageRegister(const char *name, const unsigned int size, LiveDevStorageId LiveDevStorageRegister(
void *(*Alloc)(unsigned int), void (*Free)(void *)); const char *name, const unsigned int size, void (*Free)(void *));
#endif /* SURICATA_DEVICE_STORAGE_H */ #endif /* SURICATA_DEVICE_STORAGE_H */

@ -47,11 +47,6 @@ int FlowSetStorageById(Flow *f, FlowStorageId id, void *ptr)
return StorageSetById(f->storage, STORAGE_FLOW, id.id, ptr); return StorageSetById(f->storage, STORAGE_FLOW, id.id, ptr);
} }
void *FlowAllocStorageById(Flow *f, FlowStorageId id)
{
return StorageAllocByIdPrealloc(f->storage, STORAGE_FLOW, id.id);
}
void FlowFreeStorageById(Flow *f, FlowStorageId id) void FlowFreeStorageById(Flow *f, FlowStorageId id)
{ {
StorageFreeById(f->storage, STORAGE_FLOW, id.id); StorageFreeById(f->storage, STORAGE_FLOW, id.id);
@ -63,21 +58,15 @@ void FlowFreeStorage(Flow *f)
StorageFreeAll(f->storage, STORAGE_FLOW); StorageFreeAll(f->storage, STORAGE_FLOW);
} }
FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, void (*Free)(void *))
void *(*Alloc)(unsigned int), void (*Free)(void *))
{ {
int id = StorageRegister(STORAGE_FLOW, name, size, Alloc, Free); int id = StorageRegister(STORAGE_FLOW, name, size, Free);
FlowStorageId fsi = { .id = id }; FlowStorageId fsi = { .id = id };
return fsi; return fsi;
} }
#ifdef UNITTESTS #ifdef UNITTESTS
static void *StorageTestAlloc(unsigned int size)
{
void *x = SCMalloc(size);
return x;
}
static void StorageTestFree(void *x) static void StorageTestFree(void *x)
{ {
if (x) if (x)
@ -89,12 +78,11 @@ static int FlowStorageTest01(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
FlowStorageId id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); FlowStorageId id1 = FlowStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
FlowStorageId id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); FlowStorageId id2 = FlowStorageRegister("variable", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
FlowStorageId id3 = FlowStorageId id3 = FlowStorageRegister("store", sizeof(void *), StorageTestFree);
FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -111,12 +99,15 @@ static int FlowStorageTest01(void)
ptr = FlowGetStorageById(f, id3); ptr = FlowGetStorageById(f, id3);
FAIL_IF_NOT_NULL(ptr); FAIL_IF_NOT_NULL(ptr);
void *ptr1a = FlowAllocStorageById(f, id1); void *ptr1a = SCMalloc(8);
FAIL_IF_NULL(ptr1a); FAIL_IF_NULL(ptr1a);
void *ptr2a = FlowAllocStorageById(f, id2); FAIL_IF(FlowSetStorageById(f, id1, ptr1a) != 0);
void *ptr2a = SCMalloc(24);
FAIL_IF_NULL(ptr2a); FAIL_IF_NULL(ptr2a);
void *ptr3a = FlowAllocStorageById(f, id3); FAIL_IF(FlowSetStorageById(f, id2, ptr2a) != 0);
void *ptr3a = SCMalloc(16);
FAIL_IF_NULL(ptr3a); FAIL_IF_NULL(ptr3a);
FAIL_IF(FlowSetStorageById(f, id3, ptr3a) != 0);
void *ptr1b = FlowGetStorageById(f, id1); void *ptr1b = FlowGetStorageById(f, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);
@ -137,7 +128,7 @@ static int FlowStorageTest02(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
FlowStorageId id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree); FlowStorageId id1 = FlowStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -168,11 +159,11 @@ static int FlowStorageTest03(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
FlowStorageId id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); FlowStorageId id1 = FlowStorageRegister("test1", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
FlowStorageId id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); FlowStorageId id2 = FlowStorageRegister("test2", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
FlowStorageId id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); FlowStorageId id3 = FlowStorageRegister("test3", sizeof(void *), StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -192,8 +183,9 @@ static int FlowStorageTest03(void)
FAIL_IF_NULL(ptr2a); FAIL_IF_NULL(ptr2a);
FlowSetStorageById(f, id2, ptr2a); FlowSetStorageById(f, id2, ptr2a);
void *ptr3a = FlowAllocStorageById(f, id3); void *ptr3a = SCMalloc(32);
FAIL_IF_NULL(ptr3a); FAIL_IF_NULL(ptr3a);
FlowSetStorageById(f, id3, ptr3a);
void *ptr1b = FlowGetStorageById(f, id1); void *ptr1b = FlowGetStorageById(f, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);

@ -36,14 +36,12 @@ unsigned int FlowStorageSize(void);
void *FlowGetStorageById(const Flow *h, FlowStorageId id); void *FlowGetStorageById(const Flow *h, FlowStorageId id);
int FlowSetStorageById(Flow *h, FlowStorageId id, void *ptr); int FlowSetStorageById(Flow *h, FlowStorageId id, void *ptr);
void *FlowAllocStorageById(Flow *h, FlowStorageId id);
void FlowFreeStorageById(Flow *h, FlowStorageId id); void FlowFreeStorageById(Flow *h, FlowStorageId id);
void FlowFreeStorage(Flow *h); void FlowFreeStorage(Flow *h);
void RegisterFlowStorageTests(void); void RegisterFlowStorageTests(void);
FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, void (*Free)(void *));
void *(*Alloc)(unsigned int), void (*Free)(void *));
#endif /* SURICATA_FLOW_STORAGE_H */ #endif /* SURICATA_FLOW_STORAGE_H */

@ -239,8 +239,7 @@ static void FlowBypassFree(void *x)
void RegisterFlowBypassInfo(void) void RegisterFlowBypassInfo(void)
{ {
g_bypass_info_id = FlowStorageRegister("bypass_counters", sizeof(void *), g_bypass_info_id = FlowStorageRegister("bypass_counters", sizeof(void *), FlowBypassFree);
NULL, FlowBypassFree);
} }
void FlowEndCountersRegister(ThreadVars *t, FlowEndCounters *fec) void FlowEndCountersRegister(ThreadVars *t, FlowEndCounters *fec)

@ -48,7 +48,7 @@ static void HostBitFreeAll(void *store)
void HostBitInitCtx(void) void HostBitInitCtx(void)
{ {
host_bit_id = HostStorageRegister("bit", sizeof(void *), NULL, HostBitFreeAll); host_bit_id = HostStorageRegister("bit", sizeof(void *), HostBitFreeAll);
if (host_bit_id.id == -1) { if (host_bit_id.id == -1) {
FatalError("Can't initiate host storage for bits"); FatalError("Can't initiate host storage for bits");
} }

@ -47,8 +47,7 @@ unsigned int HostStorageSize(void)
* \brief Register a Host storage * \brief Register a Host storage
* *
* \param name the name of the storage * \param name the name of the storage
* \param size integer coding the size of the stored value (sizeof(void *) is best choice here) * \param size integer coding the size of the stored value (sizeof(void *) is expected here)
* \param Alloc allocation function for the storage (can be null)
* \param Free free function for the new storage * \param Free free function for the new storage
* *
* \retval The ID of the newly register storage that will be used to access data * \retval The ID of the newly register storage that will be used to access data
@ -56,10 +55,9 @@ unsigned int HostStorageSize(void)
* It has to be called once during the init of the sub system * It has to be called once during the init of the sub system
*/ */
HostStorageId HostStorageRegister(const char *name, const unsigned int size, HostStorageId HostStorageRegister(const char *name, const unsigned int size, void (*Free)(void *))
void *(*Alloc)(unsigned int), void (*Free)(void *))
{ {
int id = StorageRegister(STORAGE_HOST, name, size, Alloc, Free); int id = StorageRegister(STORAGE_HOST, name, size, Free);
HostStorageId hsi = { .id = id }; HostStorageId hsi = { .id = id };
return hsi; return hsi;
} }
@ -96,11 +94,6 @@ void *HostGetStorageById(Host *h, HostStorageId id)
/* Start of "private" function */ /* Start of "private" function */
void *HostAllocStorageById(Host *h, HostStorageId id)
{
return StorageAllocByIdPrealloc(h->storage, STORAGE_HOST, id.id);
}
void HostFreeStorage(Host *h) void HostFreeStorage(Host *h)
{ {
if (HostStorageSize() > 0) if (HostStorageSize() > 0)
@ -110,11 +103,6 @@ void HostFreeStorage(Host *h)
#ifdef UNITTESTS #ifdef UNITTESTS
static void *StorageTestAlloc(unsigned int size)
{
void *x = SCMalloc(size);
return x;
}
static void StorageTestFree(void *x) static void StorageTestFree(void *x)
{ {
if (x) if (x)
@ -126,12 +114,11 @@ static int HostStorageTest01(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
HostStorageId id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); HostStorageId id1 = HostStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
HostStorageId id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); HostStorageId id2 = HostStorageRegister("variable", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
HostStorageId id3 = HostStorageId id3 = HostStorageRegister("store", sizeof(void *), StorageTestFree);
HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -152,12 +139,15 @@ static int HostStorageTest01(void)
ptr = HostGetStorageById(h, id3); ptr = HostGetStorageById(h, id3);
FAIL_IF_NOT_NULL(ptr); FAIL_IF_NOT_NULL(ptr);
void *ptr1a = HostAllocStorageById(h, id1); void *ptr1a = SCMalloc(8);
FAIL_IF_NULL(ptr1a); FAIL_IF_NULL(ptr1a);
void *ptr2a = HostAllocStorageById(h, id2); FAIL_IF(HostSetStorageById(h, id1, ptr1a) != 0);
void *ptr2a = SCMalloc(24);
FAIL_IF_NULL(ptr2a); FAIL_IF_NULL(ptr2a);
void *ptr3a = HostAllocStorageById(h, id3); FAIL_IF(HostSetStorageById(h, id2, ptr2a) != 0);
void *ptr3a = SCMalloc(16);
FAIL_IF_NULL(ptr3a); FAIL_IF_NULL(ptr3a);
FAIL_IF(HostSetStorageById(h, id3, ptr3a) != 0);
void *ptr1b = HostGetStorageById(h, id1); void *ptr1b = HostGetStorageById(h, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);
@ -178,7 +168,7 @@ static int HostStorageTest02(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
HostStorageId id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree); HostStorageId id1 = HostStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -214,11 +204,11 @@ static int HostStorageTest03(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
HostStorageId id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); HostStorageId id3 = HostStorageRegister("test3", sizeof(void *), StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -243,8 +233,9 @@ static int HostStorageTest03(void)
FAIL_IF_NULL(ptr2a); FAIL_IF_NULL(ptr2a);
HostSetStorageById(h, id2, ptr2a); HostSetStorageById(h, id2, ptr2a);
void *ptr3a = HostAllocStorageById(h, id3); void *ptr3a = SCMalloc(32);
FAIL_IF_NULL(ptr3a); FAIL_IF_NULL(ptr3a);
HostSetStorageById(h, id3, ptr3a);
void *ptr1b = HostGetStorageById(h, id1); void *ptr1b = HostGetStorageById(h, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);

@ -36,13 +36,11 @@ unsigned int HostStorageSize(void);
void *HostGetStorageById(Host *h, HostStorageId id); void *HostGetStorageById(Host *h, HostStorageId id);
int HostSetStorageById(Host *h, HostStorageId id, void *ptr); int HostSetStorageById(Host *h, HostStorageId id, void *ptr);
void *HostAllocStorageById(Host *h, HostStorageId id);
void HostFreeStorage(Host *h); void HostFreeStorage(Host *h);
void RegisterHostStorageTests(void); void RegisterHostStorageTests(void);
HostStorageId HostStorageRegister(const char *name, const unsigned int size, HostStorageId HostStorageRegister(const char *name, const unsigned int size, void (*Free)(void *));
void *(*Alloc)(unsigned int), void (*Free)(void *));
#endif /* SURICATA_HOST_STORAGE_H */ #endif /* SURICATA_HOST_STORAGE_H */

@ -48,7 +48,7 @@ static void XBitFreeAll(void *store)
void IPPairBitInitCtx(void) void IPPairBitInitCtx(void)
{ {
g_ippair_bit_storage_id = IPPairStorageRegister("bit", sizeof(void *), NULL, XBitFreeAll); g_ippair_bit_storage_id = IPPairStorageRegister("bit", sizeof(void *), XBitFreeAll);
if (g_ippair_bit_storage_id.id == -1) { if (g_ippair_bit_storage_id.id == -1) {
FatalError("Can't initiate ippair storage for bits"); FatalError("Can't initiate ippair storage for bits");
} }

@ -42,32 +42,22 @@ int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr)
return StorageSetById(h->storage, STORAGE_IPPAIR, id.id, ptr); return StorageSetById(h->storage, STORAGE_IPPAIR, id.id, ptr);
} }
void *IPPairAllocStorageById(IPPair *h, IPPairStorageId id)
{
return StorageAllocByIdPrealloc(h->storage, STORAGE_IPPAIR, id.id);
}
void IPPairFreeStorage(IPPair *h) void IPPairFreeStorage(IPPair *h)
{ {
if (IPPairStorageSize() > 0) if (IPPairStorageSize() > 0)
StorageFreeAll(h->storage, STORAGE_IPPAIR); StorageFreeAll(h->storage, STORAGE_IPPAIR);
} }
IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size, IPPairStorageId IPPairStorageRegister(
void *(*Alloc)(unsigned int), void (*Free)(void *)) const char *name, const unsigned int size, void (*Free)(void *))
{ {
int id = StorageRegister(STORAGE_IPPAIR, name, size, Alloc, Free); int id = StorageRegister(STORAGE_IPPAIR, name, size, Free);
IPPairStorageId ippsi = { .id = id }; IPPairStorageId ippsi = { .id = id };
return ippsi; return ippsi;
} }
#ifdef UNITTESTS #ifdef UNITTESTS
static void *StorageTestAlloc(unsigned int size)
{
void *x = SCMalloc(size);
return x;
}
static void StorageTestFree(void *x) static void StorageTestFree(void *x)
{ {
if (x) if (x)
@ -79,12 +69,11 @@ static int IPPairStorageTest01(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
IPPairStorageId id1 = IPPairStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); IPPairStorageId id1 = IPPairStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
IPPairStorageId id2 = IPPairStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); IPPairStorageId id2 = IPPairStorageRegister("variable", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
IPPairStorageId id3 = IPPairStorageId id3 = IPPairStorageRegister("store", sizeof(void *), StorageTestFree);
IPPairStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -108,12 +97,15 @@ static int IPPairStorageTest01(void)
ptr = IPPairGetStorageById(h, id3); ptr = IPPairGetStorageById(h, id3);
FAIL_IF_NOT_NULL(ptr); FAIL_IF_NOT_NULL(ptr);
void *ptr1a = IPPairAllocStorageById(h, id1); void *ptr1a = SCMalloc(8);
FAIL_IF(ptr1a == NULL); FAIL_IF(ptr1a == NULL);
void *ptr2a = IPPairAllocStorageById(h, id2); FAIL_IF(IPPairSetStorageById(h, id1, ptr1a) != 0);
void *ptr2a = SCMalloc(24);
FAIL_IF(ptr2a == NULL); FAIL_IF(ptr2a == NULL);
void *ptr3a = IPPairAllocStorageById(h, id3); FAIL_IF(IPPairSetStorageById(h, id2, ptr2a) != 0);
void *ptr3a = SCMalloc(16);
FAIL_IF(ptr3a == NULL); FAIL_IF(ptr3a == NULL);
FAIL_IF(IPPairSetStorageById(h, id3, ptr3a) != 0);
void *ptr1b = IPPairGetStorageById(h, id1); void *ptr1b = IPPairGetStorageById(h, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);
@ -133,7 +125,7 @@ static int IPPairStorageTest02(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
IPPairStorageId id1 = IPPairStorageRegister("test", sizeof(void *), NULL, StorageTestFree); IPPairStorageId id1 = IPPairStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -172,11 +164,11 @@ static int IPPairStorageTest03(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
IPPairStorageId id1 = IPPairStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); IPPairStorageId id1 = IPPairStorageRegister("test1", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
IPPairStorageId id2 = IPPairStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); IPPairStorageId id2 = IPPairStorageRegister("test2", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
IPPairStorageId id3 = IPPairStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); IPPairStorageId id3 = IPPairStorageRegister("test3", sizeof(void *), StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -206,8 +198,9 @@ static int IPPairStorageTest03(void)
IPPairSetStorageById(h, id2, ptr2a); IPPairSetStorageById(h, id2, ptr2a);
void *ptr3a = IPPairAllocStorageById(h, id3); void *ptr3a = SCMalloc(32);
FAIL_IF(ptr3a == NULL); FAIL_IF(ptr3a == NULL);
IPPairSetStorageById(h, id3, ptr3a);
void *ptr1b = IPPairGetStorageById(h, id1); void *ptr1b = IPPairGetStorageById(h, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);

@ -36,13 +36,12 @@ unsigned int IPPairStorageSize(void);
void *IPPairGetStorageById(IPPair *h, IPPairStorageId id); void *IPPairGetStorageById(IPPair *h, IPPairStorageId id);
int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr); int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr);
void *IPPairAllocStorageById(IPPair *h, IPPairStorageId id);
void IPPairFreeStorage(IPPair *h); void IPPairFreeStorage(IPPair *h);
void RegisterIPPairStorageTests(void); void RegisterIPPairStorageTests(void);
IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size, IPPairStorageId IPPairStorageRegister(
void *(*Alloc)(unsigned int), void (*Free)(void *)); const char *name, const unsigned int size, void (*Free)(void *));
#endif /* SURICATA_IPPAIR_STORAGE_H */ #endif /* SURICATA_IPPAIR_STORAGE_H */

@ -37,11 +37,6 @@ int ThreadSetStorageById(ThreadVars *tv, ThreadStorageId id, void *ptr)
return StorageSetById(tv->storage, storage_type, id.id, ptr); return StorageSetById(tv->storage, storage_type, id.id, ptr);
} }
void *ThreadAllocStorageById(ThreadVars *tv, ThreadStorageId id)
{
return StorageAllocByIdPrealloc(tv->storage, storage_type, id.id);
}
void ThreadFreeStorageById(ThreadVars *tv, ThreadStorageId id) void ThreadFreeStorageById(ThreadVars *tv, ThreadStorageId id)
{ {
StorageFreeById(tv->storage, storage_type, id.id); StorageFreeById(tv->storage, storage_type, id.id);
@ -53,21 +48,16 @@ void ThreadFreeStorage(ThreadVars *tv)
StorageFreeAll(tv->storage, storage_type); StorageFreeAll(tv->storage, storage_type);
} }
ThreadStorageId ThreadStorageRegister(const char *name, const unsigned int size, ThreadStorageId ThreadStorageRegister(
void *(*Alloc)(unsigned int), void (*Free)(void *)) const char *name, const unsigned int size, void (*Free)(void *))
{ {
int id = StorageRegister(storage_type, name, size, Alloc, Free); int id = StorageRegister(storage_type, name, size, Free);
ThreadStorageId tsi = { .id = id }; ThreadStorageId tsi = { .id = id };
return tsi; return tsi;
} }
#ifdef UNITTESTS #ifdef UNITTESTS
static void *StorageTestAlloc(unsigned int size)
{
return SCCalloc(1, size);
}
static void StorageTestFree(void *x) static void StorageTestFree(void *x)
{ {
SCFree(x); SCFree(x);
@ -78,14 +68,13 @@ static int ThreadStorageTest01(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
ThreadStorageId id1 = ThreadStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); ThreadStorageId id1 = ThreadStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
ThreadStorageId id2 = ThreadStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); ThreadStorageId id2 = ThreadStorageRegister("variable", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
ThreadStorageId id3 = ThreadStorageId id3 = ThreadStorageRegister("store", sizeof(void *), StorageTestFree);
ThreadStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -102,14 +91,17 @@ static int ThreadStorageTest01(void)
ptr = ThreadGetStorageById(tv, id3); ptr = ThreadGetStorageById(tv, id3);
FAIL_IF_NOT_NULL(ptr); FAIL_IF_NOT_NULL(ptr);
void *ptr1a = ThreadAllocStorageById(tv, id1); void *ptr1a = SCMalloc(8);
FAIL_IF_NULL(ptr1a); FAIL_IF_NULL(ptr1a);
FAIL_IF(ThreadSetStorageById(tv, id1, ptr1a) != 0);
void *ptr2a = ThreadAllocStorageById(tv, id2); void *ptr2a = SCMalloc(24);
FAIL_IF_NULL(ptr2a); FAIL_IF_NULL(ptr2a);
FAIL_IF(ThreadSetStorageById(tv, id2, ptr2a) != 0);
void *ptr3a = ThreadAllocStorageById(tv, id3); void *ptr3a = SCMalloc(16);
FAIL_IF_NULL(ptr3a); FAIL_IF_NULL(ptr3a);
FAIL_IF(ThreadSetStorageById(tv, id3, ptr3a) != 0);
void *ptr1b = ThreadGetStorageById(tv, id1); void *ptr1b = ThreadGetStorageById(tv, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);
@ -131,7 +123,7 @@ static int ThreadStorageTest02(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
ThreadStorageId id1 = ThreadStorageRegister("test", sizeof(void *), NULL, StorageTestFree); ThreadStorageId id1 = ThreadStorageRegister("test", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -161,13 +153,13 @@ static int ThreadStorageTest03(void)
StorageCleanup(); StorageCleanup();
StorageInit(); StorageInit();
ThreadStorageId id1 = ThreadStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); ThreadStorageId id1 = ThreadStorageRegister("test1", sizeof(void *), StorageTestFree);
FAIL_IF(id1.id < 0); FAIL_IF(id1.id < 0);
ThreadStorageId id2 = ThreadStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); ThreadStorageId id2 = ThreadStorageRegister("test2", sizeof(void *), StorageTestFree);
FAIL_IF(id2.id < 0); FAIL_IF(id2.id < 0);
ThreadStorageId id3 = ThreadStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); ThreadStorageId id3 = ThreadStorageRegister("test3", sizeof(void *), StorageTestFree);
FAIL_IF(id3.id < 0); FAIL_IF(id3.id < 0);
FAIL_IF(StorageFinalize() < 0); FAIL_IF(StorageFinalize() < 0);
@ -188,8 +180,9 @@ static int ThreadStorageTest03(void)
ThreadSetStorageById(tv, id2, ptr2a); ThreadSetStorageById(tv, id2, ptr2a);
void *ptr3a = ThreadAllocStorageById(tv, id3); void *ptr3a = SCMalloc(32);
FAIL_IF_NULL(ptr3a); FAIL_IF_NULL(ptr3a);
ThreadSetStorageById(tv, id3, ptr3a);
void *ptr1b = ThreadGetStorageById(tv, id1); void *ptr1b = ThreadGetStorageById(tv, id1);
FAIL_IF(ptr1a != ptr1b); FAIL_IF(ptr1a != ptr1b);

@ -32,14 +32,13 @@ unsigned int ThreadStorageSize(void);
void *ThreadGetStorageById(const ThreadVars *tv, ThreadStorageId id); void *ThreadGetStorageById(const ThreadVars *tv, ThreadStorageId id);
int ThreadSetStorageById(ThreadVars *tv, ThreadStorageId id, void *ptr); int ThreadSetStorageById(ThreadVars *tv, ThreadStorageId id, void *ptr);
void *ThreadAllocStorageById(ThreadVars *tv, ThreadStorageId id);
void ThreadFreeStorageById(ThreadVars *tv, ThreadStorageId id); void ThreadFreeStorageById(ThreadVars *tv, ThreadStorageId id);
void ThreadFreeStorage(ThreadVars *tv); void ThreadFreeStorage(ThreadVars *tv);
void RegisterThreadStorageTests(void); void RegisterThreadStorageTests(void);
ThreadStorageId ThreadStorageRegister(const char *name, const unsigned int size, ThreadStorageId ThreadStorageRegister(
void *(*Alloc)(unsigned int), void (*Free)(void *)); const char *name, const unsigned int size, void (*Free)(void *));
#endif /* SURICATA_THREAD_STORAGE_H */ #endif /* SURICATA_THREAD_STORAGE_H */

@ -484,8 +484,8 @@ static void LiveDevExtensionFree(void *x)
*/ */
void LiveDevRegisterExtension(void) void LiveDevRegisterExtension(void)
{ {
g_bypass_storage_id = LiveDevStorageRegister("bypass_stats", sizeof(void *), g_bypass_storage_id =
NULL, LiveDevExtensionFree); LiveDevStorageRegister("bypass_stats", sizeof(void *), LiveDevExtensionFree);
} }
/** /**

@ -929,8 +929,8 @@ int EBPFCheckBypassedFlowCreate(ThreadVars *th_v, struct timespec *curtime, void
void EBPFRegisterExtension(void) void EBPFRegisterExtension(void)
{ {
g_livedev_storage_id = LiveDevStorageRegister("bpfmap", sizeof(void *), NULL, BpfMapsInfoFree); g_livedev_storage_id = LiveDevStorageRegister("bpfmap", sizeof(void *), BpfMapsInfoFree);
g_flow_storage_id = FlowStorageRegister("bypassedlist", sizeof(void *), NULL, BypassedListFree); g_flow_storage_id = FlowStorageRegister("bypassedlist", sizeof(void *), BypassedListFree);
} }

@ -90,8 +90,7 @@ void FlowRateRegisterFlowStorage(void)
} }
flow_rate_config.interval = SCTIME_ADD_SECS(interval, secs); flow_rate_config.interval = SCTIME_ADD_SECS(interval, secs);
g_flowrate_storage_id = g_flowrate_storage_id = FlowStorageRegister("flowrate", sizeof(void *), FlowRateStoreFree);
FlowStorageRegister("flowrate", sizeof(void *), NULL, FlowRateStoreFree);
} }
bool FlowRateStorageEnabled(void) bool FlowRateStorageEnabled(void)

@ -73,7 +73,7 @@ void MacSetRegisterFlowStorage(void)
SCConfNodeLookupChildValue(node->head.tqh_first, "ethernet"); SCConfNodeLookupChildValue(node->head.tqh_first, "ethernet");
if (ethernet != NULL && SCConfValIsTrue(ethernet)) { if (ethernet != NULL && SCConfValIsTrue(ethernet)) {
g_macset_storage_id = FlowStorageRegister( g_macset_storage_id = FlowStorageRegister(
"macset", sizeof(void *), NULL, (void (*)(void *))MacSetFree); "macset", sizeof(void *), (void (*)(void *))MacSetFree);
return; return;
} }
} }

@ -32,7 +32,6 @@ typedef struct StorageMapping_ {
const char *name; const char *name;
StorageEnum type; // host, flow, tx, stream, ssn, etc StorageEnum type; // host, flow, tx, stream, ssn, etc
unsigned int size; unsigned int size;
void *(*Alloc)(unsigned int);
void (*Free)(void *); void (*Free)(void *);
} StorageMapping; } StorageMapping;
@ -99,13 +98,14 @@ void StorageCleanup(void)
storage_list = NULL; storage_list = NULL;
} }
int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) int StorageRegister(
const StorageEnum type, const char *name, const unsigned int size, void (*Free)(void *))
{ {
if (storage_registration_closed) if (storage_registration_closed)
return -1; return -1;
if (type >= STORAGE_MAX || name == NULL || strlen(name) == 0 || if (type >= STORAGE_MAX || name == NULL || strlen(name) == 0 || size == 0 ||
size == 0 || (size != sizeof(void *) && Alloc == NULL) || Free == NULL) size != sizeof(void *) || Free == NULL)
return -1; return -1;
StorageList *list = storage_list; StorageList *list = storage_list;
@ -127,7 +127,6 @@ int StorageRegister(const StorageEnum type, const char *name, const unsigned int
entry->map.type = type; entry->map.type = type;
entry->map.name = name; entry->map.name = name;
entry->map.size = size; entry->map.size = size;
entry->map.Alloc = Alloc;
entry->map.Free = Free; entry->map.Free = Free;
entry->id = storage_max_id[type]++; entry->id = storage_max_id[type]++;
@ -170,7 +169,6 @@ int StorageFinalize(void)
storage_map[entry->map.type][entry->id].name = entry->map.name; storage_map[entry->map.type][entry->id].name = entry->map.name;
storage_map[entry->map.type][entry->id].type = entry->map.type; storage_map[entry->map.type][entry->id].type = entry->map.type;
storage_map[entry->map.type][entry->id].size = entry->map.size; storage_map[entry->map.type][entry->id].size = entry->map.size;
storage_map[entry->map.type][entry->id].Alloc = entry->map.Alloc;
storage_map[entry->map.type][entry->id].Free = entry->map.Free; storage_map[entry->map.type][entry->id].Free = entry->map.Free;
} }
@ -235,24 +233,6 @@ int StorageSetById(Storage *storage, const StorageEnum type, const int id, void
return 0; return 0;
} }
void *StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id)
{
#ifdef DEBUG
BUG_ON(!storage_registration_closed);
#endif
SCLogDebug("storage %p id %d", storage, id);
StorageMapping *map = &storage_map[type][id];
if (storage[id].ptr == NULL && map->Alloc != NULL) {
storage[id].ptr = map->Alloc(map->size);
if (storage[id].ptr == NULL) {
return NULL;
}
}
return storage[id].ptr;
}
void StorageFreeById(Storage *storage, StorageEnum type, int id) void StorageFreeById(Storage *storage, StorageEnum type, int id)
{ {
#ifdef DEBUG #ifdef DEBUG

@ -49,13 +49,13 @@ void StorageCleanup(void);
* \param type type from StorageEnum * \param type type from StorageEnum
* \param name name * \param name name
* \param size size of the per instance storage * \param size size of the per instance storage
* \param Alloc alloc function for per instance storage
* \param Free free function for per instance storage * \param Free free function for per instance storage
* *
* \note if size == ptr size (so sizeof(void *)) and Alloc == NULL the API just * \note if size == ptr size (so sizeof(void *)) and Alloc == NULL the API just
* gives the caller a ptr to store something it alloc'ed itself. * gives the caller a ptr to store something it alloc'ed itself.
*/ */
int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)); int StorageRegister(
const StorageEnum type, const char *name, const unsigned int size, void (*Free)(void *));
int StorageFinalize(void); int StorageFinalize(void);
unsigned int StorageGetCnt(const StorageEnum type); unsigned int StorageGetCnt(const StorageEnum type);

Loading…
Cancel
Save