Add a generic 'capacity_in_bytes' function to allow inspection of memory usage of various data structures.

llvm-svn: 136233
This commit is contained in:
Ted Kremenek 2011-07-27 18:40:45 +00:00
parent 21f78d88e1
commit 666bec46a0
3 changed files with 51 additions and 10 deletions

View File

@ -541,6 +541,12 @@ private:
}
};
template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
static inline size_t
capacity_in_bytes(const DenseMap<KeyT, ValueT, KeyInfoT, ValueInfoT> &X) {
return X.getMemorySize();
}
} // end namespace llvm
#endif

View File

@ -78,6 +78,11 @@ protected:
return BeginX == static_cast<const void*>(&FirstEl);
}
/// grow_pod - This is an implementation of the grow() method which only works
/// on POD-like data types and is out of line to reduce code duplication.
void grow_pod(size_t MinSizeInBytes, size_t TSize);
public:
/// size_in_bytes - This returns size()*sizeof(T).
size_t size_in_bytes() const {
return size_t((char*)EndX - (char*)BeginX);
@ -88,11 +93,6 @@ protected:
return size_t((char*)CapacityX - (char*)BeginX);
}
/// grow_pod - This is an implementation of the grow() method which only works
/// on POD-like data types and is out of line to reduce code duplication.
void grow_pod(size_t MinSizeInBytes, size_t TSize);
public:
bool empty() const { return BeginX == EndX; }
};
@ -738,6 +738,11 @@ public:
};
template<typename T, unsigned N>
static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
return X.capacity_in_bytes();
}
} // End llvm namespace
namespace std {

View File

@ -0,0 +1,30 @@
//===--- Capacity.h - Generic computation of ADT memory use -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the capacity function that computes the amount of
// memory used by an ADT.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_CAPACITY_H
#define LLVM_SUPPORT_CAPACITY_H
namespace llvm {
template <typename T>
static inline size_t capacity_in_bytes(const T &x) {
// This default definition of capacity should work for things like std::vector
// and friends. More specialized versions will work for others.
return x.capacity() * sizeof(typename T::value_type);
}
} // end namespace llvm
#endif