forked from foonathan/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracking.cpp
More file actions
71 lines (57 loc) · 2.47 KB
/
Copy pathtracking.cpp
File metadata and controls
71 lines (57 loc) · 2.47 KB
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
// Copyright (C) 2015-2016 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 http://foonathan.github.io/doc/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) FOONATHAN_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) FOONATHAN_NOEXCEPT
{
std::clog << this << " array allocated: ";
std::clog << mem << " (" << count << " * " << size << ") " << '\n';
}
void on_node_deallocation(void* ptr, std::size_t, std::size_t) FOONATHAN_NOEXCEPT
{
std::clog << this << " node deallocated: " << ptr << " \n";
}
void on_array_deallocation(void* ptr, std::size_t, std::size_t,
std::size_t) FOONATHAN_NOEXCEPT
{
std::clog << this << " array deallocated: " << ptr << " \n";
}
};
// create a tracked memory_pool to see what kind of allocations are made
// this can also take an already existing allocator
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);
for (auto i : set)
std::clog << i << ' ';
std::clog << '\n';
set.erase(2);
}