Debian Codegraph @

pypy/module/cpyext/src/object.c

raw symbols SCIP
1 #include "Python.h"
2
3 /* Taken from cpython/Include/internal/pycore_object.h */
4
5 // Fast inlined version of PyType_HasFeature()
6 static inline int
7 _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
8 return ((type->tp_flags & feature) != 0);
9 }
10
11
12 /* Inline functions trading binary compatibility for speed:
13 _PyObject_Init() is the fast version of PyObject_Init(), and
14 _PyObject_InitVar() is the fast version of PyObject_InitVar().
15
16 These inline functions must not be called with op=NULL. */
17 static inline void
18 _PyObject_Init(PyObject *op, PyTypeObject *typeobj)
19 {
20 assert(op != NULL);
21 Py_SET_TYPE(op, typeobj);
22 if (_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE)) {
23 Py_INCREF(typeobj);
24 }
25 _Py_NewReference(op);
26 }
27
28 static inline void
29 _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
30 {
31 assert(op != NULL);
32 Py_SET_SIZE(op, size);
33 _PyObject_Init((PyObject *)op, typeobj);
34 }
35
36
37
38 /* Generic object operations; and implementation of None (NoObject) */
39
40 #include "Python.h"
41
42 extern void _PyPy_Free(void *ptr);
43 extern void *_PyPy_Malloc(Py_ssize_t size);
44
45 /*
46 * The actual value of this variable will be the address of
47 * pyobject.w_marker_deallocating, and will be set by
48 * pyobject.write_w_marker_deallocating().
49 *
50 * The value set here is used only as a marker by tests (because during the
51 * tests we cannot call set_marker(), so we need to set a special value
52 * directly here)
53 */
54 void* _pypy_rawrefcount_w_marker_deallocating = (void*) 0xDEADFFF;
55
56 /*
57 * Mangle for translation.
58 * For tests, we want to mangle as if they were c-api functions so
59 * it will not be confused with the host's similarly named function
60 */
61
62 #ifdef CPYEXT_TESTS
63 #define _Py_Dealloc _cpyexttest_Dealloc
64 #ifdef __GNUC__
65 __attribute__((visibility("default")))
66 #else
67 __declspec(dllexport)
68 #endif
69 #else /* CPYEXT_TESTS */
70 #define _Py_Dealloc _PyPy_Dealloc
71 #endif /* CPYEXT_TESTS */
72 void
73 _Py_Dealloc(PyObject *obj)
74 {
75 PyTypeObject *pto = obj->ob_type;
76 /* this is the same as rawrefcount.mark_deallocating() */
77 obj->ob_pypy_link = (Py_ssize_t)_pypy_rawrefcount_w_marker_deallocating;
78 pto->tp_dealloc(obj);
79 }
80
81 #ifdef CPYEXT_TESTS
82 #define _Py_object_dealloc _cpyexttest_object_dealloc
83 #ifdef __GNUC__
84 __attribute__((visibility("default")))
85 #else
86 __declspec(dllexport)
87 #endif
88 #else /* CPYEXT_TESTS */
89 #define _Py_object_dealloc _PyPy_object_dealloc
90 #endif /* CPYEXT_TESTS */
91 void
92 _Py_object_dealloc(PyObject *obj)
93 {
94 PyTypeObject *pto;
95 assert(obj->ob_refcnt == 0);
96 pto = obj->ob_type;
97 pto->tp_free(obj);
98 if (pto->tp_flags & Py_TPFLAGS_HEAPTYPE)
99 Py_DECREF(pto);
100 }
101
102 void
103 PyObject_Free(void *obj)
104 {
105 _PyPy_Free(obj);
106 }
107
108 void
109 PyObject_GC_Del(void *obj)
110 {
111 _PyPy_Free(obj);
112 }
113
114 PyObject *
115 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
116 {
117 return (PyObject*)_PyObject_NewVar(type, nitems);
118 }
119
120 PyObject *
121 _PyObject_New(PyTypeObject *type)
122 {
123 return (PyObject*)_PyObject_NewVar(type, 0);
124 }
125
126 PyObject * _PyObject_GC_Malloc(size_t size)
127 {
128 return (PyObject *)PyObject_Malloc(size);
129 }
130
131 PyObject * _PyObject_GC_New(PyTypeObject *type)
132 {
133 return _PyObject_New(type);
134 }
135
136 PyVarObject * _PyObject_GC_NewVar(PyTypeObject *type, Py_ssize_t nitems)
137 {
138 return _PyObject_NewVar(type, nitems);
139 }
140
141 static PyObject *
142 _generic_alloc(PyTypeObject *type, Py_ssize_t nitems)
143 {
144 PyObject *pyobj;
145 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
146 Py_INCREF(type);
147
148 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
149 /* note that we need to add one, for the sentinel */
150
151 pyobj = (PyObject*)_PyPy_Malloc(size);
152 if (pyobj == NULL)
153 return NULL;
154
155 if (type->tp_itemsize)
156 ((PyVarObject*)pyobj)->ob_size = nitems;
157
158 pyobj->ob_refcnt = 1;
159 /* pyobj->ob_pypy_link should get assigned very quickly */
160 pyobj->ob_type = type;
161 return pyobj;
162 }
163
164 PyVarObject *
165 _PyObject_NewVar(PyTypeObject *type, Py_ssize_t nitems)
166 {
167 /* Note that this logic is slightly different than the one used by
168 CPython. The plan is to try to follow as closely as possible the
169 current cpyext logic here, and fix it when the migration to C is
170 completed
171 */
172 PyObject *py_obj = _generic_alloc(type, nitems);
173 if (!py_obj)
174 return (PyVarObject*)PyErr_NoMemory();
175
176 if (type->tp_itemsize == 0)
177 return (PyVarObject*)PyObject_INIT(py_obj, type);
178 else
179 return PyObject_INIT_VAR((PyVarObject*)py_obj, type, nitems);
180 }
181
182
183 PyObject *
184 PyObject_Init(PyObject *op, PyTypeObject *tp)
185 {
186 if (op == NULL) {
187 return PyErr_NoMemory();
188 }
189
190 _PyObject_Init(op, tp);
191 return op;
192 }
193
194 PyVarObject *
195 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
196 {
197 if (op == NULL) {
198 return (PyVarObject *) PyErr_NoMemory();
199 }
200
201 _PyObject_InitVar(op, tp, size);
202 return op;
203 }
204
205 int
206 PyObject_CallFinalizerFromDealloc(PyObject *self)
207 {
208 /* STUB */
209 if (self->ob_type->tp_finalize) {
210 fprintf(stderr, "WARNING: PyObject_CallFinalizerFromDealloc() "
211 "not implemented (objects of type '%s')\n",
212 self->ob_type->tp_name);
213 self->ob_type->tp_finalize = NULL; /* only once */
214 }
215 return 0;
216 }
217
218 const char *
219 _PyType_Name(PyTypeObject *type)
220 {
221 assert(type->tp_name != NULL);
222 const char *s = strrchr(type->tp_name, '.');
223 if (s == NULL) {
224 s = type->tp_name;
225 }
226 else {
227 s++;
228 }
229 return s;
230 }
231
232 PyObject *
233 PyType_GetName(PyTypeObject *type)
234 {
235 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
236 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
237
238 Py_INCREF(et->ht_name);
239 return et->ht_name;
240 }
241 else {
242 return PyUnicode_FromString(_PyType_Name(type));
243 }
244 }
245
246
247 PyObject *
248 PyType_GetQualName(PyTypeObject *type)
249 {
250 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
251 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
252 Py_INCREF(et->ht_qualname);
253 return et->ht_qualname;
254 }
255 else {
256 return PyUnicode_FromString(_PyType_Name(type));
257 }
258 }
259
260
261 void
262 _Py_NewReference(PyObject *op)
263 {
264 #ifndef PYPY_VERSION
265 if (_Py_tracemalloc_config.tracing) {
266 _PyTraceMalloc_NewReference(op);
267 }
268 #endif
269 #ifdef Py_REF_DEBUG
270 _Py_RefTotal++;
271 #endif
272 Py_SET_REFCNT(op, 1);
273 ((PyObject *)(op))->ob_pypy_link = 0;
274 #ifdef Py_TRACE_REFS
275 _Py_AddToAllObjects(op, 1);
276 #endif
277 }
278
279