annotate DEPENDENCIES/mingw32/Python27/include/objimpl.h @ 118:770eb830ec19 emscripten

Typo fix
author Chris Cannam
date Wed, 18 May 2016 16:14:08 +0100
parents 2a2c65a20a8b
children
rev   line source
Chris@87 1 /* The PyObject_ memory family: high-level object memory interfaces.
Chris@87 2 See pymem.h for the low-level PyMem_ family.
Chris@87 3 */
Chris@87 4
Chris@87 5 #ifndef Py_OBJIMPL_H
Chris@87 6 #define Py_OBJIMPL_H
Chris@87 7
Chris@87 8 #include "pymem.h"
Chris@87 9
Chris@87 10 #ifdef __cplusplus
Chris@87 11 extern "C" {
Chris@87 12 #endif
Chris@87 13
Chris@87 14 /* BEWARE:
Chris@87 15
Chris@87 16 Each interface exports both functions and macros. Extension modules should
Chris@87 17 use the functions, to ensure binary compatibility across Python versions.
Chris@87 18 Because the Python implementation is free to change internal details, and
Chris@87 19 the macros may (or may not) expose details for speed, if you do use the
Chris@87 20 macros you must recompile your extensions with each Python release.
Chris@87 21
Chris@87 22 Never mix calls to PyObject_ memory functions with calls to the platform
Chris@87 23 malloc/realloc/ calloc/free, or with calls to PyMem_.
Chris@87 24 */
Chris@87 25
Chris@87 26 /*
Chris@87 27 Functions and macros for modules that implement new object types.
Chris@87 28
Chris@87 29 - PyObject_New(type, typeobj) allocates memory for a new object of the given
Chris@87 30 type, and initializes part of it. 'type' must be the C structure type used
Chris@87 31 to represent the object, and 'typeobj' the address of the corresponding
Chris@87 32 type object. Reference count and type pointer are filled in; the rest of
Chris@87 33 the bytes of the object are *undefined*! The resulting expression type is
Chris@87 34 'type *'. The size of the object is determined by the tp_basicsize field
Chris@87 35 of the type object.
Chris@87 36
Chris@87 37 - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
Chris@87 38 object with room for n items. In addition to the refcount and type pointer
Chris@87 39 fields, this also fills in the ob_size field.
Chris@87 40
Chris@87 41 - PyObject_Del(op) releases the memory allocated for an object. It does not
Chris@87 42 run a destructor -- it only frees the memory. PyObject_Free is identical.
Chris@87 43
Chris@87 44 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
Chris@87 45 allocate memory. Instead of a 'type' parameter, they take a pointer to a
Chris@87 46 new object (allocated by an arbitrary allocator), and initialize its object
Chris@87 47 header fields.
Chris@87 48
Chris@87 49 Note that objects created with PyObject_{New, NewVar} are allocated using the
Chris@87 50 specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
Chris@87 51 enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
Chris@87 52 is also #defined.
Chris@87 53
Chris@87 54 In case a specific form of memory management is needed (for example, if you
Chris@87 55 must use the platform malloc heap(s), or shared memory, or C++ local storage or
Chris@87 56 operator new), you must first allocate the object with your custom allocator,
Chris@87 57 then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
Chris@87 58 specific fields: reference count, type pointer, possibly others. You should
Chris@87 59 be aware that Python no control over these objects because they don't
Chris@87 60 cooperate with the Python memory manager. Such objects may not be eligible
Chris@87 61 for automatic garbage collection and you have to make sure that they are
Chris@87 62 released accordingly whenever their destructor gets called (cf. the specific
Chris@87 63 form of memory management you're using).
Chris@87 64
Chris@87 65 Unless you have specific memory management requirements, use
Chris@87 66 PyObject_{New, NewVar, Del}.
Chris@87 67 */
Chris@87 68
Chris@87 69 /*
Chris@87 70 * Raw object memory interface
Chris@87 71 * ===========================
Chris@87 72 */
Chris@87 73
Chris@87 74 /* Functions to call the same malloc/realloc/free as used by Python's
Chris@87 75 object allocator. If WITH_PYMALLOC is enabled, these may differ from
Chris@87 76 the platform malloc/realloc/free. The Python object allocator is
Chris@87 77 designed for fast, cache-conscious allocation of many "small" objects,
Chris@87 78 and with low hidden memory overhead.
Chris@87 79
Chris@87 80 PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
Chris@87 81
Chris@87 82 PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
Chris@87 83 PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
Chris@87 84 at p.
Chris@87 85
Chris@87 86 Returned pointers must be checked for NULL explicitly; no action is
Chris@87 87 performed on failure other than to return NULL (no warning it printed, no
Chris@87 88 exception is set, etc).
Chris@87 89
Chris@87 90 For allocating objects, use PyObject_{New, NewVar} instead whenever
Chris@87 91 possible. The PyObject_{Malloc, Realloc, Free} family is exposed
Chris@87 92 so that you can exploit Python's small-block allocator for non-object
Chris@87 93 uses. If you must use these routines to allocate object memory, make sure
Chris@87 94 the object gets initialized via PyObject_{Init, InitVar} after obtaining
Chris@87 95 the raw memory.
Chris@87 96 */
Chris@87 97 PyAPI_FUNC(void *) PyObject_Malloc(size_t);
Chris@87 98 PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t);
Chris@87 99 PyAPI_FUNC(void) PyObject_Free(void *);
Chris@87 100
Chris@87 101
Chris@87 102 /* Macros */
Chris@87 103 #ifdef WITH_PYMALLOC
Chris@87 104 #ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */
Chris@87 105 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
Chris@87 106 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
Chris@87 107 PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
Chris@87 108 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
Chris@87 109 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
Chris@87 110 PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
Chris@87 111 PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
Chris@87 112 PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
Chris@87 113 PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
Chris@87 114 PyAPI_FUNC(void) _PyObject_DebugCheckAddressApi(char api, const void *p);
Chris@87 115 PyAPI_FUNC(void *) _PyMem_DebugMalloc(size_t nbytes);
Chris@87 116 PyAPI_FUNC(void *) _PyMem_DebugRealloc(void *p, size_t nbytes);
Chris@87 117 PyAPI_FUNC(void) _PyMem_DebugFree(void *p);
Chris@87 118 #define PyObject_MALLOC _PyObject_DebugMalloc
Chris@87 119 #define PyObject_Malloc _PyObject_DebugMalloc
Chris@87 120 #define PyObject_REALLOC _PyObject_DebugRealloc
Chris@87 121 #define PyObject_Realloc _PyObject_DebugRealloc
Chris@87 122 #define PyObject_FREE _PyObject_DebugFree
Chris@87 123 #define PyObject_Free _PyObject_DebugFree
Chris@87 124
Chris@87 125 #else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
Chris@87 126 #define PyObject_MALLOC PyObject_Malloc
Chris@87 127 #define PyObject_REALLOC PyObject_Realloc
Chris@87 128 #define PyObject_FREE PyObject_Free
Chris@87 129 #endif
Chris@87 130
Chris@87 131 #else /* ! WITH_PYMALLOC */
Chris@87 132 #define PyObject_MALLOC PyMem_MALLOC
Chris@87 133 #define PyObject_REALLOC PyMem_REALLOC
Chris@87 134 #define PyObject_FREE PyMem_FREE
Chris@87 135
Chris@87 136 #endif /* WITH_PYMALLOC */
Chris@87 137
Chris@87 138 #define PyObject_Del PyObject_Free
Chris@87 139 #define PyObject_DEL PyObject_FREE
Chris@87 140
Chris@87 141 /* for source compatibility with 2.2 */
Chris@87 142 #define _PyObject_Del PyObject_Free
Chris@87 143
Chris@87 144 /*
Chris@87 145 * Generic object allocator interface
Chris@87 146 * ==================================
Chris@87 147 */
Chris@87 148
Chris@87 149 /* Functions */
Chris@87 150 PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
Chris@87 151 PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
Chris@87 152 PyTypeObject *, Py_ssize_t);
Chris@87 153 PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
Chris@87 154 PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
Chris@87 155
Chris@87 156 #define PyObject_New(type, typeobj) \
Chris@87 157 ( (type *) _PyObject_New(typeobj) )
Chris@87 158 #define PyObject_NewVar(type, typeobj, n) \
Chris@87 159 ( (type *) _PyObject_NewVar((typeobj), (n)) )
Chris@87 160
Chris@87 161 /* Macros trading binary compatibility for speed. See also pymem.h.
Chris@87 162 Note that these macros expect non-NULL object pointers.*/
Chris@87 163 #define PyObject_INIT(op, typeobj) \
Chris@87 164 ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
Chris@87 165 #define PyObject_INIT_VAR(op, typeobj, size) \
Chris@87 166 ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
Chris@87 167
Chris@87 168 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
Chris@87 169
Chris@87 170 /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
Chris@87 171 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
Chris@87 172 value is rounded up to the closest multiple of sizeof(void *), in order to
Chris@87 173 ensure that pointer fields at the end of the object are correctly aligned
Chris@87 174 for the platform (this is of special importance for subclasses of, e.g.,
Chris@87 175 str or long, so that pointers can be stored after the embedded data).
Chris@87 176
Chris@87 177 Note that there's no memory wastage in doing this, as malloc has to
Chris@87 178 return (at worst) pointer-aligned memory anyway.
Chris@87 179 */
Chris@87 180 #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
Chris@87 181 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
Chris@87 182 #endif
Chris@87 183
Chris@87 184 #define _PyObject_VAR_SIZE(typeobj, nitems) \
Chris@87 185 (size_t) \
Chris@87 186 ( ( (typeobj)->tp_basicsize + \
Chris@87 187 (nitems)*(typeobj)->tp_itemsize + \
Chris@87 188 (SIZEOF_VOID_P - 1) \
Chris@87 189 ) & ~(SIZEOF_VOID_P - 1) \
Chris@87 190 )
Chris@87 191
Chris@87 192 #define PyObject_NEW(type, typeobj) \
Chris@87 193 ( (type *) PyObject_Init( \
Chris@87 194 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
Chris@87 195
Chris@87 196 #define PyObject_NEW_VAR(type, typeobj, n) \
Chris@87 197 ( (type *) PyObject_InitVar( \
Chris@87 198 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
Chris@87 199 (typeobj), (n)) )
Chris@87 200
Chris@87 201 /* This example code implements an object constructor with a custom
Chris@87 202 allocator, where PyObject_New is inlined, and shows the important
Chris@87 203 distinction between two steps (at least):
Chris@87 204 1) the actual allocation of the object storage;
Chris@87 205 2) the initialization of the Python specific fields
Chris@87 206 in this storage with PyObject_{Init, InitVar}.
Chris@87 207
Chris@87 208 PyObject *
Chris@87 209 YourObject_New(...)
Chris@87 210 {
Chris@87 211 PyObject *op;
Chris@87 212
Chris@87 213 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
Chris@87 214 if (op == NULL)
Chris@87 215 return PyErr_NoMemory();
Chris@87 216
Chris@87 217 PyObject_Init(op, &YourTypeStruct);
Chris@87 218
Chris@87 219 op->ob_field = value;
Chris@87 220 ...
Chris@87 221 return op;
Chris@87 222 }
Chris@87 223
Chris@87 224 Note that in C++, the use of the new operator usually implies that
Chris@87 225 the 1st step is performed automatically for you, so in a C++ class
Chris@87 226 constructor you would start directly with PyObject_Init/InitVar
Chris@87 227 */
Chris@87 228
Chris@87 229 /*
Chris@87 230 * Garbage Collection Support
Chris@87 231 * ==========================
Chris@87 232 */
Chris@87 233
Chris@87 234 /* C equivalent of gc.collect(). */
Chris@87 235 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
Chris@87 236
Chris@87 237 /* Test if a type has a GC head */
Chris@87 238 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Chris@87 239
Chris@87 240 /* Test if an object has a GC head */
Chris@87 241 #define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \
Chris@87 242 (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
Chris@87 243
Chris@87 244 PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
Chris@87 245 #define PyObject_GC_Resize(type, op, n) \
Chris@87 246 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
Chris@87 247
Chris@87 248 /* for source compatibility with 2.2 */
Chris@87 249 #define _PyObject_GC_Del PyObject_GC_Del
Chris@87 250
Chris@87 251 /* GC information is stored BEFORE the object structure. */
Chris@87 252 typedef union _gc_head {
Chris@87 253 struct {
Chris@87 254 union _gc_head *gc_next;
Chris@87 255 union _gc_head *gc_prev;
Chris@87 256 Py_ssize_t gc_refs;
Chris@87 257 } gc;
Chris@87 258 long double dummy; /* force worst-case alignment */
Chris@87 259 } PyGC_Head;
Chris@87 260
Chris@87 261 extern PyGC_Head *_PyGC_generation0;
Chris@87 262
Chris@87 263 #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
Chris@87 264
Chris@87 265 #define _PyGC_REFS_UNTRACKED (-2)
Chris@87 266 #define _PyGC_REFS_REACHABLE (-3)
Chris@87 267 #define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
Chris@87 268
Chris@87 269 /* Tell the GC to track this object. NB: While the object is tracked the
Chris@87 270 * collector it must be safe to call the ob_traverse method. */
Chris@87 271 #define _PyObject_GC_TRACK(o) do { \
Chris@87 272 PyGC_Head *g = _Py_AS_GC(o); \
Chris@87 273 if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \
Chris@87 274 Py_FatalError("GC object already tracked"); \
Chris@87 275 g->gc.gc_refs = _PyGC_REFS_REACHABLE; \
Chris@87 276 g->gc.gc_next = _PyGC_generation0; \
Chris@87 277 g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
Chris@87 278 g->gc.gc_prev->gc.gc_next = g; \
Chris@87 279 _PyGC_generation0->gc.gc_prev = g; \
Chris@87 280 } while (0);
Chris@87 281
Chris@87 282 /* Tell the GC to stop tracking this object.
Chris@87 283 * gc_next doesn't need to be set to NULL, but doing so is a good
Chris@87 284 * way to provoke memory errors if calling code is confused.
Chris@87 285 */
Chris@87 286 #define _PyObject_GC_UNTRACK(o) do { \
Chris@87 287 PyGC_Head *g = _Py_AS_GC(o); \
Chris@87 288 assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \
Chris@87 289 g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \
Chris@87 290 g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
Chris@87 291 g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
Chris@87 292 g->gc.gc_next = NULL; \
Chris@87 293 } while (0);
Chris@87 294
Chris@87 295 /* True if the object is currently tracked by the GC. */
Chris@87 296 #define _PyObject_GC_IS_TRACKED(o) \
Chris@87 297 ((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED)
Chris@87 298
Chris@87 299 /* True if the object may be tracked by the GC in the future, or already is.
Chris@87 300 This can be useful to implement some optimizations. */
Chris@87 301 #define _PyObject_GC_MAY_BE_TRACKED(obj) \
Chris@87 302 (PyObject_IS_GC(obj) && \
Chris@87 303 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
Chris@87 304
Chris@87 305
Chris@87 306 PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
Chris@87 307 PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
Chris@87 308 PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
Chris@87 309 PyAPI_FUNC(void) PyObject_GC_Track(void *);
Chris@87 310 PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
Chris@87 311 PyAPI_FUNC(void) PyObject_GC_Del(void *);
Chris@87 312
Chris@87 313 #define PyObject_GC_New(type, typeobj) \
Chris@87 314 ( (type *) _PyObject_GC_New(typeobj) )
Chris@87 315 #define PyObject_GC_NewVar(type, typeobj, n) \
Chris@87 316 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
Chris@87 317
Chris@87 318
Chris@87 319 /* Utility macro to help write tp_traverse functions.
Chris@87 320 * To use this macro, the tp_traverse function must name its arguments
Chris@87 321 * "visit" and "arg". This is intended to keep tp_traverse functions
Chris@87 322 * looking as much alike as possible.
Chris@87 323 */
Chris@87 324 #define Py_VISIT(op) \
Chris@87 325 do { \
Chris@87 326 if (op) { \
Chris@87 327 int vret = visit((PyObject *)(op), arg); \
Chris@87 328 if (vret) \
Chris@87 329 return vret; \
Chris@87 330 } \
Chris@87 331 } while (0)
Chris@87 332
Chris@87 333 /* This is here for the sake of backwards compatibility. Extensions that
Chris@87 334 * use the old GC API will still compile but the objects will not be
Chris@87 335 * tracked by the GC. */
Chris@87 336 #define PyGC_HEAD_SIZE 0
Chris@87 337 #define PyObject_GC_Init(op)
Chris@87 338 #define PyObject_GC_Fini(op)
Chris@87 339 #define PyObject_AS_GC(op) (op)
Chris@87 340 #define PyObject_FROM_GC(op) (op)
Chris@87 341
Chris@87 342
Chris@87 343 /* Test if a type supports weak references */
Chris@87 344 #define PyType_SUPPORTS_WEAKREFS(t) \
Chris@87 345 (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
Chris@87 346 && ((t)->tp_weaklistoffset > 0))
Chris@87 347
Chris@87 348 #define PyObject_GET_WEAKREFS_LISTPTR(o) \
Chris@87 349 ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
Chris@87 350
Chris@87 351 #ifdef __cplusplus
Chris@87 352 }
Chris@87 353 #endif
Chris@87 354 #endif /* !Py_OBJIMPL_H */