Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
memoryallocation.h
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#ifndef MEMORYALLOCATION_H
23#define MEMORYALLOCATION_H
24
25#include <cstdlib>
26#include <cstdint>
27#include <cstddef>
28#include <cstdint>
29#include <stdexcept>
30#include <string.h>
31
32#ifdef USE_JEMALLOC
33#include "jemalloc/jemalloc.h"
34#endif
35
36#if defined(DEBUG_VLASIATOR) || defined(DEBUG_SPATIAL_CELL)
37#ifndef INITIALIZE_ALIGNED_MALLOC_WITH_NAN
38#define INITIALIZE_ALIGNED_MALLOC_WITH_NAN
39#endif
40#endif
41
43void memory_purge();
44
47
49inline void * aligned_malloc(size_t size,std::size_t align) {
50 /* Allocate necessary memory area
51 * client request - size parameter -
52 * plus area to store the address
53 * of the memory returned by standard
54 * malloc(), or je_malloc().
55 */
56 void *ptr;
57#ifdef USE_JEMALLOC
58 void *p = je_malloc(size + align - 1 + sizeof(void*));
59#else
60 void *p = malloc(size + align - 1 + sizeof(void*));
61#endif
62#ifdef INITIALIZE_ALIGNED_MALLOC_WITH_NAN
63 memset(p, ~0u, size + align - 1 + sizeof(void*));
64#endif
65
66 if (p != NULL) {
67 /* Address of the aligned memory according to the align parameter*/
68 ptr = (void*) (((unsigned long)p + sizeof(void*) + align -1) & ~(align-1));
69 /* store the address of the malloc() above
70 * at the beginning of our total memory area.
71 * You can also use *((void **)ptr-1) = p
72 * instead of the one below.
73 */
74 *((void**)((unsigned long)ptr - sizeof(void*))) = p;
75 /* Return the address of aligned memory */
76 return ptr;
77 }
78 return NULL;
79}
80
81inline void aligned_free(void *p) {
82 /* Get the address of the memory, stored at the
83 * start of our total memory area. Alternatively,
84 * you can use void *ptr = *((void **)p-1) instead
85 * of the one below.
86 */
87 void *ptr = *((void**)((unsigned long)p - sizeof(void*)));
88#ifdef USE_JEMALLOC
89 je_free(ptr);
90#else
91 free(ptr);
92#endif
93 return;
94}
95
96
97
98
99
107
108template <typename T, std::size_t Alignment>
110{
111public:
112
113 // The following will be the same for virtually all allocators.
114 typedef T * pointer;
115 typedef const T * const_pointer;
116 typedef T& reference;
117 typedef const T& const_reference;
118 typedef T value_type;
119 typedef std::size_t size_type;
120 typedef ptrdiff_t difference_type;
121
122 T * address(T& r) const
123 {
124 return &r;
125 }
126
127 const T * address(const T& s) const
128 {
129 return &s;
130 }
131
132 std::size_t max_size() const
133 {
134 // The following has been carefully written to be independent of
135 // the definition of size_t and to avoid signed/unsigned warnings.
136 return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
137 }
138
139
140 // The following must be the same for all allocators.
141 template <typename U>
142 struct rebind
143 {
145 } ;
146
147 bool operator!=(const aligned_allocator& other) const
148 {
149 return !(*this == other);
150 }
151
152 void construct(T * const p, const T& t) const
153 {
154 void * const pv = static_cast<void *>(p);
155
156 new (pv) T(t);
157 }
158
159 void destroy(T * const p) const
160 {
161 p->~T();
162 }
163
164 // Returns true if and only if storage allocated from *this
165 // can be deallocated from other, and vice versa.
166 // Always returns true for stateless allocators.
167 bool operator==(const aligned_allocator& other) const
168 {
169 return true;
170 }
171
172
173 // Default constructor, copy constructor, rebinding constructor, and destructor.
174 // Empty for stateless allocators.
176
178
179 template <typename U> aligned_allocator(const aligned_allocator<U, Alignment>&) { }
180
182
183
184 // The following will be different for each allocator.
185 T * allocate(const std::size_t n) const
186 {
187 // The return value of allocate(0) is unspecified.
188 // Mallocator returns NULL in order to avoid depending
189 // on malloc(0)'s implementation-defined behavior
190 // (the implementation can define malloc(0) to return NULL,
191 // in which case the bad_alloc check below would fire).
192 // All allocators can return NULL in this case.
193 if (n == 0) {
194 return NULL;
195 }
196
197 // All allocators should contain an integer overflow check.
198 // The Standardization Committee recommends that std::length_error
199 // be thrown in the case of integer overflow.
200 if (n > max_size())
201 {
202 throw std::length_error("aligned_allocator<T>::allocate() - Integer overflow.");
203 }
204
205 // Mallocator wraps malloc().
206 void * const pv = aligned_malloc(n * sizeof(T), Alignment);
207
208 // Allocators should throw std::bad_alloc in the case of memory allocation failure.
209 if (pv == NULL)
210 {
211 throw std::bad_alloc();
212 }
213
214 return static_cast<T *>(pv);
215 }
216
217 void deallocate(T * const p, const std::size_t ) const
218 {
219 aligned_free(p);
220 }
221
222
223 // The following will be the same for all allocators that ignore hints.
224 template <typename U>
225 T * allocate(const std::size_t n, const U * /* const hint */) const
226 {
227 return allocate(n);
228 }
229
230
231 // Allocators are not required to be assignable, so
232 // all allocators should have a private unimplemented
233 // assignment operator. Note that this will trigger the
234 // off-by-default (enabled under /Wall) warning C4626
235 // "assignment operator could not be generated because a
236 // base class assignment operator is inaccessible" within
237 // the STL headers, but that warning is useless.
238private:
240};
241
242
243#endif
void construct(T *const p, const T &t) const
bool operator==(const aligned_allocator &other) const
aligned_allocator & operator=(const aligned_allocator &)
T * allocate(const std::size_t n) const
T * allocate(const std::size_t n, const U *) const
aligned_allocator(const aligned_allocator &)
std::size_t max_size() const
bool operator!=(const aligned_allocator &other) const
aligned_allocator(const aligned_allocator< U, Alignment > &)
T * address(T &r) const
void destroy(T *const p) const
void deallocate(T *const p, const std::size_t) const
const T * address(const T &s) const
void memory_purge()
void * aligned_malloc(size_t size, std::size_t align)
void memory_configurator()
void aligned_free(void *p)
aligned_allocator< U, Alignment > other