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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* Copyright (c) 1999-2001
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <limits.h>
typedef __rcintptr pageid;
#if 0
#define FREEPAGE ((region)-1) /* Id of a free page */
#else
#define FREEPAGE (&zeroregion)
#endif
#ifdef NMEMDEBUG
#define ASSERT_FREE(p)
#define ASSERT_INUSE(p, r)
#else
#define ASSERT_FREE(p) assert(regionof(p) == FREEPAGE)
#ifdef DUPLICATES
#define ASSERT_INUSE(p, r) assert(regionof(p) == r->base)
#else
#define ASSERT_INUSE(p, r) assert(regionof(p) == r)
#endif
#endif
/* Page allocator for region-based memory management */
/* TBD: special free list for size == K ?? */
#define PAGECOUNTBITS (CHAR_BIT * sizeof(pageid) - 1)
struct page
{
/* Next page in region or in free list */
struct page *next;
/* Doubly linked list of pages sorted by address */
struct page *next_address, *prev_address;
/* number of pages in this allocation unit. Negative for free pages. */
pageid pagecount : PAGECOUNTBITS;
unsigned int free : 1;
/* Only in free pages not in the single_pages list */
struct page *previous;
};
/* The pages are kept in a single list sorted by address via the
next_address and prev_address fields. The first page's prev_address and
the last page's next_address fields points to pages_byaddress.
page_byaddress.next_address is the first page
page_byaddress.prev_address is the last page
This list is used for coalescing operations.
*/
static struct page pages_byaddress;
struct page *alloc_single_page(struct page *next);
void free_single_page(region r, struct page *p);
struct page *alloc_pages(int n, struct page *next);
void free_pages(region r, struct page *p);
/* a list of free individual pages */
struct page *single_pages;
/* free pages (not including those in single_pages) */
struct page *unused_pages;
static void init_pages(void)
{
pages_byaddress.next_address = &pages_byaddress;
pages_byaddress.prev_address = &pages_byaddress;
}
static void insertbefore_address(struct page *p, struct page *before)
{
p->prev_address = before->prev_address;
p->next_address = before;
before->prev_address = p;
p->prev_address->next_address = p;
}
static void unlink_address(struct page *p)
{
p->prev_address->next_address = p->next_address;
p->next_address->prev_address = p->prev_address;
}
static void addbyaddress(struct page *p)
{
struct page *address_scan;
/* Warning: this is slow. Calls to it should not be frequent (once app
reaches a steady state of memory usage). */
for (address_scan = pages_byaddress.next_address; ;
address_scan = address_scan->next_address)
if (p < address_scan || address_scan == &pages_byaddress)
{
insertbefore_address(p, address_scan);
return;
}
}
/* Doubly linked page list management */
void addfront(struct page **list, struct page *p)
/* Effects: Adds p to the front of doubly-linked list list */
{
p->previous = NULL;
p->next = *list;
if (*list) (*list)->previous = p;
*list = p;
}
void unlink_page(struct page **list, struct page *p)
/* Effects: Remove p from its doubly linked list */
{
if (p->previous)
p->previous->next = p->next;
else
*list = p->next;
if (p->next)
p->next->previous = p->previous;
}
void *region_get_mem(size_t s)
{
void *mem = malloc(s + RPAGESIZE - 1);
return (void *)ALIGN((__rcintptr)mem, RPAGESIZE);
}
/* Page to region map management */
/* ----------------------------- */
RADIX_TREE(__rcregionmap);
static void set_page_region(pageid pagenb, region r)
{
radix_tree_delete (&__rcregionmap, pagenb);
radix_tree_insert (&__rcregionmap, pagenb, r);
}
#define page_region(pagenb) (radix_tree_lookup (&__rcregionmap, (pagenb)))
void set_region(struct page *p, int npages, region r)
{
pageid pnb = PAGENB(p);
while (npages-- > 0)
set_page_region(pnb++, r);
}
/* Mark the memory range from 'from' (inclusive) to 'to' (exclusive)
as belonging to region with id 'rid' */
void set_region_range(void *from, void *to, region r)
{
pageid first = PAGENB(from), last = PAGENB((pageid)to - 1);
while (first <= last)
set_page_region(first++, r);
}
/* Multi-page allocation management */
/* -------------------------------- */
struct page *alloc_new(int n, struct page *next)
/* Assumes freepages_lock held */
{
struct page *newp = region_get_mem(n << RPAGELOG);
if (!newp)
{
if (nomem_h)
nomem_h();
abort();
}
assert(!((long)newp & (RPAGESIZE - 1)));
newp->next = next;
newp->pagecount = n;
newp->free = 0;
addbyaddress(newp);
#ifndef NMEMDEBUG
{
pageid i, pnb = PAGENB(newp);
for (i = pnb; i < pnb + n; i++)
set_page_region(i, FREEPAGE);
}
#endif
return newp;
}
struct page *alloc_split(struct page *split, int n, struct page *next)
/* Assumes freepages_lock held */
{
#ifndef NMEMDEBUG
/* These pages had better be free */
pageid i, pnb = PAGENB(split);
assert(split->pagecount >= n);
for (i = pnb; i < pnb + split->pagecount; i++)
assert(page_region(i) == FREEPAGE);
#endif
if (split->pagecount > n)
{
struct page *splitoff;
/* Keep first part of block */
split->pagecount -= n;
/* Return latter part of block */
splitoff = split;
split = (struct page *)((char *)split + (split->pagecount << RPAGELOG));
/* Update the by adress list */
insertbefore_address(split, splitoff->next_address);
}
else
{
/* remove split from list */
unlink_page(&unused_pages, split);
}
split->next = next;
split->pagecount = n;
split->free = 0;
return split;
}
struct page *alloc_pages(int n, struct page *next)
{
struct page *best;
int bestn;
struct page *scan;
assert(n >= K);
scan = unused_pages;
/* Find first fit */
for (;;)
{
if (!scan)
return alloc_new(n, next);
if (scan->pagecount >= n) break;
scan = scan->next;
}
/* Now find best fit */
best = scan;
bestn = scan->pagecount;
for (;;)
{
scan = scan->next;
if (!scan)
return alloc_split(best, n, next);
if (scan->pagecount >=n && scan->pagecount < bestn)
{
best = scan;
bestn = scan->pagecount;
}
}
}
static void coalesce(struct page *p)
{
struct page *prev = p->prev_address, *next;
p->free = 1;
/* Coalesce with predecessor ? */
if (prev->free && (char *)prev + (prev->pagecount << RPAGELOG) == (char *)p)
{
prev->pagecount += p->pagecount;
unlink_address(p);
p = prev;
}
else /* No, add to free pages list */
addfront(&unused_pages, p);
next = p->next_address;
/* Coalesce with successor ? */
if (next->free && (char *)p + (p->pagecount << RPAGELOG) == (char *)next)
{
unlink_page(&unused_pages, next);
p->pagecount += next->pagecount;
unlink_address(next);
}
}
void free_pages(region r, struct page *p)
/* Assumes freepages_lock held */
{
#ifndef NMEMDEBUG
pageid i, pnb = PAGENB(p);
for (i = pnb; i < pnb + p->pagecount; i++)
{
assert(page_region(i) == r);
set_page_region(i, FREEPAGE);
}
#endif
coalesce(p);
}
/* Single page management */
/* ---------------------- */
static int single_page_count;
static void add_single_pages(struct page *base)
/* Effects: Adds pages at base to the single_pages list */
{
pageid n = base->pagecount;
struct page *prev = base->prev_address, *basenext = base->next_address,
*next;
single_page_count += n;
for (;;)
{
ASSERT_FREE(base);
base->free = 0; /* Not free so that coalesce won't steal these back */
base->prev_address = prev;
prev = base;
base->next = single_pages;
single_pages = base;
if (--n == 0)
break;
next = (struct page *)((char *)base + RPAGESIZE);
base->next_address = next;
base = next;
}
base->next_address = basenext;
basenext->prev_address = base;
}
void scavenge_single_pages(int n)
{
/* Add n pages to the single_pages list */
struct page *scan, *best;
__rcintptr bestn;
/* Take any group in unused_pages that is <= n or < K.
Remember smallest entry > n too. This is sortof equivalent to
a best fit where we allow partial allocations to make up a whole */
best = NULL;
bestn = (__rcintptr)1 << (sizeof(__rcintptr) * CHAR_BIT - 2);
scan = unused_pages;
while (scan)
{
/* The pages < K can't be used for anything but single pages so we
might as well grab them even if they are a little too big */
if (scan->pagecount <= n || scan->pagecount < K)
{
struct page *adding = scan;
scan = scan->next;
n -= adding->pagecount;
unlink_page(&unused_pages, adding);
add_single_pages(adding);
if (n <= 0) return;
}
else
{
if (scan->pagecount < bestn)
{
bestn = scan->pagecount;
best = scan;
}
scan = scan->next;
}
}
/* Still not enough. Split the best block if there is one, allocate
new pages otherwise */
if (!best)
add_single_pages(alloc_new(n, NULL));
else if (best->pagecount - n < K)
{
unlink_page(&unused_pages, best);
add_single_pages(best);
}
else
add_single_pages(alloc_split(best, n, NULL));
}
struct page *alloc_single_page(struct page *next)
{
struct page *p;
if (!single_pages)
{
scavenge_single_pages(PAGE_GROUP_SIZE);
}
ASSERT_FREE(single_pages);
p = single_pages;
single_pages = p->next;
p->next = next;
single_page_count--;
return p;
}
void free_single_page(region r, struct page *p)
/* Assumes freepages_lock held */
{
#ifndef NMEMDEBUG
ASSERT_INUSE(p, r);
set_page_region(PAGENB(p), FREEPAGE);
#endif
/* Once free list is big enough just coalesce the pages.
The actual threshold to use might merit further study (something
adaptive ? e.g., proportional to allocated single pages) */
if (single_page_count > PAGE_GROUP_SIZE * 2)
{
p->pagecount = 1;
coalesce(p);
}
else
{
p->next = single_pages;
single_pages = p;
single_page_count++;
}
}
|