summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-memory-arena.cpp
blob: c8ab98827218df8bc58766f676b26a360a39a376 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// unit-test-free-list.cpp

#include "../../source/core/slang-list.h"
#include "../../source/core/slang-memory-arena.h"
#include "../../source/core/slang-random-generator.h"
#include "unit-test/slang-unit-test.h"

#include <stdio.h>
#include <stdlib.h>

using namespace Slang;


namespace // anonymous
{

struct Block
{
    void* m_data;
    size_t m_size;
    uint8_t m_value;
};

enum class TestMode
{
    eUnaligned,
    eImplicitAligned, ///< Alignment is kept implicitly with Unaligned allocs of the right size
    eDefaultAligned,
    eExplicitAligned,
    eCount,
};

} // namespace

static size_t getAlignment(TestMode mode)
{
    switch (mode)
    {
    default:
    case TestMode::eUnaligned:
        return 1;
    case TestMode::eExplicitAligned:
        return 16;
    case TestMode::eImplicitAligned:
        return 32;
    case TestMode::eDefaultAligned:
        return MemoryArena::kMinAlignment;
    }
}

static bool hasValueShort(const uint8_t* data, size_t size, uint8_t value)
{
    for (size_t i = 0; i < size; ++i)
    {
        if (data[i] != value)
        {
            return false;
        }
    }
    return true;
}

static bool hasValue(const uint8_t* data, size_t size, uint8_t value)
{
    const size_t alignMask = sizeof(size_t) - 1;

    if (size <= sizeof(size_t) * 2)
    {
        return hasValueShort(data, size, value);
    }

    if (size_t(data) & alignMask)
    {
        size_t firstSize = sizeof(size_t) - (size_t(data) & alignMask);
        if (!hasValueShort(data, firstSize, value))
        {
            return false;
        }
        size -= firstSize;
        data += firstSize;

        assert((size_t(data) & alignMask) == 0);
    }

    // Now do the middle
    size_t numWords = size / sizeof(size_t);

    // Expand the byte up to a word size
    size_t wordValue = (size_t(value) << 8) | value;
    wordValue = (wordValue << 16) | wordValue;
    wordValue = (sizeof(size_t) > 4) ? size_t((uint64_t(wordValue) << 32) | wordValue) : wordValue;

    const size_t* wordData = (const size_t*)data;
    for (size_t i = 0; i < numWords; ++i)
    {
        if (wordData[i] != wordValue)
        {
            return false;
        }
    }

    // Do the end piece
    return hasValueShort(data + sizeof(size_t) * numWords, size & alignMask, value);
}

SLANG_UNIT_TEST(memoryArena)
{
    DefaultRandomGenerator randGen(0x5346536a);

    {
        const size_t blockSize = 1024;
        MemoryArena arena;
        arena.init(blockSize);

        List<void*> blocks;

        blocks.add(arena.allocate(100));
        blocks.add(arena.allocate(blockSize * 2));
        blocks.add(arena.allocate(100));
        blocks.add(arena.allocate(blockSize * 2));
        blocks.add(arena.allocate(100));

        arena.deallocateAll();
        blocks.add(arena.allocate(100));
        blocks.add(arena.allocate(blockSize * 2));

        arena.reset();

        {
            uint32_t data[] = {1, 2, 3};

            const uint32_t* copy = arena.allocateAndCopyArray(data, SLANG_COUNT_OF(data));

            SLANG_CHECK(::memcmp(copy, data, sizeof(data)) == 0);
        }
    }

    {
        int count = 0;
        const size_t blockSize = 1024;

        for (TestMode mode = TestMode(0); int(mode) < int(TestMode::eCount);
             mode = TestMode(int(mode) + 1))
        {
            const size_t alignment = getAlignment(mode);

            MemoryArena arena;
            arena.init(blockSize, alignment);

            List<Block> blocks;

            for (int i = 0; i < 10000; i++)
            {
                count++;

                const int var = randGen.nextInt32() & 0x3ff;
                if (var < 3 && blocks.getCount() > 0)
                {
                    if (var == 1)
                    {
                        // Deallocate everything
                        arena.deallocateAll();
                        blocks.clear();
                    }
                    else if (var == 2)
                    {
                        arena.reset();
                        blocks.clear();
                    }
                    else if (var == 3)
                    {
                        arena.rewindToCursor(nullptr);
                        blocks.clear();
                    }
                    else if (var == 4)
                    {
                        // Rewind to a random position
                        int rewindIndex = randGen.nextInt32UpTo(int32_t(blocks.getCount()));
                        // rewind to this block
                        arena.rewindToCursor(blocks[rewindIndex].m_data);
                        // All the blocks (includign this one) and now deallocated
                        blocks.setCount(rewindIndex);
                    }
                    else
                    {
                        size_t usedMemory = arena.calcTotalMemoryUsed();
                        size_t allocatedMemory = arena.calcTotalMemoryAllocated();

                        SLANG_CHECK(allocatedMemory >= usedMemory);
                    }
                }
                else
                {
                    size_t sizeInBytes = (randGen.nextInt32() & 255) + 1;

                    // Lets go for an oversized block
                    if ((randGen.nextInt32() & 0xff) < 2)
                    {
                        sizeInBytes += blockSize;
                    }
                    else if ((randGen.nextInt32() & 0xff) < 2)
                    {
                        // Let's try for a block that's awkwardly sized
                        sizeInBytes = blockSize / 3 + 10;
                    }

                    const uint8_t value = uint8_t(randGen.nextInt32());

                    void* mem = nullptr;
                    switch (mode)
                    {
                    default:
                    case TestMode::eUnaligned:
                        {
                            mem = arena.allocateUnaligned(sizeInBytes);
                            break;
                        }
                    case TestMode::eImplicitAligned:
                        {
                            // Fix the size to get implicit alignment
                            sizeInBytes = (sizeInBytes & ~(alignment - 1)) + alignment;
                            mem = arena.allocateUnaligned(sizeInBytes);
                            break;
                        }
                    case TestMode::eExplicitAligned:
                        {
                            mem = arena.allocateAligned(sizeInBytes, alignment);
                            break;
                        }
                    case TestMode::eDefaultAligned:
                        {
                            mem = arena.allocate(sizeInBytes);
                            break;
                        }
                    }

                    // Check it is aligned
                    SLANG_CHECK((size_t(mem) & (alignment - 1)) == 0);

                    ::memset(mem, value, sizeInBytes);

                    Block block;

                    block.m_data = mem;
                    block.m_size = sizeInBytes;
                    block.m_value = value;

                    blocks.add(block);
                }

                // Check the blocks
                for (Index j = 0; j < blocks.getCount(); ++j)
                {
                    const Block& block = blocks[j];

                    SLANG_CHECK(arena.isValid(block.m_data, block.m_size));

                    SLANG_CHECK(hasValue((uint8_t*)block.m_data, block.m_size, block.m_value));
                }
            }
        }
    }
    {
        // Do lots of allocations and test out rewind
    }
}