-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmempool.c
More file actions
64 lines (60 loc) · 2.2 KB
/
Copy pathmempool.c
File metadata and controls
64 lines (60 loc) · 2.2 KB
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
64
#include "mempool.h"
memory_pool_t* memory_pool_create(size_t size){
memory_pool_t* memory_pool = (memory_pool_t*)malloc(sizeof(memory_pool_t));
memory_pool->block = (conn_data_t*)malloc(sizeof(conn_data_t) * size);
memory_pool->size = 0;
memory_pool->max_size = size;
memory_pool->head = memory_pool->tail = NULL;
for(size_t i = 0;i < size;i++){
memory_pool->tail = &memory_pool->block[i];
if(memory_pool->head != NULL){
memory_pool->head->next = memory_pool->tail;
}
memory_pool->tail->index = (uint16_t)i;
memory_pool->tail->fd = 0;
memset(&memory_pool->tail->data, 0, sizeof(memory_pool->tail->data));
memset(&memory_pool->tail->target, 0, sizeof(target_conn_data_t));
memory_pool->head = memory_pool->tail;
}
memory_pool->head = &memory_pool->block[0];
return memory_pool;
}
int memory_pool_get(memory_pool_t* memory_pool, conn_data_t** conn_data){
if(memory_pool->size == memory_pool->max_size ||
memory_pool == NULL ||
memory_pool->head == NULL){
return -1;
}
*conn_data = memory_pool->head;
memory_pool->head = memory_pool->head->next;
(*conn_data)->flags = CONN_CLIENT;
(*conn_data)->target.flags = CONN_TARGET;
(*conn_data)->target.client = *conn_data;
(*conn_data)->next = NULL;
memory_pool->size++;
return 0;
}
int memory_pool_release(memory_pool_t* memory_pool, conn_data_t** conn_data){
if(memory_pool == NULL ||
conn_data == NULL ||
memory_pool->tail == NULL){
return -1;
}
memory_pool->tail->next = *conn_data;
memory_pool->tail = *conn_data;
memory_pool->tail->next = NULL;
memory_pool->tail->flags = 0;
memory_pool->tail->fd = 0;
if((memory_pool->tail->data.req.flags & CPROXY_ALLOCATED_BUFFER) &&
memory_pool->tail->data.req.buffer != NULL){
free(memory_pool->tail->data.req.buffer);
}
memset(&memory_pool->tail->data, 0, sizeof(memory_pool->tail->data));
memset(&memory_pool->tail->target, 0, sizeof(target_conn_data_t));
memory_pool->size--;
return 0;
}
void memory_pool_destroy(memory_pool_t* memory_pool){
free(memory_pool->block);
free(memory_pool);
}