Storage: rename Init to Alloc to reflect actual functioning. Comment updates.

pull/466/head
Victor Julien 13 years ago
parent f06694d0c1
commit 5a7bf53a6b

@ -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);

@ -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) {

@ -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;

@ -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__ */

@ -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;

@ -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__ */

@ -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;

@ -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);

Loading…
Cancel
Save