实现内存池的测试用例、继承jemalloc和自实现内存池。

This commit is contained in:
2026-01-09 20:09:05 +08:00
parent 4b4e06b33d
commit 1adb24482b
16 changed files with 664 additions and 436 deletions

280
hash.c
View File

@@ -1,228 +1,228 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
// #include <stdio.h>
// #include <string.h>
// #include <stdlib.h>
// #include <pthread.h>
#define MAX_KEY_LEN 128
#define MAX_VALUE_LEN 512
#define MAX_TABLE_SIZE 1024
// #define MAX_KEY_LEN 128
// #define MAX_VALUE_LEN 512
// #define MAX_TABLE_SIZE 1024
typedef struct hashnode_s {
// typedef struct hashnode_s {
char key[MAX_KEY_LEN];
char value[MAX_VALUE_LEN];
// char key[MAX_KEY_LEN];
// char value[MAX_VALUE_LEN];
struct hashnode_s *next;
// struct hashnode_s *next;
} hashnode_t;
// } hashnode_t;
typedef struct hashtable_s {
// typedef struct hashtable_s {
hashnode_t **nodes; //* change **,
// hashnode_t **nodes; //* change **,
int max_slots;
int count;
// int max_slots;
// int count;
pthread_mutex_t lock;
// pthread_mutex_t lock;
} hashtable_t;
// } hashtable_t;
hashtable_t hash;
// hashtable_t hash;
//Connection
// 'C' + 'o' + 'n'
static int _hash(char *key, int size) {
// //Connection
// // 'C' + 'o' + 'n'
// static int _hash(char *key, int size) {
if (!key) return -1;
// if (!key) return -1;
int sum = 0;
int i = 0;
// int sum = 0;
// int i = 0;
while (key[i] != 0) {
sum += key[i];
i ++;
}
// while (key[i] != 0) {
// sum += key[i];
// i ++;
// }
return sum % size;
// return sum % size;
}
// }
hashnode_t *_create_node(char *key, char *value) {
// hashnode_t *_create_node(char *key, char *value) {
hashnode_t *node = (hashnode_t*)malloc(sizeof(hashnode_t));
if (!node) return NULL;
// hashnode_t *node = (hashnode_t*)malloc(sizeof(hashnode_t));
// if (!node) return NULL;
strncpy(node->key, key, MAX_KEY_LEN);
strncpy(node->value, value, MAX_VALUE_LEN);
node->next = NULL;
// strncpy(node->key, key, MAX_KEY_LEN);
// strncpy(node->value, value, MAX_VALUE_LEN);
// node->next = NULL;
return node;
}
// return node;
// }
//
int init_hashtable(hashtable_t *hash) {
// //
// int init_hashtable(hashtable_t *hash) {
if (!hash) return -1;
// if (!hash) return -1;
hash->nodes = (hashnode_t**)malloc(sizeof(hashnode_t*) * MAX_TABLE_SIZE);
if (!hash->nodes) return -1;
// hash->nodes = (hashnode_t**)malloc(sizeof(hashnode_t*) * MAX_TABLE_SIZE);
// if (!hash->nodes) return -1;
hash->max_slots = MAX_TABLE_SIZE;
hash->count = 0;
// hash->max_slots = MAX_TABLE_SIZE;
// hash->count = 0;
pthread_mutex_init(&hash->lock, NULL);
// pthread_mutex_init(&hash->lock, NULL);
return 0;
}
// return 0;
// }
//
void dest_hashtable(hashtable_t *hash) {
// //
// void dest_hashtable(hashtable_t *hash) {
if (!hash) return;
// if (!hash) return;
int i = 0;
for (i = 0;i < hash->max_slots;i ++) {
hashnode_t *node = hash->nodes[i];
// int i = 0;
// for (i = 0;i < hash->max_slots;i ++) {
// hashnode_t *node = hash->nodes[i];
while (node != NULL) { // error
// while (node != NULL) { // error
hashnode_t *tmp = node;
node = node->next;
hash->nodes[i] = node;
// hashnode_t *tmp = node;
// node = node->next;
// hash->nodes[i] = node;
free(tmp);
// free(tmp);
}
}
// }
// }
free(hash->nodes);
// free(hash->nodes);
}
// }
// mp
int put_kv_hashtable(hashtable_t *hash, char *key, char *value) {
// // mp
// int put_kv_hashtable(hashtable_t *hash, char *key, char *value) {
if (!hash || !key || !value) return -1;
// if (!hash || !key || !value) return -1;
int idx = _hash(key, MAX_TABLE_SIZE);
// int idx = _hash(key, MAX_TABLE_SIZE);
pthread_mutex_lock(&hash->lock);
// pthread_mutex_lock(&hash->lock);
hashnode_t *node = hash->nodes[idx];
#if 1
while (node != NULL) {
if (strcmp(node->key, key) == 0) { // exist
pthread_mutex_unlock(&hash->lock);
return 1;
}
node = node->next;
}
#endif
// hashnode_t *node = hash->nodes[idx];
// #if 1
// while (node != NULL) {
// if (strcmp(node->key, key) == 0) { // exist
// pthread_mutex_unlock(&hash->lock);
// return 1;
// }
// node = node->next;
// }
// #endif
hashnode_t *new_node = _create_node(key, value);
new_node->next = hash->nodes[idx];
hash->nodes[idx] = new_node;
// hashnode_t *new_node = _create_node(key, value);
// new_node->next = hash->nodes[idx];
// hash->nodes[idx] = new_node;
hash->count ++;
// hash->count ++;
pthread_mutex_unlock(&hash->lock);
// pthread_mutex_unlock(&hash->lock);
return 0;
}
// return 0;
// }
char * get_kv_hashtable(hashtable_t *hash, char *key) {
// char * get_kv_hashtable(hashtable_t *hash, char *key) {
if (!hash || !key) return NULL;
// if (!hash || !key) return NULL;
int idx = _hash(key, MAX_TABLE_SIZE);
// int idx = _hash(key, MAX_TABLE_SIZE);
pthread_mutex_lock(&hash->lock);
hashnode_t *node = hash->nodes[idx];
// pthread_mutex_lock(&hash->lock);
// hashnode_t *node = hash->nodes[idx];
while (node != NULL) {
// while (node != NULL) {
if (strcmp(node->key, key) == 0) {
pthread_mutex_unlock(&hash->lock);
return node->value;
}
// if (strcmp(node->key, key) == 0) {
// pthread_mutex_unlock(&hash->lock);
// return node->value;
// }
node = node->next;
}
// node = node->next;
// }
pthread_mutex_unlock(&hash->lock);
// pthread_mutex_unlock(&hash->lock);
return NULL;
// return NULL;
}
// }
int count_kv_hashtable(hashtable_t *hash) {
return hash->count;
}
// int count_kv_hashtable(hashtable_t *hash) {
// return hash->count;
// }
int delete_kv_hashtable(hashtable_t *hash, char *key) {
if (!hash || !key) return -2;
// int delete_kv_hashtable(hashtable_t *hash, char *key) {
// if (!hash || !key) return -2;
int idx = _hash(key, MAX_TABLE_SIZE);
// int idx = _hash(key, MAX_TABLE_SIZE);
pthread_mutex_lock(&hash->lock);
hashnode_t *head = hash->nodes[idx];
if (head == NULL) return -1; // noexist
// head node
if (strcmp(head->key, key) == 0) {
hashnode_t *tmp = head->next;
hash->nodes[idx] = tmp;
// pthread_mutex_lock(&hash->lock);
// hashnode_t *head = hash->nodes[idx];
// if (head == NULL) return -1; // noexist
// // head node
// if (strcmp(head->key, key) == 0) {
// hashnode_t *tmp = head->next;
// hash->nodes[idx] = tmp;
free(head);
hash->count --;
pthread_mutex_unlock(&hash->lock);
// free(head);
// hash->count --;
// pthread_mutex_unlock(&hash->lock);
return 0;
}
// return 0;
// }
hashnode_t *cur = head;
while (cur->next != NULL) {
if (strcmp(cur->next->key, key) == 0) break; // search node
// hashnode_t *cur = head;
// while (cur->next != NULL) {
// if (strcmp(cur->next->key, key) == 0) break; // search node
cur = cur->next;
}
// cur = cur->next;
// }
if (cur->next == NULL) {
// if (cur->next == NULL) {
pthread_mutex_unlock(&hash->lock);
return -1;
}
// pthread_mutex_unlock(&hash->lock);
// return -1;
// }
hashnode_t *tmp = cur->next;
cur->next = tmp->next;
free(tmp);
hash->count --;
// hashnode_t *tmp = cur->next;
// cur->next = tmp->next;
// free(tmp);
// hash->count --;
pthread_mutex_unlock(&hash->lock);
// pthread_mutex_unlock(&hash->lock);
return 0;
}
// return 0;
// }
int exist_kv_hashtable(hashtable_t *hash, char *key) {
// int exist_kv_hashtable(hashtable_t *hash, char *key) {
char *value = get_kv_hashtable(hash, key);
if (value) return 1;
else return 0;
// char *value = get_kv_hashtable(hash, key);
// if (value) return 1;
// else return 0;
}
// }