From f12c014371ec9dc9b0c62db3e45df90cc97e9b31 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 28 七月 2020 11:02:00 +0800
Subject: [PATCH] commit
---
src/queue/include/mem_pool.h | 35 ++++++++++++++++++++++++++++++++---
1 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/src/queue/include/mem_pool.h b/src/queue/include/mem_pool.h
index 17a7c5c..72ad5b7 100644
--- a/src/queue/include/mem_pool.h
+++ b/src/queue/include/mem_pool.h
@@ -34,6 +34,33 @@
return ptr;
}
+template <typename T>
+static inline T* mem_pool_attach(int key) {
+ void *ptr;
+ // T* tptr;
+ hashtable_t *hashtable = mm_get_hashtable();
+ ptr = hashtable_get(hashtable, key);
+// printf("mem_pool_malloc_by_key malloc before %d, %p\n", key, ptr);
+ if(ptr == NULL || ptr == (void *)1 ) {
+ ptr = mm_malloc(sizeof(T));
+ hashtable_put(hashtable, key, ptr);
+ new(ptr) T;
+// printf("mem_pool_malloc_by_key use new %d, %p\n", key, ptr);
+ }
+ return (T*)ptr;
+}
+
+static inline void mem_pool_free_by_key(int key) {
+ void *ptr;
+ // T* tptr;
+ hashtable_t *hashtable = mm_get_hashtable();
+ ptr = hashtable_get(hashtable, key);
+ if(ptr != NULL) {
+ mm_free(ptr);
+ hashtable_remove(hashtable, key);
+ }
+}
+
static inline void mem_pool_free (void *ptr) {
mm_free(ptr);
// notify malloc
@@ -45,10 +72,12 @@
return mm_realloc(ptr, size);
}
-static inline hashtable_t * mem_pool_get_hashtable() {
- return mm_get_hashtable();
-
+static inline int mem_pool_alloc_key() {
+ hashtable_t *hashtable = mm_get_hashtable();
+ return hashtable_alloc_key(hashtable);
}
+
+
// extern int mm_checkheap(int verbose);
--
Gitblit v1.8.0