-
Notifications
You must be signed in to change notification settings - Fork 17
/
point.c
114 lines (91 loc) · 2.49 KB
/
point.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
/*--------------------------------------------------------------*/
/* point.c -- */
/* */
/* Memory mapped point allocation */
/*--------------------------------------------------------------*/
/* Written by Tim Edwards, April 2017, based on code from Magic */
/*--------------------------------------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include "qrouter.h"
#include "point.h"
#ifdef HAVE_SYS_MMAN_H
POINT POINTStoreFreeList = NULL;
POINT POINTStoreFreeList_end = NULL;
/* The memory mapped POINT Allocation scheme */
static void *_block_begin = NULL;
static void *_current_ptr = NULL;
static void *_block_end = NULL;
/* MMAP the point store */
static signed char
mmapPOINTStore()
{
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_ANON | MAP_PRIVATE;
u_long map_len = POINT_STORE_BLOCK_SIZE;
_block_begin = mmap(NULL, map_len, prot, flags, -1, 0);
if (_block_begin == MAP_FAILED)
{
fprintf(stderr, "mmapPOINTStore: Unable to mmap ANON SEGMENT\n");
exit(1);
}
_block_end = (void *) ((unsigned long) _block_begin + map_len);
_current_ptr = _block_begin;
return 0;
}
POINT
allocPOINT()
{
POINT _return_point = NULL;
if (!_block_begin && !_block_end) mmapPOINTStore();
/* Check if we can get the point from the
* Free list
*/
if (POINTStoreFreeList) {
_return_point = POINTStoreFreeList;
POINTStoreFreeList = (POINT)POINTStoreFreeList->next;
return _return_point;
}
/* Get it from the mmap */
if (((unsigned long)_current_ptr + sizeof(struct point_)) >
(unsigned long)_block_end)
mmapPOINTStore();
_current_ptr = (void *)((unsigned long)_current_ptr
+ sizeof(struct point_));
if ((unsigned long)_current_ptr > (unsigned long) _block_end) {
fprintf(stderr,
"allocPOINT(): internal assertion failure.");
exit(1);
}
return (POINT)((unsigned long)_current_ptr - sizeof(struct point_));
}
void
freePOINT(POINT gp)
{
if (!POINTStoreFreeList_end || !POINTStoreFreeList) {
POINTStoreFreeList_end = gp;
gp->next = (POINT)NULL;
POINTStoreFreeList = POINTStoreFreeList_end;
}
else {
POINTStoreFreeList_end->next = gp;
POINTStoreFreeList_end = gp;
POINTStoreFreeList_end->next = (POINT)NULL;
}
}
#else
POINT
allocPOINT()
{
POINT newpoint;
newpoint = (POINT)malloc((unsigned)(sizeof(struct point_)));
return (newpoint);
}
void
freePOINT(POINT gp)
{
free((char *)gp);
}
#endif /* !HAVE_SYS_MMAN_H */