File: memory_resource_adapter.cpp

package info (click to toggle)
foonathan-memory 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: cpp: 12,014; xml: 139; sh: 49; makefile: 22
file content (93 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download
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
// Copyright (C) 2015-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

#include <foonathan/memory/memory_resource_adapter.hpp>

#include <catch.hpp>
#include <new>

#include <foonathan/memory/allocator_storage.hpp>
#include <foonathan/memory/std_allocator.hpp>

using namespace foonathan::memory;

struct pmr_test_allocator
{
    std::size_t node_allocated = 0, array_allocated = 0;

    void* allocate_node(std::size_t size, std::size_t)
    {
        node_allocated += size;
        return ::operator new(size);
    }

    void* allocate_array(std::size_t n, std::size_t size, std::size_t)
    {
        array_allocated += n * size;
        return ::operator new(n* size);
    }

    void deallocate_node(void* p, std::size_t size, std::size_t)
    {
        node_allocated -= size;
        ::operator delete(p);
    }

    void deallocate_array(void* p, std::size_t n, std::size_t size, std::size_t)
    {
        array_allocated -= n * size;
        ::operator delete(p);
    }

    std::size_t max_node_size() const
    {
        return 8u;
    }
};

TEST_CASE("memory_resource_adapter", "[adapter]")
{
    auto max_node = pmr_test_allocator{}.max_node_size();

    memory_resource_adapter<pmr_test_allocator> alloc({});
    REQUIRE(alloc.get_allocator().node_allocated == 0u);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);

    auto mem = alloc.allocate(max_node / 2);
    REQUIRE(alloc.get_allocator().node_allocated == max_node / 2);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);

    alloc.deallocate(mem, max_node / 2);
    REQUIRE(alloc.get_allocator().node_allocated == 0);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);

    mem = alloc.allocate(max_node);
    REQUIRE(alloc.get_allocator().node_allocated == max_node);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);

    alloc.deallocate(mem, max_node);
    REQUIRE(alloc.get_allocator().node_allocated == 0);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);

    mem = alloc.allocate(max_node * 2);
    REQUIRE(alloc.get_allocator().node_allocated == 0u);
    REQUIRE(alloc.get_allocator().array_allocated == max_node * 2);

    alloc.deallocate(mem, max_node * 2);
    REQUIRE(alloc.get_allocator().node_allocated == 0u);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);

    mem = alloc.allocate(max_node * 2 + 1);
    REQUIRE(alloc.get_allocator().node_allocated == 0u);
    REQUIRE(alloc.get_allocator().array_allocated == max_node * 3);

    alloc.deallocate(mem, max_node * 2 + 1);
    REQUIRE(alloc.get_allocator().node_allocated == 0u);
    REQUIRE(alloc.get_allocator().array_allocated == 0u);
}

// compilation checks
template class foonathan::memory::allocator_storage<reference_storage<memory_resource_allocator>,
                                                    no_mutex>;
template class foonathan::memory::allocator_traits<memory_resource_allocator>;