10
10
*/
11
11
12
12
13
+ #include < malloc.h>
14
+ #include < stdlib.h>
15
+
13
16
#include < alaska/Heap.hpp>
14
17
#include < alaska/HugeObjectAllocator.hpp>
18
+ #include < alaska/liballoc.h>
15
19
#include < alaska/list_head.h>
16
20
17
21
namespace alaska {
18
- HugeObjectAllocator::HugeObjectAllocator () { INIT_LIST_HEAD (&this ->allocations ); }
22
+ HugeObjectAllocator::HugeObjectAllocator (HugeAllocationStrategy strat)
23
+ : strat(strat) {
24
+ INIT_LIST_HEAD (&this ->allocations );
25
+ }
19
26
20
27
HugeObjectAllocator::~HugeObjectAllocator () {
21
28
HugeHeader *entry, *temp;
@@ -30,6 +37,11 @@ namespace alaska {
30
37
31
38
32
39
void * HugeObjectAllocator::allocate (size_t size) {
40
+ if (strat == HugeAllocationStrategy::MALLOC_BACKED) {
41
+ return ::malloc (size);
42
+ }
43
+ ALASKA_ASSERT (strat == HugeAllocationStrategy::CUSTOM_MMAP_BACKED, " Invalid huge strat" );
44
+
33
45
ck::scoped_lock l (m_lock);
34
46
35
47
size_t mapping_size = ((size + sizeof (HugeHeader)) + 4095 ) & ~4095 ;
@@ -54,6 +66,11 @@ namespace alaska {
54
66
55
67
56
68
bool HugeObjectAllocator::free (void * ptr) {
69
+ if (strat == HugeAllocationStrategy::MALLOC_BACKED) {
70
+ ::free (ptr);
71
+ return true ;
72
+ }
73
+ ALASKA_ASSERT (strat == HugeAllocationStrategy::CUSTOM_MMAP_BACKED, " Invalid huge strat" );
57
74
ck::scoped_lock l (m_lock);
58
75
59
76
// Get the HugeHeader object from the user pointer
@@ -70,6 +87,8 @@ namespace alaska {
70
87
71
88
72
89
size_t HugeObjectAllocator::size_of (void * ptr) {
90
+ if (strat == HugeAllocationStrategy::MALLOC_BACKED) return ::malloc_usable_size (ptr);
91
+
73
92
ck::scoped_lock l (m_lock);
74
93
HugeHeader* header = find_header (ptr);
75
94
if (header != nullptr ) {
@@ -80,6 +99,8 @@ namespace alaska {
80
99
81
100
82
101
bool HugeObjectAllocator::owns (void * ptr) {
102
+ if (strat == HugeAllocationStrategy::MALLOC_BACKED) return true ;
103
+
83
104
ck::scoped_lock l (m_lock);
84
105
// If the header is null, this allocator doesn't own it.
85
106
return find_header (ptr) != nullptr ;
@@ -97,4 +118,4 @@ namespace alaska {
97
118
// If no matching header is found, return nullptr
98
119
return nullptr ;
99
120
}
100
- } // namespace alaska} // namespace alaska
121
+ } // namespace alaska
0 commit comments