Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-PLACET committed Apr 18, 2024
1 parent 6520ca1 commit aa6dc2f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 42 deletions.
77 changes: 42 additions & 35 deletions include/sparrow/c_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <vcruntime_new.h>

#include "sparrow/allocator.hpp"

Expand All @@ -28,36 +29,36 @@ extern "C"
struct ArrowSchema
{
// Array type description
const char* format;
const char* name;
const char* metadata;
int64_t flags;
int64_t n_children;
struct ArrowSchema** children;
struct ArrowSchema* dictionary;
const char* format = nullptr;
const char* name = nullptr;
const char* metadata = nullptr;
int64_t flags = 0;
int64_t n_children = 0;
struct ArrowSchema** children = nullptr;
struct ArrowSchema* dictionary = nullptr;

// Release callback
void (*release)(struct ArrowSchema*);
void (*release)(struct ArrowSchema*) = nullptr;
// Opaque producer-specific data
void* private_data;
void* private_data = nullptr;
};

struct ArrowArray
{
// Array data description
int64_t length;
int64_t null_count;
int64_t offset;
int64_t n_buffers;
int64_t n_children;
const void** buffers;
struct ArrowArray** children;
struct ArrowArray* dictionary;
int64_t length = 0;
int64_t null_count = 0;
int64_t offset = 0;
int64_t n_buffers = 0;
int64_t n_children = 0;
const void** buffers = nullptr;
struct ArrowArray** children = nullptr;
struct ArrowArray* dictionary = nullptr;

// Release callback
void (*release)(struct ArrowArray*);
void (*release)(struct ArrowArray*) = nullptr;
// Opaque producer-specific data
void* private_data;
void* private_data = nullptr;
};

#ifdef __cplusplus
Expand All @@ -66,23 +67,23 @@ extern "C"

namespace sparrow
{
template <class BufferType, class Allocator>
template <class BufferType, template <typename> class Allocator>
struct ArrowArrayPrivateData
{
ArrowArrayPrivateData()
: buffer_allocator(std::move(Allocator()))
: buffer_allocator(std::move(Allocator<BufferType>()))
{
}

explicit ArrowArrayPrivateData(const Allocator& allocator)
explicit ArrowArrayPrivateData(const Allocator<BufferType>& allocator)
: buffer_allocator(allocator)
{
}

any_allocator<BufferType> buffer_allocator;
};

template <template<typename> class Allocator>
template <template <typename> class Allocator>
struct ArrowSchemaPrivateData
{
ArrowSchemaPrivateData()
Expand Down Expand Up @@ -115,6 +116,7 @@ namespace sparrow
delete child;
}
delete obj.children;
obj.children = nullptr;
}

template <class T>
Expand All @@ -124,8 +126,9 @@ namespace sparrow
{
obj.dictionary->release(obj.dictionary);
assert(obj.dictionary->release == nullptr);
delete obj.dictionary;
}
delete obj.dictionary;
obj.dictionary = nullptr;
}

template <class T>
Expand All @@ -135,7 +138,7 @@ namespace sparrow
release_dictionary(obj);
}

template <typename Allocator, typename T>
template <typename T, template <typename> typename Allocator>
void delete_array(ArrowArray* array)
{
assert(array != nullptr);
Expand All @@ -146,15 +149,17 @@ namespace sparrow
const size_t buffers_size = array->n_buffers * (array->length + array->offset) * sizeof(T);
for (int64_t i = 0; i < array->n_buffers; ++i)
{
private_data->buffer_allocator.deallocate(static_cast<T*>(array->buffers[i]), buffers_size);
auto buffer = const_cast<void*>(array->buffers[i]);
private_data->buffer_allocator.deallocate(static_cast<T*>(buffer), buffers_size);
}
delete array->buffers;
array->buffers = nullptr;
delete static_cast<ArrowArrayPrivateData<T, Allocator>*>(array->private_data);
array->private_data = nullptr;
array->release = nullptr;
}

template <template<typename> class Allocator>
template <template <typename> class Allocator>
void delete_schema(ArrowSchema* schema)
{
assert(schema != nullptr);
Expand Down Expand Up @@ -191,7 +196,7 @@ namespace sparrow
schema->release = nullptr;
}

template <template<typename> class Allocator>
template <template <typename> class Allocator>
ArrowSchema make_schema_constructor(
const char* format,
const char* name,
Expand All @@ -206,9 +211,7 @@ namespace sparrow
schema.private_data = new ArrowSchemaPrivateData<Allocator>;
schema.release = delete_schema<Allocator>;

auto private_data = static_cast<ArrowSchemaPrivateData<Allocator>*>(
schema.private_data
);
auto private_data = static_cast<ArrowSchemaPrivateData<Allocator>*>(schema.private_data);

private_data->format_size = strlen(format) + 1;
schema.format = private_data->string_allocator.allocate(private_data->format_size);
Expand All @@ -232,7 +235,7 @@ namespace sparrow
return schema;
};

template <class T, class Allocator>
template <class T, template <typename> class Allocator>
ArrowArray make_array_constructor(
int64_t length,
int64_t null_count,
Expand All @@ -245,14 +248,18 @@ namespace sparrow
{
ArrowArray array;
array.private_data = new ArrowArrayPrivateData<T, Allocator>;
array.release = delete_array<Allocator, T>;
array.release = delete_array<T, Allocator>;
array.length = length;
array.null_count = null_count;
array.offset = offset;
array.n_buffers = n_buffers;
const size_t buffers_size = n_buffers * (length + offset) * sizeof(T);
array.buffers = static_cast<ArrowArrayPrivateData<T, Allocator>*>(array.private_data)
->buffer_allocator.allocate(buffers_size);
array.buffers = new const void*[n_buffers];
for (int64_t i = 0; i < n_buffers; ++i)
{
array.buffers[i] = static_cast<ArrowArrayPrivateData<T, Allocator>*>(array.private_data)
->buffer_allocator.allocate(buffers_size);
}
array.n_children = n_children;
array.children = children;
array.dictionary = dictionary;
Expand Down
59 changes: 52 additions & 7 deletions test/test_c_data_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

#include <cstring>
#include <memory_resource>

#include "sparrow/c_interface.hpp"

#include "doctest/doctest.h"
Expand All @@ -21,23 +23,66 @@ TEST_SUITE("C data interface")
{
TEST_CASE("make_schema_constructor")
{
auto schema = sparrow::make_schema_constructor<std::allocator>("format", "name", "metadata", 1, 0, nullptr, nullptr);
auto children = new ArrowSchema*[2];
children[0] = new ArrowSchema;
children[1] = new ArrowSchema;
auto dictionary = new ArrowSchema;

auto schema = sparrow::make_schema_constructor<std::pmr::polymorphic_allocator>(
"format",
"name",
"metadata",
1,
2,
children,
dictionary
);
CHECK_EQ(strcmp(schema.format, "format"), 0);
CHECK_EQ(strcmp(schema.name, "name"), 0);
CHECK_EQ(strcmp(schema.metadata, "metadata"), 0);
CHECK_EQ(schema.flags, 1);
CHECK_EQ(schema.n_children, 0);
CHECK_EQ(schema.children, nullptr);
CHECK_EQ(schema.dictionary, nullptr);
CHECK_EQ(schema.n_children, 2);
CHECK_EQ(schema.children, children);
CHECK_EQ(schema.dictionary, dictionary);
CHECK_NE(schema.release, nullptr);
CHECK_NE(schema.private_data, nullptr);

schema.release(&schema);
CHECK_EQ(schema.release, nullptr);
CHECK_EQ(schema.private_data, nullptr);

CHECK_EQ(schema.format, nullptr);
CHECK_EQ(schema.name, nullptr);
CHECK_EQ(schema.metadata, nullptr);
CHECK_EQ(schema.children, nullptr);
CHECK_EQ(schema.dictionary, nullptr);
CHECK_EQ(schema.release, nullptr);
CHECK_EQ(schema.private_data, nullptr);
}

TEST_CASE("make_array_constructor")
{
auto children = new ArrowArray*[2];
children[0] = new ArrowArray;
children[1] = new ArrowArray;
auto dictionary = new ArrowArray;

auto array = sparrow::make_array_constructor<int, std::allocator>(1, 0, 0, 1, 2, children, dictionary);
CHECK_EQ(array.length, 1);
CHECK_EQ(array.null_count, 0);
CHECK_EQ(array.offset, 0);
CHECK_EQ(array.n_buffers, 1);
CHECK_EQ(array.n_children, 2);
CHECK_NE(array.buffers, nullptr);
CHECK_EQ(array.children, children);
CHECK_EQ(array.dictionary, dictionary);
CHECK_NE(array.release, nullptr);
CHECK_NE(array.private_data, nullptr);

array.release(&array);

CHECK_EQ(array.buffers, nullptr);
CHECK_EQ(array.children, nullptr);
CHECK_EQ(array.release, nullptr);
CHECK_EQ(array.private_data, nullptr);
}

// TEST_CASE_TEMPLATE_INVOKE(c_interface, std::allocator, std::pmr::polymorphic_allocator<int>);
Expand Down

0 comments on commit aa6dc2f

Please sign in to comment.