7 #ifndef CVMFS_UTIL_SMALLOC_H_
8 #define CVMFS_UTIL_SMALLOC_H_
20 #define PLATFORM_MAP_ANONYMOUS MAP_ANON
22 #define PLATFORM_MAP_ANONYMOUS MAP_ANONYMOUS
26 #ifdef CVMFS_NAMESPACE_GUARD
27 namespace CVMFS_NAMESPACE_GUARD {
38 return (size + 7) & ~static_cast<uint64_t>(7);
43 #ifdef CVMFS_SUPPRESS_ASSERTS
47 #ifdef CVMFS_SUPPRESS_ASSERTS
48 }
while ((size > 0) && (mem == NULL));
50 assert((mem || (size == 0)) &&
"Out Of Memory");
57 #ifdef CVMFS_SUPPRESS_ASSERTS
60 mem = realloc(ptr, size);
61 #ifdef CVMFS_SUPPRESS_ASSERTS
62 }
while ((size > 0) && (mem == NULL));
64 assert((mem || (size == 0)) &&
"Out Of Memory");
68 static inline void *
__attribute__((used)) scalloc(
size_t count,
size_t size) {
71 #ifdef CVMFS_SUPPRESS_ASSERTS
74 mem = calloc(count, size);
75 #ifdef CVMFS_SUPPRESS_ASSERTS
76 }
while ((count * size > 0) && (mem == NULL));
78 assert((mem || ((count * size) == 0)) &&
"Out Of Memory");
82 static inline void *
__attribute__((used)) smmap(
size_t size) {
85 assert(size < std::numeric_limits<size_t>::max() - 4096);
87 const int anonymous_fd = -1;
88 const off_t offset = 0;
89 size_t pages = ((size + 2*
sizeof(size_t))+4095)/4096;
90 unsigned char *mem = NULL;
92 #ifdef CVMFS_SUPPRESS_ASSERTS
95 mem =
static_cast<unsigned char *
>(
98 PROT_READ | PROT_WRITE,
103 #ifdef CVMFS_SUPPRESS_ASSERTS
104 }
while (mem == MAP_FAILED);
108 assert((mem != MAP_FAILED) &&
"Out Of Memory");
110 *(
reinterpret_cast<size_t *
>(
mem) + 1) = pages;
111 return mem + 2*
sizeof(size_t);
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);
120 assert((retval == 0) &&
"Invalid umnmap");
127 static inline void *
__attribute__((used)) sxmmap(
size_t size) {
128 const int anonymous_fd = -1;
129 const off_t offset = 0;
132 #ifdef CVMFS_SUPPRESS_ASSERTS
137 PROT_READ | PROT_WRITE,
142 #ifdef CVMFS_SUPPRESS_ASSERTS
143 }
while (mem == MAP_FAILED);
146 assert((mem != MAP_FAILED) &&
"Out Of Memory");
154 static inline void __attribute__((used)) sxunmap(
void *mem,
size_t size) {
155 int retval = munmap(mem, size);
156 assert((retval == 0) &&
"Invalid umnmap");
164 static inline void *
__attribute__((used)) sxmmap_align(
size_t size) {
165 assert((size % (2 * 1024 * 1024)) == 0);
167 #ifdef CVMFS_SUPPRESS_ASSERTS
170 mem =
reinterpret_cast<char *
>(sxmmap(2 * size));
171 #ifdef CVMFS_SUPPRESS_ASSERTS
172 }
while (mem == MAP_FAILED);
175 uintptr_t head = size - (uintptr_t(mem) %
size);
178 uintptr_t tail = size - head;
180 sxunmap(mem + size, tail);
185 #ifdef CVMFS_NAMESPACE_GUARD
189 #endif // CVMFS_UTIL_SMALLOC_H_
assert((mem||(size==0))&&"Out Of Memory")
struct cvmcache_object_info __attribute__
static uint64_t RoundUp8(const uint64_t size)
#define PLATFORM_MAP_ANONYMOUS
const uint32_t kMemMarker