File: tracking.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 (88 lines) | stat: -rw-r--r-- 3,041 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
// 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.

// this example shows how to track allocations
// see https://foonathan.net/memory/md_doc_adapters_storage.html for further details

#include <iostream>

#include <foonathan/memory/container.hpp>   // set, set_node_size
#include <foonathan/memory/memory_pool.hpp> // memory_pool
#include <foonathan/memory/tracking.hpp>    // make_tracked_allocator

// alias namespace foonathan::memory as memory for easier access
#include <foonathan/memory/namespace_alias.hpp>

int main()
{
    using namespace memory::literals;

    // tracker class that logs internal behavior of the allocator
    struct tracker
    {
        void on_node_allocation(void* mem, std::size_t size, std::size_t) noexcept
        {
            std::clog << this << " node allocated: ";
            std::clog << mem << " (" << size << ") " << '\n';
        }

        void on_array_allocation(void* mem, std::size_t count, std::size_t size,
                                 std::size_t) noexcept
        {
            std::clog << this << " array allocated: ";
            std::clog << mem << " (" << count << " * " << size << ") " << '\n';
        }

        void on_node_deallocation(void* ptr, std::size_t, std::size_t) noexcept
        {
            std::clog << this << " node deallocated: " << ptr << " \n";
        }

        void on_array_deallocation(void* ptr, std::size_t, std::size_t, std::size_t) noexcept
        {
            std::clog << this << " array deallocated: " << ptr << " \n";
        }
    };

    {
        // create a tracked default allocator
        auto tracked_allocator =
            memory::make_tracked_allocator(tracker{}, memory::default_allocator{});

        // use the allocator as usual
        // decltype(tracked_allocator) can be used below, too
        memory::vector<int, memory::tracked_allocator<tracker, memory::default_allocator>>
            vec({1, 2, 3, 4}, tracked_allocator);

        std::clog << "vec: ";
        for (auto i : vec)
            std::clog << i << ' ';
        std::clog << '\n';
    }

    {
        // create a tracked memory_pool to see what kind of allocations are made
        auto tracked_pool =
            memory::make_tracked_allocator(tracker{},
                                           memory::memory_pool<>(memory::set_node_size<int>::value,
                                                                 4_KiB));

        // use the allocator as usual
        // decltype(tracked_pool) can be used below, too
        memory::set<int, memory::tracked_allocator<tracker, memory::memory_pool<>>>
            set(std::less<int>(), tracked_pool);

        set.insert(1);
        set.insert(2);
        set.insert(3);
        set.insert(1);

        std::clog << "set: ";
        for (auto i : set)
            std::clog << i << ' ';
        std::clog << '\n';

        set.erase(2);
    }
}