Skip to content

Commit

Permalink
hdf5: mark types with pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jun 29, 2018
1 parent 21dab89 commit 5de2378
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
12 changes: 9 additions & 3 deletions h5d_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (

type Dataset struct {
Identifier

typ *Datatype
}

func newDataset(id C.hid_t) *Dataset {
return &Dataset{Identifier{id}}
func newDataset(id C.hid_t, typ *Datatype) *Dataset {
return &Dataset{Identifier: Identifier{id}, typ: typ}
}

func createDataset(id C.hid_t, name string, dtype *Datatype, dspace *Dataspace, dcpl *PropList) (*Dataset, error) {
Expand All @@ -35,7 +37,7 @@ func createDataset(id C.hid_t, name string, dtype *Datatype, dspace *Dataspace,
if err := checkID(hid); err != nil {
return nil, err
}
return newDataset(hid), nil
return newDataset(hid, dtype), nil
}

// Close releases and terminates access to a dataset.
Expand Down Expand Up @@ -180,3 +182,7 @@ func (s *Dataset) Datatype() (*Datatype, error) {
}
return NewDatatype(dtype_id), nil
}

func (s *Dataset) hasGoPointer() bool {
return s.typ != nil && s.typ._hasGoPointer
}
2 changes: 1 addition & 1 deletion h5g_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (g *CommonFG) OpenDataset(name string) (*Dataset, error) {
if err := checkID(hid); err != nil {
return nil, err
}
return newDataset(hid), nil
return newDataset(hid, nil), nil
}

// NumObjects returns the number of objects in the Group.
Expand Down
1 change: 1 addition & 0 deletions h5t_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,6 @@ func makeGoStringDatatype() *Datatype {
if err != nil {
panic(err)
}
dt._hasGoPointer = true
return dt
}
33 changes: 24 additions & 9 deletions h5t_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (

type Datatype struct {
Identifier

_hasGoPointer bool
}

type TypeClass C.H5T_class_t
Expand Down Expand Up @@ -108,8 +110,7 @@ func OpenDatatype(c CommonFG, name string, tapl_id int) (*Datatype, error) {

// NewDatatype creates a Datatype from an hdf5 id.
func NewDatatype(id C.hid_t) *Datatype {
t := &Datatype{Identifier{id}}
return t
return &Datatype{Identifier: Identifier{id}}
}

// CreateDatatype creates a new datatype. The value of class must be T_COMPOUND,
Expand Down Expand Up @@ -152,10 +153,14 @@ func (t *Datatype) Committed() bool {
return C.H5Tcommitted(t.id) > 0
}

// Copy copies an existing datatype. The returned datatype must be closed by the
// user when it is no longer needed.
// Copy copies an existing datatype.
func (t *Datatype) Copy() (*Datatype, error) {
return copyDatatype(t.id)
c, err := copyDatatype(t.id)
if err != nil {
return nil, err
}
c._hasGoPointer = t._hasGoPointer
return c, nil
}

// copyDatatype should be called by any function wishing to return
Expand Down Expand Up @@ -204,7 +209,7 @@ func NewArrayType(base_type *Datatype, dims []int) (*ArrayType, error) {
if err := checkID(hid); err != nil {
return nil, err
}
t := &ArrayType{Datatype{Identifier{hid}}}
t := &ArrayType{Datatype{Identifier: Identifier{hid}}}
return t, nil
}

Expand Down Expand Up @@ -242,7 +247,8 @@ func NewVarLenType(base_type *Datatype) (*VarLenType, error) {
if err := checkID(id); err != nil {
return nil, err
}
t := &VarLenType{Datatype{Identifier{id}}}
t := &VarLenType{Datatype{Identifier: Identifier{id}}}
t._hasGoPointer = true
return t, nil
}

Expand All @@ -263,7 +269,7 @@ func NewCompoundType(size int) (*CompoundType, error) {
if err := checkID(id); err != nil {
return nil, err
}
t := &CompoundType{Datatype{Identifier{id}}}
t := &CompoundType{Datatype{Identifier: Identifier{id}}}
return t, nil
}

Expand Down Expand Up @@ -446,6 +452,9 @@ func NewDataTypeFromType(t reflect.Type) (*Datatype, error) {
if err != nil {
return nil, err
}
if field_dt._hasGoPointer {
cdt._hasGoPointer = true
}
offset := int(f.Offset + 0)
if field_dt == nil {
return nil, fmt.Errorf("pb with field [%d-%s]", i, f.Name)
Expand All @@ -462,7 +471,13 @@ func NewDataTypeFromType(t reflect.Type) (*Datatype, error) {
dt = &cdt.Datatype

case reflect.Ptr:
return NewDataTypeFromType(t.Elem())
dt, err = NewDataTypeFromType(t.Elem())
// TODO(kortschak): Make this less strict.
// This currently identifies all types with any pointers
// as _hasGoPointer. This is not necesssary since the CGo
// rules only ban Go pointers to Go pointers.
// Maybe _hasGoPointer should be _pointerPathCount int.
dt._hasGoPointer = true

default:
// Should never happen.
Expand Down

0 comments on commit 5de2378

Please sign in to comment.