Implement pvalloc replacement

Despite being an obsolete function, pvalloc is still present in GLIBC and should
work correctly when jemalloc replaces libc allocator.
This commit is contained in:
Alex Lapenkou
2022-04-19 19:51:27 -07:00
committed by Alexander Lapenkov
parent cd5aaf308a
commit 5b1f2cc5d7
7 changed files with 75 additions and 0 deletions

View File

@@ -3250,6 +3250,49 @@ je_valloc(size_t size) {
}
#endif
#ifdef JEMALLOC_OVERRIDE_PVALLOC
JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
void JEMALLOC_NOTHROW *
JEMALLOC_ATTR(malloc)
je_pvalloc(size_t size) {
void *ret;
static_opts_t sopts;
dynamic_opts_t dopts;
LOG("core.pvalloc.entry", "size: %zu\n", size);
static_opts_init(&sopts);
dynamic_opts_init(&dopts);
sopts.null_out_result_on_error = true;
sopts.min_alignment = PAGE;
sopts.oom_string =
"<jemalloc>: Error allocating aligned memory: out of memory\n";
sopts.invalid_alignment_string =
"<jemalloc>: Error allocating aligned memory: invalid alignment\n";
dopts.result = &ret;
dopts.num_items = 1;
/*
* This is the only difference from je_valloc - size is rounded up to
* a PAGE multiple.
*/
dopts.item_size = PAGE_CEILING(size);
dopts.alignment = PAGE;
imalloc(&sopts, &dopts);
if (sopts.slow) {
uintptr_t args[3] = {size};
hook_invoke_alloc(hook_alloc_pvalloc, ret, (uintptr_t)ret,
args);
}
LOG("core.pvalloc.exit", "result: %p\n", ret);
return ret;
}
#endif
#if defined(JEMALLOC_IS_MALLOC) && defined(JEMALLOC_GLIBC_MALLOC_HOOK)
/*
* glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible
@@ -3297,6 +3340,9 @@ void *__libc_realloc(void* ptr, size_t size) PREALIAS(je_realloc);
# ifdef JEMALLOC_OVERRIDE___LIBC_VALLOC
void *__libc_valloc(size_t size) PREALIAS(je_valloc);
# endif
# ifdef JEMALLOC_OVERRIDE___LIBC_PVALLOC
void *__libc_pvalloc(size_t size) PREALIAS(je_pvalloc);
# endif
# ifdef JEMALLOC_OVERRIDE___POSIX_MEMALIGN
int __posix_memalign(void** r, size_t a, size_t s) PREALIAS(je_posix_memalign);
# endif