summaryrefslogtreecommitdiff
path: root/source/core/slang-memory-arena.cpp
blob: bdfd87602ad958b3143e5a90638129e14330fef2 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

#include "slang-memory-arena.h"

namespace Slang {

MemoryArena::MemoryArena()
{
    // Mark as invalid so any alloc call will fail
    m_blockAlignment = 0;
    m_blockSize = 0;

    // Set up as empty
    m_blocks = nullptr;
    _setCurrentBlock(nullptr);
    m_blockFreeList.init(sizeof(Block), sizeof(void*), 16);
}

MemoryArena::~MemoryArena()
{
    _deallocateBlocks();
}

MemoryArena::MemoryArena(size_t blockSize, size_t blockAlignment)
{
    _initialize(blockSize, blockAlignment);
}

void MemoryArena::init(size_t blockSize, size_t blockAlignment)
{
    _deallocateBlocks();
    m_blockFreeList.reset();
    _initialize(blockSize, blockAlignment);
}
  
void MemoryArena::_initialize(size_t blockSize, size_t alignment)
{
    // Alignment must be a power of 2
    assert(((alignment - 1) & alignment) == 0);

    // Must be at least sizeof(void*) in size, as that is the minimum the backing allocator will be
    alignment = (alignment < kMinAlignment) ? kMinAlignment : alignment;

    // If alignment required is larger then the backing allocators then
    // make larger to ensure when alignment correction takes place it will be aligned
    if (alignment > kMinAlignment)
    {
        blockSize += alignment;
    }

    m_blockSize = blockSize;
    m_blockAlignment = alignment;
    m_blocks = nullptr;
    _setCurrentBlock(nullptr);
    m_blockFreeList.init(sizeof(Block), sizeof(void*), 16);
}

void* MemoryArena::_allocateAligned(size_t size, size_t alignment)
{
    assert(size);
    // Can't be space in the current block -> so we can either place in next, or in a new block
    _newCurrentBlock(size, alignment);
    uint8_t* const current = m_current;
    // If everything has gone to plan, must be space here...
    assert(current + size <= m_end);
    m_current = current + size;
    return current;
}

void MemoryArena::_deallocateBlocks()
{
    Block* currentBlock = m_blocks;
    while (currentBlock)
    {
        // Deallocate the block
        ::free(currentBlock->m_alloc);
        // next block
        currentBlock = currentBlock->m_next;
    }
    // Can deallocate all blocks to
    m_blockFreeList.deallocateAll();
}

void MemoryArena::_setCurrentBlock(Block* block)
{
    if (block)
    {
        m_end = block->m_end;
        m_start = block->m_start;
        m_current = m_start;
    }
    else
    {
        m_start = nullptr;
        m_end = nullptr;
        m_current = nullptr;
    }
    m_currentBlock = block;
}

MemoryArena::Block* MemoryArena::_newCurrentBlock(size_t size, size_t alignment)
{
    // Make sure init has been called (or has been set up in parameterized constructor)
    assert(m_blockSize > 0);
    // Alignment must be a power of 2
    assert(((alignment - 1) & alignment) == 0);

    // Alignment must at a minimum be block alignment (such if reused the constraints hold)
    alignment = (alignment < m_blockAlignment) ? m_blockAlignment : alignment;

    const size_t alignMask = alignment - 1;

    // First try the next block (if there is one)
    {
        Block* next = m_currentBlock ? m_currentBlock->m_next : m_blocks;
        if (next)
        {
            // Align could be done from the actual allocation start, but doing so would mean a pointer which
            // didn't hit the constraint of being between start/end
            // So have to align conservatively using start
            uint8_t* memory = (uint8_t*)((size_t(next->m_start) + alignMask) & ~alignMask);

            // Check if can fit block in
            if (memory + size <= next->m_end)
            {
                _setCurrentBlock(next);
                return next;
            }
        }
    }

    // The size of the block must be at least large enough to take into account alignment
    size_t allocSize = (alignment <= kMinAlignment) ? size : (size + alignment);

    // The minimum block size should be at least m_blockSize
    allocSize = (allocSize < m_blockSize) ? m_blockSize : allocSize;

    // Allocate block
    Block* block = (Block*)m_blockFreeList.allocate();
    if (!block)
    {
        return nullptr;
    }
    // Allocate the memory
    uint8_t* alloc = (uint8_t*)::malloc(allocSize);
    if (!alloc)
    {
        m_blockFreeList.deallocate(block);
        return nullptr;
    }
    // Do the alignment on the allocation
    uint8_t* const start = (uint8_t*)((size_t(alloc) + alignMask) & ~alignMask);

    // Setup the block
    block->m_alloc = alloc;
    block->m_start = start;
    block->m_end = alloc + allocSize;
    block->m_next = nullptr;

    // Insert block into list
    if (m_currentBlock)
    {
        // Insert after current block
        block->m_next = m_currentBlock->m_next;
        m_currentBlock->m_next = block;
    }
    else
    {
        // Add to start of the list of the blocks
        block->m_next = m_blocks;
        m_blocks = block;
    }
    _setCurrentBlock(block);
    return block;
}

MemoryArena::Block* MemoryArena::_findBlock(const void* alloc, Block* endBlock) const
{
    const uint8_t* ptr = (const uint8_t*)alloc;

    Block* block = m_blocks;
    while (block != endBlock)
    {
        if (ptr >= block->m_start && ptr < block->m_end)
        {
            return block;
        }
        block = block->m_next;
    }
    return nullptr;
}

MemoryArena::Block* MemoryArena::_findPreviousBlock(Block* block)
{
    Block* currentBlock = m_blocks;
    while (currentBlock)
    {
        if (currentBlock->m_next == block)
        {
            return currentBlock;
        }
        currentBlock = currentBlock->m_next;
    }
    return nullptr;
}

void MemoryArena::deallocateAll()
{
    Block** prev = &m_blocks;
    Block* block = m_blocks;

    while (block)
    {
        if (size_t(block->m_end - block->m_alloc) > m_blockSize)
        {
            // Oversized block so we need to free it and remove from the list
            Block* nextBlock = block->m_next;
            *prev = nextBlock;
            // Free the backing memory
            ::free(block->m_alloc);
            // Free the block
            m_blockFreeList.deallocate(block);
            // prev stays the same, now working on next tho
            block = nextBlock;
        }
        else
        {
            // Onto next
            prev = &block->m_next;
            block = block->m_next;
        }
    }

    // Make the first current (if any)
    _setCurrentBlock(m_blocks);
}

void MemoryArena::reset()
{
    _deallocateBlocks();
    m_blocks = nullptr;
    _setCurrentBlock(nullptr);
}

} // namespace Slang