YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
default_allocator.cpp
Go to the documentation of this file.
2
3#include <sys/mman.h>
4#include <unistd.h>
5
6namespace yaclib::detail::fiber {
7namespace {
8
9const auto kPageSize = static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
10
11std::uint32_t gCacheSize = 100;
12
13} // namespace
14
16 if (!_pool.empty()) {
17 auto allocation = _pool.back();
18 _pool.pop_back();
19 return allocation;
20 }
21 std::size_t size = _stack_size_pages * kPageSize;
22
23 void* start = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
24 YACLIB_DEBUG(start == MAP_FAILED, "mmap for stack failed");
25 auto status = mprotect(static_cast<void*>(start), kPageSize, PROT_NONE);
26 YACLIB_DEBUG(status == -1, "mprotect for stack failed");
27
28 auto allocation = Allocation{static_cast<char*>(start), size};
29 return allocation;
30}
31
33 if (_pool.size() < gCacheSize) {
34 _pool.push_back(allocation);
35 } else {
36 if (allocation.start == nullptr) {
37 return;
38 }
39
40 auto status = munmap(static_cast<void*>(allocation.start), allocation.size);
41 YACLIB_DEBUG(status == -1, "munmap for stack failed");
42 }
43}
44
45void DefaultAllocator::SetMinStackSize(std::size_t pages) noexcept {
46 if (pages > _stack_size_pages) {
47 for (auto& allocation : _pool) {
48 Release(allocation);
49 }
50 _pool.clear();
51 _stack_size_pages = pages + 1;
52 }
53}
54
56 return _stack_size_pages;
57}
58
59void DefaultAllocator::SetCacheSize(std::uint32_t size) noexcept {
60 gCacheSize = size;
61}
62
63} // namespace yaclib::detail::fiber
static void SetCacheSize(std::uint32_t size) noexcept
std::size_t GetMinStackSize() noexcept final
void Release(Allocation allocation) final
void SetMinStackSize(std::size_t pages) noexcept final
#define YACLIB_DEBUG(cond, message)
Definition log.hpp:84
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25