memory_usage.h 864 B

12345678910111213141516171819202122232425
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <unordered_map>
  7. namespace ROCKSDB_NAMESPACE {
  8. // Helper methods to estimate memroy usage by std containers.
  9. template <class Key, class Value, class Hash>
  10. size_t ApproximateMemoryUsage(
  11. const std::unordered_map<Key, Value, Hash>& umap) {
  12. typedef std::unordered_map<Key, Value, Hash> Map;
  13. return sizeof(umap) +
  14. // Size of all items plus a next pointer for each item.
  15. (sizeof(typename Map::value_type) + sizeof(void*)) * umap.size() +
  16. // Size of hash buckets.
  17. umap.bucket_count() * sizeof(void*);
  18. }
  19. } // namespace ROCKSDB_NAMESPACE