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
69 lines (58 loc) · 1.64 KB
/
Copy pathnew_allocator.cpp
File metadata and controls
69 lines (58 loc) · 1.64 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
// 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.
#include "new_allocator.hpp"
#if FOONATHAN_HOSTED_IMPLEMENTATION
#include <memory>
#endif
#include <new>
#include <foonathan/get_new_handler.hpp>
#include "error.hpp"
using namespace foonathan::memory;
allocator_info detail::new_allocator_impl::info() FOONATHAN_NOEXCEPT
{
return {FOONATHAN_MEMORY_LOG_PREFIX "::new_allocator", nullptr};
}
void* detail::new_allocator_impl::allocate(std::size_t size, size_t) FOONATHAN_NOEXCEPT
{
void* memory = nullptr;
while (true)
{
memory = ::operator new(size, std::nothrow);
if (memory)
break;
auto handler = foonathan_comp::get_new_handler();
if (handler)
{
FOONATHAN_TRY
{
handler();
}
FOONATHAN_CATCH_ALL
{
return nullptr;
}
}
else
{
return nullptr;
}
}
return memory;
}
void detail::new_allocator_impl::deallocate(void* ptr, std::size_t, size_t) FOONATHAN_NOEXCEPT
{
::operator delete(ptr);
}
std::size_t detail::new_allocator_impl::max_node_size() FOONATHAN_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