forked from liexusong/php-beast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beast_mm.c
397 lines (313 loc) · 9.44 KB
/
beast_mm.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* +--------------------------------+
* | share memory manager algorithm |
* +--------------------------------+
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "spinlock.h"
#include "beast_log.h"
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0
#endif
#define BEAST_SEGMENT_DEFAULT_SIZE (256 * 1024)
#define _BLOCKAT(offset) ((beast_block_t *)((char *)shmaddr + (offset)))
#define _OFFSET(block) ((int)(((char *)(block)) - (char *)shmaddr))
#ifdef max
#undef max
#endif
#define max(a, b) ((a) > (b) ? (a) : (b))
typedef struct beast_header_s {
int segsize; /* size of entire segment */
int avail; /* bytes available memorys */
} beast_header_t;
typedef struct beast_block_s {
int size; /* size of this block */
int next; /* offset in segment of next free block */
} beast_block_t;
static int beast_mm_initialized = 0;
static void *beast_mm_block = NULL;
static int beast_mm_block_size = 0;
static beast_atomic_t *mm_lock;
static int mm_pid = -1;
void beast_mm_lock()
{
if (mm_pid == -1) {
mm_pid = (int)getpid();
}
beast_spinlock(mm_lock, mm_pid);
}
void beast_mm_unlock()
{
if (mm_pid == -1) {
mm_pid = (int)getpid();
}
beast_spinunlock(mm_lock, mm_pid);
}
/*
* memory align function
* @param bits, align bits
*/
static inline int beast_mm_alignmem(int bits)
{
typedef union {
void* p;
int i;
long l;
double d;
void (*f)();
} beast_word_t; /* may be 8 bits */
return sizeof(beast_word_t) * (1 + ((bits - 1) / sizeof(beast_word_t)));
}
static int beast_mm_allocate(void *shmaddr, int size)
{
beast_header_t *header; /* header of shared memory segment */
beast_block_t *prv; /* block prior to working block */
beast_block_t *cur; /* working block in list */
beast_block_t *prvbestfit; /* block before best fit */
int realsize; /* actual size of block needed, including header */
int minsize; /* for finding best fit */
/* Realsize must be aligned to a word boundary on some architectures. */
realsize = beast_mm_alignmem(
max(size + beast_mm_alignmem(sizeof(int)), sizeof(beast_block_t)));
/*
* First, insure that the segment contains at least realsize free bytes,
* even if they are not contiguous.
*/
header = (beast_header_t *)shmaddr;
if (header->avail < realsize) {
beast_write_log(beast_log_error,
"Not enough memory for beast_mm_alloc()");
return -1;
}
prvbestfit = 0; /* best block prev's node */
minsize = INT_MAX;
prv = _BLOCKAT(sizeof(beast_header_t)); /* free list header */
while (prv->next != 0) {
cur = _BLOCKAT(prv->next); /* current active block */
if (cur->size == realsize) {
prvbestfit = prv;
break;
}
else if (cur->size > (sizeof(beast_block_t) + realsize) &&
cur->size < minsize)
{
prvbestfit = prv;
minsize = cur->size;
}
prv = cur;
}
if (prvbestfit == 0) { /* not found best block */
return -1;
}
prv = prvbestfit;
cur = _BLOCKAT(prv->next);
/* update the block header */
header->avail -= realsize;
if (cur->size == realsize) {
prv->next = cur->next;
} else {
beast_block_t *nxt; /* the new block (chopped part of cur) */
int nxtoffset; /* offset of the block currently after cur */
int oldsize; /* size of cur before split */
/* bestfit is too big; split it into two smaller blocks */
nxtoffset = cur->next;
oldsize = cur->size;
prv->next += realsize;
cur->size = realsize;
nxt = _BLOCKAT(prv->next);
nxt->next = nxtoffset;
nxt->size = oldsize - realsize;
}
return _OFFSET(cur) + beast_mm_alignmem(sizeof(int)); /* skip size field */
}
static int beast_mm_deallocate(void *shmaddr, int offset)
{
beast_header_t *header; /* header of shared memory segment */
beast_block_t *cur; /* the new block to insert */
beast_block_t *prv; /* the block before cur */
beast_block_t *nxt; /* the block after cur */
int size; /* size of deallocated block */
offset -= beast_mm_alignmem(sizeof(int)); /* really offset */
/* find position of new block in free list */
prv = _BLOCKAT(sizeof(beast_header_t));
while (prv->next != 0 && prv->next < offset) {
prv = _BLOCKAT(prv->next);
}
/* insert new block after prv */
cur = _BLOCKAT(offset);
cur->next = prv->next;
prv->next = offset;
/* update the block header */
header = (beast_header_t *)shmaddr;
header->avail += cur->size;
size = cur->size;
if (((char *)prv) + prv->size == (char *) cur) {
/* cur and prv share an edge, combine them */
prv->size += cur->size;
prv->next = cur->next;
cur = prv;
}
nxt = _BLOCKAT(cur->next);
if (((char *)cur) + cur->size == (char *) nxt) {
/* cur and nxt shared an edge, combine them */
cur->size += nxt->size;
cur->next = nxt->next;
}
return size;
}
/*
* init memory manager
*/
int beast_mm_init(int block_size)
{
beast_header_t *header;
beast_block_t *block;
void *shmaddr;
if (beast_mm_initialized) {
return 0;
}
/* init memory manager lock */
mm_lock = (int *)mmap(NULL,
sizeof(int),
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANON,
-1,
0);
if (!mm_lock) {
beast_write_log(beast_log_error,
"Unable alloc share memory for memory manager lock");
return -1;
}
*mm_lock = 0;
/* init share memory for beast */
if (block_size < BEAST_SEGMENT_DEFAULT_SIZE) {
beast_mm_block_size = BEAST_SEGMENT_DEFAULT_SIZE;
} else {
beast_mm_block_size = block_size;
}
shmaddr = beast_mm_block = (void *)mmap(NULL,
beast_mm_block_size,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANON,
-1,
0);
if (!beast_mm_block) {
beast_write_log(beast_log_error,
"Unable alloc share memory for beast");
munmap((void *)mm_lock, sizeof(int));
return -1;
}
header = (beast_header_t *)beast_mm_block;
header->segsize = beast_mm_block_size;
/* avail size */
header->avail = beast_mm_block_size
- sizeof(beast_header_t)
- sizeof(beast_block_t)
- beast_mm_alignmem(sizeof(int));
/* the free list head block node */
block = _BLOCKAT(sizeof(beast_header_t));
block->size = 0;
block->next = sizeof(beast_header_t) + sizeof(beast_block_t);
/* the avail block */
block = _BLOCKAT(block->next);
block->size = header->avail;
block->next = 0;
beast_mm_initialized = 1;
return 0;
}
void *beast_mm_malloc(int size)
{
int offset;
void *p = NULL;
beast_mm_lock();
offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
}
beast_mm_unlock();
return p;
}
void *beast_mm_calloc(int size)
{
int offset;
void *p = NULL;
beast_mm_lock();
offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
}
beast_mm_unlock();
if (NULL != p) {
memset(p, 0, size);
}
return p;
}
void beast_mm_free(void *p)
{
int offset;
offset = (unsigned int)((char *)p - (char *)beast_mm_block);
if (offset <= 0) {
return;
}
beast_mm_lock();
beast_mm_deallocate(beast_mm_block, offset);
beast_mm_unlock();
}
void beast_mm_flush()
{
beast_header_t *header;
beast_block_t *block;
void *shmaddr;
beast_mm_lock();
shmaddr = beast_mm_block;
header = (beast_header_t *)shmaddr;
header->avail = beast_mm_block_size
- sizeof(beast_header_t)
- sizeof(beast_block_t)
- beast_mm_alignmem(sizeof(int));
/* the free list head block node */
block = _BLOCKAT(sizeof(beast_header_t));
block->size = 0;
block->next = sizeof(beast_header_t) + sizeof(beast_block_t);
/* the avail block */
block = _BLOCKAT(block->next);
block->size = header->avail;
block->next = 0;
beast_mm_unlock();
}
/*
* Get the avail's memory space
*/
int beast_mm_availspace()
{
int size;
beast_header_t *header = (beast_header_t *)beast_mm_block;
beast_mm_lock();
size = header->avail;
beast_mm_unlock();
return size;
}
/*
* Don't locked here, because the segsize not change forever
*/
int beast_mm_realspace()
{
return ((beast_header_t *)beast_mm_block)->segsize;
}
/*
* Destroy memory's manager
*/
void beast_mm_destroy()
{
if (beast_mm_initialized) {
/* Free cache memory */
munmap((void *)beast_mm_block, beast_mm_block_size);
/* Free memory lock */
munmap((void *)mm_lock, sizeof(int));
beast_mm_initialized = 0;
}
}