Compare commits
2 Commits
a4d1f91670
...
25cc427ec5
Author | SHA1 | Date | |
---|---|---|---|
25cc427ec5 | |||
c15c2e7b44 |
@@ -558,7 +558,6 @@ template <class T> struct BoundedFreeListAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void release(T *p) {
|
void release(T *p) {
|
||||||
static_assert(std::is_trivially_destructible_v<T>);
|
|
||||||
if (freeListBytes >= kFreeListMaxMemory) {
|
if (freeListBytes >= kFreeListMaxMemory) {
|
||||||
removeNode(p);
|
removeNode(p);
|
||||||
return safe_free(p, sizeof(T) + p->partialKeyCapacity);
|
return safe_free(p, sizeof(T) + p->partialKeyCapacity);
|
||||||
|
34
Internal.h
34
Internal.h
@@ -60,6 +60,10 @@ inline int64_t peakMallocBytes = 0;
|
|||||||
|
|
||||||
inline thread_local int64_t mallocBytesDelta = 0;
|
inline thread_local int64_t mallocBytesDelta = 0;
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
constexpr auto kMallocHeaderSize = 16;
|
||||||
|
#endif
|
||||||
|
|
||||||
// malloc that aborts on OOM and thus always returns a non-null pointer. Must be
|
// malloc that aborts on OOM and thus always returns a non-null pointer. Must be
|
||||||
// paired with `safe_free`.
|
// paired with `safe_free`.
|
||||||
__attribute__((always_inline)) inline void *safe_malloc(size_t s) {
|
__attribute__((always_inline)) inline void *safe_malloc(size_t s) {
|
||||||
@@ -69,18 +73,20 @@ __attribute__((always_inline)) inline void *safe_malloc(size_t s) {
|
|||||||
if (mallocBytes > peakMallocBytes) {
|
if (mallocBytes > peakMallocBytes) {
|
||||||
peakMallocBytes = mallocBytes;
|
peakMallocBytes = mallocBytes;
|
||||||
}
|
}
|
||||||
void *p = malloc(s);
|
|
||||||
if (p == nullptr) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
#else
|
|
||||||
void *p = malloc(s);
|
|
||||||
if (p == nullptr) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
#endif
|
#endif
|
||||||
|
void *p = malloc(s
|
||||||
|
#ifndef NDEBUG
|
||||||
|
+ kMallocHeaderSize
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
if (p == nullptr) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
#ifndef NDEBUG
|
||||||
|
memcpy(p, &s, sizeof(s));
|
||||||
|
(char *&)p += kMallocHeaderSize;
|
||||||
|
#endif
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must be paired with `safe_malloc`.
|
// Must be paired with `safe_malloc`.
|
||||||
@@ -93,6 +99,12 @@ __attribute__((always_inline)) inline void safe_free(void *p, size_t s) {
|
|||||||
mallocBytes -= s;
|
mallocBytes -= s;
|
||||||
free(p);
|
free(p);
|
||||||
#else
|
#else
|
||||||
|
#ifndef NDEBUG
|
||||||
|
(char *&)p -= kMallocHeaderSize;
|
||||||
|
size_t expected;
|
||||||
|
memcpy(&expected, p, sizeof(expected));
|
||||||
|
assert(s == expected);
|
||||||
|
#endif
|
||||||
free(p);
|
free(p);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user