-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmulti_region.cc
More file actions
186 lines (150 loc) · 4.41 KB
/
multi_region.cc
File metadata and controls
186 lines (150 loc) · 4.41 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
#include "test/setup.h"
#include <iostream>
#include <snmalloc/backend/fixedglobalconfig.h>
#include <snmalloc/backend/standard_range.h>
#include <snmalloc/backend_helpers/backend_helpers.h>
#include <snmalloc/snmalloc.h>
#ifdef assert
# undef assert
#endif
#define assert please_use_SNMALLOC_ASSERT
using namespace snmalloc;
/**
* A single fixed address range allocator configuration
*/
template<SNMALLOC_CONCEPT(IsPAL) PAL = DefaultPal>
class MultiRegionConfig final : public CommonConfig
{
public:
using PagemapEntry = DefaultPagemapEntry;
private:
using ConcretePagemap = FlatPagemap<MIN_CHUNK_BITS, PagemapEntry, PAL, false>;
using Pagemap = BasicPagemap<PAL, ConcretePagemap, PagemapEntry, false>;
static inline FlagWord pagemap_init_lock{};
public:
class LocalState
{
public:
using ObjectRange = Pipe<
EmptyRange<>,
LargeBuddyRange<bits::BITS - 1, bits::BITS - 1, Pagemap>,
SmallBuddyRange>;
// Dummy impl to keep concept happy.
using Stats = Pipe<EmptyRange<>, StatsRange>;
private:
ObjectRange object_range;
void ensure_pagemap_init()
{
auto& pagemap = Pagemap::concretePagemap;
if (pagemap.is_initialised())
return;
FlagLock lock(pagemap_init_lock);
if (pagemap.is_initialised())
return;
pagemap.init();
}
public:
// This should not be called.
using GlobalMetaRange = EmptyRange<>;
// Where we get user allocations from.
ObjectRange* get_object_range()
{
return &object_range;
}
// Where we get meta-data allocations from.
ObjectRange& get_meta_range()
{
// Use the object range to service meta-data requests.
return object_range;
}
LocalState(void* base, size_t size) : object_range()
{
// Ensure the communal pagemap is initialised.
ensure_pagemap_init();
// Notify that pagemap requires committed memory for this range.
Pagemap::register_range(address_cast(base), size);
// Fill the range owned by this region with memory.
object_range.dealloc_range(capptr::Arena<void>::unsafe_from(base), size);
}
};
using Backend = BackendAllocator<PAL, PagemapEntry, Pagemap, LocalState>;
using Pal = PAL;
private:
public:
constexpr static snmalloc::Flags Options{
.IsQueueInline = true,
.CoreAllocOwnsLocalState = false,
.CoreAllocIsPoolAllocated = false,
.LocalAllocSupportsLazyInit = false,
.QueueHeadsAreTame = true,
.HasDomesticate = false,
};
static void register_clean_up() {}
};
using CustomConfig = MultiRegionConfig<DefaultPal>;
using FixedAlloc = LocalAllocator<CustomConfig>;
using CoreAlloc = CoreAllocator<CustomConfig>;
class Region
{
public:
FixedAlloc alloc;
private:
CustomConfig::LocalState region_state;
CoreAlloc core_alloc;
public:
Region(void* base, size_t size)
: region_state(base, size),
core_alloc(&alloc.get_local_cache(), ®ion_state)
{
// Bind the core_alloc into the region local allocator
alloc.init(&core_alloc);
}
};
int main()
{
#ifndef SNMALLOC_PASS_THROUGH // Depends on snmalloc specific features
setup();
// 28 is large enough to produce a nested allocator.
// It is also large enough for the example to run in.
auto size = bits::one_at_bit(28);
auto base = DefaultPal::reserve(size);
DefaultPal::notify_using<NoZero>(base, size);
auto end = pointer_offset(base, size);
std::cout << "Allocated region " << base << " - "
<< pointer_offset(base, size) << std::endl;
Region r(base, size);
auto& a = r.alloc;
size_t object_size = 128;
size_t count = 0;
size_t i = 0;
while (true)
{
auto r1 = a.alloc(object_size);
count += object_size;
i++;
if (i == 1024)
{
i = 0;
std::cout << ".";
}
// Run until we exhaust the fixed region.
// This should return null.
if (r1 == nullptr)
break;
if (base > r1)
{
std::cout << "Allocated: " << r1 << std::endl;
abort();
}
if (end < r1)
{
std::cout << "Allocated: " << r1 << std::endl;
abort();
}
}
std::cout << "Total allocated: " << count << " out of " << size << std::endl;
std::cout << "Overhead: 1/" << (double)size / (double)(size - count)
<< std::endl;
a.teardown();
#endif
}