-
Notifications
You must be signed in to change notification settings - Fork 2
/
get.go
275 lines (219 loc) · 6.26 KB
/
get.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package hdf5
// #include <stdlib.h>
// #include <string.h>
// #include <hdf5.h>
import "C"
import (
"errors"
"reflect"
"unsafe"
)
// Get reads data from the file.
func (f *File) Get(name string, something interface{}) error {
value := reflect.ValueOf(something)
if value.Kind() != reflect.Ptr {
return errors.New("expected a pointer")
}
value = reflect.Indirect(value)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
did := C.H5Dopen2(f.fid, cname, C.H5P_DEFAULT)
if did < 0 {
return errors.New("cannot find the dataset")
}
defer C.H5Dclose(did)
object := newObject()
defer object.free()
object.tid = C.H5Dget_type(did)
if object.tid < 0 {
return errors.New("cannot get the datatype of the dataset")
}
if err := initializeToGet(object, value); err != nil {
return err
}
if C.H5Dread(did, object.tid, C.H5S_ALL, C.H5S_ALL, C.H5P_DEFAULT, object.data) < 0 {
return errors.New("cannot read the dataset from the file")
}
if err := finalizeToGet(object, value); err != nil {
return err
}
one := C.hsize_t(1)
sid := C.H5Screate_simple(1, (*C.hsize_t)(unsafe.Pointer(&one)), nil)
if sid < 0 {
return errors.New("cannot create a data space")
}
defer C.H5Sclose(sid)
if C.H5Dvlen_reclaim(object.tid, sid, C.H5P_DEFAULT, object.data) < 0 {
return errors.New("cannot reclaim memory")
}
return nil
}
func initializeToGet(object *object, value reflect.Value) error {
switch value.Kind() {
case reflect.Slice:
return initializeSliceToGet(object, value)
case reflect.Struct:
return initializeStructToGet(object, value)
default:
return initializeScalarToGet(object, value)
}
}
func finalizeToGet(object *object, value reflect.Value) error {
switch value.Kind() {
case reflect.Struct:
return finalizeStructToGet(object, value)
default:
return nil
}
}
func initializeScalarToGet(object *object, value reflect.Value) error {
bid, ok := kindTypeMapping[value.Kind()]
if !ok {
return errors.New("encountered an unsupported datatype")
}
if err := checkArrayType(object.tid, bid); err != nil {
return err
}
if length, err := computeArrayLength(object.tid); err != nil {
return err
} else if length != 1 {
return errors.New("expected an array with a single element")
}
object.data = unsafe.Pointer(value.Addr().Pointer())
return nil
}
func initializeSliceToGet(object *object, value reflect.Value) error {
typo := value.Type()
bid, ok := kindTypeMapping[typo.Elem().Kind()]
if !ok {
return errors.New("encountered an unsupported datatype")
}
if err := checkArrayType(object.tid, bid); err != nil {
return err
}
length, err := computeArrayLength(object.tid)
if err != nil {
return err
}
buffer := reflect.MakeSlice(typo, int(length), int(length))
shadow := reflect.Indirect(reflect.New(typo))
shadow.Set(buffer)
src := (*reflect.SliceHeader)(unsafe.Pointer(shadow.UnsafeAddr()))
dst := (*reflect.SliceHeader)(unsafe.Pointer(value.UnsafeAddr()))
dst.Data, src.Data = src.Data, dst.Data
dst.Cap, src.Cap = src.Cap, dst.Cap
dst.Len, src.Len = src.Len, dst.Len
object.data = unsafe.Pointer(dst.Data)
object.flag |= flagVariableLength
return nil
}
func initializeStructToGet(object *object, value reflect.Value) error {
if tid := C.H5Tget_class(object.tid); tid < 0 {
return errors.New("cannot get a data class")
} else if tid != C.H5T_COMPOUND {
return errors.New("expected a compound datatype")
}
size := C.H5Tget_size(object.tid)
if size < 0 {
return errors.New("cannot get the size of a compound datatype")
}
object.data = C.malloc(size)
if object.data == nil {
return errors.New("cannot allocate memory")
}
object.flag |= flagOwnedMemory
return nil
}
func finalizeStructToGet(object *object, value reflect.Value) error {
typo := value.Type()
count := typo.NumField()
for i := 0; i < count; i++ {
field := typo.Field(i)
if len(field.PkgPath) > 0 {
continue
}
cname := C.CString(field.Name)
defer C.free(unsafe.Pointer(cname))
j := C.H5Tget_member_index(object.tid, cname)
if j < 0 {
continue
}
o := object.new()
o.tid = C.H5Tget_member_type(object.tid, C.uint(j))
if o.tid < 0 {
return errors.New("cannot get the datatype of a field")
}
if cid := C.H5Tget_class(o.tid); cid < 0 {
return errors.New("cannot get the data class of a field")
} else if cid == C.H5T_VLEN {
if tid := C.H5Tget_super(o.tid); tid < 0 { // Close?
return errors.New("cannot get the base type of a field")
} else {
o = object.new()
o.tid = tid
}
}
if err := initializeToGet(o, value.Field(i)); err != nil {
return err
}
size := C.H5Tget_size(o.tid)
if size < 0 {
return errors.New("cannot get a size")
}
offset := C.H5Tget_member_offset(object.tid, C.uint(j))
address := unsafe.Pointer(uintptr(object.data) + uintptr(offset))
if o.flag&flagVariableLength != 0 {
h := (*C.hvl_t)(address)
if h.len != 1 {
return errors.New("expected a variable-length datatype with a single element")
}
address = h.p
}
copyMemory(uintptr(o.data), uintptr(address), int(size))
if err := finalizeToGet(o, value.Field(i)); err != nil {
return err
}
}
return nil
}
func checkArrayType(tid C.hid_t, bid C.hid_t) error {
if cid := C.H5Tget_class(tid); cid < 0 {
return errors.New("cannot get a data class")
} else if cid != C.H5T_ARRAY {
return errors.New("expected an array datatype")
}
if tid := C.H5Tget_super(tid); tid < 0 { // Close?
return errors.New("cannot get the base type of a datatype")
} else if C.H5Tequal(bid, tid) == 0 {
return errors.New("the types do not match")
}
return nil
}
func computeArrayLength(tid C.hid_t) (C.hsize_t, error) {
nd := C.H5Tget_array_ndims(tid)
if nd < 0 {
return 0, errors.New("cannot get the dimensionality of an array")
}
dimensions := make([]C.hsize_t, nd)
if C.H5Tget_array_dims2(tid, (*C.hsize_t)(unsafe.Pointer(&dimensions[0]))) != nd {
return 0, errors.New("cannot get the dimensions of an array")
}
length := C.hsize_t(1)
for i := range dimensions {
length *= dimensions[i]
}
return length, nil
}
func copyMemory(into, from uintptr, size int) {
intoHeader := reflect.SliceHeader{
Data: into,
Len: size,
Cap: size,
}
fromHeader := reflect.SliceHeader{
Data: from,
Len: size,
Cap: size,
}
copy(*(*[]byte)(unsafe.Pointer(&intoHeader)), *(*[]byte)(unsafe.Pointer(&fromHeader)))
}