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 int flow_tag_id = -1; /**< Flow storage id for tags */
+static int host_tag_id = -1; /**< Host storage id for tags */
+static int flow_tag_id = -1; /**< Flow storage id for tags */
void TagInitCtx(void) {
SC_ATOMIC_INIT(num_tags);
void ThresholdInit(void) {
threshold_id = HostStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree);
+ if (threshold_id == -1) {
+ SCLogError(SC_ERR_HOST_INIT, "Can't initiate host storage for thresholding");
+ exit(EXIT_FAILURE);
+ }
}
int ThresholdHostHasThreshold(Host *host) {
StorageFreeAll((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW);
}
-int FlowStorageRegister(const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *)) {
- return StorageRegister(STORAGE_FLOW, name, size, Init, Free);
+int FlowStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
+ return StorageRegister(STORAGE_FLOW, name, size, Alloc, Free);
}
#ifdef UNITTESTS
-static void *StorageTestInit(unsigned int size) {
+static void *StorageTestAlloc(unsigned int size) {
void *x = SCMalloc(size);
return x;
}
static int FlowStorageTest01(void) {
StorageInit();
- int id1 = FlowStorageRegister("test", 8, StorageTestInit, StorageTestFree);
+ int id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1 < 0)
goto error;
- int id2 = FlowStorageRegister("variable", 24, StorageTestInit, StorageTestFree);
+ int id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2 < 0)
goto error;
- int id3 = FlowStorageRegister("store", sizeof(void *), StorageTestInit, StorageTestFree);
+ int id3 = FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3 < 0)
goto error;
int id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2 < 0)
goto error;
- int id3 = FlowStorageRegister("test3", 32, StorageTestInit, StorageTestFree);
+ int id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3 < 0)
goto error;
void RegisterFlowStorageTests(void);
-int FlowStorageRegister(const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *));
+int FlowStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
#endif /* __FLOW_STORAGE_H__ */
StorageFreeAll((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST);
}
-int HostStorageRegister(const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *)) {
- return StorageRegister(STORAGE_HOST, name, size, Init, Free);
+int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
+ return StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
}
#ifdef UNITTESTS
-static void *StorageTestInit(unsigned int size) {
+static void *StorageTestAlloc(unsigned int size) {
void *x = SCMalloc(size);
return x;
}
static int HostStorageTest01(void) {
StorageInit();
- int id1 = HostStorageRegister("test", 8, StorageTestInit, StorageTestFree);
+ int id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1 < 0)
goto error;
- int id2 = HostStorageRegister("variable", 24, StorageTestInit, StorageTestFree);
+ int id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2 < 0)
goto error;
- int id3 = HostStorageRegister("store", sizeof(void *), StorageTestInit, StorageTestFree);
+ int id3 = HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3 < 0)
goto error;
int id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2 < 0)
goto error;
- int id3 = HostStorageRegister("test3", 32, StorageTestInit, StorageTestFree);
+ int id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3 < 0)
goto error;
void RegisterHostStorageTests(void);
-int HostStorageRegister(const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *));
+int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
#endif /* __HOST_STORAGE_H__ */
const char *name;
StorageEnum type; // host, flow, tx, stream, ssn, etc
unsigned int size;
- void *(*Init)(unsigned int);
+ void *(*Alloc)(unsigned int);
void (*Free)(void *);
} StorageMapping;
storage_list = NULL;
}
-int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *)) {
+int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
if (storage_registraton_closed)
return -1;
if (type >= STORAGE_MAX || name == NULL || strlen(name) == 0 ||
- size == 0 || (size != sizeof(void *) && Init == NULL) || Free == NULL)
+ size == 0 || (size != sizeof(void *) && Alloc == NULL) || Free == NULL)
return -1;
StorageList *list = storage_list;
entry->map.type = type;
entry->map.name = name;
entry->map.size = size;
- entry->map.Init = Init;
+ entry->map.Alloc = Alloc;
entry->map.Free = Free;
entry->id = storage_max_id[type]++;
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].size = entry->map.size;
- storage_map[entry->map.type][entry->id].Init = entry->map.Init;
+ storage_map[entry->map.type][entry->id].Alloc = entry->map.Alloc;
storage_map[entry->map.type][entry->id].Free = entry->map.Free;
}
SCLogDebug("storage %p id %d", storage, id);
StorageMapping *map = &storage_map[type][id];
- if (storage[id] == NULL && map->Init != NULL) {
- storage[id] = map->Init(map->size);
+ if (storage[id] == NULL && map->Alloc != NULL) {
+ storage[id] = map->Alloc(map->size);
if (storage[id] == NULL) {
return NULL;
}
}
SCLogDebug("store %p", store);
- if (store[id] == NULL && map->Init != NULL) {
- store[id] = map->Init(map->size);
+ if (store[id] == NULL && map->Alloc != NULL) {
+ store[id] = map->Alloc(map->size);
if (store[id] == NULL) {
SCFree(store);
*storage = NULL;
#ifdef UNITTESTS
-static void *StorageTestInit(unsigned int size) {
+static void *StorageTestAlloc(unsigned int size) {
void *x = SCMalloc(size);
return x;
}
static int StorageTest01(void) {
StorageInit();
- int id = StorageRegister(STORAGE_HOST, "test", 8, StorageTestInit, StorageTestFree);
+ int id = StorageRegister(STORAGE_HOST, "test", 8, StorageTestAlloc, StorageTestFree);
if (id < 0)
goto error;
- id = StorageRegister(STORAGE_HOST, "variable", 24, StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_HOST, "variable", 24, StorageTestAlloc, StorageTestFree);
if (id < 0)
goto error;
- id = StorageRegister(STORAGE_FLOW, "store", sizeof(void *), StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_FLOW, "store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id < 0)
goto error;
static int StorageTest03(void) {
StorageInit();
- int id = StorageRegister(STORAGE_HOST, "test", 8, StorageTestInit, StorageTestFree);
+ int id = StorageRegister(STORAGE_HOST, "test", 8, StorageTestAlloc, StorageTestFree);
if (id < 0)
goto error;
- id = StorageRegister(STORAGE_HOST, "test", 8, StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_HOST, "test", 8, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed: ");
goto error;
goto error;
}
- id = StorageRegister(STORAGE_HOST, "test2", 8, StorageTestInit, NULL);
+ id = StorageRegister(STORAGE_HOST, "test2", 8, StorageTestAlloc, NULL);
if (id != -1) {
printf("duplicate registration should have failed (3): ");
goto error;
}
- id = StorageRegister(STORAGE_HOST, "test3", 0, StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_HOST, "test3", 0, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed (4): ");
goto error;
}
- id = StorageRegister(STORAGE_HOST, "", 8, StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_HOST, "", 8, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed (5): ");
goto error;
}
- id = StorageRegister(STORAGE_HOST, NULL, 8, StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_HOST, NULL, 8, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed (6): ");
goto error;
}
- id = StorageRegister(STORAGE_MAX, "test4", 8, StorageTestInit, StorageTestFree);
+ id = StorageRegister(STORAGE_MAX, "test4", 8, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed (7): ");
goto error;
}
- id = StorageRegister(38, "test5", 8, StorageTestInit, StorageTestFree);
+ id = StorageRegister(38, "test5", 8, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed (8): ");
goto error;
}
- id = StorageRegister(-1, "test6", 8, StorageTestInit, StorageTestFree);
+ id = StorageRegister(-1, "test6", 8, StorageTestAlloc, StorageTestFree);
if (id != -1) {
printf("duplicate registration should have failed (9): ");
goto error;
void StorageInit(void);
void StorageCleanup(void);
-int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *));
+
+/** \brief Register new storage
+ *
+ * \param type type from StorageEnum
+ * \param name name
+ * \param size size of the per instance storage
+ * \param Alloc alloc 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
+ * 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 StorageFinalize(void);
unsigned int StorageGetCnt(const StorageEnum type);
unsigned int StorageGetSize(const StorageEnum type);
+/** \brief get storage for id */
void *StorageGetById(const Storage *storage, const StorageEnum type, const int id);
+/** \brief set storage for id */
int StorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr);
+
+/** \brief AllocById func for prealloc'd base storage (storage ptrs are part
+ * of another memory block) */
void *StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id);
+/** \brief AllocById func for when we manage the Storage ptr itself */
void *StorageAllocById(Storage **storage, const StorageEnum type, const int id);
void StorageFreeById(Storage *storage, const StorageEnum type, const int id);
void StorageFreeAll(Storage *storage, const StorageEnum type);