Skip to content

Commit 670f632

Browse files
mdh1418Copilot
andcommitted
minipal: deduplicate inline header helpers
Keep small platform helpers inline for callers while providing one external fallback definition. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1dce0cb commit 670f632

5 files changed

Lines changed: 45 additions & 19 deletions

File tree

src/native/minipal/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(SOURCES
99
getexepath.c
1010
memorybarrierprocesswide.c
1111
mutex.c
12+
ospagesize.c
1213
guid.c
1314
random.c
1415
debugger.c
@@ -20,19 +21,10 @@ set(SOURCES
2021
log.c
2122
)
2223

23-
# ospagesize is provided inline in the header on Windows and WASM; the .c file
24-
# only contains the POSIX implementation. Including it on those platforms would
25-
# produce a redefinition error (mono builds for wasi/browser set HOST_WASM but
26-
# not CLR_CMAKE_TARGET_ARCH_WASM, so check both). In cross-component builds
27-
# (e.g. host=x64, target=wasm) the code runs on the host, so we still need the
28-
# POSIX implementation; only skip when the HOST is actually wasm.
29-
if(NOT WIN32 AND NOT HOST_WASM AND NOT (CLR_CMAKE_TARGET_ARCH_WASM AND NOT CLR_CROSS_COMPONENTS_BUILD))
30-
list(APPEND SOURCES ospagesize.c)
31-
endif()
32-
3324
if(CLR_CMAKE_HOST_UNIX)
3425
list(APPEND SOURCES
3526
cpucount.c
27+
cpuid.c
3628
thread.c
3729
)
3830
endif()

src/native/minipal/cpuid.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#include "cpuid.h"
5+
6+
#if defined(HOST_X86) || defined(HOST_AMD64)
7+
#if defined(HOST_UNIX)
8+
9+
#if !__has_builtin(__cpuid)
10+
extern void __cpuid(int cpuInfo[4], int function_id);
11+
#endif
12+
13+
#if !__has_builtin(__cpuidex)
14+
extern void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id);
15+
#endif
16+
17+
#endif // HOST_UNIX
18+
#endif // defined(HOST_X86) || defined(HOST_AMD64)

src/native/minipal/cpuid.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616

1717
#include <minipal/utils.h>
1818

19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
1923
// MSVC directly defines intrinsics for __cpuid and __cpuidex matching the below signatures
2024
// We define matching signatures for use on Unix platforms.
2125
//
2226
// IMPORTANT: Unlike MSVC, Unix does not explicitly zero ECX for __cpuid
2327

2428
#if !__has_builtin(__cpuid)
25-
static void __cpuid(int cpuInfo[4], int function_id)
29+
inline void __cpuid(int cpuInfo[4], int function_id) __asm("minipal_cpuid");
30+
31+
inline void __cpuid(int cpuInfo[4], int function_id)
2632
{
2733
// Based on the Clang implementation provided in cpuid.h:
2834
// https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/cpuid.h
@@ -37,7 +43,9 @@ void __cpuid(int cpuInfo[4], int function_id);
3743
#endif
3844

3945
#if !__has_builtin(__cpuidex)
40-
static void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id)
46+
inline void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id) __asm("minipal_cpuidex");
47+
48+
inline void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id)
4149
{
4250
// Based on the Clang implementation provided in cpuid.h:
4351
// https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/cpuid.h
@@ -51,6 +59,10 @@ static void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id)
5159
void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id);
5260
#endif
5361

62+
#ifdef __cplusplus
63+
}
64+
#endif // extern "C"
65+
5466
#endif // HOST_UNIX
5567
#endif // defined(HOST_X86) || defined(HOST_AMD64)
5668

src/native/minipal/ospagesize.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
// POSIX implementation of minipal_getpagesize. On WASM and Windows the page size
5-
// is a compile-time constant and minipal_getpagesize is defined inline in the
6-
// header; this file is excluded from the build on those platforms by
7-
// src/native/minipal/CMakeLists.txt to avoid an empty translation unit.
4+
#include "ospagesize.h"
5+
6+
#if defined(HOST_WASM) || defined(HOST_WINDOWS)
7+
8+
extern uint32_t minipal_getpagesize(void);
9+
10+
#else
811

912
#include <unistd.h>
1013
#include <stdlib.h>
1114
#include <stdatomic.h>
12-
#include "ospagesize.h"
1315

1416
uint32_t minipal_getpagesize(void)
1517
{
@@ -38,3 +40,5 @@ uint32_t minipal_getpagesize(void)
3840
}
3941
return page_size;
4042
}
43+
44+
#endif // HOST_WASM || HOST_WINDOWS

src/native/minipal/ospagesize.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ extern "C" {
1919
// On other platforms the value is queried from the OS once and cached; the
2020
// definition lives in ospagesize.c so there is exactly one cache per process.
2121
#if defined(HOST_WASM)
22-
static inline uint32_t minipal_getpagesize(void)
22+
inline uint32_t minipal_getpagesize(void)
2323
{
2424
// WASM has no hardware pages; getpagesize() returns the 64KB memory.grow granularity,
2525
// which is too coarse for GC alignment and thresholds. Reduce the OS page size used
2626
// by the runtime on WASM to 16KB.
2727
return 16 * 1024;
2828
}
2929
#elif defined(HOST_WINDOWS)
30-
static inline uint32_t minipal_getpagesize(void)
30+
inline uint32_t minipal_getpagesize(void)
3131
{
3232
// The page size on Windows is 4KB and is not going to change.
3333
return 4 * 1024;

0 commit comments

Comments
 (0)