objimpl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* The PyObject_ memory family: high-level object memory interfaces.
  2. See pymem.h for the low-level PyMem_ family.
  3. */
  4. #ifndef Py_OBJIMPL_H
  5. #define Py_OBJIMPL_H
  6. #include "pymem.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /* BEWARE:
  11. Each interface exports both functions and macros. Extension modules should
  12. use the functions, to ensure binary compatibility across Python versions.
  13. Because the Python implementation is free to change internal details, and
  14. the macros may (or may not) expose details for speed, if you do use the
  15. macros you must recompile your extensions with each Python release.
  16. Never mix calls to PyObject_ memory functions with calls to the platform
  17. malloc/realloc/ calloc/free, or with calls to PyMem_.
  18. */
  19. /*
  20. Functions and macros for modules that implement new object types.
  21. - PyObject_New(type, typeobj) allocates memory for a new object of the given
  22. type, and initializes part of it. 'type' must be the C structure type used
  23. to represent the object, and 'typeobj' the address of the corresponding
  24. type object. Reference count and type pointer are filled in; the rest of
  25. the bytes of the object are *undefined*! The resulting expression type is
  26. 'type *'. The size of the object is determined by the tp_basicsize field
  27. of the type object.
  28. - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
  29. object with room for n items. In addition to the refcount and type pointer
  30. fields, this also fills in the ob_size field.
  31. - PyObject_Del(op) releases the memory allocated for an object. It does not
  32. run a destructor -- it only frees the memory. PyObject_Free is identical.
  33. - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
  34. allocate memory. Instead of a 'type' parameter, they take a pointer to a
  35. new object (allocated by an arbitrary allocator), and initialize its object
  36. header fields.
  37. Note that objects created with PyObject_{New, NewVar} are allocated using the
  38. specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
  39. enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
  40. is also #defined.
  41. In case a specific form of memory management is needed (for example, if you
  42. must use the platform malloc heap(s), or shared memory, or C++ local storage or
  43. operator new), you must first allocate the object with your custom allocator,
  44. then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
  45. specific fields: reference count, type pointer, possibly others. You should
  46. be aware that Python has no control over these objects because they don't
  47. cooperate with the Python memory manager. Such objects may not be eligible
  48. for automatic garbage collection and you have to make sure that they are
  49. released accordingly whenever their destructor gets called (cf. the specific
  50. form of memory management you're using).
  51. Unless you have specific memory management requirements, use
  52. PyObject_{New, NewVar, Del}.
  53. */
  54. /*
  55. * Raw object memory interface
  56. * ===========================
  57. */
  58. /* Functions to call the same malloc/realloc/free as used by Python's
  59. object allocator. If WITH_PYMALLOC is enabled, these may differ from
  60. the platform malloc/realloc/free. The Python object allocator is
  61. designed for fast, cache-conscious allocation of many "small" objects,
  62. and with low hidden memory overhead.
  63. PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
  64. PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
  65. PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
  66. at p.
  67. Returned pointers must be checked for NULL explicitly; no action is
  68. performed on failure other than to return NULL (no warning it printed, no
  69. exception is set, etc).
  70. For allocating objects, use PyObject_{New, NewVar} instead whenever
  71. possible. The PyObject_{Malloc, Realloc, Free} family is exposed
  72. so that you can exploit Python's small-block allocator for non-object
  73. uses. If you must use these routines to allocate object memory, make sure
  74. the object gets initialized via PyObject_{Init, InitVar} after obtaining
  75. the raw memory.
  76. */
  77. PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
  78. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  79. PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
  80. #endif
  81. PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
  82. PyAPI_FUNC(void) PyObject_Free(void *ptr);
  83. /* Macros */
  84. #define PyObject_MALLOC PyObject_Malloc
  85. #define PyObject_REALLOC PyObject_Realloc
  86. #define PyObject_FREE PyObject_Free
  87. #define PyObject_Del PyObject_Free
  88. #define PyObject_DEL PyObject_Free
  89. /*
  90. * Generic object allocator interface
  91. * ==================================
  92. */
  93. /* Functions */
  94. PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
  95. PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
  96. PyTypeObject *, Py_ssize_t);
  97. PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
  98. PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
  99. #define PyObject_New(type, typeobj) \
  100. ( (type *) _PyObject_New(typeobj) )
  101. #define PyObject_NewVar(type, typeobj, n) \
  102. ( (type *) _PyObject_NewVar((typeobj), (n)) )
  103. /* Inline functions trading binary compatibility for speed:
  104. PyObject_INIT() is the fast version of PyObject_Init(), and
  105. PyObject_INIT_VAR() is the fast version of PyObject_InitVar.
  106. See also pymem.h.
  107. These inline functions expect non-NULL object pointers. */
  108. static inline PyObject*
  109. _PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
  110. {
  111. assert(op != NULL);
  112. Py_TYPE(op) = typeobj;
  113. if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
  114. Py_INCREF(typeobj);
  115. }
  116. _Py_NewReference(op);
  117. return op;
  118. }
  119. #define PyObject_INIT(op, typeobj) \
  120. _PyObject_INIT(_PyObject_CAST(op), (typeobj))
  121. static inline PyVarObject*
  122. _PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
  123. {
  124. assert(op != NULL);
  125. Py_SIZE(op) = size;
  126. PyObject_INIT((PyObject *)op, typeobj);
  127. return op;
  128. }
  129. #define PyObject_INIT_VAR(op, typeobj, size) \
  130. _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
  131. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  132. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  133. vrbl-size object with nitems items, exclusive of gc overhead (if any). The
  134. value is rounded up to the closest multiple of sizeof(void *), in order to
  135. ensure that pointer fields at the end of the object are correctly aligned
  136. for the platform (this is of special importance for subclasses of, e.g.,
  137. str or int, so that pointers can be stored after the embedded data).
  138. Note that there's no memory wastage in doing this, as malloc has to
  139. return (at worst) pointer-aligned memory anyway.
  140. */
  141. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  142. # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  143. #endif
  144. #define _PyObject_VAR_SIZE(typeobj, nitems) \
  145. _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
  146. (nitems)*(typeobj)->tp_itemsize, \
  147. SIZEOF_VOID_P)
  148. #define PyObject_NEW(type, typeobj) \
  149. ( (type *) PyObject_Init( \
  150. (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
  151. #define PyObject_NEW_VAR(type, typeobj, n) \
  152. ( (type *) PyObject_InitVar( \
  153. (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
  154. (typeobj), (n)) )
  155. /* This example code implements an object constructor with a custom
  156. allocator, where PyObject_New is inlined, and shows the important
  157. distinction between two steps (at least):
  158. 1) the actual allocation of the object storage;
  159. 2) the initialization of the Python specific fields
  160. in this storage with PyObject_{Init, InitVar}.
  161. PyObject *
  162. YourObject_New(...)
  163. {
  164. PyObject *op;
  165. op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  166. if (op == NULL)
  167. return PyErr_NoMemory();
  168. PyObject_Init(op, &YourTypeStruct);
  169. op->ob_field = value;
  170. ...
  171. return op;
  172. }
  173. Note that in C++, the use of the new operator usually implies that
  174. the 1st step is performed automatically for you, so in a C++ class
  175. constructor you would start directly with PyObject_Init/InitVar
  176. */
  177. /*
  178. * Garbage Collection Support
  179. * ==========================
  180. */
  181. /* C equivalent of gc.collect() which ignores the state of gc.enabled. */
  182. PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
  183. /* Test if a type has a GC head */
  184. #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
  185. PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
  186. #define PyObject_GC_Resize(type, op, n) \
  187. ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
  188. PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
  189. PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
  190. /* Tell the GC to track this object.
  191. *
  192. * See also private _PyObject_GC_TRACK() macro. */
  193. PyAPI_FUNC(void) PyObject_GC_Track(void *);
  194. /* Tell the GC to stop tracking this object.
  195. *
  196. * See also private _PyObject_GC_UNTRACK() macro. */
  197. PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
  198. PyAPI_FUNC(void) PyObject_GC_Del(void *);
  199. #define PyObject_GC_New(type, typeobj) \
  200. ( (type *) _PyObject_GC_New(typeobj) )
  201. #define PyObject_GC_NewVar(type, typeobj, n) \
  202. ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
  203. /* Utility macro to help write tp_traverse functions.
  204. * To use this macro, the tp_traverse function must name its arguments
  205. * "visit" and "arg". This is intended to keep tp_traverse functions
  206. * looking as much alike as possible.
  207. */
  208. #define Py_VISIT(op) \
  209. do { \
  210. if (op) { \
  211. int vret = visit(_PyObject_CAST(op), arg); \
  212. if (vret) \
  213. return vret; \
  214. } \
  215. } while (0)
  216. #ifndef Py_LIMITED_API
  217. # define Py_CPYTHON_OBJIMPL_H
  218. # include "cpython/objimpl.h"
  219. # undef Py_CPYTHON_OBJIMPL_H
  220. #endif
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. #endif /* !Py_OBJIMPL_H */