提交 06abeb9e 编写于 作者: C ChenJie

support jemalloc

Signed-off-by: NChenJie <chenjie174@huawei.com>
上级 ffdb7b7c
......@@ -32,6 +32,7 @@ declare_args() {
runtime_lib_path =
"//prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/12.0.1/lib"
user_custom_libc = true
use_jemalloc = false
musl_ported_dir = "intermidiates/${musl_target_os}/musl_src_ported"
musl_inc_out_dir = "usr/include/${musl_target_triple}"
uapi_dir = "//kernel/linux/patches/linux-5.10/prebuilts/usr/include"
......
......@@ -207,6 +207,66 @@ template("musl_libs") {
}
}
config("soft_jemalloc") {
configs = []
include_dirs = [
"${target_out_dir}/${musl_ported_dir}/arch/${musl_arch}",
"${target_out_dir}/${musl_ported_dir}/arch/generic",
"${target_out_dir}/${musl_ported_dir}/src/internal",
"${target_out_dir}/${musl_ported_dir}/src/include",
"${target_out_dir}/${musl_ported_dir}/include",
"${target_out_dir}/${musl_inc_out_dir}",
"${clang_base_path}/lib/clang/${clang_version}/include",
]
cflags = [
"--target=${musl_target_triple}",
"-D_GNU_SOURCE",
"-D_REENTRANT",
"-Wall",
"-Wshorten-64-to-32",
"-Wsign-compare",
"-Wundef",
"-Wno-format-zero-length",
"-pipe",
"-g3",
"-fvisibility=hidden",
"-O3",
"-funroll-loops",
# The following flags are for avoiding errors when compiling.
"-Wno-unused-parameter",
"-Wno-unused-function",
"-Wno-missing-field-initializers",
"-U_FORTIFY_SOURCE",
"-DOHOS_ENABLE_TCACHE",
"-DOHOS_LG_TCACHE_MAXCLASS_DEFAULT=16",
"-DOHOS_NUM_ARENAS=2",
"-DOHOS_TCACHE_NSLOTS_SMALL_MAX=8",
"-DOHOS_TCACHE_NSLOTS_LARGE=16",
]
if (musl_arch == "arm") {
cflags += [ "-march=armv7-a" ]
} else if (musl_arch == "aarch64") {
cflags += [ "-march=armv8" ]
} else if (musl_arch == "x86_64") {
cflags += [ "-march=x86-64" ]
}
include_dirs += [
"//third_party",
"//third_party/musl/src/include/",
"//third_party/jemalloc/include/",
"//third_party/jemalloc/include/jemalloc/internal",
"//third_party/jemalloc/include/jemalloc",
]
}
source_set("soft_musl_crt") {
sources = [
"${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/crti.s",
......@@ -450,6 +510,11 @@ template("musl_libs") {
"-fstack-protector-strong",
]
if (use_jemalloc) {
defines += [ "USE_JEMALLOC" ]
include_dirs = [ "//third_party/jemalloc/include/jemalloc" ]
}
configs -= musl_inherited_configs
configs += [ ":soft_musl_config" ]
......@@ -575,6 +640,24 @@ template("musl_libs") {
]
}
source_set("soft_musl_jemalloc") {
sources = [ "./porting/linux/user/src/malloc/jemalloc/jemalloc.c" ]
deps = [
"//third_party/musl:create_alltypes_h",
"//third_party/musl:create_porting_src",
"//third_party/musl:create_syscall_h",
"//third_party/musl:create_version_h",
"//third_party/musl:musl_copy_inc_bits",
"//third_party/musl:musl_copy_inc_root",
"//third_party/musl:musl_copy_inc_sys",
]
configs -= musl_inherited_configs
configs += [ ":soft_jemalloc" ]
}
static_library("soft_libc_musl_static") {
output_name = "libc"
complete_static_lib = true
......@@ -692,6 +775,10 @@ template("musl_libs") {
if (is_standard_system) {
deps += [ "//base/startup/init/services/param/base:parameterbase" ]
}
if (use_jemalloc) {
deps += [ ":soft_musl_jemalloc" ]
}
}
action_foreach("soft_musl_crt_install_action") {
......
#include "jemalloc/src/static.c"
\ No newline at end of file
......@@ -12,6 +12,14 @@
#include "malloc_random.h"
#include <sys/prctl.h>
#ifdef USE_JEMALLOC
#include <malloc.h>
extern void* je_malloc(size_t size);
extern void* je_calloc(size_t count, size_t size);
extern void* je_realloc(void* p, size_t newsize);
extern void je_free(void* p);
#endif
#if defined(__GNUC__) && defined(__PIC__)
#define inline inline __attribute__((always_inline))
#endif
......@@ -448,6 +456,9 @@ void *__libc_malloc(size_t n)
void *malloc(size_t n)
#endif
{
#ifdef USE_JEMALLOC
return je_malloc(n);
#endif
return internal_malloc(n);
}
......@@ -555,6 +566,9 @@ static size_t mal0_clear(char *p, size_t pagesz, size_t n)
void *calloc(size_t m, size_t n)
{
#ifdef USE_JEMALLOC
return je_calloc(m, n);
#endif
if(n && m > (size_t)-1/n){
errno=ENOMEM;
return 0;
......@@ -591,6 +605,9 @@ void *internal_calloc(size_t m, size_t n)
void *realloc(void *p, size_t n)
{
#ifdef USE_JEMALLOC
return je_realloc(p, n);
#endif
struct chunk *self, *next;
size_t n0, n1;
void *new;
......@@ -1036,6 +1053,9 @@ void __libc_free(void *p)
void free(void *p)
#endif
{
#ifdef USE_JEMALLOC
return je_free(p);
#endif
return internal_free(p);
}
......@@ -1065,6 +1085,7 @@ void internal_free(void *p)
void __malloc_donate(char *start, char *end)
{
#ifndef USE_JEMALLOC
size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - OVERHEAD);
size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end;
......@@ -1085,4 +1106,5 @@ void __malloc_donate(char *start, char *end)
chunk_checksum_set(c);
#endif
__bin_chunk(c);
#endif
}
......@@ -5,10 +5,18 @@
#include "atomic.h"
#endif
#ifdef USE_JEMALLOC
extern size_t je_malloc_usable_size(void *p);
#endif
hidden void *(*const __realloc_dep)(void *, size_t) = realloc;
size_t malloc_usable_size(void *p)
{
#ifdef USE_JEMALLOC
return je_malloc_usable_size(p);
#endif
#ifndef MALLOC_RED_ZONE
return p ? CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD : 0;
#else
......
#include <stdlib.h>
#ifndef USE_JEMALLOC
#include <stdint.h>
#include <errno.h>
#include "malloc_impl.h"
#include "malloc_config.h"
#else
extern void* je_memalign(size_t align, size_t len);
#endif
void *__memalign(size_t align, size_t len)
{
#ifdef USE_JEMALLOC
return je_memalign(align, len);
#else
unsigned char *mem, *new;
if ((align & -align) != align) {
......@@ -66,6 +74,7 @@ void *__memalign(size_t align, size_t len)
__bin_chunk(c);
return new;
#endif
}
weak_alias(__memalign, memalign);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册