return storage[id];
}
-void *StorageAllocById(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];
- Storage *store = *storage;
- if (store == NULL) {
- // coverity[suspicious_sizeof : FALSE]
- store = SCCalloc(storage_max_id[type], sizeof(void *));
- if (unlikely(store == NULL))
- return NULL;
- }
- SCLogDebug("store %p", store);
-
- if (store[id] == NULL && map->Alloc != NULL) {
- store[id] = map->Alloc(map->size);
- if (store[id] == NULL) {
- SCFree(store);
- *storage = NULL;
- return NULL;
- }
- }
-
- *storage = store;
- return store[id];
-}
-
void StorageFreeById(Storage *storage, StorageEnum type, int id)
{
#ifdef DEBUG
}
}
-void StorageFree(Storage **storage, StorageEnum type)
-{
- if (*storage == NULL)
- return;
-
-#ifdef DEBUG
- BUG_ON(!storage_registration_closed);
-#endif
-#ifdef UNITTESTS
- if (storage_map == NULL)
- return;
-#endif
-
- Storage *store = *storage;
- int i;
- for (i = 0; i < storage_max_id[type]; i++) {
- if (store[i] != NULL) {
- StorageMapping *map = &storage_map[type][i];
- map->Free(store[i]);
- store[i] = NULL;
- }
- }
- SCFree(*storage);
- *storage = NULL;
-}
-
#ifdef UNITTESTS
static void *StorageTestAlloc(unsigned int size)
return 0;
}
-struct StorageTest02Data {
- int abc;
-};
-
-static void *StorageTest02Init(unsigned int size)
-{
- struct StorageTest02Data *data = (struct StorageTest02Data *)SCMalloc(size);
- if (data != NULL)
- data->abc = 1234;
- return (void *)data;
-}
-
-static int StorageTest02(void)
-{
- struct StorageTest02Data *test = NULL;
-
- StorageInit();
-
- int id1 = StorageRegister(STORAGE_HOST, "test", 4, StorageTest02Init, StorageTestFree);
- if (id1 < 0) {
- printf("StorageRegister failed (2): ");
- goto error;
- }
- int id2 = StorageRegister(STORAGE_HOST, "test2", 4, StorageTest02Init, StorageTestFree);
- if (id2 < 0) {
- printf("StorageRegister failed (2): ");
- goto error;
- }
-
- if (StorageFinalize() < 0) {
- printf("StorageFinalize failed: ");
- goto error;
- }
-
- Storage *storage = NULL;
- void *data = StorageAllocById(&storage, STORAGE_HOST, id1);
- if (data == NULL) {
- printf("StorageAllocById failed, data == NULL, storage %p: ", storage);
- goto error;
- }
- test = (struct StorageTest02Data *)data;
- if (test->abc != 1234) {
- printf("setup failed, test->abc != 1234, but %d (1):", test->abc);
- goto error;
- }
- test->abc = 4321;
-
- data = StorageAllocById(&storage, STORAGE_HOST, id2);
- if (data == NULL) {
- printf("StorageAllocById failed, data == NULL, storage %p: ", storage);
- goto error;
- }
- test = (struct StorageTest02Data *)data;
- if (test->abc != 1234) {
- printf("setup failed, test->abc != 1234, but %d (2):", test->abc);
- goto error;
- }
-
- data = StorageGetById(storage, STORAGE_HOST, id1);
- if (data == NULL) {
- printf("StorageAllocById failed, data == NULL, storage %p: ", storage);
- goto error;
- }
- test = (struct StorageTest02Data *)data;
- if (test->abc != 4321) {
- printf("setup failed, test->abc != 4321, but %d (3):", test->abc);
- goto error;
- }
-
- //StorageFreeById(storage, STORAGE_HOST, id1);
- //StorageFreeById(storage, STORAGE_HOST, id2);
-
- StorageFree(&storage, STORAGE_HOST);
-
- StorageCleanup();
- return 1;
-error:
- StorageCleanup();
- return 0;
-}
-
static int StorageTest03(void)
{
StorageInit();
void StorageRegisterTests(void)
{
UtRegisterTest("StorageTest01", StorageTest01);
- UtRegisterTest("StorageTest02", StorageTest02);
UtRegisterTest("StorageTest03", StorageTest03);
}
#endif