]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/go/arrow/cdata/exports.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / cdata / exports.go
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package cdata
18
19 import (
20 "reflect"
21 "sync"
22 "sync/atomic"
23 "unsafe"
24
25 "github.com/apache/arrow/go/v6/arrow/array"
26 )
27
28 // #include <stdlib.h>
29 // #include "arrow/c/helpers.h"
30 import "C"
31
32 var (
33 handles = sync.Map{}
34 handleIdx uintptr
35 )
36
37 type dataHandle uintptr
38
39 func storeData(d *array.Data) dataHandle {
40 h := atomic.AddUintptr(&handleIdx, 1)
41 if h == 0 {
42 panic("cgo: ran out of space")
43 }
44 d.Retain()
45 handles.Store(h, d)
46 return dataHandle(h)
47 }
48
49 func (d dataHandle) releaseData() {
50 arrd, ok := handles.LoadAndDelete(uintptr(d))
51 if !ok {
52 panic("cgo: invalid datahandle")
53 }
54 arrd.(*array.Data).Release()
55 }
56
57 //export releaseExportedSchema
58 func releaseExportedSchema(schema *CArrowSchema) {
59 if C.ArrowSchemaIsReleased(schema) == 1 {
60 return
61 }
62 defer C.ArrowSchemaMarkReleased(schema)
63
64 C.free(unsafe.Pointer(schema.name))
65 C.free(unsafe.Pointer(schema.format))
66 C.free(unsafe.Pointer(schema.metadata))
67
68 if schema.n_children == 0 {
69 return
70 }
71
72 var children []*CArrowSchema
73 s := (*reflect.SliceHeader)(unsafe.Pointer(&children))
74 s.Data = uintptr(unsafe.Pointer(schema.children))
75 s.Len = int(schema.n_children)
76 s.Cap = int(schema.n_children)
77
78 for _, c := range children {
79 C.ArrowSchemaRelease(c)
80 }
81
82 C.free(unsafe.Pointer(children[0]))
83 C.free(unsafe.Pointer(schema.children))
84 }
85
86 //export releaseExportedArray
87 func releaseExportedArray(arr *CArrowArray) {
88 if C.ArrowArrayIsReleased(arr) == 1 {
89 return
90 }
91 defer C.ArrowArrayMarkReleased(arr)
92
93 if arr.n_buffers > 0 {
94 C.free(unsafe.Pointer(arr.buffers))
95 }
96
97 if arr.n_children > 0 {
98 var children []*CArrowArray
99 s := (*reflect.SliceHeader)(unsafe.Pointer(&children))
100 s.Data = uintptr(unsafe.Pointer(arr.children))
101 s.Len = int(arr.n_children)
102 s.Cap = int(arr.n_children)
103
104 for _, c := range children {
105 C.ArrowArrayRelease(c)
106 }
107 C.free(unsafe.Pointer(children[0]))
108 C.free(unsafe.Pointer(arr.children))
109 }
110
111 h := dataHandle(arr.private_data)
112 h.releaseData()
113 }