[llvm-commits] [llvm] r136233 - in /llvm/trunk/include/llvm: ADT/DenseMap.h ADT/SmallVector.h Support/Capacity.h

Ted Kremenek kremenek at apple.com
Wed Jul 27 11:40:45 PDT 2011


Author: kremenek
Date: Wed Jul 27 13:40:45 2011
New Revision: 136233

URL: http://llvm.org/viewvc/llvm-project?rev=136233&view=rev
Log:
Add a generic 'capacity_in_bytes' function to allow inspection of memory usage of various data structures.

Added:
    llvm/trunk/include/llvm/Support/Capacity.h
Modified:
    llvm/trunk/include/llvm/ADT/DenseMap.h
    llvm/trunk/include/llvm/ADT/SmallVector.h

Modified: llvm/trunk/include/llvm/ADT/DenseMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/DenseMap.h?rev=136233&r1=136232&r2=136233&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/DenseMap.h (original)
+++ llvm/trunk/include/llvm/ADT/DenseMap.h Wed Jul 27 13:40:45 2011
@@ -540,6 +540,12 @@
       ++Ptr;
   }
 };
+  
+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
 

Modified: llvm/trunk/include/llvm/ADT/SmallVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=136233&r1=136232&r2=136233&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallVector.h Wed Jul 27 13:40:45 2011
@@ -78,21 +78,21 @@
     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);
   }
-
+  
   /// capacity_in_bytes - This returns capacity()*sizeof(T).
   size_t capacity_in_bytes() const {
     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 @@
 
 };
 
+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 {

Added: llvm/trunk/include/llvm/Support/Capacity.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Capacity.h?rev=136233&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/Capacity.h (added)
+++ llvm/trunk/include/llvm/Support/Capacity.h Wed Jul 27 13:40:45 2011
@@ -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
+





More information about the llvm-commits mailing list