提交 c76b3d0e 编写于 作者: Y Yifan Hao

Hash table cleanup [4/n]

1. Minor refactoring of taosHashInit
2. Complete error handling path of taosHashInit

* Testing
compile tdengine
cmake .. && cmake --build .
上级 89afece7
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
#include "tulog.h" #include "tulog.h"
#include "taosdef.h" #include "taosdef.h"
/*
* Macro definition
*/
#define EXT_SIZE 1024 #define EXT_SIZE 1024
#define HASH_MAX_CAPACITY (1024 * 1024 * 16) #define HASH_MAX_CAPACITY (1024 * 1024 * 16)
#define HASH_DEFAULT_LOAD_FACTOR (0.75) #define HASH_DEFAULT_LOAD_FACTOR (0.75)
...@@ -43,6 +47,10 @@ ...@@ -43,6 +47,10 @@
#define GET_HASH_NODE_DATA(_n) ((char*)(_n) + sizeof(SHashNode)) #define GET_HASH_NODE_DATA(_n) ((char*)(_n) + sizeof(SHashNode))
#define GET_HASH_PNODE(_n) ((SHashNode *)((char*)(_n) - sizeof(SHashNode))) #define GET_HASH_PNODE(_n) ((SHashNode *)((char*)(_n) - sizeof(SHashNode)))
/*
* typedef
*/
typedef void (*_hash_free_fn_t)(void *param); typedef void (*_hash_free_fn_t)(void *param);
typedef struct SHashEntry { typedef struct SHashEntry {
...@@ -65,6 +73,10 @@ typedef struct SHashObj { ...@@ -65,6 +73,10 @@ typedef struct SHashObj {
SArray *pMemBlock; // memory block allocated for SHashEntry SArray *pMemBlock; // memory block allocated for SHashEntry
} SHashObj; } SHashObj;
/*
* Function definition
*/
static FORCE_INLINE void __wr_lock(void *lock, int32_t type) { static FORCE_INLINE void __wr_lock(void *lock, int32_t type) {
if (type == HASH_NO_LOCK) { if (type == HASH_NO_LOCK) {
return; return;
...@@ -168,8 +180,8 @@ static FORCE_INLINE void doUpdateHashNode(SHashObj *pHashObj, SHashEntry* pe, SH ...@@ -168,8 +180,8 @@ static FORCE_INLINE void doUpdateHashNode(SHashObj *pHashObj, SHashEntry* pe, SH
/** /**
* Insert the hash node at the front of the linked list * Insert the hash node at the front of the linked list
* *
* @param pHashObj * @param pHashObj hash table object
* @param pNode * @param pNode the old node with requested key
*/ */
static void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode); static void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode);
...@@ -191,9 +203,9 @@ static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj); ...@@ -191,9 +203,9 @@ static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj);
* @return hash table object * @return hash table object
*/ */
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type) { SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type) {
assert(fn != NULL);
if (fn == NULL) { if (fn == NULL) {
uError("hash table must have a valid hash function"); uError("hash table must have a valid hash function");
assert(0);
return NULL; return NULL;
} }
...@@ -209,28 +221,43 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp ...@@ -209,28 +221,43 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp
// the max slots is not defined by user // the max slots is not defined by user
pHashObj->capacity = taosHashCapacity((int32_t)capacity); pHashObj->capacity = taosHashCapacity((int32_t)capacity);
assert((pHashObj->capacity & (pHashObj->capacity - 1)) == 0);
pHashObj->equalFp = memcmp; pHashObj->equalFp = memcmp;
pHashObj->hashFp = fn; pHashObj->hashFp = fn;
pHashObj->type = type; pHashObj->type = type;
pHashObj->enableUpdate = update; pHashObj->enableUpdate = update;
assert((pHashObj->capacity & (pHashObj->capacity - 1)) == 0);
pHashObj->hashList = (SHashEntry **)calloc(pHashObj->capacity, sizeof(void *)); pHashObj->hashList = (SHashEntry **)calloc(pHashObj->capacity, sizeof(void *));
if (pHashObj->hashList == NULL) { if (pHashObj->hashList == NULL) {
free(pHashObj); free(pHashObj);
uError("failed to allocate memory, reason:%s", strerror(errno)); uError("failed to allocate memory, reason:%s", strerror(errno));
return NULL; return NULL;
} else { }
pHashObj->pMemBlock = taosArrayInit(8, sizeof(void *));
void *p = calloc(pHashObj->capacity, sizeof(SHashEntry)); pHashObj->pMemBlock = taosArrayInit(8, sizeof(void *));
for (int32_t i = 0; i < pHashObj->capacity; ++i) { if (pHashObj->pMemBlock == NULL) {
pHashObj->hashList[i] = (void *)((char *)p + i * sizeof(SHashEntry)); free(pHashObj->hashList);
} free(pHashObj);
uError("failed to allocate memory, reason:%s", strerror(errno));
return NULL;
}
void *p = calloc(pHashObj->capacity, sizeof(SHashEntry));
if (p == NULL) {
taosArrayDestroy(pHashObj->pMemBlock);
free(pHashObj->hashList);
free(pHashObj);
uError("failed to allocate memory, reason:%s", strerror(errno));
return NULL;
}
taosArrayPush(pHashObj->pMemBlock, &p); for (int32_t i = 0; i < pHashObj->capacity; ++i) {
pHashObj->hashList[i] = (void *)((char *)p + i * sizeof(SHashEntry));
} }
taosArrayPush(pHashObj->pMemBlock, &p);
return pHashObj; return pHashObj;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册