-
Notifications
You must be signed in to change notification settings - Fork 1
/
alignedmmapheap.h
77 lines (52 loc) · 1.72 KB
/
alignedmmapheap.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
// -*- C++ -*-
#ifndef _ALIGNEDMMAPHEAP_H_
#define _ALIGNEDMMAPHEAP_H_
#include "vamcommon.h"
#include "mapsizeheap.h"
#include "mmapheap.h"
#include "heaplayers.h"
using namespace HL;
namespace VAM {
// AlignedMmapHeap: an mmap-based heap that allocates aligned memory
class AlignedMmapHeap : public MapSizeHeap<PrivateMmapHeap> {
public:
// allocate aligned mmap region
inline void * malloc(size_t size, size_t alignment = PAGE_SIZE) {
assert(size != 0 && (size & ~PAGE_MASK) == 0);
assert(alignment != 0 && (alignment & ~PAGE_MASK) == 0);
if (size == 0 || (size & ~PAGE_MASK) != 0 || alignment == 0 || (alignment & ~PAGE_MASK) != 0)
return NULL;
size_t start = reinterpret_cast<size_t>(PrivateMmapHeap::malloc(size + alignment));
abort_on(start == 0);
size_t ptr = ((start - 1) / alignment + 1) * alignment;
if (ptr != start) {
PrivateMmapHeap::free(reinterpret_cast<void *>(start), ptr - start);
}
PrivateMmapHeap::free(reinterpret_cast<void *>(ptr + size), start + alignment - ptr);
_map_lock.lock();
_ptr_size_map[reinterpret_cast<void * const>(ptr)] = size;
_map_lock.unlock();
return reinterpret_cast<void *>(ptr);
}
}; //end of class AlignedMmapHeap
// TheOneAlignedMmapHeap: singleton of AlignedMmapHeap
class TheOneAlignedMmapHeap {
public:
TheOneAlignedMmapHeap() {
static AlignedMmapHeap aligned_mmap_heap;
_heap = &aligned_mmap_heap;
}
inline void * malloc(size_t size, size_t alignment = PAGE_SIZE) {
return _heap->malloc(size, alignment);
}
inline void free(void * ptr) {
_heap->free(ptr);
}
inline size_t getSize(void * ptr) {
return _heap->getSize(ptr);
}
private:
AlignedMmapHeap * _heap;
}; // end of class TheOneAlignedMmapHeap
}; // end of namespace VAM
#endif