10000 bpo-35053: Add Include/tracemalloc.h (GH-10091) · python/cpython@6279c1c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6279c1c

Browse files
authored
bpo-35053: Add Include/tracemalloc.h (GH-10091)
* Modify object.h to ensure that pymem.h is included, to get _Py_tracemalloc_config variable. * Move _PyTraceMalloc_XXX() functions to tracemalloc.h, they need PyObject type. Break circular dependency between pymem.h and object.h.
1 parent 9e00e80 commit 6279c1c

File tree

7 files changed

+54
-37
lines changed

7 files changed

+54
-37
lines changed

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,6 @@
137137
#include "dtoa.h"
138138
#include "fileutils.h"
139139
#include "pyfpe.h"
140+
#include "tracemalloc.h"
140141

141142
#endif /* !Py_PYTHON_H */

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#ifndef Py_OBJECT_H
22
#define Py_OBJECT_H
3+
4+
#include "pymem.h" /* _Py_tracemalloc_config */
5+
36
#ifdef __cplusplus
47
extern "C" {
58
#endif

Include/pymem.h

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,6 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt);
2424
/* Try to get the allocators name set by _PyMem_SetupAllocators(). */
2525
PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void);
2626

27-
/* Track an allocated memory block in the tracemalloc module.
28-
Return 0 on success, return -1 on error (failed to allocate memory to store
29-
the trace).
30-
31-
Return -2 if tracemalloc is disabled.
32-
33-
If memory block is already tracked, update the existing trace. */
34-
PyAPI_FUNC(int) PyTraceMalloc_Track(
35-
unsigned int domain,
36-
uintptr_t ptr,
37-
size_t size);
38-
39-
/* Update the Python traceback of an object.
40-
This function can be used when a memory block is reused from a free list. */
41-
PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
42-
43-
/* Untrack an allocated memory block in the tracemalloc module.
44-
Do nothing if the block was not tracked.
45-
46-
Return -2 if tracemalloc is disabled, otherwise return 0. */
47-
PyAPI_FUNC(int) PyTraceMalloc_Untrack(
48-
unsigned int domain,
49-
uintptr_t ptr);
50-
51-
/* Get the traceback where a memory block was allocated.
52-
53-
Return a tuple of (filename: str, lineno: int) tuples.
54-
55-
Return None if the tracemalloc module is disabled or if the memory block
56-
is not tracked by tracemalloc.
57-
58-
Raise an exception and return NULL on error. */
59-
PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
60-
unsigned int domain,
61-
uintptr_t ptr);
62-
6327
PyAPI_FUNC(int) _PyMem_IsFreed(void *ptr, size_t size);
6428
#endif /* !defined(Py_LIMITED_API) */
6529

@@ -246,7 +210,9 @@ PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
246210

247211
/* bpo-35053: expose _Py_tracemalloc_config for performance:
248212
_Py_NewReference() needs an efficient check to test if tracemalloc is
249-
tracing. */
213+
tracing.
214+
215+
It has to be defined in pymem.h, before object.h is included. */
250216
struct _PyTraceMalloc_Config {
251217
/* Module initialized?
252218
Variable protected by the GIL */

Include/tracemalloc.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef Py_TRACEMALLOC_H
2+
#define Py_TRACEMALLOC_H
3+
4+
#ifndef Py_LIMITED_API
5+
/* Track an allocated memory block in the tracemalloc module.
6+
Return 0 on success, return -1 on error (failed to allocate memory to store
7+
the trace).
8+
9+
Return -2 if tracemalloc is disabled.
10+
11+
If memory block is already tracked, update the existing trace. */
12+
PyAPI_FUNC(int) PyTraceMalloc_Track(
13+
unsigned int domain,
14+
uintptr_t ptr,
15+
size_t size);
16+
17+
/* Update the Python traceback of an object.
18+
This function can be used when a memory block is reused from a free list. */
19+
PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
20+
21+
/* Untrack an allocated memory block in the tracemalloc module.
22+
Do nothing if the block was not tracked.
23+
24+
Return -2 if tracemalloc is disabled, otherwise return 0. */
25+
PyAPI_FUNC(int) PyTraceMalloc_Untrack(
26+
unsigned int domain,
27+
uintptr_t ptr);
28+
29+
/* Get the traceback where a memory block was allocated.
30+
31+
Return a tuple of (filename: str, lineno: int) tuples.
32+
33+
Return None if the tracemalloc module is disabled or if the memory block
34+
is not tracked by tracemalloc.
35+
36+
Raise an exception and return NULL on error. */
37+
PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
38+
unsigned int domain,
39+
uintptr_t ptr);
40+
#endif
41+
42+
#endif /* !Py_TRACEMALLOC_H */

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ PYTHON_HEADERS= \
10171017
$(srcdir)/Include/symtable.h \
10181018
$(srcdir)/Include/sysmodule.h \
10191019
$(srcdir)/Include/traceback.h \
1020+
$(srcdir)/Include/tracemalloc.h \
10201021
$(srcdir)/Include/tupleobject.h \
10211022
$(srcdir)/Include/ucnhash.h \
10221023
$(srcdir)/Include/unicodeobject.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
<ClInclude Include="..\Include\sysmodule.h" />
178178
<ClInclude Include="..\Include\token.h" />
179179
<ClInclude Include="..\Include\traceback.h" />
180+
<ClInclude Include="..\Include\tracemalloc.h" />
180181
<ClInclude Include="..\Include\tupleobject.h" />
181182
<ClInclude Include="..\Include\ucnhash.h" />
182183
<ClInclude Include="..\Include\unicodeobject.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@
321321
<ClInclude Include="..\Include\traceback.h">
322322
<Filter>Include</Filter>
323323
</ClInclude>
324+
<ClInclude Include="..\Include\tracemalloc.h">
325+
<Filter>Include</Filter>
326+
</ClInclude>
324327
<ClInclude Include="..\Include\tupleobject.h">
325328
<Filter>Include</Filter>
326329
</ClInclude>

0 commit comments

Comments
 (0)
0