Commit 9d374465 authored by Mark Florisson's avatar Mark Florisson

Remove alignment check and track aligned pointer

parent be851994
...@@ -25,6 +25,7 @@ cdef extern from *: ...@@ -25,6 +25,7 @@ cdef extern from *:
void __Pyx_ReleaseBuffer(Py_buffer *) void __Pyx_ReleaseBuffer(Py_buffer *)
ctypedef struct PyObject ctypedef struct PyObject
ctypedef Py_ssize_t Py_intptr_t
void Py_INCREF(PyObject *) void Py_INCREF(PyObject *)
void Py_DECREF(PyObject *) void Py_DECREF(PyObject *)
...@@ -277,6 +278,21 @@ cdef indirect_contiguous = Enum("<contiguous and indirect>") ...@@ -277,6 +278,21 @@ cdef indirect_contiguous = Enum("<contiguous and indirect>")
# 'follow' is implied when the first or last axis is ::1 # 'follow' is implied when the first or last axis is ::1
@cname('__pyx_align_pointer')
cdef void *align_pointer(void *memory, size_t alignment) nogil:
"Align pointer memory on a given boundary"
cdef Py_intptr_t aligned_p = <Py_intptr_t> memory
cdef size_t offset
with cython.cdivision(True):
offset = aligned_p % alignment
if offset > 0:
aligned_p += alignment - offset
return <void *> aligned_p
@cname('__pyx_memoryview') @cname('__pyx_memoryview')
cdef class memoryview(object): cdef class memoryview(object):
...@@ -284,7 +300,10 @@ cdef class memoryview(object): ...@@ -284,7 +300,10 @@ cdef class memoryview(object):
cdef object _size cdef object _size
cdef object _array_interface cdef object _array_interface
cdef PyThread_type_lock lock cdef PyThread_type_lock lock
cdef __pyx_atomic_int acquisition_count # the following array will contain a single __pyx_atomic int with
# suitable alignment
cdef __pyx_atomic_int acquisition_count[2]
cdef __pyx_atomic_int *acquisition_count_aligned_p
cdef Py_buffer view cdef Py_buffer view
cdef int flags cdef int flags
cdef bint dtype_is_object cdef bint dtype_is_object
...@@ -307,6 +326,10 @@ cdef class memoryview(object): ...@@ -307,6 +326,10 @@ cdef class memoryview(object):
else: else:
self.dtype_is_object = dtype_is_object self.dtype_is_object = dtype_is_object
self.acquisition_count_aligned_p = <__pyx_atomic_int *> align_pointer(
<void *> &self.acquisition_count[0], sizeof(__pyx_atomic_int))
def __dealloc__(memoryview self): def __dealloc__(memoryview self):
if self.obj is not None: if self.obj is not None:
__Pyx_ReleaseBuffer(&self.view) __Pyx_ReleaseBuffer(&self.view)
......
...@@ -61,46 +61,15 @@ typedef struct { ...@@ -61,46 +61,15 @@ typedef struct {
typedef volatile __pyx_atomic_int_type __pyx_atomic_int; typedef volatile __pyx_atomic_int_type __pyx_atomic_int;
#if CYTHON_ATOMICS #if CYTHON_ATOMICS
static CYTHON_INLINE __pyx_atomic_int_type
__pyx_atomic_incr_maybealigned(__pyx_atomic_int *value, PyThread_type_lock lock);
static CYTHON_INLINE __pyx_atomic_int_type
__pyx_atomic_decr_maybealigned(__pyx_atomic_int *value, PyThread_type_lock lock);
#define __pyx_add_acquisition_count(memview) \ #define __pyx_add_acquisition_count(memview) \
__pyx_atomic_incr_maybealigned(&memview->acquisition_count, memview->lock) __pyx_atomic_incr_aligned(__pyx_get_slice_count_pointer(memview), memview->lock)
#define __pyx_sub_acquisition_count(memview) \ #define __pyx_sub_acquisition_count(memview) \
__pyx_atomic_decr_maybealigned(&memview->acquisition_count, memview->lock) __pyx_atomic_decr_aligned(__pyx_get_slice_count_pointer(memview), memview->lock)
#else #else
#define __pyx_add_acquisition_count(memview) \ #define __pyx_add_acquisition_count(memview) \
__pyx_add_acquisition_count_locked(&memview->acquisition_count, memview->lock) __pyx_add_acquisition_count_locked(__pyx_get_slice_count_pointer(memview), memview->lock)
#define __pyx_sub_acquisition_count(memview) \ #define __pyx_sub_acquisition_count(memview) \
__pyx_sub_acquisition_count_locked(&memview->acquisition_count, memview->lock) __pyx_sub_acquisition_count_locked(__pyx_get_slice_count_pointer(memview), memview->lock)
#endif
////////// Atomics //////////
#if CYTHON_ATOMICS
#define __pyx_atomic_unaligned(pointer) \
(((Py_intptr_t) pointer) & (sizeof(pointer) - 1))
static CYTHON_INLINE __pyx_atomic_int_type
__pyx_atomic_incr_maybealigned(__pyx_atomic_int *value, PyThread_type_lock lock)
{
if (unlikely(__pyx_atomic_unaligned(value)))
return __pyx_add_acquisition_count_locked(value, lock);
else
return __pyx_atomic_incr_aligned(value, lock);
}
static CYTHON_INLINE __pyx_atomic_int_type
__pyx_atomic_decr_maybealigned(__pyx_atomic_int *value, PyThread_type_lock lock)
{
if (unlikely(__pyx_atomic_unaligned(value)))
return __pyx_sub_acquisition_count_locked(value, lock);
else
return __pyx_atomic_decr_aligned(value, lock);
}
#endif #endif
/////////////// ObjectToMemviewSlice.proto /////////////// /////////////// ObjectToMemviewSlice.proto ///////////////
...@@ -133,6 +102,8 @@ static CYTHON_INLINE int __pyx_add_acquisition_count_locked(__pyx_atomic_int *ac ...@@ -133,6 +102,8 @@ static CYTHON_INLINE int __pyx_add_acquisition_count_locked(__pyx_atomic_int *ac
PyThread_type_lock lock); PyThread_type_lock lock);
static CYTHON_INLINE int __pyx_sub_acquisition_count_locked(__pyx_atomic_int *acquisition_count, static CYTHON_INLINE int __pyx_sub_acquisition_count_locked(__pyx_atomic_int *acquisition_count,
PyThread_type_lock lock); PyThread_type_lock lock);
#define __pyx_get_slice_count_pointer(memview) (memview->acquisition_count_aligned_p)
#define __pyx_get_slice_count(memview) (*__pyx_get_slice_count_pointer(memview))
#define __PYX_INC_MEMVIEW(slice, have_gil) __Pyx_INC_MEMVIEW(slice, have_gil, __LINE__) #define __PYX_INC_MEMVIEW(slice, have_gil) __Pyx_INC_MEMVIEW(slice, have_gil, __LINE__)
#define __PYX_XDEC_MEMVIEW(slice, have_gil) __Pyx_XDEC_MEMVIEW(slice, have_gil, __LINE__) #define __PYX_XDEC_MEMVIEW(slice, have_gil) __Pyx_XDEC_MEMVIEW(slice, have_gil, __LINE__)
static CYTHON_INLINE void __Pyx_INC_MEMVIEW({{memviewslice_name}} *, int, int); static CYTHON_INLINE void __Pyx_INC_MEMVIEW({{memviewslice_name}} *, int, int);
...@@ -409,9 +380,9 @@ __Pyx_INC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil, int lineno) ...@@ -409,9 +380,9 @@ __Pyx_INC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil, int lineno)
if (!memview || (PyObject *) memview == Py_None) if (!memview || (PyObject *) memview == Py_None)
return; /* allow uninitialized memoryview assignment */ return; /* allow uninitialized memoryview assignment */
if (memview->acquisition_count < 0) if (__pyx_get_slice_count(memview) < 0)
__pyx_fatalerror("Acquisition count is %d (line %d)", __pyx_fatalerror("Acquisition count is %d (line %d)",
memview->acquisition_count, lineno); __pyx_get_slice_count(memview), lineno);
first_time = __pyx_add_acquisition_count(memview) == 0; first_time = __pyx_add_acquisition_count(memview) == 0;
...@@ -438,9 +409,9 @@ static CYTHON_INLINE void __Pyx_XDEC_MEMVIEW({{memviewslice_name}} *memslice, ...@@ -438,9 +409,9 @@ static CYTHON_INLINE void __Pyx_XDEC_MEMVIEW({{memviewslice_name}} *memslice,
return; return;
} }
if (memview->acquisition_count <= 0) if (__pyx_get_slice_count(memview) <= 0)
__pyx_fatalerror("Acquisition count is %d (line %d)", __pyx_fatalerror("Acquisition count is %d (line %d)",
memview->acquisition_count, lineno); __pyx_get_slice_count(memview), lineno);
last_time = __pyx_sub_acquisition_count(memview) == 1; last_time = __pyx_sub_acquisition_count(memview) == 1;
memslice->data = NULL; memslice->data = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment