13 for(index=0;index< 64;index++) {
14 if((data)&(0x01<<index))
break;
21 uint64_t current_size=size;
22 int digit = 0, tmp_digit=0;
23 while(current_size != 0) {
25 digit += tmp_digit + 1;
26 current_size = current_size >> tmp_digit + 1;
68 if(arg) pthread_mutex_unlock((pthread_mutex_t *)arg);
72 if(this->lock) pthread_mutex_lock(this->lock);
74 #define MPOOL_LOCK(this) \ 75 pthread_mutex_lock_(this);\ 76 pthread_cleanup_push(pthread_mutex_unlock_, this->lock); 77 #define MPOOL_UNLOCK pthread_cleanup_pop(1); 84 data->
prev = this->tail;
87 if(this->tail) this->tail->
next = data;
91 if(!this->head) this->head = data;
98 if(this->head == data) this->head = data->
next;
102 if(this->tail == data) this->tail = data->
prev;
110 data->
next = this->head;
112 this->head->
prev = data;
118 if(!this->head->used) {
139 memory = memory->
prev;
143 if(memory && memory->
used)
return memory->
mem;
159 return ((uint64_t)ptr - (uint64_t)buffer_list) >> this->slide_bit;
164 return (uint64_t)ptr < (uint64_t)this->user_buf || (uint64_t)(this->user_buf) + this->max_cnt * this->max_size < (uint64_t)ptr;
170 MemoryPool mpool_create(
size_t max_size,
size_t max_cnt,
int is_multithread,
void (*constructor)(
void *
this,
void *parameter),
void * constructor_parameter) {
177 if(is_multithread) mutex_size =
sizeof(pthread_mutex_t);
183 max_size = 0x01 << slide_bit;
185 instance = malloc(
sizeof(*instance) + mutex_size + max_cnt * (
sizeof(
malloc_data_t) + max_size));
186 if(!instance)
return NULL;
187 memset(instance, 0,
sizeof(*instance) + mutex_size + max_cnt * (
sizeof(
malloc_data_t) + max_size));
192 void *correut = (instance + 1);
195 instance->
lock=(pthread_mutex_t *) (correut);
196 correut = instance->
lock + 1;
197 pthread_mutex_init(instance->
lock, NULL);
199 instance->
lock = NULL;
203 instance->
buf = (uint8_t *)(correut);
212 for(i=0;i<max_cnt;i++) {
214 memory->
mem = correut;
215 correut = memory->
mem + max_size;
216 if(constructor) constructor(memory->
mem, constructor_parameter);
228 destructor(memory->
mem);
229 memory = memory->
next;
240 if(mem) this->cur_cnt++;
261 retval = this->cur_cnt;
283 fprintf(stderr,
"used:%d\n", memory->
used);
284 memory = memory->
next;
static uint64_t mpool_get_buffer_place(MemoryPool this, uint8_t *buffer_list, void *ptr)
uint8_t * buf
list of buffer for user + list of malloc_data_t
void mpool_release(MemoryPool this, void *ptr)
release memory to pool
void * mpool_get(MemoryPool this)
get memory from pool
struct maloc_data_t * prev
static void mpool_list_pull(MemoryPool this, malloc_data_t *data)
static void mpool_list_push(MemoryPool this, malloc_data_t *data)
void * mpool_get_next_usedmem(MemoryPool this, void *ptr)
get used memory
void(* free)(EventInstance this)
struct maloc_data_t malloc_data_t
size_t slide_bit
keep slide bit size related to max_size, to search buffer fast
static int get_bit_digit_index_over_size(uint64_t size)
get minimum of x, which 2^x > input_value
struct memorypool_t memorypool_t
void mpool_show(MemoryPool this)
static int mpool_is_not_ptr_in_buf(MemoryPool this, void *ptr)
struct maloc_data_t * next
static void mpool_unuse_memory(MemoryPool this, void *ptr)
static void pthread_mutex_lock_(MemoryPool this)
static void * mpool_get_next_memory(MemoryPool this, void *ptr)
uint8_t * user_buf
list of buffer for user
static void mpool_list_head(MemoryPool this, malloc_data_t *data)
pthread_mutex_t * lock
mutex
static void * mpool_get_memory(MemoryPool this)
size_t mpool_get_usedcnt(MemoryPool this)
get used cnt
static void pthread_mutex_unlock_(void *arg)
static int get_far_right_bit_index(uint64_t data)
get right bit index
void mpool_delete(MemoryPool this, void(*destructor)(void *))
free MemoryPool class
MemoryPool class member definition, detail is defined in C file.
This is API for memory pool.
MemoryPool mpool_create(size_t max_size, size_t max_cnt, int is_multithread, void(*constructor)(void *this, void *parameter), void *constructor_parameter)