]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
add concurrent hashmap pert
authorseantywork <seantywork@gmail.com>
Tue, 19 Aug 2025 05:56:19 +0000 (14:56 +0900)
committerseantywork <seantywork@gmail.com>
Tue, 19 Aug 2025 05:56:19 +0000 (14:56 +0900)
hashmap-concurrent-perf/2508-03.xyz.md [new file with mode: 0644]
hashmap-concurrent-perf/Makefile [new file with mode: 0644]
hashmap-concurrent-perf/cmap.c [new file with mode: 0644]
hashmap-concurrent-perf/cmap.h [new file with mode: 0644]

diff --git a/hashmap-concurrent-perf/2508-03.xyz.md b/hashmap-concurrent-perf/2508-03.xyz.md
new file mode 100644 (file)
index 0000000..792d600
--- /dev/null
@@ -0,0 +1 @@
+#
diff --git a/hashmap-concurrent-perf/Makefile b/hashmap-concurrent-perf/Makefile
new file mode 100644 (file)
index 0000000..0833ac0
--- /dev/null
@@ -0,0 +1,7 @@
+all:
+
+       gcc -g -O2 -o cmap.out cmap.c -lpthread -lcrypto -lssl
+
+clean:
+       
+       rm -rf *.out *.o
\ No newline at end of file
diff --git a/hashmap-concurrent-perf/cmap.c b/hashmap-concurrent-perf/cmap.c
new file mode 100644 (file)
index 0000000..6e4aa3b
--- /dev/null
@@ -0,0 +1,151 @@
+#include "cmap.h"
+
+
+#ifdef HASHCUSTOM
+static inline uint64_t _hashfunc(uint8_t* data, size_t size){}
+
+#else
+#define HASHLEN 32
+#define HASHTRUNCLEN 8
+static inline uint64_t _hashfunc(uint8_t* data, size_t size, uint64_t div){
+
+       uint64_t hashtrunc = 0;
+       uint8_t hash[HASHLEN] = {0};
+       EVP_MD_CTX *mdctx;
+       int digest_len = 0;
+       mdctx = EVP_MD_CTX_new();
+       EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
+       EVP_DigestUpdate(mdctx, data, size);
+       EVP_DigestFinal_ex(mdctx, hash, &digest_len);
+       EVP_MD_CTX_free(mdctx);
+       memcpy(&hashtrunc, hash, HASHTRUNCLEN);
+       
+       return hashtrunc % div;
+}
+#endif
+
+BOGUS_CMAP* cmap_alloc(int buck_size, int data_size){
+
+    BOGUS_CMAP* cm = (BOGUS_CMAP*)malloc(sizeof(BOGUS_CMAP));
+    cm->buck = (BOGUS_BUCKET*)malloc(buck_size * sizeof(BOGUS_BUCKET));
+    cm->buck_size = buck_size;
+    for(int i = 0; i < buck_size; i++){
+        pthread_mutex_init(&cm->buck[i].lock, NULL);
+        for(int j = 0 ; j < data_size; j++){
+            BOGUS_DATA* data = (BOGUS_DATA*)malloc(sizeof(BOGUS_DATA));
+            memset(data, 0, sizeof(BOGUS_DATA));
+            data->next = cm->buck[i].data;
+            cm->buck[i].data = data;
+        }
+    }
+    return cm;
+}
+
+int cmap_set(BOGUS_CMAP* cm, BOGUS_DATA* val){
+    int idx = _hashfunc((void*)val, sizeof(BOGUS_KEY), cm->buck_size);
+    pthread_mutex_lock(&cm->buck[idx].lock);
+    BOGUS_DATA* data = cm->buck[idx].data;
+    for(;;){
+        if(data == NULL){
+            BOGUS_DATA* newdata = (BOGUS_DATA*)malloc(sizeof(BOGUS_DATA));
+            memset(newdata, 0, sizeof(BOGUS_DATA));
+            newdata->index = val->index;
+            newdata->value = val->value;
+            newdata->in_use = 1;
+            newdata->next = cm->buck[idx].data;
+            cm->buck[idx].data = newdata;
+            break;
+        }
+        if(data->in_use){
+            data = data->next;
+            continue;
+        }
+        data->index = val->index;
+        data->value = val->value;
+        data->in_use = 1;
+        break;
+    }
+    pthread_mutex_unlock(&cm->buck[idx].lock);
+    return idx;
+}
+
+int cmap_get(BOGUS_CMAP* cm, BOGUS_DATA* key, void* ret, void (*cb)(void* ret, void* data)){
+    int idx = _hashfunc((void*)key, sizeof(BOGUS_KEY), cm->buck_size);
+    pthread_mutex_lock(&cm->buck[idx].lock);
+    BOGUS_DATA* data = cm->buck[idx].data;
+    for(;;){
+        if(data == NULL){
+            idx = -1;
+            goto err;
+        }
+        if(!data->in_use){
+            data = data->next;
+            continue;
+        }
+        if(data->index == key->index){
+            goto found;
+        }
+        data = data->next;
+    }
+found:
+    if(ret != NULL && cb != NULL){
+        cb(ret, data);
+    }
+err:
+    pthread_mutex_unlock(&cm->buck[idx].lock);
+    return idx;
+}
+
+int cmap_del(BOGUS_CMAP* cm, BOGUS_DATA* key){
+    int idx = _hashfunc((void*)key, sizeof(BOGUS_KEY), cm->buck_size);
+    pthread_mutex_lock(&cm->buck[idx].lock);
+    BOGUS_DATA* data = cm->buck[idx].data;
+    BOGUS_DATA* tmp = NULL;
+    for(;;){
+        if(data == NULL){
+            idx = -1;
+            break;
+        }
+        if(!data->in_use){
+            data = data->next;
+            continue;
+        }
+        if(data->index == key->index){
+            tmp = data->next;
+            memset(data, 0, sizeof(BOGUS_DATA));
+            data->next = tmp;
+            break;
+        }
+        data = data->next;
+    }
+    pthread_mutex_unlock(&cm->buck[idx].lock);
+    return idx;
+}
+
+void cmap_free(BOGUS_CMAP* cm){
+
+    for(int i = 0 ; i < cm->buck_size; i++){
+        BOGUS_DATA* tmp = NULL;
+        pthread_mutex_lock(&cm->buck[i].lock);
+        for(;;){
+            if(cm->buck[i].data == NULL){
+                break;
+            }
+            tmp = cm->buck[i].data->next;
+            free(cm->buck[i].data);
+            cm->buck[i].data = tmp;
+        }
+        pthread_mutex_unlock(&cm->buck[i].lock);
+    }
+    free(cm->buck);
+    free(cm);
+
+}
+
+
+int main(int argc, char** argv){
+
+
+
+    return 0;
+}
\ No newline at end of file
diff --git a/hashmap-concurrent-perf/cmap.h b/hashmap-concurrent-perf/cmap.h
new file mode 100644 (file)
index 0000000..c12a2fc
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef _CMAP_PERF_H_ 
+#define _CMAP_PERF_H_
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <pthread.h>
+
+#include <openssl/evp.h>
+#include <openssl/hmac.h>
+#include <openssl/err.h>
+
+#define _BOGUS_KEY struct { \
+    uint64_t index; \
+}
+
+#define BOGUS_KEY _BOGUS_KEY
+
+typedef struct BOGUS_DATA BOGUS_DATA;
+
+struct BOGUS_DATA {
+    BOGUS_KEY;
+    uint64_t value;
+    uint64_t in_use;
+    BOGUS_DATA* next;
+};
+
+typedef struct BOGUS_BUCKET{
+    BOGUS_DATA* data;
+    pthread_mutex_t lock;
+} BOGUS_BUCKET;
+
+typedef struct BOGUS_CMAP{
+    BOGUS_BUCKET* buck;
+    int buck_size;
+} BOGUS_CMAP;
+
+BOGUS_CMAP* cmap_alloc(int buck_size, int data_size);
+int cmap_set(BOGUS_CMAP* cm, BOGUS_DATA* val);
+int cmap_get(BOGUS_CMAP* cm, BOGUS_DATA* key, void* ret, void (*cb)(void* ret, void* data));
+int cmap_del(BOGUS_CMAP* cm, BOGUS_DATA* key);
+void cmap_free(BOGUS_CMAP* cm);
+
+
+
+#endif
\ No newline at end of file