From 65eacf4f8cef2f66724649ebddc898cdd3b894ab Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Mon, 15 Apr 2024 16:13:53 +0200 Subject: [PATCH] Add C interface --- CMakeLists.txt | 1 + include/sparrow/c_interface.hpp | 385 ++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 1 + test/test_c_data_interface.cpp | 126 +++++++++++ 4 files changed, 513 insertions(+) create mode 100644 include/sparrow/c_interface.hpp create mode 100644 test/test_c_data_interface.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ce38f02c..8663efa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(SPARROW_HEADERS ${SPARROW_INCLUDE_DIR}/sparrow/sparrow_version.hpp ${SPARROW_INCLUDE_DIR}/sparrow/typed_array.hpp ${SPARROW_INCLUDE_DIR}/sparrow/variable_size_binary_layout.hpp + ${SPARROW_INCLUDE_DIR}/sparrow/c_interface.hpp ${SPARROW_INCLUDE_DIR}/sparrow/details/3rdparty/float16_t.hpp ) diff --git a/include/sparrow/c_interface.hpp b/include/sparrow/c_interface.hpp new file mode 100644 index 00000000..3bb20199 --- /dev/null +++ b/include/sparrow/c_interface.hpp @@ -0,0 +1,385 @@ +// Copyright 2024 Man Group Operations Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include +#include + +#include "sparrow/allocator.hpp" +#include "sparrow/contracts.hpp" + +#ifdef __cplusplus +extern "C" +{ +#endif + + struct ArrowSchema + { + // Array type description + 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*) = nullptr; + // Opaque producer-specific data + void* private_data = nullptr; + }; + + struct ArrowArray + { + // Array data description + 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*) = nullptr; + // Opaque producer-specific data + void* private_data = nullptr; + }; + +#ifdef __cplusplus +} // extern "C" +#endif + +namespace sparrow +{ + /** + * Holds the allocator for the buffers of an ArrowArray. + * + * @tparam BufferType The type of the buffers. + * @tparam Allocator An allocator for BufferType. + */ + template class Allocator> + requires sparrow::allocator> + struct ArrowArrayPrivateData + { + ArrowArrayPrivateData() + : buffer_allocator(std::move(Allocator())) + { + } + + explicit ArrowArrayPrivateData(const Allocator& allocator) + : buffer_allocator(allocator) + { + } + + any_allocator buffer_allocator; + }; + + /** + * Holds the allocator for the strings of an ArrowSchema. + * + * @tparam Allocator An allocator for char. + */ + template