File: test_allocator.hpp

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 (89 lines) | stat: -rw-r--r-- 2,291 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
// 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.

#ifndef FOONATHAN_MEMORY_TEST_TEST_ALLOCATOR_HPP
#define FOONATHAN_MEMORY_TEST_TEST_ALLOCATOR_HPP

#include <unordered_map>

#include <foonathan/memory/heap_allocator.hpp>

struct memory_info
{
    void*       memory;
    std::size_t size, alignment;
};

// RawAllocator with various security checks
class test_allocator
{
public:
    using is_stateful = std::true_type;

    void* allocate_node(std::size_t size, std::size_t alignment)
    {
        auto mem        = foonathan::memory::heap_allocator().allocate_node(size, alignment);
        last_allocated_ = {mem, size, alignment};
        allocated_[mem] = last_allocated_;
        return mem;
    }

    void deallocate_node(void* node, std::size_t size, std::size_t alignment) noexcept
    {
        ++dealloc_count_;
        auto iter = allocated_.find(node);
        if (iter == allocated_.end() || iter->second.size != size
            || iter->second.alignment != alignment)
        {
            last_valid_ = false;
            return;
        }
        else
            allocated_.erase(iter);
        foonathan::memory::heap_allocator().deallocate_node(node, size, alignment);
    }

    std::size_t max_node_size() const noexcept
    {
        return std::size_t(-1);
    }

    bool last_deallocation_valid() noexcept
    {
        return last_valid_;
    }

    void reset_last_deallocation_valid() noexcept
    {
        last_valid_ = true;
    }

    memory_info last_allocated() const noexcept
    {
        return last_allocated_;
    }

    std::size_t no_allocated() const noexcept
    {
        return allocated_.size();
    }

    std::size_t no_deallocated() const noexcept
    {
        return dealloc_count_;
    }

    void reset_deallocation_count() noexcept
    {
        dealloc_count_ = 0u;
    }

private:
    std::unordered_map<void*, memory_info> allocated_;
    memory_info                            last_allocated_;
    std::size_t                            dealloc_count_ = 0u;
    bool                                   last_valid_    = true;
};

#endif //FOONATHAN_MEMORY_TEST_TEST_ALLOCATOR_HPP