Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization 4427 v2 #6057

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app-layer-expectation.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#include "util-print.h"
#include "queue.h"

static int g_ippair_expectation_id = -1;
static IPPairStorageId g_ippair_expectation_id = { .id = -1 };
static FlowStorageId g_flow_expectation_id = { .id = -1 };

SC_ATOMIC_DECLARE(uint32_t, expectation_count);
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-expectation.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2017 Open Information Security Foundation
/* Copyright (C) 2017-2021 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
Expand Down
8 changes: 4 additions & 4 deletions src/detect-engine-tag.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation
/* Copyright (C) 2007-2021 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
Expand Down Expand Up @@ -45,15 +45,15 @@
SC_ATOMIC_DECLARE(unsigned int, num_tags); /**< Atomic counter, to know if we
have tagged hosts/sessions,
to avoid locking */
static int host_tag_id = -1; /**< Host storage id for tags */
static HostStorageId host_tag_id = { .id = -1 }; /**< Host storage id for tags */
static FlowStorageId flow_tag_id = { .id = -1 }; /**< Flow storage id for tags */

void TagInitCtx(void)
{
SC_ATOMIC_INIT(num_tags);

host_tag_id = HostStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree);
if (host_tag_id == -1) {
if (host_tag_id.id == -1) {
FatalError(SC_ERR_FATAL, "Can't initiate host storage for tag");
}
flow_tag_id = FlowStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree);
Expand Down Expand Up @@ -117,7 +117,7 @@ static DetectTagDataEntry *DetectTagDataCopy(DetectTagDataEntry *dtd)
* \param p pointer to the current packet
* \param tde pointer to the new DetectTagDataEntry
*
* \retval 0 if the tde was added succesfuly
* \retval 0 if the tde was added successfully
* \retval 1 if an entry of this sid/gid already exist and was updated
*/
int TagFlowAdd(Packet *p, DetectTagDataEntry *tde)
Expand Down
12 changes: 6 additions & 6 deletions src/detect-engine-threshold.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2015 Open Information Security Foundation
/* Copyright (C) 2007-2021 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
Expand Down Expand Up @@ -69,23 +69,23 @@
#include "util-var-name.h"
#include "tm-threads.h"

static int host_threshold_id = -1; /**< host storage id for thresholds */
static int ippair_threshold_id = -1; /**< ip pair storage id for thresholds */
static HostStorageId host_threshold_id = { .id = -1 }; /**< host storage id for thresholds */
static IPPairStorageId ippair_threshold_id = { .id = -1 }; /**< ip pair storage id for thresholds */

int ThresholdHostStorageId(void)
HostStorageId ThresholdHostStorageId(void)
{
return host_threshold_id;
}

void ThresholdInit(void)
{
host_threshold_id = HostStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree);
if (host_threshold_id == -1) {
if (host_threshold_id.id == -1) {
FatalError(SC_ERR_FATAL,
"Can't initiate host storage for thresholding");
}
ippair_threshold_id = IPPairStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree);
if (ippair_threshold_id == -1) {
if (ippair_threshold_id.id == -1) {
FatalError(SC_ERR_FATAL,
"Can't initiate IP pair storage for thresholding");
}
Expand Down
5 changes: 3 additions & 2 deletions src/detect-engine-threshold.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2010 Open Information Security Foundation
/* Copyright (C) 2007-2021 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
Expand Down Expand Up @@ -28,10 +28,11 @@
#include "detect.h"
#include "host.h"
#include "ippair.h"
#include "host-storage.h"

void ThresholdInit(void);

int ThresholdHostStorageId(void);
HostStorageId ThresholdHostStorageId(void);
int ThresholdHostHasThreshold(Host *);

int ThresholdIPPairHasThreshold(IPPair *pair);
Expand Down
27 changes: 16 additions & 11 deletions src/device-storage.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2018 Open Information Security Foundation
/* Copyright (C) 2018-2021 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
Expand Down Expand Up @@ -56,8 +56,12 @@ unsigned int LiveDevStorageSize(void)
* It has to be called once during the init of the sub system
*/

int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
return StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free);
LiveDevStorageId LiveDevStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *))
{
int id = StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free);
LiveDevStorageId ldsi = { .id = id };
return ldsi;
}

/**
Expand All @@ -68,9 +72,9 @@ int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Al
* \param ptr pointer to the data to store
*/

int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr)
int LiveDevSetStorageById(LiveDevice *d, LiveDevStorageId id, void *ptr)
{
return StorageSetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id, ptr);
return StorageSetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id, ptr);
}

/**
Expand All @@ -81,9 +85,9 @@ int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr)
*
*/

void *LiveDevGetStorageById(LiveDevice *d, int id)
void *LiveDevGetStorageById(LiveDevice *d, LiveDevStorageId id)
{
return StorageGetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
return StorageGetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id);
}

/**
Expand All @@ -92,14 +96,15 @@ void *LiveDevGetStorageById(LiveDevice *d, int id)

/* Start of "private" function */

void *LiveDevAllocStorageById(LiveDevice *d, int id)
void *LiveDevAllocStorageById(LiveDevice *d, LiveDevStorageId id)
{
return StorageAllocByIdPrealloc((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
return StorageAllocByIdPrealloc(
(Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id);
}

void LiveDevFreeStorageById(LiveDevice *d, int id)
void LiveDevFreeStorageById(LiveDevice *d, LiveDevStorageId id)
{
StorageFreeById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
StorageFreeById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id);
}

void LiveDevFreeStorage(LiveDevice *d)
Expand Down
17 changes: 11 additions & 6 deletions src/device-storage.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2018 Open Information Security Foundation
/* Copyright (C) 2018-2021 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
Expand Down Expand Up @@ -29,17 +29,22 @@
#include "util-storage.h"
#include "util-device.h"

typedef struct LiveDevStorageId_ {
int id;
} LiveDevStorageId;

unsigned int LiveDevStorageSize(void);

void *LiveDevGetStorageById(LiveDevice *d, int id);
int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr);
void *LiveDevAllocStorageById(LiveDevice *d, int id);
void *LiveDevGetStorageById(LiveDevice *d, LiveDevStorageId id);
int LiveDevSetStorageById(LiveDevice *d, LiveDevStorageId id, void *ptr);
void *LiveDevAllocStorageById(LiveDevice *d, LiveDevStorageId id);

void LiveDevFreeStorageById(LiveDevice *d, int id);
void LiveDevFreeStorageById(LiveDevice *d, LiveDevStorageId id);
void LiveDevFreeStorage(LiveDevice *d);

void RegisterLiveDevStorageTests(void);

int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
LiveDevStorageId LiveDevStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *));

#endif /* __DEVICE_STORAGE_H__ */
6 changes: 3 additions & 3 deletions src/host-bit.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2014 Open Information Security Foundation
/* Copyright (C) 2014-2021 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
Expand Down Expand Up @@ -38,7 +38,7 @@
#include "util-unittest.h"
#include "host-storage.h"

static int host_bit_id = -1; /**< Host storage id for bits */
static HostStorageId host_bit_id = { .id = -1 }; /**< Host storage id for bits */

static void HostBitFreeAll(void *store)
{
Expand All @@ -49,7 +49,7 @@ static void HostBitFreeAll(void *store)
void HostBitInitCtx(void)
{
host_bit_id = HostStorageRegister("bit", sizeof(void *), NULL, HostBitFreeAll);
if (host_bit_id == -1) {
if (host_bit_id.id == -1) {
FatalError(SC_ERR_FATAL, "Can't initiate host storage for bits");
}
}
Expand Down
55 changes: 30 additions & 25 deletions src/host-storage.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation
/* Copyright (C) 2007-2021 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
Expand Down Expand Up @@ -56,8 +56,12 @@ unsigned int HostStorageSize(void)
* It has to be called once during the init of the sub system
*/

int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
return StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
HostStorageId HostStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *))
{
int id = StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
HostStorageId hsi = { .id = id };
return hsi;
}

/**
Expand All @@ -68,9 +72,9 @@ int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc
* \param ptr pointer to the data to store
*/

int HostSetStorageById(Host *h, int id, void *ptr)
int HostSetStorageById(Host *h, HostStorageId id, void *ptr)
{
return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id, ptr);
return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id, ptr);
}

/**
Expand All @@ -81,9 +85,9 @@ int HostSetStorageById(Host *h, int id, void *ptr)
*
*/

void *HostGetStorageById(Host *h, int id)
void *HostGetStorageById(Host *h, HostStorageId id)
{
return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
}

/**
Expand All @@ -92,14 +96,14 @@ void *HostGetStorageById(Host *h, int id)

/* Start of "private" function */

void *HostAllocStorageById(Host *h, int id)
void *HostAllocStorageById(Host *h, HostStorageId id)
{
return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
}

void HostFreeStorageById(Host *h, int id)
void HostFreeStorageById(Host *h, HostStorageId id)
{
StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
}

void HostFreeStorage(Host *h)
Expand All @@ -126,14 +130,15 @@ static int HostStorageTest01(void)
{
StorageInit();

int id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1 < 0)
HostStorageId id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1.id < 0)
goto error;
int id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2 < 0)
HostStorageId id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2.id < 0)
goto error;
int id3 = HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3 < 0)
HostStorageId id3 =
HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3.id < 0)
goto error;

if (StorageFinalize() < 0)
Expand Down Expand Up @@ -205,8 +210,8 @@ static int HostStorageTest02(void)
{
StorageInit();

int id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0)
HostStorageId id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1.id < 0)
goto error;

if (StorageFinalize() < 0)
Expand Down Expand Up @@ -255,14 +260,14 @@ static int HostStorageTest03(void)
{
StorageInit();

int id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0)
HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1.id < 0)
goto error;
int id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2 < 0)
HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2.id < 0)
goto error;
int id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3 < 0)
HostStorageId id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3.id < 0)
goto error;

if (StorageFinalize() < 0)
Expand Down
17 changes: 11 additions & 6 deletions src/host-storage.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation
/* Copyright (C) 2007-2021 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
Expand Down Expand Up @@ -29,17 +29,22 @@
#include "util-storage.h"
#include "host.h"

typedef struct HostStorageId_ {
int id;
} HostStorageId;

unsigned int HostStorageSize(void);

void *HostGetStorageById(Host *h, int id);
int HostSetStorageById(Host *h, int id, void *ptr);
void *HostAllocStorageById(Host *h, int id);
void *HostGetStorageById(Host *h, HostStorageId id);
int HostSetStorageById(Host *h, HostStorageId id, void *ptr);
void *HostAllocStorageById(Host *h, HostStorageId id);

void HostFreeStorageById(Host *h, int id);
void HostFreeStorageById(Host *h, HostStorageId id);
void HostFreeStorage(Host *h);

void RegisterHostStorageTests(void);

int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
HostStorageId HostStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *));

#endif /* __HOST_STORAGE_H__ */