forked from microsoft/snmalloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpal_bsd_aligned.h
54 lines (46 loc) · 1.29 KB
/
pal_bsd_aligned.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
#pragma once
#include "pal_bsd.h"
namespace snmalloc
{
/**
* FreeBSD-specific platform abstraction layer.
*
* This adds aligned allocation using `MAP_ALIGNED` to the generic BSD
* implementation. This flag is supported by NetBSD and FreeBSD.
*/
template<class OS>
class PALBSD_Aligned : public PALBSD<OS>
{
public:
/**
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*
* This class adds support for aligned allocation.
*/
static constexpr uint64_t pal_features =
AlignedAllocation | PALBSD<OS>::pal_features;
static constexpr size_t minimum_alloc_size = 4096;
/**
* Reserve memory at a specific alignment.
*/
template<bool committed>
void* reserve_aligned(size_t size) noexcept
{
// Alignment must be a power of 2.
SNMALLOC_ASSERT(size == bits::next_pow2(size));
SNMALLOC_ASSERT(size >= minimum_alloc_size);
size_t log2align = bits::next_pow2_bits(size);
void* p = mmap(
nullptr,
size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED(log2align),
-1,
0);
if (p == MAP_FAILED)
PALBSD<OS>::error("Out of memory");
return p;
}
};
} // namespace snmalloc