Skip to content

Commit

Permalink
Add H5Treclaim API and implement quick example usage for object reads
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendersonHDF committed Jan 12, 2024
1 parent 4c01efa commit 9004821
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions h5py/api_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ hdf5:
hid_t H5Tget_native_type(hid_t type_id, H5T_direction_t direction)
herr_t H5Tcommit(hid_t loc_id, char *name, hid_t dtype_id, hid_t lcpl_id, hid_t tcpl_id, hid_t tapl_id)
hid_t H5Tget_create_plist(hid_t dtype_id)
1.12.0 herr_t H5Treclaim(hid_t type_id, hid_t space_id, hid_t plist_id, void *buf)

hid_t H5Tdecode(unsigned char *buf)
herr_t H5Tencode(hid_t obj_id, unsigned char *buf, size_t *nalloc)
Expand Down
29 changes: 29 additions & 0 deletions h5py/h5a.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
Provides access to the low-level HDF5 "H5A" attribute interface.
"""

include "config.pxi"

# C-level imports
from ._objects cimport pdefault
from .h5t cimport TypeID, typewrap, py_create
Expand Down Expand Up @@ -441,3 +443,30 @@ cdef class AttrID(ObjectID):
Get the amount of storage required for this attribute.
"""
return H5Aget_storage_size(self.id)

IF HDF5_VERSION >= (1, 12, 0):

@with_phil
def reclaim(self, ndarray arr not None, TypeID mtype=None):
""" no return
Reclaims memory allocated by the HDF5 library after reading
from an attribute with a variable-length or reference datatype
"""
cdef mspace_id, mtype_id
cdef void* data

try:
if mtype is None:
mtype = py_create(arr.dtype)

mspace_id = H5Aget_space(self.id)

mtype_id = mtype.id
data = PyArray_DATA(arr)

H5Treclaim(mtype_id, mspace_id, H5P_DEFAULT, data)

finally:
if mspace_id:
H5Aclose(mspace_id)
31 changes: 30 additions & 1 deletion h5py/h5d.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,36 @@ cdef class DatasetID(ObjectID):
"""
H5Drefresh(self.id)

IF HDF5_VERSION >= (1, 12, 0):

@with_phil
def reclaim(self, SpaceID mspace not None, ndarray arr_obj not None, TypeID mtype=None):
""" no return
Reclaims memory allocated by the HDF5 library after reading
from a dataset with a variable-length or reference datatype
"""
cdef mspace_id, mtype_id
cdef void* data

try:
if mtype is None:
mtype = py_create(arr_obj.dtype)

if mspace.id == H5S_ALL:
mspace_id = H5Dget_space(self.id)
else:
mspace_id = mspace.id

mtype_id = mtype.id
data = PyArray_DATA(arr_obj)

H5Treclaim(mtype_id, mspace_id, H5P_DEFAULT, data)

finally:
if mspace_id:
H5Sclose(mspace_id)

def write_direct_chunk(self, offsets, data, filter_mask=0x00000000, PropID dxpl=None):
""" (offsets, data, uint32_t filter_mask=0x00000000, PropID dxpl=None)
Expand Down Expand Up @@ -510,7 +540,6 @@ cdef class DatasetID(ObjectID):
if space_id:
H5Sclose(space_id)


@cython.boundscheck(False)
@cython.wraparound(False)
def read_direct_chunk(self, offsets, PropID dxpl=None, unsigned char[::1] out=None):
Expand Down

0 comments on commit 9004821

Please sign in to comment.