Merge pull request #1246 from alex/freelist-sound

Make get_free_list API sound
This commit is contained in:
David Hewitt 2020-10-18 20:31:08 +01:00 committed by GitHub
commit 95ed5bb0b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 4 deletions

View File

@ -267,7 +267,7 @@ fn impl_class(
quote! { quote! {
impl pyo3::freelist::PyClassWithFreeList for #cls { impl pyo3::freelist::PyClassWithFreeList for #cls {
#[inline] #[inline]
fn get_free_list() -> &'static mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> { fn get_free_list(_py: pyo3::Python) -> &'static mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> {
static mut FREELIST: *mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> = 0 as *mut _; static mut FREELIST: *mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> = 0 as *mut _;
unsafe { unsafe {
if FREELIST.is_null() { if FREELIST.is_null() {

View File

@ -12,7 +12,7 @@ use std::os::raw::c_void;
/// The performance improvement applies to types that are often created and deleted in a row, /// The performance improvement applies to types that are often created and deleted in a row,
/// so that they can benefit from a freelist. /// so that they can benefit from a freelist.
pub trait PyClassWithFreeList { pub trait PyClassWithFreeList {
fn get_free_list() -> &'static mut FreeList<*mut ffi::PyObject>; fn get_free_list(py: Python) -> &'static mut FreeList<*mut ffi::PyObject>;
} }
pub enum Slot<T> { pub enum Slot<T> {
@ -74,7 +74,7 @@ where
unsafe fn new(py: Python, subtype: *mut ffi::PyTypeObject) -> *mut Self::Layout { unsafe fn new(py: Python, subtype: *mut ffi::PyTypeObject) -> *mut Self::Layout {
// if subtype is not equal to this type, we cannot use the freelist // if subtype is not equal to this type, we cannot use the freelist
if subtype == Self::type_object_raw(py) { if subtype == Self::type_object_raw(py) {
if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list().pop() { if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list(py).pop() {
ffi::PyObject_Init(obj, subtype); ffi::PyObject_Init(obj, subtype);
return obj as _; return obj as _;
} }
@ -91,7 +91,7 @@ where
return; return;
} }
if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list().insert(obj.as_ptr()) { if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list(py).insert(obj.as_ptr()) {
match (*ffi::Py_TYPE(obj)).tp_free { match (*ffi::Py_TYPE(obj)).tp_free {
Some(free) => free(obj as *mut c_void), Some(free) => free(obj as *mut c_void),
None => tp_free_fallback(obj), None => tp_free_fallback(obj),