cord_analysis.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2021 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_STRINGS_CORD_ANALYSIS_H_
  15. #define ABSL_STRINGS_CORD_ANALYSIS_H_
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include "absl/base/config.h"
  19. #include "absl/base/nullability.h"
  20. #include "absl/strings/internal/cord_internal.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace cord_internal {
  24. // Returns the *approximate* number of bytes held in full or in part by this
  25. // Cord (which may not remain the same between invocations). Cords that share
  26. // memory could each be "charged" independently for the same shared memory.
  27. size_t GetEstimatedMemoryUsage(absl::Nonnull<const CordRep*> rep);
  28. // Returns the *approximate* number of bytes held in full or in part by this
  29. // Cord for the distinct memory held by this cord. This is similar to
  30. // `GetEstimatedMemoryUsage()`, except that if the cord has multiple references
  31. // to the same memory, that memory is only counted once.
  32. //
  33. // For example:
  34. // absl::Cord cord;
  35. // cord.append(some_other_cord);
  36. // cord.append(some_other_cord);
  37. // // Calls GetEstimatedMemoryUsage() and counts `other_cord` twice:
  38. // cord.EstimatedMemoryUsage(kTotal);
  39. // // Calls GetMorePreciseMemoryUsage() and counts `other_cord` once:
  40. // cord.EstimatedMemoryUsage(kTotalMorePrecise);
  41. //
  42. // This is more expensive than `GetEstimatedMemoryUsage()` as it requires
  43. // deduplicating all memory references.
  44. size_t GetMorePreciseMemoryUsage(absl::Nonnull<const CordRep*> rep);
  45. // Returns the *approximate* number of bytes held in full or in part by this
  46. // CordRep weighted by the sharing ratio of that data. For example, if some data
  47. // edge is shared by 4 different Cords, then each cord is attribute 1/4th of
  48. // the total memory usage as a 'fair share' of the total memory usage.
  49. size_t GetEstimatedFairShareMemoryUsage(absl::Nonnull<const CordRep*> rep);
  50. } // namespace cord_internal
  51. ABSL_NAMESPACE_END
  52. } // namespace absl
  53. #endif // ABSL_STRINGS_CORD_ANALYSIS_H_