-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmempool.c
153 lines (119 loc) · 3.61 KB
/
kmempool.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// kmempool.c
// mylib
//
// Created by bikang on 17/11/22.
// Copyright (c) 2017年 bikang. All rights reserved.
//
#include "kmempool.h"
//真正的分配内存函数
int static kmempool_create(kmempool* km, int block_size, unsigned int block_count);
//初始化内存池
kmempool* kmempool_init(unsigned int block_size, unsigned int block_count){
kmempool *km = (kmempool *)malloc(sizeof(kmempool));
if (km == NULL) return NULL;
km->block_count = block_count;
km->block_size = block_size;
km->add_step = KM_ADD_MEM_STEP;
km->free_head = NULL;
km->free_tail = NULL;
km->used_tail = NULL;
km->used_head = NULL;
km->used_count = 0;
km->free_count = block_count;
kmempool_create(km,block_size,block_count);
return km;
}
//真正的内存分配代码
static int kmempool_create(kmempool* km, int block_size, unsigned int block_count){
kblock *pos = NULL;
unsigned int i;
km->pkblock = (char*)malloc(sizeof(kblock)*block_count);
km->pmem = (char*)malloc((block_size +sizeof(kblock*))*block_count);
for (i=0; i<block_count; i++) {
pos = (kblock*)(km->pkblock+(sizeof(kblock)*i));
pos->pdata = km->pmem+(block_size +sizeof(kblock*))*i;
pos->next = NULL;
pos->pre = km->free_tail;
if(km->free_head == NULL){
km->free_head = km->free_tail = pos;
}else{
km->free_tail->next = pos;
km->free_tail = pos;
}
}
return 0;
}
//内存池分配内存. 从一个队列移除,进入另外一个队列
void* kmempool_malloc(kmempool* km,unsigned int size){
kblock *pos = NULL;
if(km->free_head == NULL){
kmempool_create(km,km->add_step*km->block_count,km->block_size);
}
pos = km->free_head;
km->free_head = km->free_head->next;
km->free_head->pre = NULL;
pos->next = NULL;
km->free_count --;
km->used_count ++;
if(km->used_tail == NULL){
pos->pre = NULL;
}else{
pos->pre = km->used_tail;
}
if (km->used_head == NULL) {
km->used_head = km->used_tail = pos;
}else {
km->used_tail->next = pos;
km->used_tail = pos;
}
//留下data里一个指针的空间,用于保存与数据关联的块地址
pos->pdata = (char*)pos;
return (char*)(pos->pdata)+sizeof(kblock*);
}
//内存池释放内存
int kmempool_free(kmempool* km, void *ptr){
char *realMem = (char*)ptr - sizeof(kblock*);
kblock *pos = (kblock *)realMem;
//printf("%p=%p",km->used_head,pos);
if (pos == NULL) {
return -1;
}
//从已使用里面摘除
if(pos->pre == NULL){//是头
km->used_head = (kblock *)(pos->next);
if(km->used_head != NULL){
km->used_head->pre = NULL;
}
}else if(pos->next == NULL){//尾部
km->used_tail = pos->pre;
if(km->used_tail != NULL){
km->used_tail->next = NULL;
}
}else{//中间
pos->pre->next = pos->next;
pos->next->pre = pos->pre;
}
km->used_count--;
//放入到未使用里面
pos->pdata = (char*)realMem;
pos->next = NULL;
pos->pre = km->free_tail;
km->free_count++;
if(km->free_head == NULL){
km->free_head = km->free_tail = pos;
}else{
km->free_tail->next = pos;
km->free_tail = pos;
}
return 0;
}
//摧毁内存池
int kmempool_destry(kmempool* km){
if ((km != NULL) && (km->block_size > 0)){
free(km->pkblock);
free(km->pmem);
km = NULL;
}
return 0;
}