MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_memory_fixed_size_allocator.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_MEMORY_FIXED_SIZE_ALLOCATOR_H_1653211251
12#define INCGUARD_MGL_MEMORY_FIXED_SIZE_ALLOCATOR_H_1653211251
13
14#include <memory>
15#include <mutex>
16
17namespace MGL::Memory
18{
21{
22public:
24 struct Block
25 {
26 void *address;
28 };
29
31 // Note: デフォルトアロケータと絡んでいるため、これ単体で変更しない事!
32 enum class Tag : int8_t
33 {
34 Fixed0 = 0,
35 Fixed1 = 1,
36 Fixed2 = 2,
37 Fixed3 = 3,
38
39 None = -1,
40 Extra = -2,
41 };
42
44 struct Header
45 {
47 int8_t unused;
48 uint16_t usedSize;
49
50 union
51 {
53 size_t requestSize;
54 };
55 };
56 static_assert(sizeof(Header) <= 16); // ヘッダサイズは16バイト以下でなければならない
57
58 bool Initialize(Tag tag, size_t blockSize, size_t blockCount) noexcept;
59
60 /* ------------------------------------------------------------------------- */
65 /* ------------------------------------------------------------------------- */
66 constexpr void SetAltAllocator(FixedSizeAllocator *altAllocator) noexcept
67 {
68 _altAllocator = altAllocator;
69 }
70
71 /* ------------------------------------------------------------------------- */
76 /* ------------------------------------------------------------------------- */
77 [[nodiscard]] constexpr size_t GetBlockSize() const noexcept
78 {
79 return _blockSize;
80 }
81
82 /* ------------------------------------------------------------------------- */
87 /* ------------------------------------------------------------------------- */
88 [[nodiscard]] constexpr size_t GetCapacity() const noexcept
89 {
90 return _blockCount;
91 }
92
93 /* ------------------------------------------------------------------------- */
98 /* ------------------------------------------------------------------------- */
99 size_t GetFreeCount() noexcept
100 {
101 const std::lock_guard lock(_mutex);
102 return _freeCount;
103 }
104
105 /* ------------------------------------------------------------------------- */
110 /* ------------------------------------------------------------------------- */
111 size_t GetUsedCount() noexcept
112 {
113 return GetCapacity() - GetFreeCount();
114 }
115
116 void *Allocate(size_t size) noexcept;
117 void Deallocate(void *buffer) noexcept;
118
119private:
120 Tag _tag{Tag::None};
121 size_t _blockSize{0};
122 size_t _blockCount{0};
123 std::unique_ptr<std::byte[]> _pool{nullptr};
124 size_t _poolSize{0};
125 std::unique_ptr<Block[]> _blockArray{nullptr};
126 Block *_topBlock{nullptr};
127 size_t _freeCount{0};
128 FixedSizeAllocator *_altAllocator{nullptr};
129
130 std::mutex _mutex;
131};
132} // namespace MGL::Memory
133
134#endif // INCGUARD_MGL_MEMORY_FIXED_SIZE_ALLOCATOR_H_1653211251
135
136// vim: et ts=4 sw=4 sts=4
固定サイズアロケータクラス
Definition mgl_memory_fixed_size_allocator.h:21
void Deallocate(void *buffer) noexcept
デアロケート
Definition mgl_memory_fixed_size_allocator.cc:117
constexpr size_t GetCapacity() const noexcept
確保可能な最大数を取得
Definition mgl_memory_fixed_size_allocator.h:88
void * Allocate(size_t size) noexcept
アロケート
Definition mgl_memory_fixed_size_allocator.cc:76
Tag
アロケータの種類を判別するためのタグ
Definition mgl_memory_fixed_size_allocator.h:33
@ Fixed3
インデックス3の固定サイズアロケータ
@ Fixed1
インデックス1の固定サイズアロケータ
@ Extra
外部アロケータを使用
@ Fixed0
インデックス0の固定サイズアロケータ
@ Fixed2
インデックス2の固定サイズアロケータ
size_t GetFreeCount() noexcept
利用可能なブロック数を取得
Definition mgl_memory_fixed_size_allocator.h:99
bool Initialize(Tag tag, size_t blockSize, size_t blockCount) noexcept
初期化
Definition mgl_memory_fixed_size_allocator.cc:26
size_t GetUsedCount() noexcept
利用中のブロック数を取得
Definition mgl_memory_fixed_size_allocator.h:111
constexpr void SetAltAllocator(FixedSizeAllocator *altAllocator) noexcept
代替アロケータを設定
Definition mgl_memory_fixed_size_allocator.h:66
constexpr size_t GetBlockSize() const noexcept
ブロックサイズを取得
Definition mgl_memory_fixed_size_allocator.h:77
メモリブロック情報
Definition mgl_memory_fixed_size_allocator.h:25
void * address
メモリブロックのアドレス
Definition mgl_memory_fixed_size_allocator.h:26
Block * next
次のメモリブロック
Definition mgl_memory_fixed_size_allocator.h:27
ヘッダ情報
Definition mgl_memory_fixed_size_allocator.h:45
uint16_t usedSize
ブロック内の使用領域(ヘッダを除く)
Definition mgl_memory_fixed_size_allocator.h:48
size_t requestSize
要求サイズ(外部アロケータを使用した場合)
Definition mgl_memory_fixed_size_allocator.h:53
Block * block
使用したメモリブロック(固定サイズアロケータを使用した場合)
Definition mgl_memory_fixed_size_allocator.h:52
Tag tag
タグ
Definition mgl_memory_fixed_size_allocator.h:46
int8_t unused
未使用
Definition mgl_memory_fixed_size_allocator.h:47