Skip to content

Commit

Permalink
julia 1.11: add some workarounds for jl_array changes (#137)
Browse files Browse the repository at this point in the history
* julia 1.11: add some workarounds for jl_array changes

* Array: avoid using arrayset and array_ptr_set

* arrayset: simplify code with jl_array_ptr_1d_push

this avoids the arrayset and manually dealing with the write barrier
  • Loading branch information
benlorenz authored Nov 16, 2023
1 parent 4888607 commit 260ea7e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions include/jlcxx/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
namespace jlcxx
{

/// wrapper for julia jl_array_data for different julia versions
template <typename T>
T* jlcxx_array_data(jl_array_t* arr) {
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
return jl_array_data(arr, T);
#else
return static_cast<T*>(jl_array_data(arr));
#endif
}

template<typename PointedT, typename CppT>
struct ValueExtractor
{
Expand Down Expand Up @@ -121,9 +131,8 @@ class Array
void push_back(VT&& val)
{
JL_GC_PUSH1(&m_array);
const size_t pos = jl_array_len(m_array);
jl_array_grow_end(m_array, 1);
jl_arrayset(m_array, box<ValueT>(val), pos);
jl_value_t* jval = box<ValueT>(val);
jl_array_ptr_1d_push(m_array, jval);
JL_GC_POP();
}

Expand Down Expand Up @@ -165,7 +174,6 @@ template<typename ValueT, int Dim = 1>
class ArrayRef
{
public:

using julia_t = typename detail::ArrayElementType<ValueT>::type;

ArrayRef(jl_array_t* arr) : m_array(arr)
Expand All @@ -191,22 +199,22 @@ class ArrayRef

iterator begin()
{
return iterator(static_cast<julia_t*>(jl_array_data(wrapped())));
return iterator(jlcxx_array_data<julia_t>(wrapped()));
}

const_iterator begin() const
{
return const_iterator(static_cast<julia_t*>(jl_array_data(wrapped())));
return const_iterator(jlcxx_array_data<julia_t>(wrapped()));
}

iterator end()
{
return iterator(static_cast<julia_t*>(jl_array_data(wrapped())) + jl_array_len(wrapped()));
return iterator(jlcxx_array_data<julia_t>(wrapped()) + jl_array_len(wrapped()));
}

const_iterator end() const
{
return const_iterator(static_cast<julia_t*>(jl_array_data(wrapped())) + jl_array_len(wrapped()));
return const_iterator(jlcxx_array_data<julia_t>(wrapped()) + jl_array_len(wrapped()));
}

void push_back(const ValueT& val)
Expand All @@ -217,18 +225,18 @@ class ArrayRef
JL_GC_PUSH1(&arr_ptr);
const size_t pos = size();
jl_array_grow_end(arr_ptr, 1);
jl_arrayset(arr_ptr, box<ValueT>(val), pos);
data()[pos] = val;
JL_GC_POP();
}

const julia_t* data() const
{
return (julia_t*)jl_array_data(wrapped());
return jlcxx_array_data<julia_t>(wrapped());
}

julia_t* data()
{
return (julia_t*)jl_array_data(wrapped());
return jlcxx_array_data<julia_t>(wrapped());
}

std::size_t size() const
Expand Down

0 comments on commit 260ea7e

Please sign in to comment.