reuse a single size alignment implementation
This commit is contained in:
parent
e814cf4f5c
commit
536f852538
12
h_malloc.c
12
h_malloc.c
@ -194,7 +194,7 @@ static inline struct size_info get_size_info(size_t size) {
|
|||||||
return (struct size_info){0, 0};
|
return (struct size_info){0, 0};
|
||||||
}
|
}
|
||||||
if (size <= 128) {
|
if (size <= 128) {
|
||||||
return (struct size_info){(size + 15) & ~15, ((size - 1) >> 4) + 1};
|
return (struct size_info){align(size, 16), ((size - 1) >> 4) + 1};
|
||||||
}
|
}
|
||||||
for (unsigned class = 9; class < N_SIZE_CLASSES; class++) {
|
for (unsigned class = 9; class < N_SIZE_CLASSES; class++) {
|
||||||
size_t real_size = size_classes[class];
|
size_t real_size = size_classes[class];
|
||||||
@ -217,7 +217,7 @@ static inline struct size_info get_size_info_align(size_t size, size_t alignment
|
|||||||
}
|
}
|
||||||
|
|
||||||
static size_t get_slab_size(size_t slots, size_t size) {
|
static size_t get_slab_size(size_t slots, size_t size) {
|
||||||
return PAGE_CEILING(slots * size);
|
return page_align(slots * size);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct __attribute__((aligned(CACHELINE_SIZE))) size_class {
|
struct __attribute__((aligned(CACHELINE_SIZE))) size_class {
|
||||||
@ -1202,9 +1202,9 @@ static size_t get_large_size_class(size_t size) {
|
|||||||
size = max(size, (size_t)PAGE_SIZE);
|
size = max(size, (size_t)PAGE_SIZE);
|
||||||
size_t spacing_shift = 64 - __builtin_clzl(size - 1) - 3;
|
size_t spacing_shift = 64 - __builtin_clzl(size - 1) - 3;
|
||||||
size_t spacing_class = 1ULL << spacing_shift;
|
size_t spacing_class = 1ULL << spacing_shift;
|
||||||
return (size + (spacing_class - 1)) & ~(spacing_class - 1);
|
return align(size, spacing_class);
|
||||||
}
|
}
|
||||||
return PAGE_CEILING(size);
|
return page_align(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t get_guard_size(struct random_state *state, size_t size) {
|
static size_t get_guard_size(struct random_state *state, size_t size) {
|
||||||
@ -1480,7 +1480,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
|
|||||||
deallocate_pages(old, old_size, old_guard_size);
|
deallocate_pages(old, old_size, old_guard_size);
|
||||||
} else {
|
} else {
|
||||||
memory_unmap((char *)old - old_guard_size, old_guard_size);
|
memory_unmap((char *)old - old_guard_size, old_guard_size);
|
||||||
memory_unmap((char *)old + PAGE_CEILING(old_size), old_guard_size);
|
memory_unmap((char *)old + page_align(old_size), old_guard_size);
|
||||||
}
|
}
|
||||||
thread_seal_metadata();
|
thread_seal_metadata();
|
||||||
return new;
|
return new;
|
||||||
@ -1524,7 +1524,7 @@ EXPORT void *h_valloc(size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EXPORT void *h_pvalloc(size_t size) {
|
EXPORT void *h_pvalloc(size_t size) {
|
||||||
size = PAGE_CEILING(size);
|
size = page_align(size);
|
||||||
if (unlikely(!size)) {
|
if (unlikely(!size)) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
8
pages.c
8
pages.c
@ -9,10 +9,6 @@ static bool add_guards(size_t size, size_t guard_size, size_t *total_size) {
|
|||||||
__builtin_add_overflow(*total_size, guard_size, total_size);
|
__builtin_add_overflow(*total_size, guard_size, total_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uintptr_t alignment_ceiling(uintptr_t s, uintptr_t alignment) {
|
|
||||||
return ((s) + (alignment - 1)) & ((~alignment) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name) {
|
void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name) {
|
||||||
size_t real_size;
|
size_t real_size;
|
||||||
if (unlikely(add_guards(usable_size, guard_size, &real_size))) {
|
if (unlikely(add_guards(usable_size, guard_size, &real_size))) {
|
||||||
@ -33,7 +29,7 @@ void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, cons
|
|||||||
}
|
}
|
||||||
|
|
||||||
void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name) {
|
void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name) {
|
||||||
usable_size = PAGE_CEILING(usable_size);
|
usable_size = page_align(usable_size);
|
||||||
if (unlikely(!usable_size)) {
|
if (unlikely(!usable_size)) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -59,7 +55,7 @@ void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_
|
|||||||
|
|
||||||
void *usable = (char *)real + guard_size;
|
void *usable = (char *)real + guard_size;
|
||||||
|
|
||||||
size_t lead_size = alignment_ceiling((uintptr_t)usable, alignment) - (uintptr_t)usable;
|
size_t lead_size = align((uintptr_t)usable, alignment) - (uintptr_t)usable;
|
||||||
size_t trail_size = alloc_size - lead_size - usable_size;
|
size_t trail_size = alloc_size - lead_size - usable_size;
|
||||||
void *base = (char *)usable + lead_size;
|
void *base = (char *)usable + lead_size;
|
||||||
|
|
||||||
|
7
pages.h
7
pages.h
@ -5,11 +5,16 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#define PAGE_SHIFT 12
|
#define PAGE_SHIFT 12
|
||||||
#ifndef PAGE_SIZE
|
#ifndef PAGE_SIZE
|
||||||
#define PAGE_SIZE ((size_t)1 << PAGE_SHIFT)
|
#define PAGE_SIZE ((size_t)1 << PAGE_SHIFT)
|
||||||
#endif
|
#endif
|
||||||
#define PAGE_CEILING(s) (((s) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
|
|
||||||
|
static inline size_t page_align(size_t size) {
|
||||||
|
return align(size, PAGE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name);
|
void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name);
|
||||||
void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name);
|
void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name);
|
||||||
|
6
util.h
6
util.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef UTIL_H
|
#ifndef UTIL_H
|
||||||
#define UTIL_H
|
#define UTIL_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
// C11 noreturn doesn't work in C++
|
// C11 noreturn doesn't work in C++
|
||||||
@ -32,6 +33,11 @@ static inline int ffzl(unsigned long x) {
|
|||||||
return __builtin_ffsl(~x);
|
return __builtin_ffsl(~x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline size_t align(size_t size, size_t align) {
|
||||||
|
size_t mask = align - 1;
|
||||||
|
return (size + mask) & ~mask;
|
||||||
|
}
|
||||||
|
|
||||||
COLD noreturn void fatal_error(const char *s);
|
COLD noreturn void fatal_error(const char *s);
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
|
Loading…
Reference in New Issue
Block a user