File size: 4,103 Bytes
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "core/arena.hpp"

#include <algorithm>
#include <stdexcept>
#include <sys/mman.h>
#include <unistd.h>

namespace astra {

namespace {
size_t page_align(size_t n) {
    static const size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
    return (n + page_size - 1) & ~(page_size - 1);
}

void poison_buffer(uint8_t* ptr, size_t len) {
    uint32_t* words = reinterpret_cast<uint32_t*>(ptr);
    size_t word_count = len / sizeof(uint32_t);
    for (size_t i = 0; i < word_count; ++i) {
        words[i] = SlabArena::kPoisonPattern;
    }
}
} // namespace

SlabArena::SlabArena(size_t slab_size)
    : slab_size_(page_align(std::max<size_t>(slab_size, 4096))) {}

SlabArena::~SlabArena() {
    reset(true);
    release_all_slabs();
}

SlabArena::SlabArena(SlabArena&& other) noexcept
    : slab_size_(other.slab_size_),
      slabs_(std::move(other.slabs_)),
      destructors_(std::move(other.destructors_)),
      total_used_(other.total_used_),
      total_reserved_(other.total_reserved_) {
    other.total_used_ = 0;
    other.total_reserved_ = 0;
}

SlabArena& SlabArena::operator=(SlabArena&& other) noexcept {
    if (this != &other) {
        reset(true);
        release_all_slabs();
        slab_size_ = other.slab_size_;
        slabs_ = std::move(other.slabs_);
        destructors_ = std::move(other.destructors_);
        total_used_ = other.total_used_;
        total_reserved_ = other.total_reserved_;
        other.total_used_ = 0;
        other.total_reserved_ = 0;
    }
    return *this;
}

void SlabArena::allocate_slab(size_t min_bytes) {
    size_t cap = page_align(std::max(slab_size_, min_bytes));
    void* ptr = mmap(nullptr, cap, PROT_READ | PROT_WRITE,
                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (ptr == MAP_FAILED) {
        throw std::bad_alloc();
    }
    slabs_.push_back(Slab{static_cast<uint8_t*>(ptr), cap, 0});
    total_reserved_ += cap;
}

void SlabArena::release_all_slabs() {
    for (const auto& slab : slabs_) {
        if (slab.base) {
            munmap(slab.base, slab.capacity);
        }
    }
    slabs_.clear();
    total_reserved_ = 0;
}

void* SlabArena::allocate(size_t bytes, size_t alignment) {
    if (bytes == 0) bytes = 1;
    if (alignment == 0) alignment = 1;

    if (!slabs_.empty()) {
        Slab& cur = slabs_.back();
        size_t aligned = (cur.offset + (alignment - 1)) & ~(alignment - 1);
        if (aligned + bytes <= cur.capacity) {
            void* result = cur.base + aligned;
            cur.offset = aligned + bytes;
            total_used_ += bytes;
            return result;
        }
    }

    allocate_slab(bytes + alignment);
    Slab& cur = slabs_.back();
    size_t aligned = (cur.offset + (alignment - 1)) & ~(alignment - 1);
    void* result = cur.base + aligned;
    cur.offset = aligned + bytes;
    total_used_ += bytes;
    return result;
}

std::string_view SlabArena::intern(std::string_view s) {
    if (s.empty()) return {};
    char* dst = static_cast<char*>(allocate(s.size() + 1, 1));
    std::memcpy(dst, s.data(), s.size());
    dst[s.size()] = '\0';
    return std::string_view(dst, s.size());
}

void SlabArena::reset(bool release_to_os) {
    // Destroy objects in reverse allocation order.
    for (auto it = destructors_.rbegin(); it != destructors_.rend(); ++it) {
        it->fn(it->ptr);
    }
    destructors_.clear();

    if (release_to_os) {
        // Poison active bytes then unmap all slabs so RSS immediately drops.
        for (auto& slab : slabs_) {
            if (slab.base && slab.offset > 0) {
                poison_buffer(slab.base, std::min<size_t>(slab.offset, 4096));
                madvise(slab.base, slab.capacity, MADV_DONTNEED);
            }
        }
        release_all_slabs();
    } else {
        for (auto& slab : slabs_) {
            if (slab.base && slab.offset > 0) {
                poison_buffer(slab.base, std::min<size_t>(slab.offset, 4096));
                madvise(slab.base, slab.capacity, MADV_DONTNEED);
            }
            slab.offset = 0;
        }
    }
    total_used_ = 0;
}

} // namespace astra