MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

HugeAllocator.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013-2017 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef HUGE_ALLOCATOR_HXX
31 #define HUGE_ALLOCATOR_HXX
32
33 #include "Compiler.h"
34
35 #include <utility>
36
37 #include <stddef.h>
38
39 #ifdef __linux__
40
46 gcc_malloc
47 void *
48 HugeAllocate(size_t size);
49
54 void
55 HugeFree(void *p, size_t size) noexcept;
56
65 void
66 HugeDiscard(void *p, size_t size) noexcept;
67
68 #elif defined(WIN32)
69 #include <windows.h>
70
71 gcc_malloc
72 void *
73 HugeAllocate(size_t size);
74
75 static inline void
76 HugeFree(void *p, gcc_unused size_t size) noexcept
77 {
78 VirtualFree(p, 0, MEM_RELEASE);
79 }
80
81 static inline void
82 HugeDiscard(void *p, size_t size) noexcept
83 {
84 VirtualAlloc(p, size, MEM_RESET, PAGE_NOACCESS);
85 }
86
87 #else
88
89 /* not Linux: fall back to standard C calls */
90
91 #include <stdint.h>
92
93 gcc_malloc
94 static inline void *
95 HugeAllocate(size_t size)
96 {
97 return new uint8_t[size];
98 }
99
100 static inline void
101 HugeFree(void *_p, size_t) noexcept
102 {
103 auto *p = (uint8_t *)_p;
104 delete[] p;
105 }
106
107 static inline void
108 HugeDiscard(void *, size_t) noexcept
109 {
110 }
111
112 #endif
113
117 class HugeAllocation {
118 void *data = nullptr;
119 size_t size;
120
121 public:
122 HugeAllocation() = default;
123
124 HugeAllocation(size_t _size)
125 :data(HugeAllocate(_size)), size(_size) {}
126
127 HugeAllocation(HugeAllocation &&src) noexcept
128 :data(src.data), size(src.size) {
129 src.data = nullptr;
130 }
131
132 ~HugeAllocation() {
133 if (data != nullptr)
134 HugeFree(data, size);
135 }
136
137 HugeAllocation &operator=(HugeAllocation &&src) noexcept {
138 std::swap(data, src.data);
139 std::swap(size, src.size);
140 return *this;
141 }
142
143 void Discard() noexcept {
144 HugeDiscard(data, size);
145 }
146
147 void reset() noexcept {
148 if (data != nullptr) {
149 HugeFree(data, size);
150 data = nullptr;
151 }
152 }
153
154 void *get() noexcept {
155 return data;
156 }
157 };
158
159 #endif
HugeAllocation::~HugeAllocation
~HugeAllocation()
Definition: HugeAllocator.hxx:132
HugeAllocation::reset
void reset() noexcept
Definition: HugeAllocator.hxx:147
HugeAllocation::HugeAllocation
HugeAllocation(size_t _size)
Definition: HugeAllocator.hxx:124
gcc_malloc
#define gcc_malloc
Definition: Compiler.h:112
HugeAllocation
Automatic huge memory allocation management.
Definition: HugeAllocator.hxx:117
HugeAllocation::operator=
HugeAllocation & operator=(HugeAllocation &&src) noexcept
Definition: HugeAllocator.hxx:137
HugeDiscard
static void HugeDiscard(void *, size_t) noexcept
Definition: HugeAllocator.hxx:108
HugeAllocation::HugeAllocation
HugeAllocation()=default
Compiler.h
HugeAllocation::Discard
void Discard() noexcept
Definition: HugeAllocator.hxx:143
HugeAllocation::HugeAllocation
HugeAllocation(HugeAllocation &&src) noexcept
Definition: HugeAllocator.hxx:127
gcc_unused
#define gcc_unused
Definition: Compiler.h:118
HugeFree
static void HugeFree(void *_p, size_t) noexcept
Definition: HugeAllocator.hxx:101
HugeAllocate
static gcc_malloc void * HugeAllocate(size_t size)
Definition: HugeAllocator.hxx:95