-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathnew_allocator.cpp
More file actions
71 lines (60 loc) · 1.52 KB
/
new_allocator.cpp
File metadata and controls
71 lines (60 loc) · 1.52 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-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#include "new_allocator.hpp"
#if FOONATHAN_HOSTED_IMPLEMENTATION
#include <memory>
#endif
#include <new>
#include "error.hpp"
using namespace foonathan::memory;
allocator_info detail::new_allocator_impl::info() noexcept
{
return {FOONATHAN_MEMORY_LOG_PREFIX "::new_allocator", nullptr};
}
void* detail::new_allocator_impl::allocate(std::size_t size, size_t) noexcept
{
void* memory = nullptr;
while (true)
{
memory = ::operator new(size, std::nothrow);
if (memory)
break;
auto handler = std::get_new_handler();
if (handler)
{
#if FOONATHAN_HAS_EXCEPTION_SUPPORT
try
{
handler();
}
catch (...)
{
return nullptr;
}
#else
handler();
#endif
}
else
{
return nullptr;
}
}
return memory;
}
void detail::new_allocator_impl::deallocate(void* ptr, std::size_t, size_t) noexcept
{
::operator delete(ptr);
}
std::size_t detail::new_allocator_impl::max_node_size() noexcept
{
#if FOONATHAN_HOSTED_IMPLEMENTATION
return std::allocator_traits<std::allocator<char>>::max_size({});
#else
return std::size_t(-1);
#endif
}
#if FOONATHAN_MEMORY_EXTERN_TEMPLATE
template class detail::lowlevel_allocator<detail::new_allocator_impl>;
template class foonathan::memory::allocator_traits<new_allocator>;
#endif