| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef Py_OBJIMPL_H |
| 6 |
#define Py_OBJIMPL_H |
| 7 |
|
| 8 |
#include "pymem.h" |
| 9 |
|
| 10 |
#ifdef __cplusplus |
| 11 |
extern "C" { |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
PyAPI_FUNC(void *) PyObject_Malloc(size_t); |
| 98 |
PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t); |
| 99 |
PyAPI_FUNC(void) PyObject_Free(void *); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
#ifdef WITH_PYMALLOC |
| 104 |
#ifdef PYMALLOC_DEBUG |
| 105 |
PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes); |
| 106 |
PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes); |
| 107 |
PyAPI_FUNC(void) _PyObject_DebugFree(void *p); |
| 108 |
PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p); |
| 109 |
PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p); |
| 110 |
PyAPI_FUNC(void) _PyObject_DebugMallocStats(void); |
| 111 |
PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes); |
| 112 |
PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes); |
| 113 |
PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p); |
| 114 |
PyAPI_FUNC(void) _PyObject_DebugCheckAddressApi(char api, const void *p); |
| 115 |
PyAPI_FUNC(void *) _PyMem_DebugMalloc(size_t nbytes); |
| 116 |
PyAPI_FUNC(void *) _PyMem_DebugRealloc(void *p, size_t nbytes); |
| 117 |
PyAPI_FUNC(void) _PyMem_DebugFree(void *p); |
| 118 |
#define PyObject_MALLOC _PyObject_DebugMalloc |
| 119 |
#define PyObject_Malloc _PyObject_DebugMalloc |
| 120 |
#define PyObject_REALLOC _PyObject_DebugRealloc |
| 121 |
#define PyObject_Realloc _PyObject_DebugRealloc |
| 122 |
#define PyObject_FREE _PyObject_DebugFree |
| 123 |
#define PyObject_Free _PyObject_DebugFree |
| 124 |
|
| 125 |
#else |
| 126 |
#define PyObject_MALLOC PyObject_Malloc |
| 127 |
#define PyObject_REALLOC PyObject_Realloc |
| 128 |
#define PyObject_FREE PyObject_Free |
| 129 |
#endif |
| 130 |
|
| 131 |
#else |
| 132 |
#define PyObject_MALLOC PyMem_MALLOC |
| 133 |
#define PyObject_REALLOC PyMem_REALLOC |
| 134 |
#define PyObject_FREE PyMem_FREE |
| 135 |
|
| 136 |
#endif |
| 137 |
|
| 138 |
#define PyObject_Del PyObject_Free |
| 139 |
#define PyObject_DEL PyObject_FREE |
| 140 |
|
| 141 |
|
| 142 |
#define _PyObject_Del PyObject_Free |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *); |
| 151 |
PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *, |
| 152 |
PyTypeObject *, Py_ssize_t); |
| 153 |
PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *); |
| 154 |
PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t); |
| 155 |
|
| 156 |
#define PyObject_New(type, typeobj) \ |
| 157 |
( (type *) _PyObject_New(typeobj) ) |
| 158 |
#define PyObject_NewVar(type, typeobj, n) \ |
| 159 |
( (type *) _PyObject_NewVar((typeobj), (n)) ) |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#define PyObject_INIT(op, typeobj) \ |
| 164 |
( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) ) |
| 165 |
#define PyObject_INIT_VAR(op, typeobj, size) \ |
| 166 |
( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) ) |
| 167 |
|
| 168 |
#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize ) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 |
| 181 |
# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" |
| 182 |
#endif |
| 183 |
|
| 184 |
#define _PyObject_VAR_SIZE(typeobj, nitems) \ |
| 185 |
(size_t) \ |
| 186 |
( ( (typeobj)->tp_basicsize + \ |
| 187 |
(nitems)*(typeobj)->tp_itemsize + \ |
| 188 |
(SIZEOF_VOID_P - 1) \ |
| 189 |
) & ~(SIZEOF_VOID_P - 1) \ |
| 190 |
) |
| 191 |
|
| 192 |
#define PyObject_NEW(type, typeobj) \ |
| 193 |
( (type *) PyObject_Init( \ |
| 194 |
(PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) ) |
| 195 |
|
| 196 |
#define PyObject_NEW_VAR(type, typeobj, n) \ |
| 197 |
( (type *) PyObject_InitVar( \ |
| 198 |
(PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\ |
| 199 |
(typeobj), (n)) ) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void); |
| 236 |
|
| 237 |
|
| 238 |
#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) |
| 239 |
|
| 240 |
|
| 241 |
#define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \ |
| 242 |
(Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) |
| 243 |
|
| 244 |
PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); |
| 245 |
#define PyObject_GC_Resize(type, op, n) \ |
| 246 |
( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) ) |
| 247 |
|
| 248 |
|
| 249 |
#define _PyObject_GC_Del PyObject_GC_Del |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
union _gc_head; |
| 256 |
union _gc_head_old { |
| 257 |
struct { |
| 258 |
union _gc_head_old *gc_next; |
| 259 |
union _gc_head_old *gc_prev; |
| 260 |
Py_ssize_t gc_refs; |
| 261 |
} gc; |
| 262 |
long double dummy; |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
typedef union _gc_head { |
| 267 |
struct { |
| 268 |
union _gc_head *gc_next; |
| 269 |
union _gc_head *gc_prev; |
| 270 |
Py_ssize_t gc_refs; |
| 271 |
} gc; |
| 272 |
double dummy; |
| 273 |
char dummy_padding[sizeof(union _gc_head_old)]; |
| 274 |
} PyGC_Head; |
| 275 |
|
| 276 |
extern PyGC_Head *_PyGC_generation0; |
| 277 |
|
| 278 |
#define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) |
| 279 |
|
| 280 |
#define _PyGC_REFS_UNTRACKED (-2) |
| 281 |
#define _PyGC_REFS_REACHABLE (-3) |
| 282 |
#define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4) |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
#define _PyObject_GC_TRACK(o) do { \ |
| 287 |
PyGC_Head *g = _Py_AS_GC(o); \ |
| 288 |
if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \ |
| 289 |
Py_FatalError("GC object already tracked"); \ |
| 290 |
g->gc.gc_refs = _PyGC_REFS_REACHABLE; \ |
| 291 |
g->gc.gc_next = _PyGC_generation0; \ |
| 292 |
g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \ |
| 293 |
g->gc.gc_prev->gc.gc_next = g; \ |
| 294 |
_PyGC_generation0->gc.gc_prev = g; \ |
| 295 |
} while (0); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#define _PyObject_GC_UNTRACK(o) do { \ |
| 302 |
PyGC_Head *g = _Py_AS_GC(o); \ |
| 303 |
assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \ |
| 304 |
g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \ |
| 305 |
g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \ |
| 306 |
g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \ |
| 307 |
g->gc.gc_next = NULL; \ |
| 308 |
} while (0); |
| 309 |
|
| 310 |
|
| 311 |
#define _PyObject_GC_IS_TRACKED(o) \ |
| 312 |
((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED) |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
#define _PyObject_GC_MAY_BE_TRACKED(obj) \ |
| 317 |
(PyObject_IS_GC(obj) && \ |
| 318 |
(!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) |
| 319 |
|
| 320 |
|
| 321 |
PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t); |
| 322 |
PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *); |
| 323 |
PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t); |
| 324 |
PyAPI_FUNC(void) PyObject_GC_Track(void *); |
| 325 |
PyAPI_FUNC(void) PyObject_GC_UnTrack(void *); |
| 326 |
PyAPI_FUNC(void) PyObject_GC_Del(void *); |
| 327 |
|
| 328 |
#define PyObject_GC_New(type, typeobj) \ |
| 329 |
( (type *) _PyObject_GC_New(typeobj) ) |
| 330 |
#define PyObject_GC_NewVar(type, typeobj, n) \ |
| 331 |
( (type *) _PyObject_GC_NewVar((typeobj), (n)) ) |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
#define Py_VISIT(op) \ |
| 340 |
do { \ |
| 341 |
if (op) { \ |
| 342 |
int vret = visit((PyObject *)(op), arg); \ |
| 343 |
if (vret) \ |
| 344 |
return vret; \ |
| 345 |
} \ |
| 346 |
} while (0) |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
#define PyGC_HEAD_SIZE 0 |
| 352 |
#define PyObject_GC_Init(op) |
| 353 |
#define PyObject_GC_Fini(op) |
| 354 |
#define PyObject_AS_GC(op) (op) |
| 355 |
#define PyObject_FROM_GC(op) (op) |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
#define PyType_SUPPORTS_WEAKREFS(t) \ |
| 360 |
(PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \ |
| 361 |
&& ((t)->tp_weaklistoffset > 0)) |
| 362 |
|
| 363 |
#define PyObject_GET_WEAKREFS_LISTPTR(o) \ |
| 364 |
((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset)) |
| 365 |
|
| 366 |
#ifdef __cplusplus |
| 367 |
} |
| 368 |
#endif |
| 369 |
#endif |