forked from foonathan/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_allocator.cpp
More file actions
85 lines (69 loc) · 1.82 KB
/
Copy pathheap_allocator.cpp
File metadata and controls
85 lines (69 loc) · 1.82 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
80
81
82
83
84
85
// 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 "heap_allocator.hpp"
#include "error.hpp"
using namespace foonathan::memory;
#ifdef _WIN32
#include <malloc.h>
#include <windows.h>
namespace
{
HANDLE get_process_heap() FOONATHAN_NOEXCEPT
{
static auto heap = GetProcessHeap();
return heap;
}
std::size_t max_size() FOONATHAN_NOEXCEPT
{
return _HEAP_MAXREQ;
}
}
void* foonathan::memory::heap_alloc(std::size_t size) FOONATHAN_NOEXCEPT
{
return HeapAlloc(get_process_heap(), 0, size);
}
void foonathan::memory::heap_dealloc(void* ptr, std::size_t) FOONATHAN_NOEXCEPT
{
HeapFree(get_process_heap(), 0, ptr);
}
#elif FOONATHAN_HOSTED_IMPLEMENTATION
#include <cstdlib>
#include <memory>
void* foonathan::memory::heap_alloc(std::size_t size) FOONATHAN_NOEXCEPT
{
return std::malloc(size);
}
void foonathan::memory::heap_dealloc(void* ptr, std::size_t) FOONATHAN_NOEXCEPT
{
std::free(ptr);
}
namespace
{
std::size_t max_size() FOONATHAN_NOEXCEPT
{
return std::allocator_traits<std::allocator<char>>::max_size({});
}
}
#else
// no implementation for heap_alloc/heap_dealloc
namespace
{
std::size_t max_size() FOONATHAN_NOEXCEPT
{
return std::size_t(-1);
}
}
#endif
allocator_info detail::heap_allocator_impl::info() FOONATHAN_NOEXCEPT
{
return {FOONATHAN_MEMORY_LOG_PREFIX "::heap_allocator", nullptr};
}
std::size_t detail::heap_allocator_impl::max_node_size() FOONATHAN_NOEXCEPT
{
return max_size();
}
#if FOONATHAN_MEMORY_EXTERN_TEMPLATE
template class detail::lowlevel_allocator<detail::heap_allocator_impl>;
template class foonathan::memory::allocator_traits<heap_allocator>;
#endif