diff --git a/CMakeLists.txt b/CMakeLists.txt index e15e0b9..a8bd990 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(UDPConnection) set(UDPConnection_SOURCES src/UDPConnection.c src/UDPC_Deque.c + src/UDPC_HashMap.c ) set(CMAKE_C_FLAGS "-Wall -Wno-missing-braces") diff --git a/src/UDPC_Deque.h b/src/UDPC_Deque.h index 567ac78..c3fa935 100644 --- a/src/UDPC_Deque.h +++ b/src/UDPC_Deque.h @@ -3,8 +3,7 @@ #include -typedef struct -{ +typedef struct { uint32_t head; uint32_t tail; uint32_t size; diff --git a/src/UDPC_HashMap.c b/src/UDPC_HashMap.c new file mode 100644 index 0000000..19dc757 --- /dev/null +++ b/src/UDPC_HashMap.c @@ -0,0 +1,69 @@ +#include "UDPC_HashMap.h" + +#include + +UDPC_HashMap* UDPC_HashMap_init(uint32_t capacity, uint32_t unitSize) +{ + UDPC_HashMap *m = malloc(sizeof(UDPC_HashMap)); + if(!m) + { + return NULL; + } + + int fail = 0; + m->size = 0; + m->capacity = (capacity > 9 ? capacity : 10); + m->unitSize = unitSize; + m->buckets = malloc(sizeof(UDPC_Deque) * m->capacity); + if(!m->buckets) + { + free(m); + return NULL; + } + + for(int x = 0; x < m->capacity; ++x) + { + if(fail != 0) + { + (&m->buckets[x])->buf = NULL; + continue; + } + + UDPC_Deque_clear(m->buckets + x); + (m->buckets + x)->alloc_size = 8 * (sizeof(uint32_t) + unitSize); + (m->buckets + x)->buf = malloc(8 * (sizeof(uint32_t) + unitSize)); + if(!(m->buckets + x)->buf) + { + fail = 1; + } + } + + if(fail != 0) + { + for(int x = 0; x < m->capacity; ++x) + { + if((m->buckets + x)->buf) + { + free((m->buckets + x)->buf); + } + } + free(m->buckets); + free(m); + return NULL; + } + + return m; +} + +void UDPC_HashMap_destroy(UDPC_HashMap *hashMap) +{ + for(int x = 0; x < hashMap->capacity; ++x) + { + while((hashMap->buckets + x)->size > 0) + { + UDPC_Deque_pop_back(hashMap->buckets + x, sizeof(uint32_t) + hashMap->unitSize); + } + } + free(hashMap->buckets); + free(hashMap); +} diff --git a/src/UDPC_HashMap.h b/src/UDPC_HashMap.h new file mode 100644 index 0000000..2685441 --- /dev/null +++ b/src/UDPC_HashMap.h @@ -0,0 +1,17 @@ +#ifndef UDPC_HASHMAP_H +#define UDPC_HASHMAP_H + +#include "UDPC_Deque.h" + +typedef struct { + uint32_t size; + uint32_t capacity; + uint32_t unitSize; + UDPC_Deque *buckets; +} UDPC_HashMap; + +UDPC_HashMap* UDPC_HashMap_init(uint32_t capacity, uint32_t unitSize); + +void UDPC_HashMap_destroy(UDPC_HashMap *hashMap); + +#endif diff --git a/src/UDPConnection.h b/src/UDPConnection.h index 0ed0646..9a1c86c 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -27,8 +27,7 @@ #define UDPC_ATOSTR_BUF_SIZE 16 /// This struct should not be used outside of this library -typedef struct -{ +typedef struct { uint32_t addr; // in network order (big-endian) uint32_t id; /* @@ -43,8 +42,7 @@ typedef struct } UDPC_INTERNAL_PacketInfo; /// This struct should not be used outside of this library -typedef struct -{ +typedef struct { /* * 0x1 - trigger send * 0x2 - is good mode @@ -69,8 +67,7 @@ typedef struct } UDPC_INTERNAL_ConnectionData; /// This struct should not be modified, only passed to functions that require it -typedef struct -{ +typedef struct { /* * 0x1 - is threaded * 0x2 - is client