-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMempool.c
274 lines (183 loc) · 6.07 KB
/
Mempool.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
2020 © Copyright (c) BiDaE Technology Inc.
Provided under BiDaE SHAREWARE LICENSE-1.0 in the LICENSE.
Project Name:
BeDIS
File Name:
Mempool.c
File Description:
This file contains the program to allow memory allocation for structs of
identical size.
Note: The code is referred to the site:
https://codereview.stackexchange.com/questions/48919/simple-memory-pool-
%20using-no-extra-memory
Version:
2.0, 20190415
Abstract:
BeDIS uses LBeacons to deliver 3D coordinates and textual descriptions of
their locations to users' devices. Basically, a LBeacon is an inexpensive,
Bluetooth Smart Ready device. The 3D coordinates and location description
of every LBeacon are retrieved from BeDIS (Building/environment Data and
Information System) and stored locally during deployment and maintenance
times. Once initialized, each LBeacon broadcasts its coordinates and
location description to Bluetooth enabled user devices within its coverage
area.
Authors:
Holly Wang, [email protected]
*/
#include "Mempool.h"
size_t get_current_size_mempool(Memory_Pool *mp){
size_t mem_size;
pthread_mutex_lock(&mp->mem_lock);
mem_size = mp->alloc_time * mp->size * mp->slots;
pthread_mutex_unlock(&mp->mem_lock);
return mem_size;
}
int mp_init(Memory_Pool *mp, size_t size, size_t slots){
char *end;
char *ite;
void *temp;
int return_value;
/* initialize and set parameters */
mp->head = NULL;
mp->size = size;
mp->slots = slots;
mp->used_slots = 0;
mp->alloc_time = 0;
mp->blocks = 0;
pthread_mutex_init( &mp->mem_lock, 0);
return_value = mp_expand(mp);
#ifdef debugging
zlog_info(category_debug,
"[Mempool] Current MemPool [%d]\n[Mempool] Remain blocks [%d]",
mp, mp->blocks);
#endif
return return_value;
}
int mp_expand(Memory_Pool *mp){
int alloc_count;
char *end;
void *temp;
char *ite;
alloc_count = mp->alloc_time;
if(alloc_count == MAX_EXP_TIME)
return MEMORY_POOL_ERROR;
mp->memory[alloc_count] = malloc(mp->size * mp->slots);
if(mp->memory[alloc_count] == NULL )
return MEMORY_POOL_ERROR;
memset(mp->memory[alloc_count], 0, mp->size * mp->slots);
/* add every slot to the free list */
end = (char *)mp->memory[alloc_count] + mp->size * mp->slots;
for(ite = mp->memory[alloc_count]; ite < end; ite += mp->size){
/* store first address */
temp = mp->head;
/* link the new node */
mp->head = (void *)ite;
/* link to the list from new node */
*mp->head = temp;
mp->blocks ++;
}
mp->alloc_time ++;
#ifdef debugging
zlog_info(category_debug,
"[Mempool] Current MemPool [%d]\n[Mempool] Remain blocks [%d]",
mp, mp->blocks);
#endif
return MEMORY_POOL_SUCCESS;
}
void mp_destroy(Memory_Pool *mp){
int i;
pthread_mutex_lock( &mp->mem_lock);
for(i = 0; i < MAX_EXP_TIME; i++){
mp->memory[i] = NULL;
free(mp->memory[i]);
}
mp->head = NULL;
mp->size = 0;
mp->slots = 0;
mp->alloc_time = 0;
mp->blocks = 0;
#ifdef debugging
zlog_info(category_debug,
"[Mempool] Current MemPool [%d]\n[Mempool] Remain blocks [%d]",
mp, mp->blocks);
#endif
pthread_mutex_unlock( &mp->mem_lock);
pthread_mutex_destroy( &mp->mem_lock);
}
void *mp_alloc(Memory_Pool *mp){
void *temp;
/*zlog_info(category_debug, "[mp_alloc] Attemp to mp_alloc, current " \
"blocks = [%d], current alloc times = [%d]", mp->blocks,
mp->alloc_time);
*/
pthread_mutex_lock(&mp->mem_lock);
if(mp->head == NULL){
/* If the next position which mp->head is pointing to is NULL,
expand the memory pool. */
if(mp_expand(mp) == MEMORY_POOL_ERROR){
pthread_mutex_unlock(&mp->mem_lock);
return NULL;
}
}
/* store first address, i.e., address of the start of first element */
temp = mp->head;
/* link one past it */
mp->head = *mp->head;
// count the slots usage
mp->used_slots = mp->used_slots + 1;
mp->blocks --;
#ifdef debugging
zlog_info(category_debug,
"[Mempool] Current MemPool [%d]\n[Mempool] Remain blocks [%d]",
mp, mp->blocks);
#endif
memset(temp, 0, mp->size);
pthread_mutex_unlock( &mp->mem_lock);
/* return the first address */
return temp;
}
int mp_free(Memory_Pool *mp, void *mem){
int closest = -1;
int i;
int differenceinbyte;
void *temp;
pthread_mutex_lock(&mp->mem_lock);
/* Check all the expanded memory space, to find the closest and
most relevant mem_head for the current freeing memory. */
for(i = 0; i < mp->alloc_time; i++){
/* Calculate the offset from mem to mp->memory */
differenceinbyte = (int)mem - (int)mp->memory[i];
/* Only consider the positive offset */
if((differenceinbyte > 0) && ((differenceinbyte < closest) ||
(closest == -1)))
closest = differenceinbyte;
}
/* check if mem is correct, i.e. is pointing to the struct of a slot */
if((closest % mp->size) != 0){
pthread_mutex_unlock(&mp->mem_lock);
return MEMORY_POOL_ERROR;
}
memset(mem, 0, mp->size);
/* store first address */
temp = mp->head;
/* link new node */
mp->head = mem;
/* link to the list from new node */
*mp->head = temp;
mp->blocks ++;
#ifdef debugging
zlog_info(category_debug,
"[Mempool] Current MemPool [%d]\n[Mempool] Remain blocks [%d]",
mp, mp->blocks);
#endif
// count the slots usage
mp->used_slots = mp->used_slots - 1;
pthread_mutex_unlock(&mp->mem_lock);
return MEMORY_POOL_SUCCESS;
}
float mp_slots_usage_percentage(Memory_Pool *mp){
float usage_percentage = 0;
usage_percentage = (mp->used_slots*1.0) / (mp->alloc_time * mp->slots);
return usage_percentage;
}