Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Musl
提交
c2c8d9f6
T
Third Party Musl
项目概览
OpenHarmony
/
Third Party Musl
大约 1 年 前同步成功
通知
37
Star
125
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Musl
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c2c8d9f6
编写于
2月 09, 2023
作者:
Z
zhang-cui11
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Malloc hook optimization
Signed-off-by:
N
zhang-cui11
<
zhangcui11@huawei.com
>
上级
6d8d4fd7
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
62 addition
and
30 deletion
+62
-30
musl_template.gni
musl_template.gni
+15
-0
porting/linux/user/src/hook/malloc_common.c
porting/linux/user/src/hook/malloc_common.c
+29
-11
porting/linux/user/src/hook/musl_malloc.h
porting/linux/user/src/hook/musl_malloc.h
+10
-2
porting/linux/user/src/hook/musl_preinit.c
porting/linux/user/src/hook/musl_preinit.c
+2
-2
porting/linux/user/src/hook/musl_preinit_common.c
porting/linux/user/src/hook/musl_preinit_common.c
+2
-2
porting/linux/user/src/malloc/malloc.c
porting/linux/user/src/malloc/malloc.c
+4
-13
未找到文件。
musl_template.gni
浏览文件 @
c2c8d9f6
...
@@ -724,12 +724,27 @@ template("musl_libs") {
...
@@ -724,12 +724,27 @@ template("musl_libs") {
deps += [ "//base/startup/init/services/param/base:parameterbase" ]
deps += [ "//base/startup/init/services/param/base:parameterbase" ]
}
}
if (use_jemalloc) {
defines += [ "USE_JEMALLOC" ]
if (use_jemalloc_dfx_intf) {
defines += [ "USE_JEMALLOC_DFX_INTF" ]
}
include_dirs = [ "//third_party/jemalloc/include/jemalloc" ]
}
configs -= musl_inherited_configs
configs -= musl_inherited_configs
configs += [
configs += [
"//build/config/compiler:compiler",
"//build/config/compiler:compiler",
":soft_hook",
":soft_hook",
]
]
cflags = [
"-mllvm",
"--instcombine-max-iterations=0",
"-ffp-contract=fast",
"-O3",
]
}
}
source_set("soft_musl_jemalloc") {
source_set("soft_musl_jemalloc") {
...
...
porting/linux/user/src/hook/malloc_common.c
浏览文件 @
c2c8d9f6
...
@@ -9,26 +9,44 @@
...
@@ -9,26 +9,44 @@
void
*
malloc
(
size_t
bytes
)
void
*
malloc
(
size_t
bytes
)
{
{
volatile
const
struct
MallocDispatchType
*
dispatch_table
=
get_current_dispatch_table
();
volatile
const
struct
MallocDispatchType
*
dispatch_table
=
(
struct
MallocDispatchType
*
)
atomic_load_explicit
(
&
__musl_libc_globals
.
current_dispatch_table
,
memory_order_acquire
);
if
(
__predict_false
(
dispatch_table
!=
NULL
))
{
if
(
__predict_false
(
dispatch_table
!=
NULL
))
{
void
*
ret
=
dispatch_table
->
malloc
(
bytes
);
if
(
__get_memleak_hook_flag
())
{
return
ret
;
return
dispatch_table
->
malloc
(
bytes
);
}
if
(
!
__get_global_hook_flag
())
{
return
MuslFunc
(
malloc
)(
bytes
);
}
else
if
(
!
__get_hook_flag
())
{
return
MuslFunc
(
malloc
)(
bytes
);
}
}
void
*
result
=
MuslMalloc
(
malloc
)(
bytes
);
return
dispatch_table
->
malloc
(
bytes
);
if
(
__predict_false
(
result
==
NULL
))
{
//__musl_log(__MUSL_LOG_WARN, "malloc(%zu) failed: returning null pointer\n", bytes);
}
}
return
result
;
return
MuslFunc
(
malloc
)(
bytes
)
;
}
}
void
free
(
void
*
mem
)
void
free
(
void
*
mem
)
{
{
volatile
const
struct
MallocDispatchType
*
dispatch_table
=
get_current_dispatch_table
();
volatile
const
struct
MallocDispatchType
*
dispatch_table
=
(
struct
MallocDispatchType
*
)
atomic_load_explicit
(
&
__musl_libc_globals
.
current_dispatch_table
,
memory_order_acquire
);
if
(
__predict_false
(
dispatch_table
!=
NULL
))
{
if
(
__predict_false
(
dispatch_table
!=
NULL
))
{
if
(
__get_memleak_hook_flag
())
{
dispatch_table
->
free
(
mem
);
dispatch_table
->
free
(
mem
);
}
else
{
return
;
MuslMalloc
(
free
)(
mem
);
}
if
(
!
__get_global_hook_flag
())
{
MuslFunc
(
free
)(
mem
);
return
;
}
else
if
(
!
__get_hook_flag
())
{
MuslFunc
(
free
)(
mem
);
return
;
}
dispatch_table
->
free
(
mem
);
return
;
}
}
MuslFunc
(
free
)(
mem
);
}
}
void
*
mmap
(
void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
int
fd
,
off_t
offset
)
void
*
mmap
(
void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
int
fd
,
off_t
offset
)
...
...
porting/linux/user/src/hook/musl_malloc.h
浏览文件 @
c2c8d9f6
...
@@ -13,12 +13,20 @@ extern "C" {
...
@@ -13,12 +13,20 @@ extern "C" {
#define MuslMalloc(func) func
#define MuslMalloc(func) func
#endif
#endif
#ifdef USE_JEMALLOC
extern
void
*
je_malloc
(
size_t
size
);
extern
void
je_free
(
void
*
p
);
#define MuslFunc(func) je_ ## func
#else
extern
void
*
internal_malloc
(
size_t
size
);
extern
void
internal_free
(
void
*
p
);
#define MuslFunc(func) internal_ ## func
#endif
void
*
__libc_mmap
(
void
*
,
size_t
,
int
,
int
,
int
,
off_t
);
void
*
__libc_mmap
(
void
*
,
size_t
,
int
,
int
,
int
,
off_t
);
int
__libc_munmap
(
void
*
,
size_t
);
int
__libc_munmap
(
void
*
,
size_t
);
void
*
__libc_malloc
(
size_t
);
void
*
__libc_calloc
(
size_t
,
size_t
);
void
*
__libc_calloc
(
size_t
,
size_t
);
void
*
__libc_realloc
(
void
*
,
size_t
);
void
*
__libc_realloc
(
void
*
,
size_t
);
void
__libc_free
(
void
*
);
void
*
__libc_valloc
(
size_t
);
void
*
__libc_valloc
(
size_t
);
void
*
__libc_memalign
(
size_t
,
size_t
);
void
*
__libc_memalign
(
size_t
,
size_t
);
size_t
__libc_malloc_usable_size
(
void
*
);
size_t
__libc_malloc_usable_size
(
void
*
);
...
...
porting/linux/user/src/hook/musl_preinit.c
浏览文件 @
c2c8d9f6
...
@@ -40,7 +40,7 @@ void* ohos_malloc_hook_init_function(size_t bytes);
...
@@ -40,7 +40,7 @@ void* ohos_malloc_hook_init_function(size_t bytes);
static
struct
MallocDispatchType
__ohos_malloc_hook_init_dispatch
=
{
static
struct
MallocDispatchType
__ohos_malloc_hook_init_dispatch
=
{
.
malloc
=
ohos_malloc_hook_init_function
,
.
malloc
=
ohos_malloc_hook_init_function
,
.
free
=
Musl
Mallo
c
(
free
),
.
free
=
Musl
Fun
c
(
free
),
.
mmap
=
MuslMalloc
(
mmap
),
.
mmap
=
MuslMalloc
(
mmap
),
.
munmap
=
MuslMalloc
(
munmap
),
.
munmap
=
MuslMalloc
(
munmap
),
.
calloc
=
MuslMalloc
(
calloc
),
.
calloc
=
MuslMalloc
(
calloc
),
...
@@ -459,7 +459,7 @@ void* ohos_malloc_hook_init_function(size_t bytes)
...
@@ -459,7 +459,7 @@ void* ohos_malloc_hook_init_function(size_t bytes)
// __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_detach\n", getprogname());
// __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_detach\n", getprogname());
}
}
}
}
void
*
ptr
=
Musl
Mallo
c
(
malloc
)(
bytes
);
void
*
ptr
=
Musl
Fun
c
(
malloc
)(
bytes
);
return
ptr
;
return
ptr
;
}
}
...
...
porting/linux/user/src/hook/musl_preinit_common.c
浏览文件 @
c2c8d9f6
...
@@ -8,8 +8,8 @@
...
@@ -8,8 +8,8 @@
struct
musl_libc_globals
__musl_libc_globals
;
struct
musl_libc_globals
__musl_libc_globals
;
struct
MallocDispatchType
__libc_malloc_default_dispatch
=
{
struct
MallocDispatchType
__libc_malloc_default_dispatch
=
{
.
malloc
=
Musl
Mallo
c
(
malloc
),
.
malloc
=
Musl
Fun
c
(
malloc
),
.
free
=
Musl
Mallo
c
(
free
),
.
free
=
Musl
Fun
c
(
free
),
.
mmap
=
MuslMalloc
(
mmap
),
.
mmap
=
MuslMalloc
(
mmap
),
.
munmap
=
MuslMalloc
(
munmap
),
.
munmap
=
MuslMalloc
(
munmap
),
.
calloc
=
MuslMalloc
(
calloc
),
.
calloc
=
MuslMalloc
(
calloc
),
...
...
porting/linux/user/src/malloc/malloc.c
浏览文件 @
c2c8d9f6
...
@@ -30,11 +30,6 @@ extern int je_mallopt(int param, int value);
...
@@ -30,11 +30,6 @@ extern int je_mallopt(int param, int value);
#define inline inline __attribute__((always_inline))
#define inline inline __attribute__((always_inline))
#endif
#endif
#ifdef HOOK_ENABLE
void
*
__libc_malloc
(
size_t
);
void
__libc_free
(
void
*
p
);
#endif
static
struct
{
static
struct
{
volatile
uint64_t
binmap
;
volatile
uint64_t
binmap
;
struct
bin
bins
[
BINS_COUNT
];
struct
bin
bins
[
BINS_COUNT
];
...
@@ -627,17 +622,15 @@ static void trim(struct chunk *self, size_t n)
...
@@ -627,17 +622,15 @@ static void trim(struct chunk *self, size_t n)
__bin_chunk
(
split
);
__bin_chunk
(
split
);
}
}
#ifdef HOOK_ENABLE
#ifndef HOOK_ENABLE
void
*
__libc_malloc
(
size_t
n
)
#else
void
*
malloc
(
size_t
n
)
void
*
malloc
(
size_t
n
)
#endif
{
{
#ifdef USE_JEMALLOC
#ifdef USE_JEMALLOC
return
je_malloc
(
n
);
return
je_malloc
(
n
);
#endif
#endif
return
internal_malloc
(
n
);
return
internal_malloc
(
n
);
}
}
#endif
void
*
internal_malloc
(
size_t
n
)
void
*
internal_malloc
(
size_t
n
)
{
{
...
@@ -1244,17 +1237,15 @@ static void quarantine_bin(struct chunk *self)
...
@@ -1244,17 +1237,15 @@ static void quarantine_bin(struct chunk *self)
}
}
#endif
#endif
#ifdef HOOK_ENABLE
#ifndef HOOK_ENABLE
void
__libc_free
(
void
*
p
)
#else
void
free
(
void
*
p
)
void
free
(
void
*
p
)
#endif
{
{
#ifdef USE_JEMALLOC
#ifdef USE_JEMALLOC
return
je_free
(
p
);
return
je_free
(
p
);
#endif
#endif
return
internal_free
(
p
);
return
internal_free
(
p
);
}
}
#endif
void
internal_free
(
void
*
p
)
void
internal_free
(
void
*
p
)
{
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录