CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
smalloc.h
Go to the documentation of this file.
1 
7 #ifndef CVMFS_UTIL_SMALLOC_H_
8 #define CVMFS_UTIL_SMALLOC_H_
9 
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <sys/mman.h>
13 
14 #include <cassert>
15 // #include <cstdio>
16 
17 #include <limits>
18 
19 #ifdef __APPLE__
20 #define PLATFORM_MAP_ANONYMOUS MAP_ANON
21 #else
22 #define PLATFORM_MAP_ANONYMOUS MAP_ANONYMOUS
23 #endif
24 
25 
26 #ifdef CVMFS_NAMESPACE_GUARD
27 namespace CVMFS_NAMESPACE_GUARD {
28 #endif
29 
30 // Checkerboard marker (binary 101010...)
31 const uint32_t kMemMarker = 0xAAAAAAAA;
32 
37 static inline uint64_t RoundUp8(const uint64_t size) {
38  return (size + 7) & ~static_cast<uint64_t>(7);
39 }
40 
41 static inline void * __attribute__((used)) smalloc(size_t size) {
42  void *mem = NULL;
43 #ifdef CVMFS_SUPPRESS_ASSERTS
44  do {
45 #endif
46  mem = malloc(size);
47 #ifdef CVMFS_SUPPRESS_ASSERTS
48  } while ((size > 0) && (mem == NULL));
49 #endif
50  assert((mem || (size == 0)) && "Out Of Memory");
51  return mem;
52 }
53 
54 static inline void * __attribute__((used)) srealloc(void *ptr, size_t size) {
55  void *mem = NULL;
56 
57 #ifdef CVMFS_SUPPRESS_ASSERTS
58  do {
59 #endif
60  mem = realloc(ptr, size);
61 #ifdef CVMFS_SUPPRESS_ASSERTS
62  } while ((size > 0) && (mem == NULL));
63 #endif
64  assert((mem || (size == 0)) && "Out Of Memory");
65  return mem;
66 }
67 
68 static inline void * __attribute__((used)) scalloc(size_t count, size_t size) {
69  void *mem = NULL;
70 
71 #ifdef CVMFS_SUPPRESS_ASSERTS
72  do {
73 #endif
74  mem = calloc(count, size);
75 #ifdef CVMFS_SUPPRESS_ASSERTS
76  } while ((count * size > 0) && (mem == NULL));
77 #endif
78  assert((mem || ((count * size) == 0)) && "Out Of Memory");
79  return mem;
80 }
81 
82 static inline void * __attribute__((used)) smmap(size_t size) {
83  // TODO(reneme): make page size platform independent
84  assert(size > 0);
85  assert(size < std::numeric_limits<size_t>::max() - 4096);
86 
87  const int anonymous_fd = -1;
88  const off_t offset = 0;
89  size_t pages = ((size + 2*sizeof(size_t))+4095)/4096; // round to full page
90  unsigned char *mem = NULL;
91 
92 #ifdef CVMFS_SUPPRESS_ASSERTS
93  do {
94 #endif
95  mem = static_cast<unsigned char *>(
96  mmap(NULL,
97  pages*4096,
98  PROT_READ | PROT_WRITE,
99  MAP_PRIVATE | PLATFORM_MAP_ANONYMOUS,
100  anonymous_fd,
101  offset));
102 
103 #ifdef CVMFS_SUPPRESS_ASSERTS
104  } while (mem == MAP_FAILED);
105 #endif
106  // printf("SMMAP %d bytes at %p\n", pages*4096, mem);
107  // NOLINTNEXTLINE(performance-no-int-to-ptr)
108  assert((mem != MAP_FAILED) && "Out Of Memory");
109  *(reinterpret_cast<size_t *>(mem)) = kMemMarker;
110  *(reinterpret_cast<size_t *>(mem) + 1) = pages;
111  return mem + 2*sizeof(size_t);
112 }
113 
114 static inline void __attribute__((used)) smunmap(void *mem) {
115  unsigned char *area = static_cast<unsigned char *>(mem);
116  area = area - sizeof(size_t);
117  size_t pages = *(reinterpret_cast<size_t *>(area));
118  int retval = munmap(area-sizeof(size_t), pages*4096);
119  // printf("SUNMMAP %d bytes at %p\n", pages*4096, area);
120  assert((retval == 0) && "Invalid umnmap");
121 }
122 
123 
127 static inline void * __attribute__((used)) sxmmap(size_t size) {
128  const int anonymous_fd = -1;
129  const off_t offset = 0;
130  void *mem = NULL;
131 
132 #ifdef CVMFS_SUPPRESS_ASSERTS
133  do {
134 #endif
135  mem = mmap(NULL,
136  size,
137  PROT_READ | PROT_WRITE,
138  MAP_PRIVATE | PLATFORM_MAP_ANONYMOUS,
139  anonymous_fd,
140  offset);
141 
142 #ifdef CVMFS_SUPPRESS_ASSERTS
143  } while (mem == MAP_FAILED);
144 #endif
145  // NOLINTNEXTLINE(performance-no-int-to-ptr)
146  assert((mem != MAP_FAILED) && "Out Of Memory");
147  return mem;
148 }
149 
150 
154 static inline void __attribute__((used)) sxunmap(void *mem, size_t size) {
155  int retval = munmap(mem, size);
156  assert((retval == 0) && "Invalid umnmap");
157 }
158 
159 
164 static inline void * __attribute__((used)) sxmmap_align(size_t size) {
165  assert((size % (2 * 1024 * 1024)) == 0);
166  char *mem = NULL;
167 #ifdef CVMFS_SUPPRESS_ASSERTS
168  do {
169 #endif
170  mem = reinterpret_cast<char *>(sxmmap(2 * size));
171 #ifdef CVMFS_SUPPRESS_ASSERTS
172  } while (mem == MAP_FAILED);
173 #endif
174 
175  uintptr_t head = size - (uintptr_t(mem) % size);
176  sxunmap(mem, head);
177  mem += head;
178  uintptr_t tail = size - head;
179  if (tail > 0)
180  sxunmap(mem + size, tail);
181  return mem;
182 }
183 
184 
185 #ifdef CVMFS_NAMESPACE_GUARD
186 } // namespace CVMFS_NAMESPACE_GUARD
187 #endif
188 
189 #endif // CVMFS_UTIL_SMALLOC_H_
assert((mem||(size==0))&&"Out Of Memory")
struct cvmcache_object_info __attribute__
Definition: atomic.h:24
static uint64_t RoundUp8(const uint64_t size)
Definition: smalloc.h:37
#define PLATFORM_MAP_ANONYMOUS
Definition: smalloc.h:22
mem
Definition: smalloc.h:60
static void size_t size
Definition: smalloc.h:54
const uint32_t kMemMarker
Definition: smalloc.h:31