Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add virtual heapsize() to ImageInput and ImageOutput #4323

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/include/OpenImageIO/imageio.h
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,16 @@ class OIIO_API ImageInput {
/// `ImageInput*`.
typedef ImageInput* (*Creator)();

/// Memory tracking method.
/// Return the total heap memory allocated by `ImageInput`.
/// Overridable version of heapsize defined in memory.h.
virtual size_t heapsize() const;

/// Memory tracking method.
/// Return the total memory footprint of `ImageInput`.
/// Overridable version of footprint defined in memory.h.
virtual size_t footprint() const;

protected:
ImageSpec m_spec; // format spec of the current open subimage/MIPlevel
// BEWARE using m_spec directly -- not thread-safe
Expand Down Expand Up @@ -1886,7 +1896,7 @@ class OIIO_API ImageInput {

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

/// declare a friend heapsize definition
/// declare friend heapsize and footprint definitions
template <typename T> friend size_t pvt::heapsize(const T&);
};

Expand Down Expand Up @@ -2570,6 +2580,16 @@ class OIIO_API ImageOutput {
/// `ImageOutput*`.
typedef ImageOutput* (*Creator)();

/// Memory tracking method.
/// Return the total heap memory allocated by `ImageOutput`.
/// Overridable version of heapsize defined in memory.h.
virtual size_t heapsize() const;

/// Memory tracking method.
/// Return the total memory footprint of `ImageOutput`.
/// Overridable version of footprint defined in memory.h.
virtual size_t footprint() const;

protected:
/// @{
/// @name Helper functions for ImageOutput implementations.
Expand Down Expand Up @@ -2793,7 +2813,7 @@ class OIIO_API ImageOutput {

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

/// declare a friend heapsize definition
/// declare friend heapsize and footprint definitions
template <typename T> friend size_t pvt::heapsize(const T&);
};

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

// heapsize specialization for `ImageInput`
// heapsize and footprint specializations for `ImageInput`
template <> OIIO_API size_t pvt::heapsize<ImageInput>(const ImageInput&);
template <> OIIO_API size_t pvt::footprint<ImageInput>(const ImageInput&);

// heapsize specialization for `ImageOutput`
// heapsize and footprint specializations for `ImageOutput`
template <> OIIO_API size_t pvt::heapsize<ImageOutput>(const ImageOutput&);
template <> OIIO_API size_t pvt::footprint<ImageOutput>(const ImageOutput&);



Expand Down
40 changes: 37 additions & 3 deletions src/libOpenImageIO/imageinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,13 +1341,47 @@ ImageInput::check_open(const ImageSpec& spec, ROI range, uint64_t /*flags*/)



template<>
inline size_t
pvt::heapsize<ImageInput::Impl>(const ImageInput::Impl& impl)
{
return impl.m_io_local ? sizeof(Filesystem::IOProxy) : 0;
}



size_t
ImageInput::heapsize() const
{
size_t size = pvt::heapsize(m_impl);
size += pvt::heapsize(m_spec);
return size;
}



size_t
ImageInput::footprint() const
{
return sizeof(ImageInput) + heapsize();
}



template<>
size_t
pvt::heapsize<ImageInput>(const ImageInput& input)
{
//! TODO: change ImageInput API to add a virtual heapsize() function
//! to allow per image input override, and call that function here.
return pvt::heapsize(input.m_spec);
return input.heapsize();
}



template<>
size_t
pvt::footprint<ImageInput>(const ImageInput& input)
{
return input.footprint();
}


Expand Down
40 changes: 37 additions & 3 deletions src/libOpenImageIO/imageoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,13 +1021,47 @@ ImageOutput::check_open(OpenMode mode, const ImageSpec& userspec, ROI range,



template<>
inline size_t
pvt::heapsize<ImageOutput::Impl>(const ImageOutput::Impl& impl)
{
return impl.m_io_local ? sizeof(Filesystem::IOProxy) : 0;
}



size_t
ImageOutput::heapsize() const
{
size_t size = pvt::heapsize(m_impl);
size += pvt::heapsize(m_spec);
return size;
}



size_t
ImageOutput::footprint() const
{
return sizeof(ImageOutput) + heapsize();
}



template<>
size_t
pvt::heapsize<ImageOutput>(const ImageOutput& output)
{
//! TODO: change ImageOutput API to add a virtual heapsize() function
//! to allow per image output override, and call that function here.
return pvt::heapsize(output.m_spec);
return output.heapsize();
}



template<>
size_t
pvt::footprint<ImageOutput>(const ImageOutput& output)
{
return output.footprint();
}


Expand Down
Loading