|
| 1 | +#ifndef Py_INTERNAL_OWNERSHIP_H |
| 2 | +#define Py_INTERNAL_OWNERSHIP_H |
| 3 | +#ifdef __cplusplus |
| 4 | +extern "C" { |
| 5 | +#endif |
| 6 | + |
| 7 | +#ifndef Py_BUILD_CORE |
| 8 | +# error "Py_BUILD_CORE must be defined to include this header" |
| 9 | +#endif |
| 10 | + |
| 11 | +#include "exports.h" |
| 12 | + |
| 13 | +typedef struct _Py_ownership_state { |
| 14 | + /* Temporary value until the state always has a field to indicate this. */ |
| 15 | + int is_initialized; |
| 16 | +#ifdef Py_OWNERSHIP_INVARIANT |
| 17 | + /* Tracks the state of the ownership invariant. Some ownership-related |
| 18 | + * operations may temporarily violate the invariant. To handle this safely, |
| 19 | + * the invariant must be suspended during such operations and only resumed |
| 20 | + * once all of them complete. This is necessary to support re-entrancy. |
| 21 | + * |
| 22 | + * For example, during freezing, the object graph is traversed and objects |
| 23 | + * are marked as immutable — even while they may still reference mutable |
| 24 | + * objects. If the invariant were enforced mid-way, it would raise a |
| 25 | + * (premature) error, despite the state being corrected as the operation |
| 26 | + * completes. To avoid this, the invariant must be paused during the freeze. |
| 27 | + * |
| 28 | + * States: |
| 29 | + * -1 => The invariant is disabled. |
| 30 | + * 0 => The invariant is active and enforced. |
| 31 | + * N => The invariant is temporarily paused. The value indicates the |
| 32 | + * number of suspensions yet to be resumed (this supports nesting). |
| 33 | + */ |
| 34 | + int invariant_state; |
| 35 | +#endif |
| 36 | +} _Py_ownership_state; |
| 37 | + |
| 38 | +/* This function returns true for C wrappers around functions, types and |
| 39 | + * all kinds of wrappers around C with immutable state. For ownership these |
| 40 | + * can be seen as immutable, meaning they can be referenced from immutable |
| 41 | + * objects and from inside regions. |
| 42 | + */ |
| 43 | +PyAPI_FUNC(int) _PyOwnership_is_c_wrapper(PyObject *obj); |
| 44 | + |
| 45 | +/* This function calls the `visit` function for the fields of the `obj` |
| 46 | + * which should be effected by ownership. The `data` pointer will be |
| 47 | + * passed along as the second argument to `visit`. |
| 48 | + */ |
| 49 | +PyAPI_FUNC(int) _PyOwnership_traverse_obj(PyObject *obj, visitproc visit, void *data); |
| 50 | + |
| 51 | +#ifdef Py_OWNERSHIP_INVARIANT |
| 52 | + |
| 53 | +#include "object.h" // PyObject, visitproc |
| 54 | +#include "pytypedefs.h" // PyThreadState |
| 55 | + |
| 56 | +#define Py_OWNERSHIP_INVARIANT_DISABLED -1 |
| 57 | +#define Py_OWNERSHIP_INVARIANT_ENABLED 0 |
| 58 | + |
| 59 | +/* This function validates that the current heap follows the ownership |
| 60 | + * rules. This is a slow operation and should only be done for debugging. |
| 61 | + * |
| 62 | + * 0 indicates a valid heap, -1 will be returned if an error was thrown. |
| 63 | + */ |
| 64 | +PyAPI_FUNC(int) _PyOwnership_check_invariant(PyThreadState *tstate); |
| 65 | + |
| 66 | +PyAPI_FUNC(int) _PyOwnership_invariant_enable(void); |
| 67 | +PyAPI_FUNC(int) _PyOwnership_invariant_pause(void); |
| 68 | +PyAPI_FUNC(int) _PyOwnership_invariant_resume(void); |
| 69 | + |
| 70 | +#else |
| 71 | +# define _PyOwnership_invariant_enable() 0 /* success */ |
| 72 | +# define _PyOwnership_invariant_pause() 0 /* success */ |
| 73 | +# define _PyOwnership_invariant_resume() 0 /* success */ |
| 74 | +#endif |
| 75 | + |
| 76 | +#ifdef __cplusplus |
| 77 | +} |
| 78 | +#endif |
| 79 | +#endif /* !Py_INTERNAL_OWNERSHIP_H */ |
0 commit comments