Skip to content

Commit

Permalink
New set of member functions h5pp::File::vlen???(...) to control tra…
Browse files Browse the repository at this point in the history
…cking of variable-length data allocations made by HDF5. Users can enable/disable tracking and call `h5pp::File::vlenReclaim()` to free all allocated memory in one go if tracking is enabled.
  • Loading branch information
DavidAce committed Oct 1, 2022
1 parent 6f91fb8 commit 9d25d58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/example-04d-tables-vlen-arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main() {
scienceReadEntry.emplace_back(ScienceEntry{helper.index, std::vector<double>(data_begin, data_end)});
}
// Free hvl_t data! h5pp stores metadata when it detects H5T_VLEN data being read, so that we can reclaim (free) it later.
file.reclaim(); // Note that you can also reclaim from TableInfo objects
file.vlenReclaim(); // Note that you can also reclaim from TableInfo objects

// Print data
h5pp::print("Wrote and read entry:\n");
Expand Down
11 changes: 10 additions & 1 deletion include/h5pp/details/h5ppFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,20 @@ namespace h5pp {
H5Eprint(H5E_DEFAULT, stderr);
}
/*! Calls H5Treclaim(...) on any data that HDF5 may have allocated for variable-length data during the last reads */
void reclaim() const {
void vlenReclaim() const {
for(auto &item : reclaimStack) { item.reclaim(); }
reclaimStack.clear();
}

/*! Drop all tracked claims to allocated variable-length data (users should call H5Treclaim or free manually) */
void vlenDropReclaims() const { reclaimStack.clear(); }

/*! Enable tracking of variable-length data allocations (e.g. when reading tables containgin H5T_VLEN members) */
void vlenEnableReclaimsTracking() { plists.vlenTrackReclaims = true; }

/*! Disable tracking of variable-length data allocations (users should call H5Treclaim or free manually) */
void vlenDisableReclaimsTracking() { plists.vlenTrackReclaims = false; }

/*! Returns an HDF5 file handle
*
* - The file permission is set when initializing h5pp::File.
Expand Down
21 changes: 12 additions & 9 deletions include/h5pp/details/h5ppInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ namespace h5pp {
std::string tag; /*!< The name of the object that was read */
mutable long counter = 0; // Counts how many instances of this object there are
public:
void drop() {
type = std::nullopt;
space = std::nullopt;
plist = std::nullopt;
buf = nullptr;
tag = "";
counter = 0;
}
void reclaim() {
if(buf != nullptr and type and space and plist) {
h5pp::logger::log->trace("Reclaiming vlen buffer from reading [{}]", tag);
Expand All @@ -147,12 +155,7 @@ namespace h5pp {
H5Dvlen_reclaim(type.value(), space.value(), plist.value(), buf);
#endif
}
type = std::nullopt;
space = std::nullopt;
plist = std::nullopt;
buf = nullptr;
tag = "";
counter = 0;
drop();
}
Reclaim() = default;
Reclaim(const hid::h5t &type, const hid::h5s &space, const hid::h5p &plist, void *buf, const std::string &tag)
Expand Down Expand Up @@ -184,13 +187,13 @@ namespace h5pp {
counter--;
if(buf != nullptr and counter == 0)
h5pp::logger::log->warn("~Reclaim: buffer for variable-length array likely remains after reading [{}]. "
"Call h5pp::File::reclaim() or <Dset|Attr|Table>Info::reclaim() to avoid a memory leak.",
tag);
"Call h5pp::File::reclaim() or <Dset|Attr|Table>Info::vlenReclaim() to avoid a memory leak.",
tag);
}
};

public:
mutable std::optional<Reclaim> reclaimInfo = /*!< Used to reclaim any memory allocated for variable-length arrays */
mutable std::optional<Reclaim> reclaimInfo = /*!< Used to vlenReclaim any memory allocated for variable-length arrays */
std::nullopt;

/*! Calls H5Treclaim(...) on memory that HDF5 allocates when reading variable-length arrays */
Expand Down

0 comments on commit 9d25d58

Please sign in to comment.