-
Notifications
You must be signed in to change notification settings - Fork 2
Adding Deep Immutability to 3.12 #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3.12.0
Are you sure you want to change the base?
Changes from 1 commit
7ad9131
3b832e6
d52b222
80b648c
697d3b1
4024677
9136246
61a0400
6a6b04b
0caa788
a5ceb51
2e54f32
32d3fe7
37f2d76
fe5de34
fef1fdb
ab10e21
a7dbd1a
dd611e4
47fab24
99f259c
4f7816a
09fb9a7
11119d0
baff5f8
93eca6d
2ab77d3
a4d42da
5a0b5b7
2fab5cd
f621a0c
a31e962
adc1319
ccb893c
30dd83f
57df4db
4866d04
0387c12
c867271
81aebf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #ifndef Py_FREEZE_H | ||
matajoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #define Py_FREEZE_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #ifndef Py_BUILD_CORE | ||
| # error "this header requires Py_BUILD_CORE define" | ||
| #endif | ||
|
|
||
| PyObject* _Py_Freeze(PyObject*); | ||
| #define Py_Freeze(op) _Py_Freeze(_PyObject_CAST(op)) | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /* !Py_FREEZE_H */ | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,25 @@ whose size is determined when the object is allocated. | |
| /* PyObject_HEAD defines the initial segment of every PyObject. */ | ||
| #define PyObject_HEAD PyObject ob_base; | ||
|
|
||
| /* | ||
| Immutability: | ||
|
|
||
| Immutability is tracked in the top bit of the reference count. The immutability | ||
| system also uses the second-to-top bit for managing immutable graphs. | ||
| */ | ||
|
|
||
| #if SIZEOF_VOID_P > 4 | ||
| #define _Py_REFCNT_MASK 0xFFFFFFFF | ||
| #define _Py_IMMUTABLE_MASK 0xC000000000 | ||
| #define _Py_IMMUTABLE_FLAG 0x4000000000 | ||
| #define _Py_IMMUTABLE_SCC_FLAG 0x8000000000 | ||
| #else | ||
| #define _Py_REFCNT_MASK 0x3FFFFFFF | ||
| #define _Py_IMMUTABLE_MASK 0xC0000000 | ||
| #define _Py_IMMUTABLE_FLAG 0x40000000 | ||
| #define _Py_IMMUTABLE_SCC_FLAG 0x80000000 | ||
matajoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #endif | ||
|
|
||
| /* | ||
| Immortalization: | ||
|
|
||
|
|
@@ -112,17 +131,17 @@ be done by checking the bit sign flag in the lower 32 bits. | |
| #else | ||
| /* | ||
| In 32 bit systems, an object will be marked as immortal by setting all of the | ||
| lower 30 bits of the reference count field, which is equal to: 0x3FFFFFFF | ||
| lower 28 bits of the reference count field, which is equal to: 0x0FFFFFFF. | ||
|
|
||
| Using the lower 30 bits makes the value backwards compatible by allowing | ||
| Using the lower 28 bits makes the value backwards compatible by allowing | ||
| C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely | ||
| increase and decrease the objects reference count. The object would lose its | ||
| immortality, but the execution would still be correct. | ||
|
|
||
| Reference count increases and decreases will first go through an immortality | ||
| check by comparing the reference count field to the immortality reference count. | ||
| */ | ||
| #define _Py_IMMORTAL_REFCNT (UINT_MAX >> 2) | ||
| #define _Py_IMMORTAL_REFCNT (UINT_MAX >> 4) | ||
| #endif | ||
|
|
||
| // Make all internal uses of PyObject_HEAD_INIT immortal while preserving the | ||
|
|
@@ -208,7 +227,7 @@ PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y); | |
|
|
||
|
|
||
| static inline Py_ssize_t Py_REFCNT(PyObject *ob) { | ||
| return ob->ob_refcnt; | ||
| return ob->ob_refcnt & _Py_REFCNT_MASK; | ||
| } | ||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 | ||
| # define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob)) | ||
|
|
@@ -242,7 +261,7 @@ static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op) | |
| #if SIZEOF_VOID_P > 4 | ||
| return _Py_CAST(PY_INT32_T, op->ob_refcnt) < 0; | ||
| #else | ||
| return op->ob_refcnt == _Py_IMMORTAL_REFCNT; | ||
| return (op->ob_refcnt & _Py_REFCNT_MASK) == _Py_IMMORTAL_REFCNT; | ||
| #endif | ||
| } | ||
| #define _Py_IsImmortal(op) _Py_IsImmortal(_PyObject_CAST(op)) | ||
|
|
@@ -254,6 +273,11 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) { | |
| # define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type)) | ||
| #endif | ||
|
|
||
| static inline Py_ALWAYS_INLINE int _Py_IsImmutable(PyObject *op) | ||
| { | ||
| return (op->ob_refcnt & _Py_IMMUTABLE_FLAG) > 0; | ||
| } | ||
| #define _Py_IsImmutable(op) _Py_IsImmutable(_PyObject_CAST(op)) | ||
|
|
||
| static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { | ||
| // This immortal check is for code that is unaware of immortal objects. | ||
|
|
@@ -263,7 +287,7 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { | |
| if (_Py_IsImmortal(ob)) { | ||
| return; | ||
| } | ||
| ob->ob_refcnt = refcnt; | ||
| ob->ob_refcnt = (ob->ob_refcnt & _Py_IMMUTABLE_MASK) | (refcnt & _Py_REFCNT_MASK); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the immutability flag always be 0 here due to the if statement above? |
||
| } | ||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 | ||
| # define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt)) | ||
|
|
@@ -687,7 +711,8 @@ static inline void Py_DECREF(const char *filename, int lineno, PyObject *op) | |
| } | ||
| _Py_DECREF_STAT_INC(); | ||
| _Py_DECREF_DecRefTotal(); | ||
| if (--op->ob_refcnt == 0) { | ||
| op->ob_refcnt -= 1; | ||
| if ((op->ob_refcnt & _Py_REFCNT_MASK) == 0) { | ||
| _Py_Dealloc(op); | ||
| } | ||
| } | ||
|
|
@@ -702,7 +727,8 @@ static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op) | |
| return; | ||
| } | ||
| _Py_DECREF_STAT_INC(); | ||
| if (--op->ob_refcnt == 0) { | ||
| op->ob_refcnt -= 1; | ||
| if ((op->ob_refcnt & _Py_REFCNT_MASK) == 0) { | ||
| _Py_Dealloc(op); | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.