-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpage_allocator.h
executable file
·95 lines (82 loc) · 2.42 KB
/
page_allocator.h
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
// The core library - copyright GarageGames. The core library is released under the MIT Open Source software license. See /license.txt in this distribution for specific license terms.
template <uint32 alignment = 4> class page_allocator
{
public:
page_allocator(zone_allocator *zone)
{
_allocator = zone;
_current_offset = zone_allocator::page_size;
_current_page_size = zone_allocator::page_size;
_current_page = 0;
}
~page_allocator()
{
clear();
}
void clear()
{
while(_current_page)
{
page *prev = _current_page->prev;
_allocator->deallocate_pages(_current_page,_current_page->page_count);
_current_page = prev;
}
_current_offset = zone_allocator::page_size;
_current_page_size = zone_allocator::page_size;
}
void *allocate(uint32 size)
{
if(!size)
return 0;
// align size:
size = (size + (alignment - 1)) & ~(alignment - 1);
if(_current_offset + size > _current_page_size)
{
// need a new page to fit this allocation:
uint32 new_page_count = (max(uint32(zone_allocator::page_size), size + page_header_size) + zone_allocator::page_size - 1) / zone_allocator::page_size;
page *new_page = (page *) _allocator->allocate_pages(new_page_count);
new_page->allocator = this;
new_page->page_count = new_page_count;
uint32 new_size = new_page_count * zone_allocator::page_size;
uint32 new_offset = page_header_size + size;
if(new_size - new_offset >= _current_page_size - _current_offset)
{
// if the new page has equal or more free space than the current page, make the new page the current page.
new_page->prev = _current_page;
_current_page = new_page;
_current_page_size = new_size;
_current_offset = new_offset;
}
else
{
// otherwise, link the new page in _after_ the current page
new_page->prev = _current_page->prev;
_current_page->prev = new_page;
}
return ((uint8 *) new_page) + page_header_size;
}
else
{
void *ret = ((uint8 *) _current_page) + _current_offset;
_current_offset += size;
return ret;
}
}
private:
struct page
{
page_allocator *allocator;
page *prev;
uint32 page_count;
};
uint32 _current_offset;
uint32 _current_page_size;
zone_allocator *_allocator;
page *_current_page;
uint32 _alignment;
public:
enum {
page_header_size = (sizeof(page) + (alignment - 1)) & ~(alignment - 1),
maximum_single_page_allocation_size = zone_allocator::page_size - page_header_size,
};
};