1 /** 2 Mirror _objimpl.h 3 */ 4 module deimos.python.objimpl; 5 6 import deimos.python.pyport; 7 import deimos.python.object; 8 9 // Python-header-file: Include/objimpl.h: 10 extern(C): 11 12 /// _ 13 void* PyObject_Malloc(size_t); 14 15 version(Python_3_5_Or_Later) { 16 /// _ 17 void* PyObject_Calloc(size_t, size_t); 18 } 19 /// _ 20 void* PyObject_Realloc(void*, size_t); 21 /// _ 22 void PyObject_Free(void*); 23 24 /** 25 Don't allocate memory. Instead of a 'type' parameter, take a pointer to a 26 new object (allocated by an arbitrary allocator), and initialize its object 27 header fields. 28 */ 29 PyObject_BorrowedRef* PyObject_Init(PyObject*, PyTypeObject*); 30 /// ditto 31 Borrowed!PyVarObject* PyObject_InitVar(PyVarObject*, 32 PyTypeObject*, Py_ssize_t); 33 /// _ 34 PyObject* _PyObject_New(PyTypeObject*); 35 /// _ 36 PyVarObject* _PyObject_NewVar(PyTypeObject*, Py_ssize_t); 37 /** 38 Allocates memory for a new object of the given 39 type, and initializes part of it. 'type' must be the C structure type used 40 to represent the object, and 'typeobj' the address of the corresponding 41 type object. Reference count and type pointer are filled in; the rest of 42 the bytes of the object are *undefined*! The resulting expression type is 43 'type *'. The size of the object is determined by the tp_basicsize field 44 of the type object. 45 */ 46 type* PyObject_New(type)(PyTypeObject* o) { 47 return cast(type*)_PyObject_New(o); 48 } 49 /** 50 PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size 51 object with room for n items. In addition to the refcount and type pointer 52 fields, this also fills in the ob_size field. 53 */ 54 type* PyObject_NewVar(type)(PyTypeObject* o, Py_ssize_t n) { 55 return cast(type*)_PyObject_NewVar(o, n); 56 } 57 58 59 /** C equivalent of gc.collect(). */ 60 C_long PyGC_Collect(); 61 62 // D translations of C macros: 63 /** Test if a type has a GC head */ 64 int PyType_IS_GC()(PyTypeObject* t) { 65 return PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC); 66 } 67 /** Test if an object has a GC head */ 68 int PyObject_IS_GC()(PyObject* o) { 69 return PyType_IS_GC(o.ob_type) 70 && (o.ob_type.tp_is_gc == null || o.ob_type.tp_is_gc(o)); 71 } 72 /// _ 73 PyVarObject* _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); 74 /// _ 75 type* PyObject_GC_Resize(type) (PyVarObject *op, Py_ssize_t n) { 76 return cast(type*)_PyObject_GC_Resize(op, n); 77 } 78 79 /** GC information is stored BEFORE the object structure. */ 80 union PyGC_Head { 81 /// _ 82 struct _gc { 83 /// _ 84 PyGC_Head *gc_next; 85 /// _ 86 PyGC_Head *gc_prev; 87 /// _ 88 Py_ssize_t gc_refs; 89 } 90 /// _ 91 _gc gc; 92 /// _ 93 real dummy; 94 } 95 96 // Numerous macro definitions that appear in objimpl.h at this point are not 97 // document. They appear to be for internal use, so they're omitted here. 98 99 /// _ 100 PyObject* _PyObject_GC_Malloc(size_t); 101 version(Python_3_5_Or_Later) { 102 /// _ 103 PyObject* _PyObject_GC_Calloc(size_t); 104 } 105 /// _ 106 PyObject* _PyObject_GC_New(PyTypeObject*); 107 /// _ 108 PyVarObject *_PyObject_GC_NewVar(PyTypeObject*, Py_ssize_t); 109 /// _ 110 void PyObject_GC_Track(void*); 111 /// _ 112 void PyObject_GC_UnTrack(void*); 113 /// _ 114 void PyObject_GC_Del(void*); 115 116 /// _ 117 type* PyObject_GC_New(type) (PyTypeObject* o) { 118 return cast(type*)_PyObject_GC_New(o); 119 } 120 /// _ 121 type* PyObject_GC_NewVar(type) (PyTypeObject* o, Py_ssize_t n) { 122 return cast(type*)_PyObject_GC_NewVar(o, n); 123 } 124 125 /** Test if a type supports weak references */ 126 auto PyType_SUPPORTS_WEAKREFS()(PyObject* t) { 127 version(Python_3_0_Or_Later) { 128 return (t.tp_weaklistoffset > 0); 129 }else{ 130 return (PyType_HasFeature(t, Py_TPFLAGS_HAVE_WEAKREFS) 131 && (t.tp_weaklistoffset > 0)); 132 } 133 } 134 135 /// _ 136 auto PyObject_GET_WEAKREFS_LISTPTR(T)(T o) { 137 return cast(PyObject**) ((cast(char *) o) + Py_TYPE(o).tp_weaklistoffset); 138 }