This repository was archived by the owner on Apr 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhuge.c
More file actions
266 lines (224 loc) · 7.37 KB
/
Copy pathhuge.c
File metadata and controls
266 lines (224 loc) · 7.37 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
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
#include <string.h>
#include "chunk.h"
#include "extent.h"
#include "huge.h"
#include "memory.h"
#include "mutex.h"
#include "purge.h"
#include "util.h"
static extent_tree huge_global;
static mutex huge_global_mutex = MUTEX_INITIALIZER;
static struct extent_node *huge_nodes;
COLD void huge_init(void) {
extent_tree_ad_new(&huge_global);
}
static struct chunk_recycler *get_recycler(struct arena *arena) {
return arena ? &arena->chunks : NULL;
}
static struct extent_node **get_huge_nodes(struct arena *arena) {
return arena ? &arena->huge_nodes : &huge_nodes;
}
static void maybe_lock_arena(struct arena *arena) {
if (arena) {
mutex_lock(&arena->mutex);
}
}
static void maybe_unlock_arena(struct arena *arena) {
if (arena) {
mutex_unlock(&arena->mutex);
}
}
static extent_tree *acquire_huge(struct arena *arena) {
if (!arena) {
mutex_lock(&huge_global_mutex);
return &huge_global;
}
return &arena->huge;
}
static void release_huge(struct arena *arena) {
if (!arena) {
mutex_unlock(&huge_global_mutex);
}
}
static void *huge_chunk_alloc(struct thread_cache *cache, size_t size, size_t alignment,
struct arena **out_arena) {
struct arena *arena = get_arena(cache);
void *chunk = chunk_recycle(&arena->chunks, NULL, size, alignment);
if (chunk) {
if (unlikely(memory_commit(chunk, size))) {
chunk_free(&arena->chunks, chunk, size);
return NULL;
}
} else {
if (unlikely(!(chunk = chunk_alloc(NULL, size, alignment)))) {
return NULL;
}
// Work around the possibility of holes created by huge_move_expand (see below).
struct arena *chunk_arena = get_huge_arena(chunk);
if (chunk_arena != arena) {
mutex_unlock(&arena->mutex);
if (chunk_arena) {
mutex_lock(&chunk_arena->mutex);
}
arena = chunk_arena;
}
}
*out_arena = arena;
return chunk;
}
void *huge_alloc(struct thread_cache *cache, size_t size, size_t alignment) {
size_t real_size = CHUNK_CEILING(size);
struct arena *arena;
void *chunk = huge_chunk_alloc(cache, real_size, alignment, &arena);
if (unlikely(!chunk)) {
return NULL;
}
extent_tree *huge = acquire_huge(arena);
struct extent_node *node = node_alloc(get_huge_nodes(arena));
if (unlikely(!node)) {
chunk_free(get_recycler(arena), chunk, real_size);
chunk = NULL;
} else {
node->size = real_size;
node->addr = chunk;
extent_tree_ad_insert(huge, node);
}
release_huge(arena);
maybe_unlock_arena(arena);
return chunk;
}
static void huge_update_size(struct arena *arena, void *ptr, size_t new_size) {
struct extent_node key;
key.addr = ptr;
extent_tree *huge = acquire_huge(arena);
struct extent_node *node = extent_tree_ad_search(huge, &key);
assert(node);
node->size = new_size;
release_huge(arena);
}
static void huge_no_move_shrink(void *ptr, size_t old_size, size_t new_size) {
void *excess_addr = (char *)ptr + new_size;
size_t excess_size = old_size - new_size;
if (purge_ratio >= 0) {
memory_decommit(excess_addr, excess_size);
}
struct arena *arena = get_huge_arena(ptr);
maybe_lock_arena(arena);
chunk_free(get_recycler(arena), excess_addr, excess_size);
huge_update_size(arena, ptr, new_size);
maybe_unlock_arena(arena);
}
static bool huge_no_move_expand(void *ptr, size_t old_size, size_t new_size) {
bool failure = true;
void *expand_addr = (char *)ptr + old_size;
size_t expand_size = new_size - old_size;
struct arena *arena = get_huge_arena(ptr);
struct chunk_recycler *chunks = get_recycler(arena);
maybe_lock_arena(arena);
if (chunk_recycle(chunks, expand_addr, expand_size, CHUNK_SIZE)) {
if (unlikely(memory_commit(expand_addr, expand_size))) {
chunk_free(chunks, expand_addr, expand_size);
} else {
huge_update_size(arena, ptr, new_size);
failure = false;
}
}
maybe_unlock_arena(arena);
return failure;
}
static void *huge_move_expand(struct thread_cache *cache, void *old_addr, size_t old_size, size_t new_size) {
struct arena *arena;
void *new_addr = huge_chunk_alloc(cache, new_size, CHUNK_SIZE, &arena);
if (unlikely(!new_addr)) {
return NULL;
}
bool gap = true;
if (unlikely(memory_remap_fixed(old_addr, old_size, new_addr, new_size))) {
memcpy(new_addr, old_addr, old_size);
if (purge_ratio >= 0) {
memory_decommit(old_addr, old_size);
}
gap = false;
} else {
// Attempt to fill the virtual memory hole. The kernel should provide a flag for preserving
// the old mapping to avoid the possibility of this failing and creating fragmentation.
//
// https://lkml.org/lkml/2014/10/2/624
void *extra = memory_map(old_addr, old_size, false);
if (likely(extra)) {
if (unlikely(extra != old_addr)) {
memory_unmap(extra, old_size);
} else {
gap = false;
}
}
}
struct extent_node key;
key.addr = old_addr;
struct arena *old_arena = get_huge_arena(old_addr);
extent_tree *huge = acquire_huge(old_arena);
struct extent_node *node = extent_tree_ad_search(huge, &key);
assert(node);
extent_tree_ad_remove(huge, node);
node->addr = new_addr;
node->size = new_size;
if (arena != old_arena) {
release_huge(old_arena);
huge = acquire_huge(arena);
}
extent_tree_ad_insert(huge, node);
release_huge(arena);
if (!gap) {
if (arena != old_arena && old_arena) {
mutex_lock(&old_arena->mutex);
}
chunk_free(get_recycler(old_arena), old_addr, old_size);
if (arena != old_arena && old_arena) {
mutex_unlock(&old_arena->mutex);
}
}
maybe_unlock_arena(arena);
return new_addr;
}
void *huge_realloc(struct thread_cache *cache, void *ptr, size_t old_size, size_t new_real_size) {
if (new_real_size > old_size) {
if (!huge_no_move_expand(ptr, old_size, new_real_size)) {
return ptr;
}
return huge_move_expand(cache, ptr, old_size, new_real_size);
} else if (new_real_size < old_size) {
huge_no_move_shrink(ptr, old_size, new_real_size);
}
return ptr;
}
void huge_free(void *ptr) {
struct extent_node *node, key;
key.addr = ptr;
struct arena *arena = get_huge_arena(ptr);
maybe_lock_arena(arena);
extent_tree *huge = acquire_huge(arena);
node = extent_tree_ad_search(huge, &key);
assert(node);
size_t size = node->size;
extent_tree_ad_remove(huge, node);
node_free(get_huge_nodes(arena), node);
release_huge(arena);
if (purge_ratio >= 0) {
memory_decommit(ptr, size);
}
chunk_free(get_recycler(arena), ptr, size);
maybe_unlock_arena(arena);
}
size_t huge_alloc_size(void *ptr) {
struct extent_node key;
key.addr = ptr;
struct arena *arena = get_huge_arena(ptr);
maybe_lock_arena(arena);
extent_tree *huge = acquire_huge(arena);
struct extent_node *node = extent_tree_ad_search(huge, &key);
assert(node);
size_t size = node->size;
release_huge(arena);
maybe_unlock_arena(arena);
return size;
}