Skip to content

Commit

Permalink
fix(arrow/cdata): move headers into parent Go package (#251)
Browse files Browse the repository at this point in the history
### Rationale for this change
fixes #244, also addresses the issue discovered by
apache/arrow-adbc#2449 (comment)

### What changes are included in this PR?
Moving the `abi.h` and `helpers.h` headers up into the parent `cdata` Go
package. Also adding `static inline` to the helper methods to fix
undefined symbol issues when building with `CGO_CFLAGS=-g3`.

### Are these changes tested?
Yes, existing unit tests will cover the changes.

### Are there any user-facing changes?
No
  • Loading branch information
zeroshade authored Jan 16, 2025
1 parent 1c5598e commit f533d20
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions arrow/cdata/cdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ package cdata

// implement handling of the Arrow C Data Interface. At least from a consuming side.

// #include "arrow/c/abi.h"
// #include "arrow/c/helpers.h"
// #include "abi.h"
// #include "helpers.h"
// #include <stdlib.h>
// int stream_get_schema(struct ArrowArrayStream* st, struct ArrowSchema* out) { return st->get_schema(st, out); }
// int stream_get_next(struct ArrowArrayStream* st, struct ArrowArray* out) { return st->get_next(st, out); }
Expand Down
2 changes: 1 addition & 1 deletion arrow/cdata/cdata_allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package cdata

// #include <stdlib.h>
// #include "arrow/c/abi.h"
// #include "abi.h"
import "C"

import (
Expand Down
4 changes: 2 additions & 2 deletions arrow/cdata/cdata_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package cdata
// #include <errno.h>
// #include <stdint.h>
// #include <stdlib.h>
// #include "arrow/c/abi.h"
// #include "arrow/c/helpers.h"
// #include "abi.h"
// #include "helpers.h"
//
// extern void releaseExportedSchema(struct ArrowSchema* schema);
// extern void releaseExportedArray(struct ArrowArray* array);
Expand Down
4 changes: 2 additions & 2 deletions arrow/cdata/cdata_fulltest.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "arrow/c/abi.h"
#include "arrow/c/helpers.h"
#include "abi.h"
#include "helpers.h"
#include "utils.h"

int is_little_endian()
Expand Down
4 changes: 2 additions & 2 deletions arrow/cdata/cdata_test_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package cdata
// #include <stdlib.h>
// #include <stdint.h>
// #include <string.h>
// #include "arrow/c/abi.h"
// #include "arrow/c/helpers.h"
// #include "abi.h"
// #include "helpers.h"
//
// void setup_array_stream_test(const int n_batches, struct ArrowArrayStream* out);
// static struct ArrowArray* get_test_arr() {
Expand Down
4 changes: 2 additions & 2 deletions arrow/cdata/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (

// #include <stdlib.h>
// #include <errno.h>
// #include "arrow/c/abi.h"
// #include "arrow/c/helpers.h"
// #include "abi.h"
// #include "helpers.h"
//
// typedef const char cchar_t;
// extern int streamGetSchema(struct ArrowArrayStream*, struct ArrowSchema*);
Expand Down
42 changes: 21 additions & 21 deletions arrow/cdata/arrow/c/helpers.h → arrow/cdata/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <stdlib.h>
#include <string.h>

#include "arrow/c/abi.h"
#include "abi.h"

#define ARROW_C_ASSERT(condition, msg) \
do { \
Expand All @@ -37,28 +37,28 @@ extern "C" {
#endif

/// Query whether the C schema is released
inline int ArrowSchemaIsReleased(const struct ArrowSchema* schema) {
static inline int ArrowSchemaIsReleased(const struct ArrowSchema* schema) {
return schema->release == NULL;
}

/// Mark the C schema released (for use in release callbacks)
inline void ArrowSchemaMarkReleased(struct ArrowSchema* schema) {
static inline void ArrowSchemaMarkReleased(struct ArrowSchema* schema) {
schema->release = NULL;
}

/// Move the C schema from `src` to `dest`
///
/// Note `dest` must *not* point to a valid schema already, otherwise there
/// will be a memory leak.
inline void ArrowSchemaMove(struct ArrowSchema* src, struct ArrowSchema* dest) {
static inline void ArrowSchemaMove(struct ArrowSchema* src, struct ArrowSchema* dest) {
assert(dest != src);
assert(!ArrowSchemaIsReleased(src));
memcpy(dest, src, sizeof(struct ArrowSchema));
ArrowSchemaMarkReleased(src);
}

/// Release the C schema, if necessary, by calling its release callback
inline void ArrowSchemaRelease(struct ArrowSchema* schema) {
static inline void ArrowSchemaRelease(struct ArrowSchema* schema) {
if (!ArrowSchemaIsReleased(schema)) {
schema->release(schema);
ARROW_C_ASSERT(ArrowSchemaIsReleased(schema),
Expand All @@ -67,33 +67,33 @@ inline void ArrowSchemaRelease(struct ArrowSchema* schema) {
}

/// Query whether the C array is released
inline int ArrowArrayIsReleased(const struct ArrowArray* array) {
static inline int ArrowArrayIsReleased(const struct ArrowArray* array) {
return array->release == NULL;
}

inline int ArrowDeviceArrayIsReleased(const struct ArrowDeviceArray* array) {
static inline int ArrowDeviceArrayIsReleased(const struct ArrowDeviceArray* array) {
return ArrowArrayIsReleased(&array->array);
}

/// Mark the C array released (for use in release callbacks)
inline void ArrowArrayMarkReleased(struct ArrowArray* array) { array->release = NULL; }
static inline void ArrowArrayMarkReleased(struct ArrowArray* array) { array->release = NULL; }

inline void ArrowDeviceArrayMarkReleased(struct ArrowDeviceArray* array) {
static inline void ArrowDeviceArrayMarkReleased(struct ArrowDeviceArray* array) {
ArrowArrayMarkReleased(&array->array);
}

/// Move the C array from `src` to `dest`
///
/// Note `dest` must *not* point to a valid array already, otherwise there
/// will be a memory leak.
inline void ArrowArrayMove(struct ArrowArray* src, struct ArrowArray* dest) {
static inline void ArrowArrayMove(struct ArrowArray* src, struct ArrowArray* dest) {
assert(dest != src);
assert(!ArrowArrayIsReleased(src));
memcpy(dest, src, sizeof(struct ArrowArray));
ArrowArrayMarkReleased(src);
}

inline void ArrowDeviceArrayMove(struct ArrowDeviceArray* src,
static inline void ArrowDeviceArrayMove(struct ArrowDeviceArray* src,
struct ArrowDeviceArray* dest) {
assert(dest != src);
assert(!ArrowDeviceArrayIsReleased(src));
Expand All @@ -102,15 +102,15 @@ inline void ArrowDeviceArrayMove(struct ArrowDeviceArray* src,
}

/// Release the C array, if necessary, by calling its release callback
inline void ArrowArrayRelease(struct ArrowArray* array) {
static inline void ArrowArrayRelease(struct ArrowArray* array) {
if (!ArrowArrayIsReleased(array)) {
array->release(array);
ARROW_C_ASSERT(ArrowArrayIsReleased(array),
"ArrowArrayRelease did not cleanup release callback");
}
}

inline void ArrowDeviceArrayRelease(struct ArrowDeviceArray* array) {
static inline void ArrowDeviceArrayRelease(struct ArrowDeviceArray* array) {
if (!ArrowDeviceArrayIsReleased(array)) {
array->array.release(&array->array);
ARROW_C_ASSERT(ArrowDeviceArrayIsReleased(array),
Expand All @@ -119,36 +119,36 @@ inline void ArrowDeviceArrayRelease(struct ArrowDeviceArray* array) {
}

/// Query whether the C array stream is released
inline int ArrowArrayStreamIsReleased(const struct ArrowArrayStream* stream) {
static inline int ArrowArrayStreamIsReleased(const struct ArrowArrayStream* stream) {
return stream->release == NULL;
}

inline int ArrowDeviceArrayStreamIsReleased(const struct ArrowDeviceArrayStream* stream) {
static inline int ArrowDeviceArrayStreamIsReleased(const struct ArrowDeviceArrayStream* stream) {
return stream->release == NULL;
}

/// Mark the C array stream released (for use in release callbacks)
inline void ArrowArrayStreamMarkReleased(struct ArrowArrayStream* stream) {
static inline void ArrowArrayStreamMarkReleased(struct ArrowArrayStream* stream) {
stream->release = NULL;
}

inline void ArrowDeviceArrayStreamMarkReleased(struct ArrowDeviceArrayStream* stream) {
static inline void ArrowDeviceArrayStreamMarkReleased(struct ArrowDeviceArrayStream* stream) {
stream->release = NULL;
}

/// Move the C array stream from `src` to `dest`
///
/// Note `dest` must *not* point to a valid stream already, otherwise there
/// will be a memory leak.
inline void ArrowArrayStreamMove(struct ArrowArrayStream* src,
static inline void ArrowArrayStreamMove(struct ArrowArrayStream* src,
struct ArrowArrayStream* dest) {
assert(dest != src);
assert(!ArrowArrayStreamIsReleased(src));
memcpy(dest, src, sizeof(struct ArrowArrayStream));
ArrowArrayStreamMarkReleased(src);
}

inline void ArrowDeviceArrayStreamMove(struct ArrowDeviceArrayStream* src,
static inline void ArrowDeviceArrayStreamMove(struct ArrowDeviceArrayStream* src,
struct ArrowDeviceArrayStream* dest) {
assert(dest != src);
assert(!ArrowDeviceArrayStreamIsReleased(src));
Expand All @@ -157,15 +157,15 @@ inline void ArrowDeviceArrayStreamMove(struct ArrowDeviceArrayStream* src,
}

/// Release the C array stream, if necessary, by calling its release callback
inline void ArrowArrayStreamRelease(struct ArrowArrayStream* stream) {
static inline void ArrowArrayStreamRelease(struct ArrowArrayStream* stream) {
if (!ArrowArrayStreamIsReleased(stream)) {
stream->release(stream);
ARROW_C_ASSERT(ArrowArrayStreamIsReleased(stream),
"ArrowArrayStreamRelease did not cleanup release callback");
}
}

inline void ArrowDeviceArrayStreamRelease(struct ArrowDeviceArrayStream* stream) {
static inline void ArrowDeviceArrayStreamRelease(struct ArrowDeviceArrayStream* stream) {
if (!ArrowDeviceArrayStreamIsReleased(stream)) {
stream->release(stream);
ARROW_C_ASSERT(ArrowDeviceArrayStreamIsReleased(stream),
Expand Down
2 changes: 1 addition & 1 deletion arrow/cdata/import_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/apache/arrow-go/v18/arrow/internal/debug"
)

// #include "arrow/c/helpers.h"
// #include "helpers.h"
// #include <stdlib.h>
import "C"

Expand Down
2 changes: 1 addition & 1 deletion arrow/cdata/trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <string.h>

#include "arrow/c/abi.h"
#include "abi.h"

int streamGetSchema(struct ArrowArrayStream*, struct ArrowSchema*);
int streamGetNext(struct ArrowArrayStream*, struct ArrowArray*);
Expand Down

0 comments on commit f533d20

Please sign in to comment.