MPD  0.20.18
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 
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 
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 
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 
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 
128  :data(src.data), size(src.size) {
129  src.data = nullptr;
130  }
131 
133  if (data != nullptr)
134  HugeFree(data, size);
135  }
136 
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
void reset() noexcept
HugeAllocation(size_t _size)
#define gcc_malloc
Definition: Compiler.h:112
Automatic huge memory allocation management.
HugeAllocation & operator=(HugeAllocation &&src) noexcept
static void HugeDiscard(void *, size_t) noexcept
HugeAllocation()=default
void Discard() noexcept
HugeAllocation(HugeAllocation &&src) noexcept
#define gcc_unused
Definition: Compiler.h:118
static void HugeFree(void *_p, size_t) noexcept
static gcc_malloc void * HugeAllocate(size_t size)