Skip to content

[libc] Add malloc.h header defining mallopt #110908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
)

if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()

set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.inttypes
libc.include.limits
libc.include.link
libc.include.malloc
libc.include.math
libc.include.pthread
libc.include.signal
Expand Down
7 changes: 7 additions & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ if(LLVM_LIBC_FULL_BUILD)
)
endif()

if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()

set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/arm/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.malloc
libc.include.math
libc.include.search
libc.include.setjmp
Expand Down
7 changes: 7 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
)

if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()

set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.stdint
libc.include.inttypes
libc.include.limits
libc.include.malloc
libc.include.math
libc.include.pthread
libc.include.sched
Expand Down
7 changes: 7 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
)

if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()

set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.inttypes
libc.include.limits
libc.include.link
libc.include.malloc
libc.include.math
libc.include.pthread
libc.include.sched
Expand Down
10 changes: 10 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ add_header_macro(
.llvm-libc-macros.limits_macros
)

add_header_macro(
malloc
../libc/newhdrgen/yaml/malloc.yaml
malloc.h.def
malloc.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.malloc_macros
)

add_header_macro(
math
../libc/newhdrgen/yaml/math.yaml
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ add_macro_header(
link-macros.h
)

add_macro_header(
malloc_macros
HDR
malloc-macros.h
)

add_macro_header(
math_macros
HDR
Expand Down
17 changes: 17 additions & 0 deletions libc/include/llvm-libc-macros/malloc-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Definition of macros to be used with malloc functions -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_MACROS_MALLOC_MACROS_H
#define LLVM_LIBC_MACROS_MALLOC_MACROS_H

// Note: these values only make sense when Scudo is used as the memory
// allocator.
#define M_PURGE (-101)
#define M_PURGE_ALL (-104)

#endif // LLVM_LIBC_MACROS_MALLOC_MACROS_H
17 changes: 17 additions & 0 deletions libc/include/malloc.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- C standard library header malloc.h --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_MALLOC_H
#define LLVM_LIBC_MALLOC_H

#include "__llvm-libc-common.h"
#include "llvm-libc-macros/malloc-macros.h"

%%public_api()

#endif // LLVM_LIBC_MALLOC_H
13 changes: 13 additions & 0 deletions libc/newhdrgen/yaml/malloc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
header: malloc.h
macros: []
types: []
enums: []
objects: []
functions:
- name: mallopt
standards:
- GNUExtensions
return_type: int
arguments:
- type: int
- type: int
11 changes: 11 additions & 0 deletions libc/spec/gnu_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
]
>;

HeaderSpec Malloc = HeaderSpec<
"malloc.h",
[], // Macros
[], // Types
[], // Enumerations
[
FunctionSpec<"mallopt", RetValSpec<IntType>, [ArgSpec<IntType>, ArgSpec<IntType>]>,
]
>;

HeaderSpec Math = HeaderSpec<
"math.h",
[], // Macros
Expand Down Expand Up @@ -291,6 +301,7 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
let Headers = [
CType,
FEnv,
Malloc,
Math,
PThread,
Sched,
Expand Down
5 changes: 5 additions & 0 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
DEPENDS
${SCUDO_DEPS}
)
add_entrypoint_external(
mallopt
DEPENDS
${SCUDO_DEPS}
)
else()
# Only use freelist malloc for baremetal targets.
add_entrypoint_object(
Expand Down
Loading