Xmipp  v3.23.11-Nereus
memory_utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef CORE_UTILS_MEMORY_UTILS_H_
27 #define CORE_UTILS_MEMORY_UTILS_H_
28 
29 #include <cstddef>
30 #include <stdlib.h>
31 #include <cstdint>
32 #include <memory>
33 #include <sys/mman.h> // MADVICE
34 #include <unistd.h> // sysconf
35 
36 namespace memoryUtils
37 {
38 
39  const static long PAGE_SIZE = sysconf(_SC_PAGESIZE);
40 
41  inline constexpr size_t operator"" _kB(unsigned long long int bytes) {
42  return 1024 * bytes;
43  }
44 
45  inline constexpr size_t operator"" _MB(unsigned long long int bytes) {
46  return 1024 * 1024 * bytes;
47  }
48 
49  inline constexpr size_t operator"" _GB(unsigned long long int bytes) {
50  return 1024 * 1024 * 1024 * bytes;
51  }
52 
53  inline constexpr double operator"" _kB(long double bytes) {
54  return 1024. * bytes;
55  }
56 
57  inline constexpr double operator"" _MB(long double bytes) {
58  return 1024. * 1024. * bytes;
59  }
60 
61  inline constexpr double operator"" _GB(long double bytes) {
62  return 1024. * 1024. * 1024. * bytes;
63  }
64 
65 
66  inline void* page_aligned_alloc(size_t bytes) {
67  return aligned_alloc(PAGE_SIZE, bytes);
68  }
69 
70  template<typename T>
71  inline T* page_aligned_alloc(size_t elems, bool initToZero) {
72  size_t bytes = elems * sizeof(T);
73  auto p = (T*)page_aligned_alloc(bytes);
74  madvise(p, bytes, MADV_HUGEPAGE);
75  if (initToZero) {
76  memset(p, 0, bytes);
77  }
78  return p;
79  }
80 
81  template<typename T>
82  inline constexpr T kB(T bytes) {
83  return bytes / (T)1024;
84  }
85 
86  template<typename T>
87  inline constexpr T MB(T bytes) {
88  return bytes / ((T)1024 * T(1024));
89  }
90 
91  template<typename T>
92  inline constexpr T GB(T bytes) {
93  return bytes / ((T)1024 * (T)1024 * (T)1024);
94  }
95 
99  inline uint32_t alignmentOf(uintptr_t ptr) {
100  for (uint32_t alignPower = 0; alignPower < 32; alignPower++) {
101  const uint32_t alignment = 1u << alignPower;
102  if ((ptr & alignment) != 0) {
103  return alignment;
104  }
105  }
106  return 1u << 31u;
107  }
108 
110  inline uint32_t alignmentOf(void * ptr) {
111  return alignmentOf((size_t) ptr);
112  }
113 
116  template<typename T>
117  inline T align(T number, uint32_t alignment) {
118  T off = number % alignment;
119  if (off == 0) {
120  return number;
121  } else {
122  return number + alignment - off;
123  }
124  }
125 
126  template<typename T, typename... Args>
127  std::unique_ptr<T> make_unique(Args&&... args)
128  {
129  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
130  }
131 
132 } // memoryUtils
133 
134 #endif /* CORE_UTILS_MEMORY_UTILS_H_ */
void * page_aligned_alloc(size_t bytes)
Definition: memory_utils.h:66
T align(T number, uint32_t alignment)
Definition: memory_utils.h:117
std::unique_ptr< T > make_unique(Args &&... args)
Definition: memory_utils.h:127
constexpr T MB(T bytes)
Definition: memory_utils.h:87
uint32_t alignmentOf(uintptr_t ptr)
Definition: memory_utils.h:99
constexpr T kB(T bytes)
Definition: memory_utils.h:82
doublereal * u
constexpr T GB(T bytes)
Definition: memory_utils.h:92