Move StackAllocator implementation to cpp.

This commit is contained in:
levlam 2021-11-18 15:42:07 +03:00
parent bf9c78e982
commit 32713560b8
2 changed files with 75 additions and 52 deletions

View File

@ -8,12 +8,59 @@
#include "td/utils/port/thread_local.h" #include "td/utils/port/thread_local.h"
namespace td { #include <array>
#include <cstdlib>
StackAllocator::Impl &StackAllocator::impl() { namespace td {
static TD_THREAD_LOCAL StackAllocator::Impl *impl; // static zero-initialized namespace {
init_thread_local<Impl>(impl); class ArrayAllocator final : public StackAllocator::AllocatorImpl {
return *impl; static const size_t MEM_SIZE = 1024 * 1024;
std::array<char, MEM_SIZE> mem;
size_t pos{0};
MutableSlice allocate(size_t size) final {
if (size == 0) {
size = 8;
} else {
if (size > MEM_SIZE) {
std::abort(); // too much memory requested
}
size = (size + 7) & -8;
}
char *res = mem.data() + pos;
pos += size;
if (pos > MEM_SIZE) {
std::abort(); // memory is over
}
return {res, size};
}
void free_ptr(char *ptr, size_t size) final {
if (size > pos || ptr != mem.data() + (pos - size)) {
std::abort(); // shouldn't happen
}
pos -= size;
}
public:
~ArrayAllocator() final {
if (pos != 0) {
std::abort(); // shouldn't happen
}
}
};
} // namespace
StackAllocator::Ptr::~Ptr() {
if (!slice_.empty()) {
allocator_->free_ptr(slice_.data(), slice_.size());
}
}
StackAllocator::AllocatorImpl *StackAllocator::impl() {
static TD_THREAD_LOCAL ArrayAllocator *array_allocator; // static zero-initialized
init_thread_local<ArrayAllocator>(array_allocator);
return array_allocator;
} }
} // namespace td } // namespace td

View File

@ -9,76 +9,52 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include <array>
#include <cstdlib>
namespace td { namespace td {
class StackAllocator { class StackAllocator {
// TODO: alloc memory with mmap and unload unused pages public:
// memory still can be corrupted, but it is better than explicit free function class AllocatorImpl {
public:
AllocatorImpl() = default;
AllocatorImpl(const AllocatorImpl &) = delete;
AllocatorImpl &operator=(const AllocatorImpl &) = delete;
AllocatorImpl(AllocatorImpl &&) = delete;
AllocatorImpl &operator=(AllocatorImpl &&) = delete;
virtual ~AllocatorImpl() = default;
virtual MutableSlice allocate(size_t size) = 0;
virtual void free_ptr(char *ptr, size_t size) = 0;
};
private:
class Ptr { class Ptr {
public: public:
Ptr(char *ptr, size_t size) : slice_(ptr, size) { Ptr(AllocatorImpl *allocator, size_t size) : allocator_(allocator), slice_(allocator_->allocate(size)) {
} }
Ptr(const Ptr &other) = delete; Ptr(const Ptr &other) = delete;
Ptr &operator=(const Ptr &other) = delete; Ptr &operator=(const Ptr &other) = delete;
Ptr(Ptr &&other) noexcept : slice_(other.slice_) { Ptr(Ptr &&other) noexcept : allocator_(other.allocator_), slice_(other.slice_) {
other.allocator_ = nullptr;
other.slice_ = MutableSlice(); other.slice_ = MutableSlice();
} }
Ptr &operator=(Ptr &&other) = delete; Ptr &operator=(Ptr &&other) = delete;
~Ptr() { ~Ptr();
if (!slice_.empty()) {
free_ptr(slice_.data(), slice_.size());
}
}
MutableSlice as_slice() const { MutableSlice as_slice() const {
return slice_; return slice_;
} }
private: private:
AllocatorImpl *allocator_;
MutableSlice slice_; MutableSlice slice_;
}; };
struct Impl { static AllocatorImpl *impl();
static const size_t MEM_SIZE = 1024 * 1024;
std::array<char, MEM_SIZE> mem;
size_t pos{0};
Ptr alloc(size_t size) {
if (size == 0) {
size = 8;
} else {
if (size > MEM_SIZE) {
std::abort(); // too much memory requested
}
size = (size + 7) & -8;
}
char *res = mem.data() + pos;
pos += size;
if (pos > MEM_SIZE) {
std::abort(); // memory is over
}
return Ptr(res, size);
}
void free_ptr(char *ptr, size_t size) {
if (size > pos || ptr != mem.data() + (pos - size)) {
std::abort(); // shouldn't happen
}
pos -= size;
}
};
static Impl &impl();
static void free_ptr(char *ptr, size_t size) {
impl().free_ptr(ptr, size);
}
public: public:
static Ptr alloc(size_t size) { static Ptr alloc(size_t size) {
return impl().alloc(size); return Ptr(impl(), size);
} }
}; };