Skip to content

Commit

Permalink
add datasize() and footprint() methods to ParamValue, ParamValueList,…
Browse files Browse the repository at this point in the history
… and ImageSpec

Signed-off-by: Basile Fraboni <[email protected]>
  • Loading branch information
bfraboni committed Jul 4, 2024
1 parent 979e5f9 commit 3d30e3d
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/include/OpenImageIO/imageio.h
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,13 @@ class OIIO_API ImageSpec {
{
return { this, name };
}

/// Return the total heap memory allocated by the ImageSpec and its
/// members.
size_t datasize() const noexcept;
/// Return the total memory used by the ImageSpec, including this
/// object itself and any heap memory allocated data.
size_t footprint() const noexcept;
};


Expand Down
80 changes: 80 additions & 0 deletions src/include/OpenImageIO/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2008-present Contributors to the OpenImageIO project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/OpenImageIO/oiio


/////////////////////////////////////////////////////////////////////////////
/// \file
///
/// Provides helper function to compute the memory footprint of common
/// containers.
///
/////////////////////////////////////////////////////////////////////////////

#pragma once

#define OPENIMAGEIO_MEMORY_H

OIIO_NAMESPACE_BEGIN

/// Return the total heap allocated memory used by the container.
/// Variant for primitive types or trivial structures.
template<class VecType>
inline size_t
vector_bytes(const VecType& v)
{
using T = typename VecType::value_type;
return v.capacity() * sizeof(T);
}

/// Return the total heap allocated memory used by the container.
/// Variant for strings, including the reserved string entries,
/// and each string capacity.
template<class VecType>
inline size_t
vector_string_datasize(const VecType& v)
{
using T = typename VecType::value_type;
// account for the size of elements
size_t size = vector_bytes(v);
// account for the size of allocated data
for (const T& ref : v)
size += ref.capacity();
return size;
}

/// Return the total heap allocated memory used by the container.
/// Variant for objects implementing the datasize() function,
/// including the reserved object entries, and each object own
/// allocations.
template<class VecType>
inline size_t
vector_datasize(const VecType& v)
{
using T = typename VecType::value_type;
// account for the size of elements
size_t size = vector_bytes(v);
// account for the size of allocated data
for (const T& ref : v)
size += ref.datasize();
return size;
}

/// Return the total heap allocated memory used by the container.
/// Variant for pointers to object implementing the footprint() function,
/// including the reserved pointer entries, and each object footprint.
template<class VecType>
inline size_t
vector_ptr_datasize(const VecType& v)
{
using T = typename VecType::value_type;
// account for the size of ptrs
size_t size = vector_bytes(v);
// account for the size of allocated objects
for (const T& ptr : v)
size += ptr->footprint();
return size;
}


OIIO_NAMESPACE_END
19 changes: 15 additions & 4 deletions src/include/OpenImageIO/paramlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,6 @@ class OIIO_UTIL_API ParamValue {
{
return m_nonlocal ? m_data.ptr : &m_data;
}
int datasize() const noexcept
{
return m_nvalues * static_cast<int>(m_type.size());
}
Interp interp() const noexcept { return (Interp)m_interp; }
void interp(Interp i) noexcept { m_interp = (unsigned char)i; }
bool is_nonlocal() const noexcept { return m_nonlocal; }
Expand Down Expand Up @@ -306,6 +302,12 @@ class OIIO_UTIL_API ParamValue {
ustring get_ustring(int maxsize = 64) const;
ustring get_ustring_indexed(int index) const;

/// Return the total heap memory allocated by the ParamValue.
size_t datasize() const noexcept;
/// Return the total memory used by the ParamValue, including this
/// object itself and any heap memory allocated data.
size_t footprint() const noexcept;

private:
ustring m_name; ///< data name
TypeDesc m_type; ///< data type, which may itself be an array
Expand Down Expand Up @@ -564,6 +566,15 @@ class OIIO_UTIL_API ParamValueList : public std::vector<ParamValue> {
{
return { this, name };
}

/// Return the total heap allocated memory used by the ParamValueList,
/// including all the reserved ParamValue entries, and any heap memory
/// allocated by the ParamValue entries to hold their data.
size_t datasize() const noexcept;
/// Return the total memory used by the ParamValue, including this
/// object itself and any heap memory allocated by itself or the
/// ParamValue entries.
size_t footprint() const noexcept;
};


Expand Down
20 changes: 20 additions & 0 deletions src/libOpenImageIO/formatspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/typedesc.h>
#include <OpenImageIO/memory.h>

#include "exif.h"
#include "imageio_pvt.h"
Expand Down Expand Up @@ -1251,4 +1252,23 @@ ImageSpec::set_colorspace(string_view colorspace)
}



size_t
ImageSpec::datasize() const noexcept
{
size_t size = vector_bytes(channelformats);
size += vector_string_datasize(channelnames);
size += extra_attribs.datasize();
return size;
}



size_t
ImageSpec::footprint() const noexcept
{
return sizeof(ImageSpec) + datasize();
}


OIIO_NAMESPACE_END
33 changes: 33 additions & 0 deletions src/libutil/paramlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <OpenImageIO/half.h>
#include <OpenImageIO/paramlist.h>
#include <OpenImageIO/ustring.h>
#include <OpenImageIO/memory.h>


OIIO_NAMESPACE_BEGIN
Expand Down Expand Up @@ -383,6 +384,22 @@ ParamValue::clear_value() noexcept



size_t
ParamValue::datasize() const noexcept
{
return m_nvalues * static_cast<int>(m_type.size());
}



size_t
ParamValue::footprint() const noexcept
{
return sizeof(ParamValue) + datasize();
}



ParamValueList::const_iterator
ParamValueList::find(ustring name, TypeDesc type, bool casesensitive) const
{
Expand Down Expand Up @@ -656,6 +673,22 @@ ParamValueList::merge(const ParamValueList& other, bool override)



size_t
ParamValueList::datasize() const noexcept
{
return vector_datasize(*this);
}



size_t
ParamValueList::footprint() const noexcept
{
return sizeof(ParamValueList) + datasize();
}



ParamValueSpan::const_iterator
ParamValueSpan::find(ustring name, TypeDesc type, bool casesensitive) const
{
Expand Down

0 comments on commit 3d30e3d

Please sign in to comment.