File size: 4,533 Bytes
7a1cef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "arrow/c/abi.h"

#define ARROW_C_ASSERT(condition, msg)                          \
  do {                                                          \
    if (!(condition)) {                                         \
      fprintf(stderr, "%s:%d:: %s", __FILE__, __LINE__, (msg)); \
      abort();                                                  \
    }                                                           \
  } while (0)

#ifdef __cplusplus
extern "C" {
#endif

/// Query whether the C schema is released
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) {
  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) {
  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) {
  if (!ArrowSchemaIsReleased(schema)) {
    schema->release(schema);
    ARROW_C_ASSERT(ArrowSchemaIsReleased(schema),
                   "ArrowSchemaRelease did not cleanup release callback");
  }
}

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

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

/// 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) {
  assert(dest != src);
  assert(!ArrowArrayIsReleased(src));
  memcpy(dest, src, sizeof(struct ArrowArray));
  ArrowArrayMarkReleased(src);
}

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

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

/// Mark the C array stream released (for use in release callbacks)
inline void ArrowArrayStreamMarkReleased(struct ArrowArrayStream* 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,
                                 struct ArrowArrayStream* dest) {
  assert(dest != src);
  assert(!ArrowArrayStreamIsReleased(src));
  memcpy(dest, src, sizeof(struct ArrowArrayStream));
  ArrowArrayStreamMarkReleased(src);
}

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

#ifdef __cplusplus
}
#endif