Align allocations like the kernel does

This commit is contained in:
Kent Overstreet 2019-03-24 23:38:17 -04:00
parent 9b08492bc7
commit 365d345005

View File

@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/log2.h>
#include <linux/page.h> #include <linux/page.h>
#include <linux/shrinker.h> #include <linux/shrinker.h>
#include <linux/types.h> #include <linux/types.h>
@ -19,7 +20,10 @@ static inline void *kmalloc(size_t size, gfp_t flags)
run_shrinkers(); run_shrinkers();
p = malloc(size); p = size
? aligned_alloc(min(rounddown_pow_of_two(size),
PAGE_SIZE), size)
: malloc(0);
if (p && (flags & __GFP_ZERO)) if (p && (flags & __GFP_ZERO))
memset(p, 0, size); memset(p, 0, size);