| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/util/smalloc.h |
| Date: | 2025-11-09 02:35:23 |
| 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 | #include <sys/types.h> | ||
| 14 | |||
| 15 | #include <cassert> | ||
| 16 | // #include <cstdio> | ||
| 17 | |||
| 18 | #include <limits> | ||
| 19 | |||
| 20 | #ifdef __APPLE__ | ||
| 21 | #define PLATFORM_MAP_ANONYMOUS MAP_ANON | ||
| 22 | #else | ||
| 23 | #define PLATFORM_MAP_ANONYMOUS MAP_ANONYMOUS | ||
| 24 | #endif | ||
| 25 | |||
| 26 | |||
| 27 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 28 | namespace CVMFS_NAMESPACE_GUARD { | ||
| 29 | #endif | ||
| 30 | |||
| 31 | // Checkerboard marker (binary 101010...) | ||
| 32 | const uint32_t kMemMarker = 0xAAAAAAAA; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Round up size to the next larger multiple of 8. This is used to enforce | ||
| 36 | * 8-byte alignment. | ||
| 37 | */ | ||
| 38 | 35732082 | static inline uint64_t RoundUp8(const uint64_t size) { | |
| 39 | 35732082 | return (size + 7) & ~static_cast<uint64_t>(7); | |
| 40 | } | ||
| 41 | |||
| 42 | 31569466 | static inline void * __attribute__((used)) smalloc(size_t size) { | |
| 43 | 31569466 | void *mem = NULL; | |
| 44 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 45 | do { | ||
| 46 | #endif | ||
| 47 | 31569466 | mem = malloc(size); | |
| 48 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 49 | } while ((size > 0) && (mem == NULL)); | ||
| 50 | #endif | ||
| 51 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 31569466 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
31569466 | assert((mem || (size == 0)) && "Out Of Memory"); |
| 52 | 31569466 | return mem; | |
| 53 | } | ||
| 54 | |||
| 55 | 4708725 | static inline void * __attribute__((used)) srealloc(void *ptr, size_t size) { | |
| 56 | 4708725 | void *mem = NULL; | |
| 57 | |||
| 58 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 59 | do { | ||
| 60 | #endif | ||
| 61 | 4708725 | mem = realloc(ptr, size); | |
| 62 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 63 | } while ((size > 0) && (mem == NULL)); | ||
| 64 | #endif | ||
| 65 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4708725 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4708725 | assert((mem || (size == 0)) && "Out Of Memory"); |
| 66 | 4708725 | return mem; | |
| 67 | } | ||
| 68 | |||
| 69 | 6134 | static inline void * __attribute__((used)) scalloc(size_t count, size_t size) { | |
| 70 | 6134 | void *mem = NULL; | |
| 71 | |||
| 72 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 73 | do { | ||
| 74 | #endif | ||
| 75 | 6134 | mem = calloc(count, size); | |
| 76 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 77 | } while ((count * size > 0) && (mem == NULL)); | ||
| 78 | #endif | ||
| 79 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6134 | assert((mem || ((count * size) == 0)) && "Out Of Memory"); |
| 80 | 6134 | return mem; | |
| 81 | } | ||
| 82 | |||
| 83 | 241440 | static inline void * __attribute__((used)) smmap(size_t size) { | |
| 84 | // TODO(reneme): make page size platform independent | ||
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 241440 times.
|
241440 | assert(size > 0); |
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 241440 times.
|
241440 | assert(size < std::numeric_limits<size_t>::max() - 4096); |
| 87 | |||
| 88 | 241440 | const int anonymous_fd = -1; | |
| 89 | 241440 | const off_t offset = 0; | |
| 90 | 241440 | const size_t pages = ((size + 2 * sizeof(size_t)) + 4095) | |
| 91 | / 4096; // round to full page | ||
| 92 | 241440 | unsigned char *mem = NULL; | |
| 93 | |||
| 94 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 95 | do { | ||
| 96 | #endif | ||
| 97 | mem = static_cast<unsigned char *>( | ||
| 98 | 241440 | mmap(NULL, | |
| 99 | pages * 4096, | ||
| 100 | PROT_READ | PROT_WRITE, | ||
| 101 | MAP_PRIVATE | PLATFORM_MAP_ANONYMOUS, | ||
| 102 | anonymous_fd, | ||
| 103 | offset)); | ||
| 104 | |||
| 105 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 106 | } while (mem == MAP_FAILED); | ||
| 107 | #endif | ||
| 108 | // printf("SMMAP %d bytes at %p\n", pages*4096, mem); | ||
| 109 | // NOLINTNEXTLINE(performance-no-int-to-ptr) | ||
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 241476 times.
|
241476 | assert((mem != MAP_FAILED) && "Out Of Memory"); |
| 111 | 241476 | *(reinterpret_cast<size_t *>(mem)) = kMemMarker; | |
| 112 | 241476 | *(reinterpret_cast<size_t *>(mem) + 1) = pages; | |
| 113 | 241476 | return mem + 2 * sizeof(size_t); | |
| 114 | } | ||
| 115 | |||
| 116 | 241247 | static inline void __attribute__((used)) smunmap(void *mem) { | |
| 117 | 241247 | unsigned char *area = static_cast<unsigned char *>(mem); | |
| 118 | 241247 | area = area - sizeof(size_t); | |
| 119 | 241247 | const size_t pages = *(reinterpret_cast<size_t *>(area)); | |
| 120 | 241247 | const int retval = munmap(area - sizeof(size_t), pages * 4096); | |
| 121 | // printf("SUNMMAP %d bytes at %p\n", pages*4096, area); | ||
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 241265 times.
|
241265 | assert((retval == 0) && "Invalid umnmap"); |
| 123 | 241265 | } | |
| 124 | |||
| 125 | |||
| 126 | /** | ||
| 127 | * Used when the caller remembers the size, so that it can call sxunmap later. | ||
| 128 | */ | ||
| 129 | 7681 | static inline void * __attribute__((used)) sxmmap(size_t size) { | |
| 130 | 7681 | const int anonymous_fd = -1; | |
| 131 | 7681 | const off_t offset = 0; | |
| 132 | 7681 | void *mem = NULL; | |
| 133 | |||
| 134 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 135 | do { | ||
| 136 | #endif | ||
| 137 | 7681 | mem = mmap(NULL, | |
| 138 | size, | ||
| 139 | PROT_READ | PROT_WRITE, | ||
| 140 | MAP_PRIVATE | PLATFORM_MAP_ANONYMOUS, | ||
| 141 | anonymous_fd, | ||
| 142 | offset); | ||
| 143 | |||
| 144 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 145 | } while (mem == MAP_FAILED); | ||
| 146 | #endif | ||
| 147 | // NOLINTNEXTLINE(performance-no-int-to-ptr) | ||
| 148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7681 times.
|
7681 | assert((mem != MAP_FAILED) && "Out Of Memory"); |
| 149 | 7681 | return mem; | |
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | /** | ||
| 154 | * Free memory acquired by sxmmap. | ||
| 155 | */ | ||
| 156 | 13881 | static inline void __attribute__((used)) sxunmap(void *mem, size_t size) { | |
| 157 | 13881 | const int retval = munmap(mem, size); | |
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13881 times.
|
13881 | assert((retval == 0) && "Invalid umnmap"); |
| 159 | 13881 | } | |
| 160 | |||
| 161 | |||
| 162 | /** | ||
| 163 | * Pointer is aligned at a multiple of the size. The size has to be a multiple | ||
| 164 | * of 2MB. | ||
| 165 | */ | ||
| 166 | 3971 | static inline void * __attribute__((used)) sxmmap_align(size_t size) { | |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3971 times.
|
3971 | assert((size % (2 * 1024 * 1024)) == 0); |
| 168 | 3971 | char *mem = NULL; | |
| 169 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 170 | do { | ||
| 171 | #endif | ||
| 172 | 3971 | mem = reinterpret_cast<char *>(sxmmap(2 * size)); | |
| 173 | #ifdef CVMFS_SUPPRESS_ASSERTS | ||
| 174 | } while (mem == MAP_FAILED); | ||
| 175 | #endif | ||
| 176 | |||
| 177 | 3971 | const uintptr_t head = size - (uintptr_t(mem) % size); | |
| 178 | 3971 | sxunmap(mem, head); | |
| 179 | 3971 | mem += head; | |
| 180 | 3971 | const uintptr_t tail = size - head; | |
| 181 |
2/2✓ Branch 0 taken 2243 times.
✓ Branch 1 taken 1728 times.
|
3971 | if (tail > 0) |
| 182 | 2243 | sxunmap(mem + size, tail); | |
| 183 | 3971 | return mem; | |
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 188 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 189 | #endif | ||
| 190 | |||
| 191 | #endif // CVMFS_UTIL_SMALLOC_H_ | ||
| 192 |