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 version(Python_3_5_Or_Later) { 35 /// _ 36 struct PyObjectArenaAllocator { 37 void* ctx; 38 void* function(void* ctx, size_t size) alloc; 39 void* function(void* ctx, void* ptr, size_t size) free; 40 } 41 42 43 /** Get the arena allocator. */ 44 void PyObject_GetArenaAllocator(PyObjectArenaAllocator* allocator); 45 46 /** Set the arena allocator. */ 47 void PyObject_SetArenaAllocator(PyObjectArenaAllocator* allocator); 48 } 49 50 /// _ 51 PyObject* _PyObject_New(PyTypeObject*); 52 /// _ 53 PyVarObject* _PyObject_NewVar(PyTypeObject*, Py_ssize_t); 54 /** 55 Allocates memory for a new object of the given 56 type, and initializes part of it. 'type' must be the C structure type used 57 to represent the object, and 'typeobj' the address of the corresponding 58 type object. Reference count and type pointer are filled in; the rest of 59 the bytes of the object are *undefined*! The resulting expression type is 60 'type *'. The size of the object is determined by the tp_basicsize field 61 of the type object. 62 */ 63 type* PyObject_New(type)(PyTypeObject* o) { 64 return cast(type*)_PyObject_New(o); 65 } 66 /** 67 PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size 68 object with room for n items. In addition to the refcount and type pointer 69 fields, this also fills in the ob_size field. 70 */ 71 type* PyObject_NewVar(type)(PyTypeObject* o, Py_ssize_t n) { 72 return cast(type*)_PyObject_NewVar(o, n); 73 } 74 75 76 /** C equivalent of gc.collect(). */ 77 C_long PyGC_Collect(); 78 79 // D translations of C macros: 80 /** Test if a type has a GC head */ 81 int PyType_IS_GC()(PyTypeObject* t) { 82 return PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC); 83 } 84 /** Test if an object has a GC head */ 85 int PyObject_IS_GC()(PyObject* o) { 86 return PyType_IS_GC(o.ob_type) 87 && (o.ob_type.tp_is_gc == null || o.ob_type.tp_is_gc(o)); 88 } 89 /// _ 90 PyVarObject* _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); 91 /// _ 92 type* PyObject_GC_Resize(type) (PyVarObject *op, Py_ssize_t n) { 93 return cast(type*)_PyObject_GC_Resize(op, n); 94 } 95 96 /** GC information is stored BEFORE the object structure. */ 97 union PyGC_Head { 98 /// _ 99 struct _gc { 100 /// _ 101 PyGC_Head *gc_next; 102 /// _ 103 PyGC_Head *gc_prev; 104 /// _ 105 Py_ssize_t gc_refs; 106 } 107 /// _ 108 _gc gc; 109 /// _ 110 real dummy; 111 } 112 113 // Numerous macro definitions that appear in objimpl.h at this point are not 114 // document. They appear to be for internal use, so they're omitted here. 115 116 /// _ 117 PyObject* _PyObject_GC_Malloc(size_t); 118 version(Python_3_5_Or_Later) { 119 /// _ 120 PyObject* _PyObject_GC_Calloc(size_t); 121 } 122 /// _ 123 PyObject* _PyObject_GC_New(PyTypeObject*); 124 /// _ 125 PyVarObject *_PyObject_GC_NewVar(PyTypeObject*, Py_ssize_t); 126 /// _ 127 void PyObject_GC_Track(void*); 128 /// _ 129 void PyObject_GC_UnTrack(void*); 130 /// _ 131 void PyObject_GC_Del(void*); 132 133 /// _ 134 type* PyObject_GC_New(type) (PyTypeObject* o) { 135 return cast(type*)_PyObject_GC_New(o); 136 } 137 /// _ 138 type* PyObject_GC_NewVar(type) (PyTypeObject* o, Py_ssize_t n) { 139 return cast(type*)_PyObject_GC_NewVar(o, n); 140 } 141 142 /** Test if a type supports weak references */ 143 auto PyType_SUPPORTS_WEAKREFS()(PyObject* t) { 144 version(Python_3_0_Or_Later) { 145 return (t.tp_weaklistoffset > 0); 146 }else{ 147 return (PyType_HasFeature(t, Py_TPFLAGS_HAVE_WEAKREFS) 148 && (t.tp_weaklistoffset > 0)); 149 } 150 } 151 152 /// _ 153 auto PyObject_GET_WEAKREFS_LISTPTR(T)(T o) { 154 return cast(PyObject**) ((cast(char *) o) + Py_TYPE(o).tp_weaklistoffset); 155 }