-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgspan_allocator.cpp
More file actions
63 lines (52 loc) · 1.25 KB
/
gspan_allocator.cpp
File metadata and controls
63 lines (52 loc) · 1.25 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
#include "gspan_allocator.hpp"
#include <new>
#include <cassert>
#ifndef BR
#define BR asm volatile ("int3;")
#endif
namespace gSpan
{
// -------------- FixedAllocator -----------------------
FixedAllocator::~FixedAllocator()
{
while (!free_ptrs_.empty())
{
void* p = free_ptrs_.back();
free_ptrs_.pop_back();
::operator delete(p);
}
}
void* FixedAllocator::allocate()
{
if (!free_ptrs_.empty())
{
void* p = free_ptrs_.back();
free_ptrs_.pop_back();
return p;
}
return ::operator new (data_size_);
}
// ------------- MemAllocator --------------------------
MemAllocator::~MemAllocator()
{
for (std::size_t i = 0; i < fallocs_.size(); ++i)
delete fallocs_[i];
}
FixedAllocator* MemAllocator::get_fixed_allocator(std::size_t data_size)
{
assert(data_size > 0);
std::size_t old_size = fallocs_.size();
if (old_size < data_size)
{
fallocs_.resize(data_size);
for (std::size_t i = old_size; i < data_size; ++i)
fallocs_[i] = new FixedAllocator(i + 1);
}
return fallocs_[data_size - 1];
}
void MemAllocator::deallocate(void* p, std::size_t data_size)
{
assert(data_size <= fallocs_.size());
fallocs_[data_size - 1]->deallocate(p);
}
}