Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
12 KiB

  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 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);
  78. PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t);
  79. PyAPI_FUNC(void) PyObject_Free(void *);
  80. /* Macros */
  81. #ifdef WITH_PYMALLOC
  82. #ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */
  83. PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
  84. PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
  85. PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
  86. PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
  87. PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
  88. PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
  89. #define PyObject_MALLOC _PyObject_DebugMalloc
  90. #define PyObject_Malloc _PyObject_DebugMalloc
  91. #define PyObject_REALLOC _PyObject_DebugRealloc
  92. #define PyObject_Realloc _PyObject_DebugRealloc
  93. #define PyObject_FREE _PyObject_DebugFree
  94. #define PyObject_Free _PyObject_DebugFree
  95. #else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
  96. #define PyObject_MALLOC PyObject_Malloc
  97. #define PyObject_REALLOC PyObject_Realloc
  98. #define PyObject_FREE PyObject_Free
  99. #endif
  100. #else /* ! WITH_PYMALLOC */
  101. #define PyObject_MALLOC PyMem_MALLOC
  102. #define PyObject_REALLOC PyMem_REALLOC
  103. #define PyObject_FREE PyMem_FREE
  104. #endif /* WITH_PYMALLOC */
  105. #define PyObject_Del PyObject_Free
  106. #define PyObject_DEL PyObject_FREE
  107. /* for source compatibility with 2.2 */
  108. #define _PyObject_Del PyObject_Free
  109. /*
  110. * Generic object allocator interface
  111. * ==================================
  112. */
  113. /* Functions */
  114. PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
  115. PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
  116. PyTypeObject *, Py_ssize_t);
  117. PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
  118. PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
  119. #define PyObject_New(type, typeobj) \
  120. ( (type *) _PyObject_New(typeobj) )
  121. #define PyObject_NewVar(type, typeobj, n) \
  122. ( (type *) _PyObject_NewVar((typeobj), (n)) )
  123. /* Macros trading binary compatibility for speed. See also pymem.h.
  124. Note that these macros expect non-NULL object pointers.*/
  125. #define PyObject_INIT(op, typeobj) \
  126. ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
  127. #define PyObject_INIT_VAR(op, typeobj, size) \
  128. ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
  129. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  130. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  131. vrbl-size object with nitems items, exclusive of gc overhead (if any). The
  132. value is rounded up to the closest multiple of sizeof(void *), in order to
  133. ensure that pointer fields at the end of the object are correctly aligned
  134. for the platform (this is of special importance for subclasses of, e.g.,
  135. str or long, so that pointers can be stored after the embedded data).
  136. Note that there's no memory wastage in doing this, as malloc has to
  137. return (at worst) pointer-aligned memory anyway.
  138. */
  139. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  140. # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  141. #endif
  142. #define _PyObject_VAR_SIZE(typeobj, nitems) \
  143. (size_t) \
  144. ( ( (typeobj)->tp_basicsize + \
  145. (nitems)*(typeobj)->tp_itemsize + \
  146. (SIZEOF_VOID_P - 1) \
  147. ) & ~(SIZEOF_VOID_P - 1) \
  148. )
  149. #define PyObject_NEW(type, typeobj) \
  150. ( (type *) PyObject_Init( \
  151. (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
  152. #define PyObject_NEW_VAR(type, typeobj, n) \
  153. ( (type *) PyObject_InitVar( \
  154. (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
  155. (typeobj), (n)) )
  156. /* This example code implements an object constructor with a custom
  157. allocator, where PyObject_New is inlined, and shows the important
  158. distinction between two steps (at least):
  159. 1) the actual allocation of the object storage;
  160. 2) the initialization of the Python specific fields
  161. in this storage with PyObject_{Init, InitVar}.
  162. PyObject *
  163. YourObject_New(...)
  164. {
  165. PyObject *op;
  166. op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  167. if (op == NULL)
  168. return PyErr_NoMemory();
  169. PyObject_Init(op, &YourTypeStruct);
  170. op->ob_field = value;
  171. ...
  172. return op;
  173. }
  174. Note that in C++, the use of the new operator usually implies that
  175. the 1st step is performed automatically for you, so in a C++ class
  176. constructor you would start directly with PyObject_Init/InitVar
  177. */
  178. /*
  179. * Garbage Collection Support
  180. * ==========================
  181. */
  182. /* C equivalent of gc.collect(). */
  183. PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
  184. /* Test if a type has a GC head */
  185. #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
  186. /* Test if an object has a GC head */
  187. #define PyObject_IS_GC(o) (PyType_IS_GC((o)->ob_type) && \
  188. ((o)->ob_type->tp_is_gc == NULL || (o)->ob_type->tp_is_gc(o)))
  189. PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
  190. #define PyObject_GC_Resize(type, op, n) \
  191. ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
  192. /* for source compatibility with 2.2 */
  193. #define _PyObject_GC_Del PyObject_GC_Del
  194. /* GC information is stored BEFORE the object structure. */
  195. typedef union _gc_head {
  196. struct {
  197. union _gc_head *gc_next;
  198. union _gc_head *gc_prev;
  199. Py_ssize_t gc_refs;
  200. } gc;
  201. long double dummy; /* force worst-case alignment */
  202. } PyGC_Head;
  203. extern PyGC_Head *_PyGC_generation0;
  204. #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
  205. #define _PyGC_REFS_UNTRACKED (-2)
  206. #define _PyGC_REFS_REACHABLE (-3)
  207. #define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
  208. /* Tell the GC to track this object. NB: While the object is tracked the
  209. * collector it must be safe to call the ob_traverse method. */
  210. #define _PyObject_GC_TRACK(o) do { \
  211. PyGC_Head *g = _Py_AS_GC(o); \
  212. if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \
  213. Py_FatalError("GC object already tracked"); \
  214. g->gc.gc_refs = _PyGC_REFS_REACHABLE; \
  215. g->gc.gc_next = _PyGC_generation0; \
  216. g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
  217. g->gc.gc_prev->gc.gc_next = g; \
  218. _PyGC_generation0->gc.gc_prev = g; \
  219. } while (0);
  220. /* Tell the GC to stop tracking this object.
  221. * gc_next doesn't need to be set to NULL, but doing so is a good
  222. * way to provoke memory errors if calling code is confused.
  223. */
  224. #define _PyObject_GC_UNTRACK(o) do { \
  225. PyGC_Head *g = _Py_AS_GC(o); \
  226. assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \
  227. g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \
  228. g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
  229. g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
  230. g->gc.gc_next = NULL; \
  231. } while (0);
  232. PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
  233. PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
  234. PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
  235. PyAPI_FUNC(void) PyObject_GC_Track(void *);
  236. PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
  237. PyAPI_FUNC(void) PyObject_GC_Del(void *);
  238. #define PyObject_GC_New(type, typeobj) \
  239. ( (type *) _PyObject_GC_New(typeobj) )
  240. #define PyObject_GC_NewVar(type, typeobj, n) \
  241. ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
  242. /* Utility macro to help write tp_traverse functions.
  243. * To use this macro, the tp_traverse function must name its arguments
  244. * "visit" and "arg". This is intended to keep tp_traverse functions
  245. * looking as much alike as possible.
  246. */
  247. #define Py_VISIT(op) \
  248. do { \
  249. if (op) { \
  250. int vret = visit((PyObject *)(op), arg); \
  251. if (vret) \
  252. return vret; \
  253. } \
  254. } while (0)
  255. /* This is here for the sake of backwards compatibility. Extensions that
  256. * use the old GC API will still compile but the objects will not be
  257. * tracked by the GC. */
  258. #define PyGC_HEAD_SIZE 0
  259. #define PyObject_GC_Init(op)
  260. #define PyObject_GC_Fini(op)
  261. #define PyObject_AS_GC(op) (op)
  262. #define PyObject_FROM_GC(op) (op)
  263. /* Test if a type supports weak references */
  264. #define PyType_SUPPORTS_WEAKREFS(t) \
  265. (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
  266. && ((t)->tp_weaklistoffset > 0))
  267. #define PyObject_GET_WEAKREFS_LISTPTR(o) \
  268. ((PyObject **) (((char *) (o)) + (o)->ob_type->tp_weaklistoffset))
  269. #ifdef __cplusplus
  270. }
  271. #endif
  272. #endif /* !Py_OBJIMPL_H */