factor out duplicated code in malloc/realloc

pull/87/head
Daniel Micay 2019-04-07 05:48:10 -04:00
parent ce36d0c826
commit c68de6141d
1 changed files with 13 additions and 14 deletions

View File

@ -1182,13 +1182,6 @@ static void deallocate_large(void *p, const size_t *expected_size) {
regions_quarantine_deallocate_pages(p, size, guard_size); regions_quarantine_deallocate_pages(p, size, guard_size);
} }
static size_t adjust_size_for_canaries(size_t size) {
if (size > 0 && size <= max_slab_size_class) {
return size + canary_size;
}
return size;
}
static int alloc_aligned(void **memptr, size_t alignment, size_t size, size_t min_alignment) { static int alloc_aligned(void **memptr, size_t alignment, size_t size, size_t min_alignment) {
if ((alignment - 1) & alignment || alignment < min_alignment) { if ((alignment - 1) & alignment || alignment < min_alignment) {
return EINVAL; return EINVAL;
@ -1240,7 +1233,14 @@ static void *alloc_aligned_simple(size_t alignment, size_t size) {
return ptr; return ptr;
} }
EXPORT void *h_malloc(size_t size) { static size_t adjust_size_for_canaries(size_t size) {
if (size > 0 && size <= max_slab_size_class) {
return size + canary_size;
}
return size;
}
static inline void *alloc(size_t size) {
init(); init();
thread_unseal_metadata(); thread_unseal_metadata();
size = adjust_size_for_canaries(size); size = adjust_size_for_canaries(size);
@ -1249,6 +1249,10 @@ EXPORT void *h_malloc(size_t size) {
return p; return p;
} }
EXPORT void *h_malloc(size_t size) {
return alloc(size);
}
EXPORT void *h_calloc(size_t nmemb, size_t size) { EXPORT void *h_calloc(size_t nmemb, size_t size) {
size_t total_size; size_t total_size;
if (unlikely(__builtin_mul_overflow(nmemb, size, &total_size))) { if (unlikely(__builtin_mul_overflow(nmemb, size, &total_size))) {
@ -1268,12 +1272,7 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
EXPORT void *h_realloc(void *old, size_t size) { EXPORT void *h_realloc(void *old, size_t size) {
if (old == NULL) { if (old == NULL) {
init(); return alloc(size);
thread_unseal_metadata();
size = adjust_size_for_canaries(size);
void *p = allocate(size);
thread_seal_metadata();
return p;
} }
size = adjust_size_for_canaries(size); size = adjust_size_for_canaries(size);