-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMempool.h
208 lines (131 loc) · 4.21 KB
/
Mempool.h
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
/*
2020 © Copyright (c) BiDaE Technology Inc.
Provided under BiDaE SHAREWARE LICENSE-1.0 in the LICENSE.
Project Name:
BeDIS
File Name:
Mempool.h
File Description:
This file contains the declarations and definition of variables used in
the Mempool.c file.
Note: This code is referred from a post by 2013Asker on 20140504 on the
stackexchange website here:
https://codereview.stackexchange.com/questions/48919/simple-memory-pool-
using-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]
*/
#ifndef MEMPOOL_H
#define MEMPOOL_H
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#endif
/* When debugging is needed */
//#define debugging
#define MEMORY_POOL_SUCCESS 1
#define MEMORY_POOL_ERROR 0
#define MEMORY_POOL_MINIMUM_SIZE sizeof(void *)
#define MAX_EXP_TIME 10
/* The structure of the memory pool */
typedef struct {
/* The head of the unused slots */
void **head;
/* An array stores the head of each malloced memory */
void *memory[MAX_EXP_TIME];
/* Counting current malloc times */
int alloc_time;
/* A per list lock */
pthread_mutex_t mem_lock;
/* The size of each slots in byte */
int size;
/* The number of slots is made each time the mempool expand */
int slots;
int blocks;
/* counter for calculating the slots usage */
int used_slots;
} Memory_Pool;
/*
get_current_size_mempool:
This function returns the current size of the memory pool.
Parameters:
mp - pointer to a specific memory pool
Return value:
mem_size- the current size of the memory pool
*/
size_t get_current_size_mempool(Memory_Pool *mp);
/*
mp_init:
This function allocates memory and initializes the memory pool and links
the slots in the pool.
Parameters:
mp - pointer to a specific memory pool
size - the size of slots in the pool
slots - the number of slots in the memory pool
Return value:
Status - the error code or the successful message
*/
int mp_init(Memory_Pool *mp, size_t size, size_t slots);
/*
mp_expand:
This function expands the number of slots and allocates more memory to the
memory pool.
Parameters:
mp - pointer to a specific memory pool
Return value:
Status - the error code or the successful message
*/
int mp_expand(Memory_Pool *mp);
/*
mp_destroy:
This function frees the memory occupied by the specified memory pool.
Parameters:
mp - pointer to the specific memory pool to be destroyed
Return value:
None
*/
void mp_destroy(Memory_Pool *mp);
/*
mp_alloc:
This function gets a free slot from the memory pool and returns a pointer
to the slot when a free slot is available and return NULL when no free slot
is available.
Parameters:
mp - pointer to the specific memory pool to be used
Return value:
void - the pointer to the struct of a free slot or NULL
*/
void *mp_alloc(Memory_Pool *mp);
/*
mp_free:
This function releases a slot back to the memory pool.
Parameters:
mp - the pointer to the specific memory pool
mem - the pointer to the strting address of the slot to be freed
Return value:
Errorcode - error code or sucessful message
*/
int mp_free(Memory_Pool *mp, void *mem);
/*
mp_slots_usage_percentage:
This function calculates the memory pool slots usage in percentage
Parameters:
mp - the pointer to the specific memory pool
Return value:
float - the memory pool slots usage in percentage
*/
float mp_slots_usage_percentage(Memory_Pool *mp);
#endif