File: memory_arena.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (99 lines) | stat: -rw-r--r-- 3,564 bytes parent folder | download | duplicates (3)
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
#include "HalideRuntime.h"

#include "common.h"
#include "printer.h"

#include "internal/memory_arena.h"

using namespace Halide::Runtime::Internal;

struct TestStruct {
    int8_t i8;
    uint16_t ui16;
    float f32;
};

int main(int argc, char **argv) {
    void *user_context = (void *)1;

    // test class interface
    {
        SystemMemoryAllocatorFns test_allocator = {allocate_system, deallocate_system};

        MemoryArena::Config config = {sizeof(int), 32, 0};
        MemoryArena arena(user_context, config, test_allocator);
        void *p1 = arena.reserve(user_context);
        HALIDE_CHECK(user_context, get_allocated_system_memory() >= (1 * sizeof(int)));
        HALIDE_CHECK(user_context, p1 != nullptr);

        void *p2 = arena.reserve(user_context, true);
        HALIDE_CHECK(user_context, get_allocated_system_memory() >= (2 * sizeof(int)));
        HALIDE_CHECK(user_context, p2 != nullptr);
        HALIDE_CHECK(user_context, (*static_cast<int *>(p2)) == 0);

        arena.reclaim(user_context, p1);
        arena.destroy(user_context);

        HALIDE_CHECK(user_context, get_allocated_system_memory() == 0);
    }

    // test dyanmic construction
    {
        SystemMemoryAllocatorFns test_allocator = {allocate_system, deallocate_system};

        MemoryArena::Config config = {sizeof(double), 32, 0};
        MemoryArena *arena = MemoryArena::create(user_context, config, test_allocator);

        constexpr size_t count = 4 * 1024;
        void *pointers[count];
        for (size_t n = 0; n < count; ++n) {
            pointers[n] = arena->reserve(user_context, true);
        }
        HALIDE_CHECK(user_context, get_allocated_system_memory() >= (count * sizeof(int)));
        for (size_t n = 0; n < count; ++n) {
            void *ptr = pointers[n];
            HALIDE_CHECK(user_context, ptr != nullptr);
            HALIDE_CHECK(user_context, (*static_cast<double *>(ptr)) == 0.0);
        }
        arena->destroy(user_context);

        MemoryArena::destroy(user_context, arena);
        HALIDE_CHECK(user_context, get_allocated_system_memory() == 0);
    }

    // test struct allocations
    {
        SystemMemoryAllocatorFns test_allocator = {allocate_system, deallocate_system};
        MemoryArena::Config config = {sizeof(TestStruct), 32, 0};
        MemoryArena arena(user_context, config, test_allocator);
        void *s1 = arena.reserve(user_context, true);
        HALIDE_CHECK(user_context, s1 != nullptr);
        HALIDE_CHECK(user_context, get_allocated_system_memory() >= (1 * sizeof(int)));
        HALIDE_CHECK(user_context, ((TestStruct *)s1)->i8 == int8_t(0));
        HALIDE_CHECK(user_context, ((TestStruct *)s1)->ui16 == uint16_t(0));
        HALIDE_CHECK(user_context, ((TestStruct *)s1)->f32 == float(0));

        arena.destroy(user_context);

        constexpr size_t count = 4 * 1024;
        void *pointers[count];
        for (size_t n = 0; n < count; ++n) {
            pointers[n] = arena.reserve(user_context, true);
        }

        for (size_t n = 0; n < count; ++n) {
            void *s1 = pointers[n];
            HALIDE_CHECK(user_context, s1 != nullptr);
            HALIDE_CHECK(user_context, ((TestStruct *)s1)->i8 == int8_t(0));
            HALIDE_CHECK(user_context, ((TestStruct *)s1)->ui16 == uint16_t(0));
            HALIDE_CHECK(user_context, ((TestStruct *)s1)->f32 == float(0));
        }

        arena.destroy(user_context);

        HALIDE_CHECK(user_context, get_allocated_system_memory() == 0);
    }

    print(user_context) << "Success!\n";
    return 0;
}