forked from foonathan/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_allocator.cpp
More file actions
79 lines (65 loc) · 2.24 KB
/
Copy pathnew_allocator.cpp
File metadata and controls
79 lines (65 loc) · 2.24 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
72
73
74
75
76
77
78
79
// Copyright (C) 2015 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 "new_allocator.hpp"
#if FOONATHAN_HOSTED_IMPLEMENTATION
#include <memory>
#endif
#include "debugging.hpp"
#include "error.hpp"
using namespace foonathan::memory;
#if FOONATHAN_MEMORY_DEBUG_LEAK_CHECK
#include <atomic>
namespace
{
std::size_t init_counter = 0u, alloc_counter = 0u;
void on_alloc(std::size_t size) FOONATHAN_NOEXCEPT
{
alloc_counter += size;
}
void on_dealloc(std::size_t size) FOONATHAN_NOEXCEPT
{
alloc_counter -= size;
}
}
detail::new_allocator_leak_checker_initializer_t::new_allocator_leak_checker_initializer_t() FOONATHAN_NOEXCEPT
{
++init_counter;
}
detail::new_allocator_leak_checker_initializer_t::~new_allocator_leak_checker_initializer_t() FOONATHAN_NOEXCEPT
{
if (--init_counter == 0u && alloc_counter != 0u)
get_leak_handler()({FOONATHAN_MEMORY_LOG_PREFIX "::new_allocator", nullptr}, alloc_counter);
}
#else
namespace
{
void on_alloc(std::size_t) FOONATHAN_NOEXCEPT {}
void on_dealloc(std::size_t) FOONATHAN_NOEXCEPT {}
}
#endif
void* new_allocator::allocate_node(std::size_t size, std::size_t)
{
auto mem = detail::try_allocate([](std::size_t size)
{
return ::operator new(size,
std::nothrow);
}, size + 2 * detail::debug_fence_size,
{FOONATHAN_MEMORY_LOG_PREFIX "::new_allocator", this});
on_alloc(size);
return detail::debug_fill_new(mem, size);
}
void new_allocator::deallocate_node(void* node, std::size_t size, std::size_t) FOONATHAN_NOEXCEPT
{
auto memory = detail::debug_fill_free(node, size);
::operator delete(memory);
on_dealloc(size);
}
std::size_t new_allocator::max_node_size() const FOONATHAN_NOEXCEPT
{
#if FOONATHAN_HOSTED_IMPLEMENTATION
return std::allocator<char>().max_size();
#else
return -1;
#endif
}