blob: 84ad1f6d0d32b63ac87a4a9773595c8e1155c0ac (
plain)
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
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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"
#include "td/utils/Slice.h"
namespace td {
class StackAllocator {
public:
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 {
public:
Ptr(AllocatorImpl *allocator, size_t size) : allocator_(allocator), slice_(allocator_->allocate(size)) {
}
Ptr(const Ptr &) = delete;
Ptr &operator=(const Ptr &) = delete;
Ptr(Ptr &&other) noexcept : allocator_(other.allocator_), slice_(other.slice_) {
other.allocator_ = nullptr;
other.slice_ = MutableSlice();
}
Ptr &operator=(Ptr &&) = delete;
~Ptr();
MutableSlice as_slice() const {
return slice_;
}
private:
AllocatorImpl *allocator_;
MutableSlice slice_;
};
static AllocatorImpl *impl();
public:
static Ptr alloc(size_t size) {
return Ptr(impl(), size);
}
};
} // namespace td
|