You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memusage.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (c) 2015-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_MEMUSAGE_H
  5. #define STARWELS_MEMUSAGE_H
  6. #include "indirectmap.h"
  7. #include <stdlib.h>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. namespace memusage
  14. {
  15. /** Compute the total memory used by allocating alloc bytes. */
  16. static size_t MallocUsage(size_t alloc);
  17. /** Dynamic memory usage for built-in types is zero. */
  18. static inline size_t DynamicUsage(const int8_t& v) { return 0; }
  19. static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
  20. static inline size_t DynamicUsage(const int16_t& v) { return 0; }
  21. static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
  22. static inline size_t DynamicUsage(const int32_t& v) { return 0; }
  23. static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
  24. static inline size_t DynamicUsage(const int64_t& v) { return 0; }
  25. static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
  26. static inline size_t DynamicUsage(const float& v) { return 0; }
  27. static inline size_t DynamicUsage(const double& v) { return 0; }
  28. template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
  29. template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
  30. /** Compute the memory used for dynamically allocated but owned data structures.
  31. * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
  32. * will compute the memory used for the vector<int>'s, but not for the ints inside.
  33. * This is for efficiency reasons, as these functions are intended to be fast. If
  34. * application data structures require more accurate inner accounting, they should
  35. * iterate themselves, or use more efficient caching + updating on modification.
  36. */
  37. static inline size_t MallocUsage(size_t alloc)
  38. {
  39. // Measured on libc6 2.19 on Linux.
  40. if (alloc == 0) {
  41. return 0;
  42. } else if (sizeof(void*) == 8) {
  43. return ((alloc + 31) >> 4) << 4;
  44. } else if (sizeof(void*) == 4) {
  45. return ((alloc + 15) >> 3) << 3;
  46. } else {
  47. assert(0);
  48. }
  49. }
  50. // STL data structures
  51. template<typename X>
  52. struct stl_tree_node
  53. {
  54. private:
  55. int color;
  56. void* parent;
  57. void* left;
  58. void* right;
  59. X x;
  60. };
  61. struct stl_shared_counter
  62. {
  63. /* Various platforms use different sized counters here.
  64. * Conservatively assume that they won't be larger than size_t. */
  65. void* class_type;
  66. size_t use_count;
  67. size_t weak_count;
  68. };
  69. template<typename X>
  70. static inline size_t DynamicUsage(const std::vector<X>& v)
  71. {
  72. return MallocUsage(v.capacity() * sizeof(X));
  73. }
  74. template<unsigned int N, typename X, typename S, typename D>
  75. static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
  76. {
  77. return MallocUsage(v.allocated_memory());
  78. }
  79. template<typename X, typename Y>
  80. static inline size_t DynamicUsage(const std::set<X, Y>& s)
  81. {
  82. return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
  83. }
  84. template<typename X, typename Y>
  85. static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
  86. {
  87. return MallocUsage(sizeof(stl_tree_node<X>));
  88. }
  89. template<typename X, typename Y, typename Z>
  90. static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
  91. {
  92. return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
  93. }
  94. template<typename X, typename Y, typename Z>
  95. static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
  96. {
  97. return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
  98. }
  99. // indirectmap has underlying map with pointer as key
  100. template<typename X, typename Y>
  101. static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
  102. {
  103. return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
  104. }
  105. template<typename X, typename Y>
  106. static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
  107. {
  108. return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
  109. }
  110. template<typename X>
  111. static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
  112. {
  113. return p ? MallocUsage(sizeof(X)) : 0;
  114. }
  115. template<typename X>
  116. static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
  117. {
  118. // A shared_ptr can either use a single continuous memory block for both
  119. // the counter and the storage (when using std::make_shared), or separate.
  120. // We can't observe the difference, however, so assume the worst.
  121. return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
  122. }
  123. template<typename X>
  124. struct unordered_node : private X
  125. {
  126. private:
  127. void* ptr;
  128. };
  129. template<typename X, typename Y>
  130. static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
  131. {
  132. return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
  133. }
  134. template<typename X, typename Y, typename Z>
  135. static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
  136. {
  137. return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
  138. }
  139. }
  140. #endif // STARWELS_MEMUSAGE_H