-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathAllocator.hpp
322 lines (262 loc) · 10.6 KB
/
Allocator.hpp
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
////////////////////////////////////////////////////////////////////////
// Copyright (c) 2010-2015, University of Washington and Battelle
// Memorial Institute. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
// * 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.
// * Neither the name of the University of Washington, Battelle
// Memorial Institute, or the names of their contributors may be
// used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
// UNIVERSITY OF WASHINGTON OR BATTELLE MEMORIAL INSTITUTE 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.
////////////////////////////////////////////////////////////////////////
/// Generic buddy allocator. Used by GlobalAllocator to implement
/// global heap.
///
/// If there's an error, it's probably because you are asking for more
/// memory than is available for allocation.
/// TODO: better error messages....
#ifndef __ALLOCATOR_HPP__
#define __ALLOCATOR_HPP__
#include <cstdlib>
#include <cassert>
#include <list>
#include <map>
#include <glog/logging.h>
#include <iostream>
#include <fstream>
#include <exception>
#include <boost/tuple/tuple.hpp>
typedef intptr_t AllocatorAddress;
struct AllocatorChunk;
typedef std::map< AllocatorAddress, AllocatorChunk > ChunkMap;
typedef std::list< ChunkMap::iterator > FreeList;
typedef std::map< size_t, FreeList > FreeListMap;
/// Private struct used in allocator
struct AllocatorChunk {
bool in_use;
AllocatorAddress address;
size_t size;
FreeList::iterator free_list_slot;
AllocatorChunk( AllocatorAddress address, size_t size )
: in_use( false )
, address( address )
, size( size )
, free_list_slot( ) // no way to make this invalid by default
{ }
std::ostream& dump( std::ostream& o ) const {
return o << "[ chunk " << (void *) address
<< " size " << size
<< " in_use " << in_use
<< " ]";
}
};
// overload output operator for debugging
std::ostream& operator<<( std::ostream& o, const AllocatorChunk& chunk );
class Allocator;
std::ostream& operator<<( std::ostream& o, const Allocator& a );
/// Generic buddy allocator. Used by GlobalAllocator to implement
/// global heap.
class Allocator {
private:
const AllocatorAddress base_;
const size_t size_;
ChunkMap chunks_; /// stores all chunks available to allocator
FreeListMap free_lists_; /// stores
/// find the next largest power of 2
/// from Stanford bit twiddling hacks
int64_t next_largest_power_of_2( int64_t v ) const {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
v++;
return v;
}
void remove_from_free_list( const ChunkMap::iterator & it ) {
free_lists_[ it->second.size ].erase( it->second.free_list_slot );
if( free_lists_[ it->second.size ].size() == 0 ) {
free_lists_.erase( it->second.size );
}
it->second.in_use = true;
}
void add_to_free_list( const ChunkMap::iterator & it ) {
free_lists_[ it->second.size ].push_front( it );
it->second.free_list_slot = free_lists_[ it->second.size ].begin();
it->second.in_use = false;
}
ChunkMap::iterator add_to_chunk_map( const AllocatorChunk& ac ) {
ChunkMap::iterator it;
bool inserted = false;
boost::tie( it, inserted ) = chunks_.insert( std::make_pair( ac.address, ac ) );
assert( inserted );
return it;
}
void try_merge_buddy_recursive( ChunkMap::iterator cmit ) {
// compute address of buddy
intptr_t address = cmit->second.address;
intptr_t buddy_address = (address ^ cmit->second.size);
DVLOG(5) << cmit->second << " buddy address " << (void *) buddy_address;
// does it exist?
ChunkMap::iterator buddy_iterator = chunks_.find( buddy_address );
if( buddy_iterator != chunks_.end() &&
buddy_iterator->second.size == cmit->second.size &&
buddy_iterator->second.in_use == false ) {
DVLOG(5) << "buddy found! address " << (void *) address << " buddy address " << (void *) buddy_address;
// remove the higher-addressed chunk
ChunkMap::iterator higher_iterator = address < buddy_address ? buddy_iterator : cmit;
remove_from_free_list( higher_iterator );
chunks_.erase( higher_iterator );
// keep the the lower-addressed chunk in the map:
// update its size and move it to the right free list
ChunkMap::iterator lower_iterator = address < buddy_address ? cmit : buddy_iterator;
remove_from_free_list( lower_iterator ); // should these be swapped? I think so.
lower_iterator->second.size *= 2;
add_to_free_list( lower_iterator );
// see if we have more to merge
try_merge_buddy_recursive( lower_iterator );
}
}
public:
class Exception : public std::exception {};
Allocator( void * base, int64_t size )
: base_( reinterpret_cast< AllocatorAddress >( base ) )
, size_( size )
, chunks_( )
, free_lists_( )
{
// must pass a non-zero chunk to constructor
CHECK( size != 0 ) << "Must pass a non-zero chunk size to constructor";
DVLOG(1) << "Allocator is responsible for addresses from " << base << " to " << (void*) ((char*) base + size);
intptr_t offset = 0;
// add chunks
while( size > 0 ) {
AllocatorAddress this_chunk_offset = offset;
size_t this_chunk_size = size;
// is this chunk's size not a power of two?
if( ( size & (size - 1) ) != 0 ) {
this_chunk_size = next_largest_power_of_2( size / 2 );
//DVLOG(5) << "Not a power of two: adding chunk at " << this_chunk_offset << " with size " << this_chunk_size;
}
ChunkMap::iterator it = add_to_chunk_map( AllocatorChunk( this_chunk_offset, this_chunk_size ) );
add_to_free_list( it );
size -= this_chunk_size;
offset += this_chunk_size;
}
}
/// Free a previously-allocated chunk.
void free( void * void_address ) {
intptr_t address = reinterpret_cast< intptr_t >( void_address ) - base_;
ChunkMap::iterator block_to_free_iterator = chunks_.find( address );
assert( block_to_free_iterator != chunks_.end() );
assert( block_to_free_iterator->second.in_use == true );
add_to_free_list( block_to_free_iterator );
try_merge_buddy_recursive( block_to_free_iterator );
}
/// Allocate size bytes
void * malloc( size_t size ) {
int64_t allocation_size = next_largest_power_of_2( size );
// find a chunk large enough to start splitting.
FreeListMap::iterator flit = free_lists_.lower_bound( allocation_size );
if( flit == free_lists_.end() ) {
LOG(ERROR) << "Out of memory in the global heap: couldn't find a chunk of size " << allocation_size
<< " to hold an allocation of " << size << " bytes. Can you increase --global_heap_fraction?";
// do this with an exception rather than CHECK_NE() so the test fixture can catch it.
throw Allocator::Exception();
}
int64_t chunk_size = flit->first;
DVLOG(5) << "chunk_size is " << chunk_size;
DVLOG(5) << "free list size is " << flit->second.size();
ChunkMap::iterator cmit = flit->second.front(); // huh. this is broken.
// I think there's nothing there, even though there isn't supposed to be.
//DVLOG(5) << "cmit is " << (void*)cmit;
if( cmit == chunks_.end() ) DVLOG(5) << "nothing found. dump:" << *this;
// subdivide chunk until we have what we need
while( chunk_size > allocation_size ) {
// remove big chunk from free list
remove_from_free_list( cmit );
// begin chopping chunk in half
chunk_size /= 2;
cmit->second.size = chunk_size;
// add to new free list
add_to_free_list( cmit );
// create new buddy chunk and add to chunk map and free list
intptr_t new_chunk_address = cmit->second.address + cmit->second.size;
cmit = add_to_chunk_map( AllocatorChunk( new_chunk_address, chunk_size ) );
add_to_free_list( cmit );
}
// finally we have a chunk of the right size
cmit->second.in_use = true;
remove_from_free_list( cmit );
return reinterpret_cast< void * >( cmit->second.address + base_ );
}
int64_t num_chunks() const {
return chunks_.size();
}
int64_t total_bytes() const {
int64_t total = 0;
for( ChunkMap::const_iterator i = chunks_.begin(); i != chunks_.end(); ++i ) {
total += i->second.size;
}
return total;
}
int64_t total_bytes_in_use() const {
int64_t total = 0;
for( ChunkMap::const_iterator i = chunks_.begin(); i != chunks_.end(); ++i ) {
if( i->second.in_use ) {
total += i->second.size;
}
}
return total;
}
int64_t total_bytes_free() const {
int64_t total = 0;
for( ChunkMap::const_iterator i = chunks_.begin(); i != chunks_.end(); ++i ) {
if( !( i->second.in_use ) ) {
total += i->second.size;
}
}
return total;
}
/// output human-readable state
std::ostream & dump( std::ostream& o = std::cout ) const {
o << "all chunks = {" << std::endl;
for( ChunkMap::const_iterator i = chunks_.begin(); i != chunks_.end(); ++i ) {
o << " " << i->second << std::endl;
}
o << "}, free lists = {" << std::endl;
for( FreeListMap::const_iterator i = free_lists_.begin(); i != free_lists_.end(); ++i ) {
o << " " << i->first << ":";
for( FreeList::const_iterator j = i->second.begin(); j != i->second.end(); ++j ) {
o << " " << (*j)->second;
}
o << std::endl;
}
return o << "}";
}
};
#endif