wangzhengquan
2021-01-30 f2a03c696bcf76bbaba349325abeef7be3979205
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef _MEM_POOL_H_
#define _MEM_POOL_H_  
#include "mm.h"
#include "sem_util.h"
#define MEM_POOL_COND_KEY 0x8801
 
 
// static int mem_pool_mutex  = SemUtil::get(MEM_POOL_COND_KEY, 1);
 
static inline void mem_pool_init(size_t heap_size) {
    mm_init(heap_size);
}
 
static inline void mem_pool_destroy(void) {
    mm_destroy();
    
}
 
static inline void *mem_pool_malloc (size_t size) {
    return  mm_malloc(size);
}
 
 
static inline void mem_pool_free (void *ptr) {
    mm_free(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) {
    return mm_free_by_key(key);
}
 
 
static inline void *mem_pool_realloc (void *ptr, size_t size) {
    return mm_realloc(ptr, size);
}
 
static inline int mem_pool_alloc_key() {
     
    return mm_alloc_key();
}
 
 
// extern int mm_checkheap(int verbose);
 
 
#endif