Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b078f9

Browse files
committed
Moving pymalloc along.
As threatened, PyMem_{Free, FREE} also invoke the object deallocator now when pymalloc is enabled (well, it does when pymalloc isn't enabled too, but in that case "the object deallocator" is plain free()). This is maximally backward-compatible, but it leaves a bitter aftertaste. Also massive reworking of comments.
1 parent fa8efab commit 8b078f9

2 files changed

Lines changed: 131 additions & 112 deletions

File tree

Include/objimpl.h

Lines changed: 89 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* The PyObject_ memory family: high-level object memory interfaces.
2+
See pymem.h for the low-level PyMem_ family.
3+
*/
14

25
#ifndef Py_OBJIMPL_H
36
#define Py_OBJIMPL_H
@@ -8,82 +11,89 @@
811
extern "C" {
912
#endif
1013

14+
/* BEWARE:
15+
16+
Each interface exports both functions and macros. Extension modules should
17+
use the functions, to ensure binary compatibility across Python versions.
18+
Because the Python implementation is free to change internal details, and
19+
the macros may (or may not) expose details for speed, if you do use the
20+
macros you must recompile your extensions with each Python release.
21+
22+
Never mix calls to PyObject_ memory functions with calls to the platform
23+
malloc/realloc/ calloc/free, or with calls to PyMem_.
24+
*/
25+
1126
/*
1227
Functions and macros for modules that implement new object types.
13-
You must first include "object.h".
14-
15-
- PyObject_New(type, typeobj) allocates memory for a new object of
16-
the given type; here 'type' must be the C structure type used to
17-
represent the object and 'typeobj' the address of the corresponding
18-
type object. Reference count and type pointer are filled in; the
19-
rest of the bytes of the object are *undefined*! The resulting
20-
expression type is 'type *'. The size of the object is determined
21-
by the tp_basicsize field of the type object.
22-
23-
- PyObject_NewVar(type, typeobj, n) is similar but allocates a
24-
variable-size object with n extra items. The size is computed as
25-
tp_basicsize plus n * tp_itemsize. This fills in the ob_size field
26-
as well.
27-
28-
- PyObject_Del(op) releases the memory allocated for an object. It
29-
does not run a destructor -- it only frees the memory. PyObject_Free
30-
is identical.
31-
32-
- PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
33-
similar to PyObject_{New, NewVar} except that they don't allocate
34-
the memory needed for an object. Instead of the 'type' parameter,
35-
they accept the pointer of a new object (allocated by an arbitrary
36-
allocator) and initialize its object header fields.
37-
38-
Note that objects created with PyObject_{New, NewVar} are allocated
39-
using the specialized Python allocator (implemented in obmalloc.c), if
40-
WITH_PYMALLOC is enabled. In addition, a special debugging allocator
41-
is used if PYMALLOC_DEBUG is also #defined.
42-
43-
In case a specific form of memory management is needed, implying that
44-
the objects would not reside in the Python heap (for example standard
45-
malloc heap(s) are mandatory, use of shared memory, C++ local storage
46-
or operator new), you must first allocate the object with your custom
47-
allocator, then pass its pointer to PyObject_{Init, InitVar} for
48-
filling in its Python-specific fields: reference count, type pointer,
49-
possibly others. You should be aware that Python has very limited
50-
control over these objects because they don't cooperate with the
51-
Python memory manager. Such objects may not be eligible for automatic
52-
garbage collection and you have to make sure that they are released
53-
accordingly whenever their destructor gets called (cf. the specific
28+
29+
- PyObject_New(type, typeobj) allocates memory for a new object of the given
30+
type, and initializes part of it. 'type' must be the C structure type used
31+
to represent the object, and 'typeobj' the address of the corresponding
32+
type object. Reference count and type pointer are filled in; the rest of
33+
the bytes of the object are *undefined*! The resulting expression type is
34+
'type *'. The size of the object is determined by the tp_basicsize field
35+
of the type object.
36+
37+
- PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
38+
object with room for n items. In addition to the refcount and type pointer
39+
fields, this also fills in the ob_size field.
40+
41+
- PyObject_Del(op) releases the memory allocated for an object. It does not
42+
run a destructor -- it only frees the memory. PyObject_Free is identical.
43+
44+
- PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
45+
allocate memory. Instead of a 'type' parameter, they take a pointer to a
46+
new object (allocated by an arbitrary allocator), and initialize its object
47+
header fields.
48+
49+
Note that objects created with PyObject_{New, NewVar} are allocated using the
50+
specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
51+
enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
52+
is also #defined.
53+
54+
In case a specific form of memory management is needed (for example, if you
55+
must use the platform malloc heap(s), or shared memory, or C++ local storage or
56+
operator new), you must first allocate the object with your custom allocator,
57+
then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
58+
specific fields: reference count, type pointer, possibly others. You should
59+
be aware that Python no control over these objects because they don't
60+
cooperate with the Python memory manager. Such objects may not be eligible
61+
for automatic garbage collection and you have to make sure that they are
62+
released accordingly whenever their destructor gets called (cf. the specific
5463
form of memory management you're using).
5564
56-
Unless you have specific memory management requirements, it is
57-
recommended to use PyObject_{New, NewVar, Del}. */
65+
Unless you have specific memory management requirements, use
66+
PyObject_{New, NewVar, Del}.
67+
*/
5868

5969
/*
6070
* Raw object memory interface
6171
* ===========================
6272
*/
6373

64-
/* The use of this API should be avoided, unless a builtin object
65-
constructor inlines PyObject_{New, NewVar}, either because the
66-
latter functions cannot allocate the exact amount of needed memory,
67-
either for speed. This situation is exceptional, but occurs for
68-
some object constructors (PyBuffer_New, PyList_New...). Inlining
69-
PyObject_{New, NewVar} for objects that are supposed to belong to
70-
the Python heap is discouraged. If you really have to, make sure
71-
the object is initialized with PyObject_{Init, InitVar}. Do *not*
72-
inline PyObject_{Init, InitVar} for user-extension types or you
73-
might seriously interfere with Python's memory management. */
74-
75-
/* Functions */
76-
7774
/* Functions to call the same malloc/realloc/free as used by Python's
7875
object allocator. If WITH_PYMALLOC is enabled, these may differ from
7976
the platform malloc/realloc/free. The Python object allocator is
8077
designed for fast, cache-conscious allocation of many "small" objects,
81-
with low hidden memory overhead. PyObject_Malloc(0) returns a unique
82-
non-NULL pointer if possible. PyObject_Realloc(NULL, n) acts like
83-
PyObject_Malloc(n). PyObject_Realloc(p != NULL, 0) does not return
84-
NULL or free the memory at p. Returned pointers must be checked for
85-
NULL explicitly; no action is performed on failure other than to return
86-
NULL. */
78+
and with low hidden memory overhead.
79+
80+
PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
81+
82+
PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
83+
PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
84+
at p.
85+
86+
Returned pointers must be checked for NULL explicitly; no action is
87+
performed on failure other than to return NULL (no warning it printed, no
88+
exception is set, etc).
89+
90+
For allocating objects, use PyObject_{New, NewVar} instead whenever
91+
possible. The PyObject_{Malloc, Realloc, Free} family is exposed
92+
so that you can exploit Python's small-block allocator for non-object
93+
uses. If you must use these routines to allocate object memory, make sure
94+
the object gets initialized via PyObject_{Init, InitVar} after obtaining
95+
the raw memory.
96+
*/
8797
extern DL_IMPORT(void *) PyObject_Malloc(size_t);
8898
extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
8999
extern DL_IMPORT(void) PyObject_Free(void *);
@@ -114,14 +124,19 @@ DL_IMPORT(void) _PyObject_DebugMallocStats(void);
114124
#else /* ! WITH_PYMALLOC */
115125
#define PyObject_MALLOC PyMem_MALLOC
116126
#define PyObject_REALLOC PyMem_REALLOC
117-
#define PyObject_FREE PyMem_FREE
127+
/* This is an odd one! For backward compatability with old extensions, the
128+
PyMem "release memory" functions have to invoke the object allocator's
129+
free() function. When pymalloc isn't enabled, that leaves us using
130+
the platform free(). */
131+
#define PyObject_FREE free
132+
118133
#endif /* WITH_PYMALLOC */
119134

120-
#define PyObject_Del PyObject_Free
121-
#define PyObject_DEL PyObject_FREE
135+
#define PyObject_Del PyObject_Free
136+
#define PyObject_DEL PyObject_FREE
122137

123138
/* for source compatibility with 2.2 */
124-
#define _PyObject_Del PyObject_Free
139+
#define _PyObject_Del PyObject_Free
125140

126141
/*
127142
* Generic object allocator interface
@@ -196,9 +211,7 @@ extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
196211
if (op == NULL)
197212
return PyErr_NoMemory();
198213
199-
op = PyObject_Init(op, &YourTypeStruct);
200-
if (op == NULL)
201-
return NULL;
214+
PyObject_Init(op, &YourTypeStruct);
202215
203216
op->ob_field = value;
204217
...
@@ -207,7 +220,8 @@ extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
207220
208221
Note that in C++, the use of the new operator usually implies that
209222
the 1st step is performed automatically for you, so in a C++ class
210-
constructor you would start directly with PyObject_Init/InitVar. */
223+
constructor you would start directly with PyObject_Init/InitVar
224+
*/
211225

212226
/*
213227
* Garbage Collection Support
@@ -234,7 +248,7 @@ extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
234248

235249
#ifdef WITH_CYCLE_GC
236250

237-
/* GC information is stored BEFORE the object structure */
251+
/* GC information is stored BEFORE the object structure. */
238252
typedef union _gc_head {
239253
struct {
240254
union _gc_head *gc_next; /* not NULL if object is tracked */
@@ -283,10 +297,10 @@ extern DL_IMPORT(void) PyObject_GC_Del(void *);
283297

284298
#else /* !WITH_CYCLE_GC */
285299

286-
#define _PyObject_GC_Malloc PyObject_Malloc
287-
#define PyObject_GC_New PyObject_New
288-
#define PyObject_GC_NewVar PyObject_NewVar
289-
#define PyObject_GC_Del PyObject_Del
300+
#define _PyObject_GC_Malloc PyObject_Malloc
301+
#define PyObject_GC_New PyObject_New
302+
#define PyObject_GC_NewVar PyObject_NewVar
303+
#define PyObject_GC_Del PyObject_Del
290304
#define _PyObject_GC_TRACK(op)
291305
#define _PyObject_GC_UNTRACK(op)
292306
#define PyObject_GC_Track(op)

Include/pymem.h

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
2-
/* Lowest-level memory allocation interface */
1+
/* The PyMem_ family: low-level memory allocation interfaces.
2+
See objimpl.h for the PyObject_ memory family.
3+
*/
34

45
#ifndef Py_PYMEM_H
56
#define Py_PYMEM_H
@@ -12,37 +13,39 @@ extern "C" {
1213

1314
/* BEWARE:
1415
15-
Each interface exports both functions and macros. Extension modules
16-
should normally use the functions for ensuring binary compatibility
17-
of the user's code across Python versions. Subsequently, if the
18-
Python runtime switches to its own malloc (different from standard
19-
malloc), no recompilation is required for the extensions.
20-
21-
The macro versions are free to trade compatibility for speed, although
22-
there's no guarantee they're ever faster. Extensions shouldn't use the
23-
macro versions, as they don't gurantee binary compatibility across
24-
releases.
25-
26-
Do not mix calls to PyMem_xyz with calls to platform
27-
malloc/realloc/calloc/free. */
16+
Each interface exports both functions and macros. Extension modules should
17+
use the functions, to ensure binary compatibility across Python versions.
18+
Because the Python implementation is free to change internal details, and
19+
the macros may (or may not) expose details for speed, if you do use the
20+
macros you must recompile your extensions with each Python release.
21+
22+
Never mix calls to PyMem_ with calls to the platform malloc/realloc/
23+
calloc/free. For example, on Windows different DLLs may end up using
24+
different heaps, and if you use PyMem_Malloc you'll get the memory from the
25+
heap used by the Python DLL; it could be a disaster if you free()'ed that
26+
directly in your own extension. Using PyMem_Free instead ensures Python
27+
can return the memory to the proper heap. As another example, in
28+
PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
29+
memory functions in special debugging wrappers that add additional
30+
debugging info to dynamic memory blocks. The system routines have no idea
31+
what to do with that stuff, and the Python wrappers have no idea what to do
32+
with raw blocks obtained directly by the system routines then.
33+
*/
2834

2935
/*
3036
* Raw memory interface
3137
* ====================
3238
*/
3339

34-
/* Functions */
40+
/* Functions
3541
36-
/* Functions supplying platform-independent semantics for malloc/realloc/
37-
free; useful if you need to be sure you're using the same memory
38-
allocator as Python (this can be especially important on Windows, if
39-
you need to make sure you're using the same MS malloc/free, and out of
40-
the same heap, as the main Python DLL uses).
41-
These functions make sure that allocating 0 bytes returns a distinct
42+
Functions supplying platform-independent semantics for malloc/realloc/
43+
free. These functions make sure that allocating 0 bytes returns a distinct
4244
non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
4345
may be returned), even if the platform malloc and realloc don't.
4446
Returned pointers must be checked for NULL explicitly. No action is
45-
performed on failure (no exception is set, no warning is printed, etc).` */
47+
performed on failure (no exception is set, no warning is printed, etc).
48+
*/
4649

4750
extern DL_IMPORT(void *) PyMem_Malloc(size_t);
4851
extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
@@ -56,7 +59,6 @@ extern DL_IMPORT(void) PyMem_Free(void *);
5659
/* Redirect all memory operations to Python's debugging allocator. */
5760
#define PyMem_MALLOC PyObject_MALLOC
5861
#define PyMem_REALLOC PyObject_REALLOC
59-
#define PyMem_FREE PyObject_FREE
6062

6163
#else /* ! PYMALLOC_DEBUG */
6264

@@ -65,41 +67,44 @@ extern DL_IMPORT(void) PyMem_Free(void *);
6567
#else
6668
#define PyMem_MALLOC malloc
6769
#endif
70+
6871
/* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to
6972
do with whether platform realloc(non-NULL, 0) normally frees the memory
7073
or returns NULL. Rather than introduce yet another config variation,
7174
just make a realloc to 0 bytes act as if to 1 instead. */
7275
#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
7376

74-
#define PyMem_FREE free
7577
#endif /* PYMALLOC_DEBUG */
7678

79+
/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
80+
PyMem_{Del, DEL} and PyMem_{Free, FREE}, the PyMem "release memory"
81+
functions have to be redirected to the object deallocator. */
82+
#define PyMem_FREE PyObject_FREE
83+
7784
/*
7885
* Type-oriented memory interface
7986
* ==============================
8087
*
8188
* These are carried along for historical reasons. There's rarely a good
82-
* reason to use them anymore.
89+
* reason to use them anymore (you can just as easily do the multiply and
90+
* cast yourself).
8391
*/
8492

85-
/* Functions */
8693
#define PyMem_New(type, n) \
8794
( (type *) PyMem_Malloc((n) * sizeof(type)) )
88-
#define PyMem_Resize(p, type, n) \
89-
( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
90-
91-
/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
92-
PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter
93-
have to be redirected to the object allocator. */
94-
#define PyMem_Del PyObject_Free
95-
96-
/* Macros */
9795
#define PyMem_NEW(type, n) \
9896
( (type *) PyMem_MALLOC((n) * sizeof(type)) )
97+
98+
#define PyMem_Resize(p, type, n) \
99+
( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
99100
#define PyMem_RESIZE(p, type, n) \
100101
( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
101102

102-
#define PyMem_DEL PyObject_FREE
103+
/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
104+
PyMem_{Del, DEL} and PyMem_{Free, FREE}, the PyMem "release memory"
105+
functions have to be redirected to the object deallocator. */
106+
#define PyMem_Del PyObject_Free
107+
#define PyMem_DEL PyObject_FREE
103108

104109
#ifdef __cplusplus
105110
}

0 commit comments

Comments
 (0)