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