Skip to content

Commit 92bebd0

Browse files
authored
api!: add virtual heapsize() and footprint() to ImageInput and ImageOutput (AcademySoftwareFoundation#4323)
Second PR of two, following @lgritz and I discussions on memory tracking in the OIIO::ImageCache. - the memory tracking system from AcademySoftwareFoundation#4322 is not sufficient to track OIIO public objects that can be overriden. - add virtual `heapsize()` method to ImageInput and ImageOutput that return the total heap allocated memory held by the structure and its members. - [ ] **TODO**: override for every internal OIIO type (bmp, tiff, etc). Related PR from Larry : AcademySoftwareFoundation#4317 First PR: AcademySoftwareFoundation#4322 Signed-off-by: Basile Fraboni <basile.fraboni@gmail.com>
1 parent 101f950 commit 92bebd0

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

src/include/OpenImageIO/imageio.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,16 @@ class OIIO_API ImageInput {
17781778
/// `ImageInput*`.
17791779
typedef ImageInput* (*Creator)();
17801780

1781+
/// Memory tracking method.
1782+
/// Return the total heap memory allocated by `ImageInput`.
1783+
/// Overridable version of heapsize defined in memory.h.
1784+
virtual size_t heapsize() const;
1785+
1786+
/// Memory tracking method.
1787+
/// Return the total memory footprint of `ImageInput`.
1788+
/// Overridable version of footprint defined in memory.h.
1789+
virtual size_t footprint() const;
1790+
17811791
protected:
17821792
ImageSpec m_spec; // format spec of the current open subimage/MIPlevel
17831793
// BEWARE using m_spec directly -- not thread-safe
@@ -1886,7 +1896,7 @@ class OIIO_API ImageInput {
18861896

18871897
void append_error(string_view message) const; // add to error message
18881898

1889-
/// declare a friend heapsize definition
1899+
/// declare friend heapsize and footprint definitions
18901900
template <typename T> friend size_t pvt::heapsize(const T&);
18911901
};
18921902

@@ -2570,6 +2580,16 @@ class OIIO_API ImageOutput {
25702580
/// `ImageOutput*`.
25712581
typedef ImageOutput* (*Creator)();
25722582

2583+
/// Memory tracking method.
2584+
/// Return the total heap memory allocated by `ImageOutput`.
2585+
/// Overridable version of heapsize defined in memory.h.
2586+
virtual size_t heapsize() const;
2587+
2588+
/// Memory tracking method.
2589+
/// Return the total memory footprint of `ImageOutput`.
2590+
/// Overridable version of footprint defined in memory.h.
2591+
virtual size_t footprint() const;
2592+
25732593
protected:
25742594
/// @{
25752595
/// @name Helper functions for ImageOutput implementations.
@@ -2793,7 +2813,7 @@ class OIIO_API ImageOutput {
27932813

27942814
void append_error(string_view message) const; // add to m_errmessage
27952815

2796-
/// declare a friend heapsize definition
2816+
/// declare friend heapsize and footprint definitions
27972817
template <typename T> friend size_t pvt::heapsize(const T&);
27982818
};
27992819

@@ -2804,11 +2824,13 @@ class OIIO_API ImageOutput {
28042824
// heapsize specialization for `ImageSpec`
28052825
template <> OIIO_API size_t pvt::heapsize<ImageSpec>(const ImageSpec&);
28062826

2807-
// heapsize specialization for `ImageInput`
2827+
// heapsize and footprint specializations for `ImageInput`
28082828
template <> OIIO_API size_t pvt::heapsize<ImageInput>(const ImageInput&);
2829+
template <> OIIO_API size_t pvt::footprint<ImageInput>(const ImageInput&);
28092830

2810-
// heapsize specialization for `ImageOutput`
2831+
// heapsize and footprint specializations for `ImageOutput`
28112832
template <> OIIO_API size_t pvt::heapsize<ImageOutput>(const ImageOutput&);
2833+
template <> OIIO_API size_t pvt::footprint<ImageOutput>(const ImageOutput&);
28122834

28132835

28142836

src/libOpenImageIO/imageinput.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,13 +1341,47 @@ ImageInput::check_open(const ImageSpec& spec, ROI range, uint64_t /*flags*/)
13411341

13421342

13431343

1344+
template<>
1345+
inline size_t
1346+
pvt::heapsize<ImageInput::Impl>(const ImageInput::Impl& impl)
1347+
{
1348+
return impl.m_io_local ? sizeof(Filesystem::IOProxy) : 0;
1349+
}
1350+
1351+
1352+
1353+
size_t
1354+
ImageInput::heapsize() const
1355+
{
1356+
size_t size = pvt::heapsize(m_impl);
1357+
size += pvt::heapsize(m_spec);
1358+
return size;
1359+
}
1360+
1361+
1362+
1363+
size_t
1364+
ImageInput::footprint() const
1365+
{
1366+
return sizeof(ImageInput) + heapsize();
1367+
}
1368+
1369+
1370+
13441371
template<>
13451372
size_t
13461373
pvt::heapsize<ImageInput>(const ImageInput& input)
13471374
{
1348-
//! TODO: change ImageInput API to add a virtual heapsize() function
1349-
//! to allow per image input override, and call that function here.
1350-
return pvt::heapsize(input.m_spec);
1375+
return input.heapsize();
1376+
}
1377+
1378+
1379+
1380+
template<>
1381+
size_t
1382+
pvt::footprint<ImageInput>(const ImageInput& input)
1383+
{
1384+
return input.footprint();
13511385
}
13521386

13531387

src/libOpenImageIO/imageoutput.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,13 +1021,47 @@ ImageOutput::check_open(OpenMode mode, const ImageSpec& userspec, ROI range,
10211021

10221022

10231023

1024+
template<>
1025+
inline size_t
1026+
pvt::heapsize<ImageOutput::Impl>(const ImageOutput::Impl& impl)
1027+
{
1028+
return impl.m_io_local ? sizeof(Filesystem::IOProxy) : 0;
1029+
}
1030+
1031+
1032+
1033+
size_t
1034+
ImageOutput::heapsize() const
1035+
{
1036+
size_t size = pvt::heapsize(m_impl);
1037+
size += pvt::heapsize(m_spec);
1038+
return size;
1039+
}
1040+
1041+
1042+
1043+
size_t
1044+
ImageOutput::footprint() const
1045+
{
1046+
return sizeof(ImageOutput) + heapsize();
1047+
}
1048+
1049+
1050+
10241051
template<>
10251052
size_t
10261053
pvt::heapsize<ImageOutput>(const ImageOutput& output)
10271054
{
1028-
//! TODO: change ImageOutput API to add a virtual heapsize() function
1029-
//! to allow per image output override, and call that function here.
1030-
return pvt::heapsize(output.m_spec);
1055+
return output.heapsize();
1056+
}
1057+
1058+
1059+
1060+
template<>
1061+
size_t
1062+
pvt::footprint<ImageOutput>(const ImageOutput& output)
1063+
{
1064+
return output.footprint();
10311065
}
10321066

10331067

0 commit comments

Comments
 (0)