Skip to content

Commit 4f7bf44

Browse files
committed
Refactor the section-generated code into an evil macro
Signed-off-by: Pablo Galindo <[email protected]>
1 parent 6a3b06f commit 4f7bf44

File tree

3 files changed

+37
-40
lines changed

3 files changed

+37
-40
lines changed

Include/internal/pycore_runtime.h

+35
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ extern "C" {
2525
#include "pycore_typeobject.h" // struct _types_runtime_state
2626
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state
2727

28+
#if defined(__APPLE__)
29+
# include <mach-o/loader.h>
30+
#endif
31+
2832
struct _getargs_runtime_state {
2933
struct _PyArg_Parser *static_parsers;
3034
};
@@ -59,6 +63,37 @@ typedef struct _Py_AuditHookEntry {
5963
void *userData;
6064
} _Py_AuditHookEntry;
6165

66+
// Macros to burn global values in custom sections so out-of-process
67+
// profilers can locate them easily
68+
69+
#define GENERATE_DEBUG_SECTION(name, declaration) \
70+
_GENERATE_DEBUG_SECTION_WINDOWS(name) \
71+
_GENERATE_DEBUG_SECTION_APPLE(name) \
72+
declaration \
73+
_GENERATE_DEBUG_SECTION_LINUX(name)
74+
75+
#if defined(MS_WINDOWS)
76+
#define _GENERATE_DEBUG_SECTION_WINDOWS(name) \
77+
_Pragma(Py_STRINGIFY(section(Py_STRINGIFY(name), read, write))) \
78+
__declspec(allocate(Py_STRINGIFY(name)))
79+
#else
80+
#define _GENERATE_DEBUG_SECTION_WINDOWS(name)
81+
#endif
82+
83+
#if defined(__APPLE__)
84+
#define _GENERATE_DEBUG_SECTION_APPLE(name) \
85+
__attribute__((section(SEG_DATA "," Py_STRINGIFY(name))))
86+
#else
87+
#define _GENERATE_DEBUG_SECTION_APPLE(name)
88+
#endif
89+
90+
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
91+
#define _GENERATE_DEBUG_SECTION_LINUX(name) \
92+
__attribute__((section("." Py_STRINGIFY(name))))
93+
#else
94+
#define _GENERATE_DEBUG_SECTION_LINUX(name)
95+
#endif
96+
6297
typedef struct _Py_DebugOffsets {
6398
char cookie[8];
6499
uint64_t version;

Modules/_asynciomodule.c

+1-19
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
#include <stddef.h> // offsetof()
1818

19-
#if defined(__APPLE__)
20-
# include <mach-o/loader.h>
21-
#endif
22-
2319
/*[clinic input]
2420
module _asyncio
2521
[clinic start generated code]*/
@@ -116,21 +112,7 @@ typedef struct _Py_AsyncioModuleDebugOffsets {
116112
} asyncio_task_object;
117113
} Py_AsyncioModuleDebugOffsets;
118114

119-
#if defined(MS_WINDOWS)
120-
121-
#pragma section("AsyncioDebug", read, write)
122-
__declspec(allocate("AsyncioDebug"))
123-
124-
#elif defined(__APPLE__)
125-
126-
__attribute__((section(SEG_DATA ",AsyncioDebug")))
127-
128-
#endif
129-
130-
Py_AsyncioModuleDebugOffsets AsyncioDebug
131-
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
132-
__attribute__((section(".AsyncioDebug")))
133-
#endif
115+
GENERATE_DEBUG_SECTION(AsyncioDebug, Py_AsyncioModuleDebugOffsets AsyncioDebug)
134116
= {.asyncio_task_object = {
135117
.size = sizeof(TaskObj),
136118
.task_name = offsetof(TaskObj, task_name),

Python/pylifecycle.c

+1-21
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
# include <unistd.h> // isatty()
4444
#endif
4545

46-
#if defined(__APPLE__)
47-
# include <mach-o/loader.h>
48-
#endif
49-
5046
#ifdef HAVE_SIGNAL_H
5147
# include <signal.h> // SIG_IGN
5248
#endif
@@ -87,23 +83,7 @@ static void call_ll_exitfuncs(_PyRuntimeState *runtime);
8783
_Py_COMP_DIAG_PUSH
8884
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
8985

90-
#if defined(MS_WINDOWS)
91-
92-
#pragma section("PyRuntime", read, write)
93-
__declspec(allocate("PyRuntime"))
94-
95-
#elif defined(__APPLE__)
96-
97-
__attribute__((
98-
section(SEG_DATA ",PyRuntime")
99-
))
100-
101-
#endif
102-
103-
_PyRuntimeState _PyRuntime
104-
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
105-
__attribute__ ((section (".PyRuntime")))
106-
#endif
86+
GENERATE_DEBUG_SECTION(PyRuntime, _PyRuntimeState _PyRuntime)
10787
= _PyRuntimeState_INIT(_PyRuntime, _Py_Debug_Cookie);
10888
_Py_COMP_DIAG_POP
10989

0 commit comments

Comments
 (0)