| 1 | /* The PyObject_ memory family: high-level object memory interfaces.
|
|---|
| 2 | See pymem.h for the low-level PyMem_ family.
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | #ifndef Py_OBJIMPL_H
|
|---|
| 6 | #define Py_OBJIMPL_H
|
|---|
| 7 |
|
|---|
| 8 | #include "pymem.h"
|
|---|
| 9 |
|
|---|
| 10 | #ifdef __cplusplus
|
|---|
| 11 | extern "C" {
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 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 |
|
|---|
| 26 | /*
|
|---|
| 27 | Functions and macros for modules that implement new object types.
|
|---|
| 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
|
|---|
|
|---|