Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/smalloc.h |
Date: | 2025-03-09 02:34:28 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 58 | 58 | 100.0% |
Branches: | 12 | 28 | 42.9% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | * | ||
4 | * Ensures that cvmfs aborts on out-of-memory errors. | ||
5 | */ | ||
6 | |||
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 | |||
33 | /** | ||
34 | * Round up size to the next larger multiple of 8. This is used to enforce | ||
35 | * 8-byte alignment. | ||
36 | */ | ||
37 | 7236504 | static inline uint64_t RoundUp8(const uint64_t size) { | |
38 | 7236504 | return (size + 7) & ~static_cast<uint64_t>(7); | |
39 | } | ||
40 | |||
41 | 1212517 | static inline void * __attribute__((used)) smalloc(size_t size) { | |
42 | 1212517 | void *mem = NULL; | |
43 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
44 | do { | ||
45 | #endif | ||
46 | 1212517 | mem = malloc(size); | |
47 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
48 | } while ((size > 0) && (mem == NULL)); | ||
49 | #endif | ||
50 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1212517 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1212517 | assert((mem || (size == 0)) && "Out Of Memory"); |
51 | 1212517 | return mem; | |
52 | } | ||
53 | |||
54 | 174915 | static inline void * __attribute__((used)) srealloc(void *ptr, size_t size) { | |
55 | 174915 | void *mem = NULL; | |
56 | |||
57 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
58 | do { | ||
59 | #endif | ||
60 | 174915 | mem = realloc(ptr, size); | |
61 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
62 | } while ((size > 0) && (mem == NULL)); | ||
63 | #endif | ||
64 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 174915 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
174915 | assert((mem || (size == 0)) && "Out Of Memory"); |
65 | 174915 | return mem; | |
66 | } | ||
67 | |||
68 | 325 | static inline void * __attribute__((used)) scalloc(size_t count, size_t size) { | |
69 | 325 | void *mem = NULL; | |
70 | |||
71 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
72 | do { | ||
73 | #endif | ||
74 | 325 | mem = calloc(count, size); | |
75 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
76 | } while ((count * size > 0) && (mem == NULL)); | ||
77 | #endif | ||
78 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
325 | assert((mem || ((count * size) == 0)) && "Out Of Memory"); |
79 | 325 | return mem; | |
80 | } | ||
81 | |||
82 | 11630 | static inline void * __attribute__((used)) smmap(size_t size) { | |
83 | // TODO(reneme): make page size platform independent | ||
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11630 times.
|
11630 | assert(size > 0); |
85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11630 times.
|
11630 | assert(size < std::numeric_limits<size_t>::max() - 4096); |
86 | |||
87 | 11630 | const int anonymous_fd = -1; | |
88 | 11630 | const off_t offset = 0; | |
89 | 11630 | size_t pages = ((size + 2*sizeof(size_t))+4095)/4096; // round to full page | |
90 | 11630 | unsigned char *mem = NULL; | |
91 | |||
92 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
93 | do { | ||
94 | #endif | ||
95 | mem = static_cast<unsigned char *>( | ||
96 | 11630 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11633 times.
|
11633 | assert((mem != MAP_FAILED) && "Out Of Memory"); |
109 | 11633 | *(reinterpret_cast<size_t *>(mem)) = kMemMarker; | |
110 | 11633 | *(reinterpret_cast<size_t *>(mem) + 1) = pages; | |
111 | 11633 | return mem + 2*sizeof(size_t); | |
112 | } | ||
113 | |||
114 | 11619 | static inline void __attribute__((used)) smunmap(void *mem) { | |
115 | 11619 | unsigned char *area = static_cast<unsigned char *>(mem); | |
116 | 11619 | area = area - sizeof(size_t); | |
117 | 11619 | size_t pages = *(reinterpret_cast<size_t *>(area)); | |
118 | 11619 | int retval = munmap(area-sizeof(size_t), pages*4096); | |
119 | // printf("SUNMMAP %d bytes at %p\n", pages*4096, area); | ||
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11620 times.
|
11620 | assert((retval == 0) && "Invalid umnmap"); |
121 | 11620 | } | |
122 | |||
123 | |||
124 | /** | ||
125 | * Used when the caller remembers the size, so that it can call sxunmap later. | ||
126 | */ | ||
127 | 561 | static inline void * __attribute__((used)) sxmmap(size_t size) { | |
128 | 561 | const int anonymous_fd = -1; | |
129 | 561 | const off_t offset = 0; | |
130 | 561 | void *mem = NULL; | |
131 | |||
132 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
133 | do { | ||
134 | #endif | ||
135 | 561 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 561 times.
|
561 | assert((mem != MAP_FAILED) && "Out Of Memory"); |
147 | 561 | return mem; | |
148 | } | ||
149 | |||
150 | |||
151 | /** | ||
152 | * Free memory acquired by sxmmap. | ||
153 | */ | ||
154 | 1219 | static inline void __attribute__((used)) sxunmap(void *mem, size_t size) { | |
155 | 1219 | int retval = munmap(mem, size); | |
156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1219 times.
|
1219 | assert((retval == 0) && "Invalid umnmap"); |
157 | 1219 | } | |
158 | |||
159 | |||
160 | /** | ||
161 | * Pointer is aligned at a multiple of the size. The size has to be a multiple | ||
162 | * of 2MB. | ||
163 | */ | ||
164 | 369 | static inline void * __attribute__((used)) sxmmap_align(size_t size) { | |
165 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
|
369 | assert((size % (2 * 1024 * 1024)) == 0); |
166 | 369 | char *mem = NULL; | |
167 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
168 | do { | ||
169 | #endif | ||
170 | 369 | mem = reinterpret_cast<char *>(sxmmap(2 * size)); | |
171 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
172 | } while (mem == MAP_FAILED); | ||
173 | #endif | ||
174 | |||
175 | 369 | uintptr_t head = size - (uintptr_t(mem) % size); | |
176 | 369 | sxunmap(mem, head); | |
177 | 369 | mem += head; | |
178 | 369 | uintptr_t tail = size - head; | |
179 |
2/2✓ Branch 0 taken 298 times.
✓ Branch 1 taken 71 times.
|
369 | if (tail > 0) |
180 | 298 | sxunmap(mem + size, tail); | |
181 | 369 | return mem; | |
182 | } | ||
183 | |||
184 | |||
185 | #ifdef CVMFS_NAMESPACE_GUARD | ||
186 | } // namespace CVMFS_NAMESPACE_GUARD | ||
187 | #endif | ||
188 | |||
189 | #endif // CVMFS_UTIL_SMALLOC_H_ | ||
190 |