xref: /aosp_15_r20/external/abseil-cpp/absl/strings/cord_analysis.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 
15 #ifndef ABSL_STRINGS_CORD_ANALYSIS_H_
16 #define ABSL_STRINGS_CORD_ANALYSIS_H_
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "absl/base/config.h"
22 #include "absl/base/nullability.h"
23 #include "absl/strings/internal/cord_internal.h"
24 
25 namespace absl {
26 ABSL_NAMESPACE_BEGIN
27 namespace cord_internal {
28 
29 // Returns the *approximate* number of bytes held in full or in part by this
30 // Cord (which may not remain the same between invocations). Cords that share
31 // memory could each be "charged" independently for the same shared memory.
32 size_t GetEstimatedMemoryUsage(absl::Nonnull<const CordRep*> rep);
33 
34 // Returns the *approximate* number of bytes held in full or in part by this
35 // Cord for the distinct memory held by this cord. This is similar to
36 // `GetEstimatedMemoryUsage()`, except that if the cord has multiple references
37 // to the same memory, that memory is only counted once.
38 //
39 // For example:
40 //   absl::Cord cord;
41 //   cord.append(some_other_cord);
42 //   cord.append(some_other_cord);
43 //    // Calls GetEstimatedMemoryUsage() and counts `other_cord` twice:
44 //   cord.EstimatedMemoryUsage(kTotal);
45 //    // Calls GetMorePreciseMemoryUsage() and counts `other_cord` once:
46 //   cord.EstimatedMemoryUsage(kTotalMorePrecise);
47 //
48 // This is more expensive than `GetEstimatedMemoryUsage()` as it requires
49 // deduplicating all memory references.
50 size_t GetMorePreciseMemoryUsage(absl::Nonnull<const CordRep*> rep);
51 
52 // Returns the *approximate* number of bytes held in full or in part by this
53 // CordRep weighted by the sharing ratio of that data. For example, if some data
54 // edge is shared by 4 different Cords, then each cord is attribute 1/4th of
55 // the total memory usage as a 'fair share' of the total memory usage.
56 size_t GetEstimatedFairShareMemoryUsage(absl::Nonnull<const CordRep*> rep);
57 
58 }  // namespace cord_internal
59 ABSL_NAMESPACE_END
60 }  // namespace absl
61 
62 
63 #endif  // ABSL_STRINGS_CORD_ANALYSIS_H_
64