@@ -199,7 +199,7 @@ impl BorrowKey {
199
199
let range = data_range ( array) ;
200
200
201
201
let data_ptr = array. data ( ) as usize ;
202
- let gcd_strides = reduce ( array. strides ( ) . iter ( ) . copied ( ) , gcd ) . unwrap_or ( 1 ) ;
202
+ let gcd_strides = gcd_strides ( array. strides ( ) ) ;
203
203
204
204
Self {
205
205
range,
@@ -252,16 +252,9 @@ impl BorrowFlags {
252
252
( * self . 0 . get ( ) ) . get_or_insert_with ( AHashMap :: new)
253
253
}
254
254
255
- fn acquire < T , D > ( & self , array : & PyArray < T , D > ) -> Result < ( ) , BorrowError >
256
- where
257
- T : Element ,
258
- D : Dimension ,
259
- {
260
- let address = base_address ( array) ;
261
- let key = BorrowKey :: from_array ( array) ;
262
-
263
- // SAFETY: Access to `&PyArray<T, D>` implies holding the GIL
264
- // and we are not calling into user code which might re-enter this function.
255
+ fn acquire ( & self , _py : Python , address : usize , key : BorrowKey ) -> Result < ( ) , BorrowError > {
256
+ // SAFETY: Having `_py` implies holding the GIL and
257
+ // we are not calling into user code which might re-enter this function.
265
258
let borrow_flags = unsafe { BORROW_FLAGS . get ( ) } ;
266
259
267
260
match borrow_flags. entry ( address) {
@@ -302,16 +295,9 @@ impl BorrowFlags {
302
295
Ok ( ( ) )
303
296
}
304
297
305
- fn release < T , D > ( & self , array : & PyArray < T , D > )
306
- where
307
- T : Element ,
308
- D : Dimension ,
309
- {
310
- let address = base_address ( array) ;
311
- let key = BorrowKey :: from_array ( array) ;
312
-
313
- // SAFETY: Access to `&PyArray<T, D>` implies holding the GIL
314
- // and we are not calling into user code which might re-enter this function.
298
+ fn release ( & self , _py : Python , address : usize , key : BorrowKey ) {
299
+ // SAFETY: Having `_py` implies holding the GIL and
300
+ // we are not calling into user code which might re-enter this function.
315
301
let borrow_flags = unsafe { BORROW_FLAGS . get ( ) } ;
316
302
317
303
let same_base_arrays = borrow_flags. get_mut ( & address) . unwrap ( ) ;
@@ -329,16 +315,9 @@ impl BorrowFlags {
329
315
}
330
316
}
331
317
332
- fn acquire_mut < T , D > ( & self , array : & PyArray < T , D > ) -> Result < ( ) , BorrowError >
333
- where
334
- T : Element ,
335
- D : Dimension ,
336
- {
337
- let address = base_address ( array) ;
338
- let key = BorrowKey :: from_array ( array) ;
339
-
340
- // SAFETY: Access to `&PyArray<T, D>` implies holding the GIL
341
- // and we are not calling into user code which might re-enter this function.
318
+ fn acquire_mut ( & self , _py : Python , address : usize , key : BorrowKey ) -> Result < ( ) , BorrowError > {
319
+ // SAFETY: Having `_py` implies holding the GIL and
320
+ // we are not calling into user code which might re-enter this function.
342
321
let borrow_flags = unsafe { BORROW_FLAGS . get ( ) } ;
343
322
344
323
match borrow_flags. entry ( address) {
@@ -373,16 +352,9 @@ impl BorrowFlags {
373
352
Ok ( ( ) )
374
353
}
375
354
376
- fn release_mut < T , D > ( & self , array : & PyArray < T , D > )
377
- where
378
- T : Element ,
379
- D : Dimension ,
380
- {
381
- let address = base_address ( array) ;
382
- let key = BorrowKey :: from_array ( array) ;
383
-
384
- // SAFETY: Access to `&PyArray<T, D>` implies holding the GIL
385
- // and we are not calling into user code which might re-enter this function.
355
+ fn release_mut ( & self , _py : Python , address : usize , key : BorrowKey ) {
356
+ // SAFETY: Having `_py` implies holding the GIL and
357
+ // we are not calling into user code which might re-enter this function.
386
358
let borrow_flags = unsafe { BORROW_FLAGS . get ( ) } ;
387
359
388
360
let same_base_arrays = borrow_flags. get_mut ( & address) . unwrap ( ) ;
@@ -454,7 +426,11 @@ where
454
426
D : Dimension ,
455
427
{
456
428
pub ( crate ) fn try_new ( array : & ' py PyArray < T , D > ) -> Result < Self , BorrowError > {
457
- BORROW_FLAGS . acquire ( array) ?;
429
+ let py = array. py ( ) ;
430
+ let address = base_address ( array) ;
431
+ let key = BorrowKey :: from_array ( array) ;
432
+
433
+ BORROW_FLAGS . acquire ( py, address, key) ?;
458
434
459
435
Ok ( Self ( array) )
460
436
}
@@ -499,7 +475,11 @@ where
499
475
D : Dimension ,
500
476
{
501
477
fn drop ( & mut self ) {
502
- BORROW_FLAGS . release ( self . 0 ) ;
478
+ let py = self . 0 . py ( ) ;
479
+ let address = base_address ( self . 0 ) ;
480
+ let key = BorrowKey :: from_array ( self . 0 ) ;
481
+
482
+ BORROW_FLAGS . release ( py, address, key) ;
503
483
}
504
484
}
505
485
@@ -581,7 +561,11 @@ where
581
561
return Err ( BorrowError :: NotWriteable ) ;
582
562
}
583
563
584
- BORROW_FLAGS . acquire_mut ( array) ?;
564
+ let py = array. py ( ) ;
565
+ let address = base_address ( array) ;
566
+ let key = BorrowKey :: from_array ( array) ;
567
+
568
+ BORROW_FLAGS . acquire_mut ( py, address, key) ?;
585
569
586
570
Ok ( Self ( array) )
587
571
}
@@ -632,14 +616,21 @@ where
632
616
/// });
633
617
/// ```
634
618
pub fn resize ( self , new_elems : usize ) -> PyResult < Self > {
635
- BORROW_FLAGS . release_mut ( self . 0 ) ;
619
+ let py = self . 0 . py ( ) ;
620
+ let address = base_address ( self . 0 ) ;
621
+ let key = BorrowKey :: from_array ( self . 0 ) ;
622
+
623
+ BORROW_FLAGS . release_mut ( py, address, key) ;
636
624
637
625
// SAFETY: Ownership of `self` proves exclusive access to the interior of the array.
638
626
unsafe {
639
627
self . 0 . resize ( new_elems) ?;
640
628
}
641
629
642
- BORROW_FLAGS . acquire_mut ( self . 0 ) ?;
630
+ let address = base_address ( self . 0 ) ;
631
+ let key = BorrowKey :: from_array ( self . 0 ) ;
632
+
633
+ BORROW_FLAGS . acquire_mut ( py, address, key) ?;
643
634
644
635
Ok ( self )
645
636
}
@@ -651,7 +642,11 @@ where
651
642
D : Dimension ,
652
643
{
653
644
fn drop ( & mut self ) {
654
- BORROW_FLAGS . release_mut ( self . 0 ) ;
645
+ let py = self . 0 . py ( ) ;
646
+ let address = base_address ( self . 0 ) ;
647
+ let key = BorrowKey :: from_array ( self . 0 ) ;
648
+
649
+ BORROW_FLAGS . release_mut ( py, address, key) ;
655
650
}
656
651
}
657
652
@@ -726,6 +721,10 @@ where
726
721
)
727
722
}
728
723
724
+ fn gcd_strides ( strides : & [ isize ] ) -> isize {
725
+ reduce ( strides. iter ( ) . copied ( ) , gcd) . unwrap_or ( 1 )
726
+ }
727
+
729
728
// FIXME(adamreichold): Use `usize::abs_diff` from std when that becomes stable.
730
729
fn abs_diff ( lhs : usize , rhs : usize ) -> usize {
731
730
if lhs >= rhs {
0 commit comments