add configuration for zero on free

pull/50/head
Daniel Micay 2018-09-07 00:33:51 -04:00
parent ba3a8b0058
commit 96c538d90f
2 changed files with 17 additions and 2 deletions

View File

@ -4,5 +4,6 @@
#define GUARD_SLABS true #define GUARD_SLABS true
#define WRITE_AFTER_FREE_CHECK true #define WRITE_AFTER_FREE_CHECK true
#define SLOT_RANDOMIZE true #define SLOT_RANDOMIZE true
#define ZERO_ON_FREE true
#endif #endif

View File

@ -20,6 +20,8 @@
static_assert(sizeof(void *) == 8, "64-bit only"); static_assert(sizeof(void *) == 8, "64-bit only");
static_assert(!WRITE_AFTER_FREE_CHECK || ZERO_ON_FREE, "WRITE_AFTER_FREE_CHECK depends on ZERO_ON_FREE");
// either sizeof(uint64_t) or 0 // either sizeof(uint64_t) or 0
static const size_t canary_size = sizeof(uint64_t); static const size_t canary_size = sizeof(uint64_t);
@ -411,7 +413,9 @@ static inline void slab_free(void *p) {
} }
if (!is_zero_size) { if (!is_zero_size) {
if (ZERO_ON_FREE) {
memset(p, 0, size - canary_size); memset(p, 0, size - canary_size);
}
if (canary_size) { if (canary_size) {
uint64_t canary_value; uint64_t canary_value;
@ -780,8 +784,18 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
} }
init(); init();
total_size = adjust_size_for_canaries(total_size); total_size = adjust_size_for_canaries(total_size);
if (ZERO_ON_FREE) {
return allocate(total_size); return allocate(total_size);
} }
void *p = allocate(total_size);
if (unlikely(p == NULL)) {
return NULL;
}
if (size) {
memset(p, 0, total_size - canary_size);
}
return p;
}
static const size_t mremap_threshold = 4 * 1024 * 1024; static const size_t mremap_threshold = 4 * 1024 * 1024;