2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2019-02-12 21:19:11 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class StackAllocator {
|
|
|
|
// TODO: alloc memory with mmap and unload unused pages
|
|
|
|
// memory still can be corrupted, but it is better than explicit free function
|
|
|
|
class Ptr {
|
|
|
|
public:
|
2021-11-17 13:15:40 +01:00
|
|
|
Ptr(char *ptr, size_t size) : slice_(ptr, size) {
|
|
|
|
}
|
|
|
|
Ptr(const Ptr &other) = delete;
|
|
|
|
Ptr &operator=(const Ptr &other) = delete;
|
|
|
|
Ptr(Ptr &&other) noexcept : slice_(other.slice_) {
|
|
|
|
other.slice_ = MutableSlice();
|
|
|
|
}
|
|
|
|
Ptr &operator=(Ptr &&other) = delete;
|
|
|
|
~Ptr() {
|
|
|
|
if (!slice_.empty()) {
|
|
|
|
free_ptr(slice_.data(), slice_.size());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice as_slice() const {
|
2021-11-17 13:15:40 +01:00
|
|
|
return slice_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-17 13:15:40 +01:00
|
|
|
MutableSlice slice_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Impl {
|
|
|
|
static const size_t MEM_SIZE = 1024 * 1024;
|
|
|
|
std::array<char, MEM_SIZE> mem;
|
|
|
|
|
|
|
|
size_t pos{0};
|
2021-11-17 13:15:40 +01:00
|
|
|
Ptr alloc(size_t size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (size == 0) {
|
2021-11-17 13:15:40 +01:00
|
|
|
size = 8;
|
|
|
|
} else {
|
|
|
|
if (size > MEM_SIZE) {
|
|
|
|
std::abort(); // too much memory requested
|
|
|
|
}
|
|
|
|
size = (size + 7) & -8;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
char *res = mem.data() + pos;
|
|
|
|
pos += size;
|
|
|
|
if (pos > MEM_SIZE) {
|
|
|
|
std::abort(); // memory is over
|
|
|
|
}
|
2021-11-17 13:15:40 +01:00
|
|
|
return Ptr(res, size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-11-17 13:15:40 +01:00
|
|
|
void free_ptr(char *ptr, size_t size) {
|
|
|
|
if (size > pos || ptr != mem.data() + (pos - size)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::abort(); // shouldn't happen
|
|
|
|
}
|
2021-11-17 13:15:40 +01:00
|
|
|
pos -= size;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static Impl &impl();
|
|
|
|
|
2021-11-17 13:15:40 +01:00
|
|
|
static void free_ptr(char *ptr, size_t size) {
|
|
|
|
impl().free_ptr(ptr, size);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
static Ptr alloc(size_t size) {
|
2021-11-17 13:15:40 +01:00
|
|
|
return impl().alloc(size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|