| 1 |
#include "Python.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
static inline int |
| 7 |
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) { |
| 8 |
return ((type->tp_flags & feature) != 0); |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void* _pypy_rawrefcount_w_marker_deallocating = (void*) 0xDEADFFF; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
| 70 |
#define _Py_Dealloc _PyPy_Dealloc |
| 71 |
#endif |
| 72 |
void |
| 73 |
_Py_Dealloc(PyObject *obj) |
| 74 |
{ |
| 75 |
PyTypeObject *pto = obj->ob_type; |
| 76 |
|
| 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 |
| 89 |
#define _Py_object_dealloc _PyPy_object_dealloc |
| 90 |
#endif |
| 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 |
|
| 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 |
|
| 160 |
pyobj->ob_type = type; |
| 161 |
return pyobj; |
| 162 |
} |
| 163 |
|
| 164 |
PyVarObject * |
| 165 |
_PyObject_NewVar(PyTypeObject *type, Py_ssize_t nitems) |
| 166 |
{ |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|
| 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; |
| 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 |
|