]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Storage: rename Init to Alloc to reflect actual functioning. Comment updates.
authorVictor Julien <victor@inliniac.net>
Mon, 22 Jul 2013 15:33:19 +0000 (17:33 +0200)
committerVictor Julien <victor@inliniac.net>
Sun, 28 Jul 2013 21:41:11 +0000 (23:41 +0200)
src/detect-engine-tag.c
src/detect-engine-threshold.c
src/flow-storage.c
src/flow-storage.h
src/host-storage.c
src/host-storage.h
src/util-storage.c
src/util-storage.h

index 084b775db0e046ba9e0e0ad13fa6a5f2076dd655..fd5496a0689a4cd56b84a5ce8d064fb5e76f639c 100644 (file)
@@ -39,8 +39,8 @@
 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);
index 51849c588317f520fb34493643f358de61715880..1c0bb4c10f4e0ca6b87516063d8ec366fad05caf 100644 (file)
@@ -74,6 +74,10 @@ int ThresholdHostStorageId(void) {
 
 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) {
index d8deca27ced89a64ff5755eff6a71914296c5a71..4a1b154710ee3ce6c7517e0358f5107bed50ce82 100644 (file)
@@ -56,13 +56,13 @@ void FlowFreeStorage(Flow *f) {
         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;
 }
@@ -74,13 +74,13 @@ static void StorageTestFree(void *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;
 
@@ -200,7 +200,7 @@ static int FlowStorageTest03(void) {
     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;
 
index aa5a98dfa52c50632445e279aee71baed1bfc2c7..93892d1684d168819282cf37c60cb203aad4765e 100644 (file)
@@ -40,6 +40,6 @@ void FlowFreeStorage(Flow *h);
 
 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__ */
index b17606ae518e76e0b39726f520095d65a4ddb6bf..13df14e4582137756abfaa0c7cfa6ba9c3171825 100644 (file)
@@ -52,13 +52,13 @@ void HostFreeStorage(Host *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;
 }
@@ -70,13 +70,13 @@ static void StorageTestFree(void *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;
 
@@ -203,7 +203,7 @@ static int HostStorageTest03(void) {
     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;
 
index e5fe4f4b4c6853a6307c342c49b17ba4fbe35113..b2bccbe26ece6300fee432ce0a742ff318ae0628 100644 (file)
@@ -40,6 +40,6 @@ void HostFreeStorage(Host *h);
 
 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__ */
index b534ca191b3ae60000f2482460d7f2bd9df656fd..2c7a96733085aabe62a78517dffd52a80a2a0436 100644 (file)
@@ -31,7 +31,7 @@ typedef struct StorageMapping_ {
     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;
 
@@ -89,12 +89,12 @@ void StorageCleanup(void) {
     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;
@@ -118,7 +118,7 @@ int StorageRegister(const StorageEnum type, const char *name, const unsigned int
     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]++;
@@ -162,7 +162,7 @@ int StorageFinalize(void) {
             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;
         }
 
@@ -227,8 +227,8 @@ void *StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id) {
     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;
         }
@@ -253,8 +253,8 @@ void *StorageAllocById(Storage **storage, StorageEnum type, int id) {
     }
     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;
@@ -325,7 +325,7 @@ void StorageFree(Storage **storage, StorageEnum type) {
 
 #ifdef UNITTESTS
 
-static void *StorageTestInit(unsigned int size) {
+static void *StorageTestAlloc(unsigned int size) {
     void *x = SCMalloc(size);
     return x;
 }
@@ -337,13 +337,13 @@ static void StorageTestFree(void *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;
 
@@ -439,10 +439,10 @@ 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;
@@ -454,43 +454,43 @@ static int StorageTest03(void) {
         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;
index 880cb129f373a4065e941b51b9522326e631e483..0165ad5d55d09abbd194697de8de99e688e24cc5 100644 (file)
@@ -38,15 +38,33 @@ typedef void* Storage;
 
 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);