forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc_cache.c
More file actions
241 lines (208 loc) · 6.96 KB
/
alloc_cache.c
File metadata and controls
241 lines (208 loc) · 6.96 KB
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
/*
Provides:
static intptr_t alloc_cache_free_page(AllocCacheBlock *blockfree, char *p, size_t len, int dirty, int originated_here)
static intptr_t void alloc_cache_flush_freed_pages(AllocCacheBlock *blockfree)
static void *alloc_cache_alloc_page(AllocCacheBlock *blockfree, size_t len, size_t alignment, int dirty_ok, intptr_t *size_diff)
Requires (defined earlier):
my_qsort --- possibly from my_qsort.c
static void os_vm_free_pages(void *p, size_t len);
static void *os_vm_alloc_pages(size_t len);
*/
/* Controls how often freed pages are actually returned to OS: */
#define BLOCKFREE_UNMAP_AGE 3
/* Controls size of the cache */
#define BLOCKFREE_CACHE_SIZE 96
/* Controls how many extra pages are requested from OS at a time: */
#define CACHE_SEED_PAGES 16
static AllocCacheBlock *alloc_cache_create() {
return ofm_malloc_zero(sizeof(AllocCacheBlock) * BLOCKFREE_CACHE_SIZE);
}
static intptr_t alloc_cache_free_all_pages(AllocCacheBlock *blockfree);
static intptr_t alloc_cache_free(AllocCacheBlock *ac) {
if (ac) {
intptr_t s = alloc_cache_free_all_pages(ac);
free(ac);
return s;
}
return 0;
}
static int alloc_cache_block_compare(const void *a, const void *b)
{
if ((uintptr_t)((AllocCacheBlock *)a)->start < (uintptr_t)((AllocCacheBlock *)b)->start)
return -1;
else
return 1;
}
static void alloc_cache_collapse_pages(AllocCacheBlock *blockfree)
{
int i;
int j;
/* sort by AllocCacheBlock->start */
my_qsort(blockfree, BLOCKFREE_CACHE_SIZE, sizeof(AllocCacheBlock), alloc_cache_block_compare);
/* collapse adjacent: */
j = 0;
for (i = 1; i < BLOCKFREE_CACHE_SIZE; i++) {
if ((blockfree[j].start + blockfree[j].len) == blockfree[i].start) {
blockfree[j].len += blockfree[i].len;
blockfree[i].start = NULL;
blockfree[i].len = 0;
if (!blockfree[i].zeroed)
blockfree[j].zeroed = 0;
} else
j = i;
}
}
inline static void *alloc_cache_find_pages(AllocCacheBlock *blockfree, size_t len, size_t alignment, int dirty_ok)
{
int i;
void *r;
/* Try an exact fit: */
for (i = 0; i < BLOCKFREE_CACHE_SIZE; i++) {
if (blockfree[i].len == len) {
r = blockfree[i].start;
if (!alignment || !((uintptr_t)r & (alignment - 1))) {
blockfree[i].start = NULL;
blockfree[i].len = 0;
if (!blockfree[i].zeroed && !dirty_ok)
memset(r, 0, len);
return r;
}
}
}
/* Try a first fit: */
for (i = 0; i < BLOCKFREE_CACHE_SIZE; i++) {
if (blockfree[i].len > len) {
/* Align at start? */
r = blockfree[i].start;
if (!alignment || !((uintptr_t)r & (alignment - 1))) {
blockfree[i].start += len;
blockfree[i].len -= len;
if (!blockfree[i].zeroed && !dirty_ok)
memset(r, 0, len);
return r;
}
/* Align at end? */
r = blockfree[i].start + (blockfree[i].len - len);
if (!((uintptr_t)r & (alignment - 1))) {
blockfree[i].len -= len;
if (!blockfree[i].zeroed && !dirty_ok)
memset(r, 0, len);
return r;
}
/* We don't try a middle alignment, because that would
split the block into three. */
}
}
/* Nothing useable in the cache... */
return NULL;
}
static intptr_t alloc_cache_free_page(AllocCacheBlock *blockfree, char *p, size_t len, int dirty, int originated_here)
{
int i;
/* Try to free pages in larger blocks, since the OS may be slow. */
for (i = 0; i < BLOCKFREE_CACHE_SIZE; i++)
if(blockfree[i].start && (blockfree[i].len < (1024 * 1024))) {
if (p == blockfree[i].start + blockfree[i].len) {
blockfree[i].len += len;
if (dirty)
blockfree[i].zeroed = 0;
return (originated_here ? 0 : len);
}
if (p + len == blockfree[i].start) {
blockfree[i].start = p;
blockfree[i].len += len;
if (dirty)
blockfree[i].zeroed = 0;
return (originated_here ? 0 : len);
}
}
for (i = 0; i < BLOCKFREE_CACHE_SIZE; i++) {
if (!blockfree[i].start) {
blockfree[i].start = p;
blockfree[i].len = len;
blockfree[i].age = 0;
blockfree[i].zeroed = !dirty;
return (originated_here ? 0 : len);
}
}
/* Might help next time around: */
alloc_cache_collapse_pages(blockfree);
os_free_pages(p, len);
return (originated_here ? -len : 0);
}
static intptr_t alloc_cache_flush_freed_pages(AllocCacheBlock *blockfree)
{
int i;
intptr_t freed = 0;
alloc_cache_collapse_pages(blockfree);
for (i = 0; i < BLOCKFREE_CACHE_SIZE; i++) {
if (blockfree[i].start) {
if (blockfree[i].age == BLOCKFREE_UNMAP_AGE) {
os_free_pages(blockfree[i].start, blockfree[i].len);
freed -= blockfree[i].len;
blockfree[i].start = NULL;
blockfree[i].len = 0;
} else
blockfree[i].age++;
}
}
return freed;
}
static intptr_t alloc_cache_free_all_pages(AllocCacheBlock *blockfree)
{
int i;
intptr_t freed = 0;
alloc_cache_collapse_pages(blockfree);
for (i = 0; i < BLOCKFREE_CACHE_SIZE; i++) {
if (blockfree[i].start) {
os_free_pages(blockfree[i].start, blockfree[i].len);
freed -= blockfree[i].len;
blockfree[i].start = NULL;
blockfree[i].len = 0;
}
}
return freed;
}
/* Instead of immediately freeing pages with munmap---only to mmap
them again---we cache BLOCKFREE_CACHE_SIZE freed pages. A page is
cached unused for at most BLOCKFREE_UNMAP_AGE cycles of the
collector. (A max age of 1 seems useful, anything more seems
dangerous.)
The cache is small enough that we don't need an elaborate search
mechanism, but we do a bit of work to collapse adjacent pages in
the cache. */
static void *alloc_cache_alloc_page(AllocCacheBlock *blockfree, size_t len, size_t alignment, int dirty_ok, intptr_t *size_diff)
{
char *r;
/* Something from the cache, perhaps? */
r = alloc_cache_find_pages(blockfree, len, alignment, dirty_ok);
if(!r) {
/* attempt to allocate from OS */
size_t extra = (alignment ? (alignment + CACHE_SEED_PAGES * APAGE_SIZE) : 0);
r = os_alloc_pages(len + extra);
if(r == (void *)-1) { return NULL; }
if (alignment) {
/* We allocated too large so we can choose the alignment. */
char *real_r = (char*)(((uintptr_t)r + (alignment - 1)) & (~(alignment - 1)));
intptr_t pre_extra = real_r - r;
/* in front extra */
if (pre_extra) {
/* printf("FREEING FRONT %p %lx\n", r, pre_extra); */
os_free_pages(r, pre_extra); }
/* in back extra exists */
if (pre_extra < extra) {
if (pre_extra == 0) {
/* Instead of actually unmapping, put it in the cache, and there's
a good chance we can use it next time: */
(*size_diff) += extra;
(*size_diff) += alloc_cache_free_page(blockfree, real_r + len, extra, 1, 1);
} else {
os_free_pages(real_r + len, extra - pre_extra);
}
}
r = real_r;
}
(*size_diff) += len;
}
return r;
}