cord_rep_crc.h
Go to the documentation of this file.
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_INTERNAL_CORD_REP_CRC_H_
16 #define ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
17 
18 #include <cassert>
19 #include <cstdint>
20 
21 #include "absl/base/config.h"
22 #include "absl/base/optimization.h"
23 #include "absl/strings/internal/cord_internal.h"
24 
25 namespace absl {
27 namespace cord_internal {
28 
29 // CordRepCrc is a CordRep node intended only to appear at the top level of a
30 // cord tree. It associates an "expected CRC" with the contained data, to allow
31 // for easy passage of checksum data in Cord data flows.
32 //
33 // From Cord's perspective, the crc value has no semantics; any validation of
34 // the contained checksum is the user's responsibility.
35 struct CordRepCrc : public CordRep {
38 
39  // Consumes `child` and returns a CordRepCrc prefixed tree containing `child`.
40  // If the specified `child` is itself a CordRepCrc node, then this method
41  // either replaces the existing node, or directly updates the crc value in it
42  // depending on the node being shared or not, i.e.: refcount.IsOne().
43  // `child` must not be null. Never returns null.
45 
46  // Destroys (deletes) the provided node. `node` must not be null.
47  static void Destroy(CordRepCrc* node);
48 };
49 
50 // Consumes `rep` and returns a CordRep* with any outer CordRepCrc wrapper
51 // removed. This is usually a no-op (returning `rep`), but this will remove and
52 // unref an outer CordRepCrc node.
54  assert(rep != nullptr);
55  if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
56  CordRep* child = rep->crc()->child;
57  if (rep->refcount.IsOne()) {
58  delete rep->crc();
59  } else {
62  }
63  return child;
64  }
65  return rep;
66 }
67 
68 // Returns `rep` if it is not a CordRepCrc node, or its child if it is.
69 // Does not consume or create a reference on `rep` or the returned value.
71  assert(rep != nullptr);
72  if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
73  return rep->crc()->child;
74  } else {
75  return rep;
76  }
77 }
78 
79 inline const CordRep* SkipCrcNode(const CordRep* rep) {
80  assert(rep != nullptr);
81  if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
82  return rep->crc()->child;
83  } else {
84  return rep;
85  }
86 }
87 
89  assert(IsCrc());
90  return static_cast<CordRepCrc*>(this);
91 }
92 
93 inline const CordRepCrc* CordRep::crc() const {
94  assert(IsCrc());
95  return static_cast<const CordRepCrc*>(this);
96 }
97 
98 } // namespace cord_internal
100 } // namespace absl
101 
102 #endif // ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
ABSL_PREDICT_FALSE
#define ABSL_PREDICT_FALSE(x)
Definition: abseil-cpp/absl/base/optimization.h:180
absl::cord_internal::CordRepCrc
Definition: cord_rep_crc.h:35
absl::cord_internal::CordRepCrc::New
static CordRepCrc * New(CordRep *child, uint32_t crc)
Definition: cord_rep_crc.cc:27
absl::cord_internal::CordRep::Unref
static void Unref(CordRep *rep)
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:642
absl::cord_internal::RemoveCrcNode
CordRep * RemoveCrcNode(CordRep *rep)
Definition: cord_rep_crc.h:53
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::cord_internal::CordRep::IsCrc
constexpr bool IsCrc() const
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:247
absl::cord_internal::CordRep
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:209
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::cord_internal::CordRep::refcount
RefcountAndFlags refcount
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:229
googletest-filter-unittest.child
child
Definition: bloaty/third_party/googletest/googletest/test/googletest-filter-unittest.py:62
absl::cord_internal::CordRepCrc::Destroy
static void Destroy(CordRepCrc *node)
Definition: cord_rep_crc.cc:47
absl::cord_internal::CordRepCrc::crc
uint32_t crc
Definition: cord_rep_crc.h:37
rep
const CordRep * rep
Definition: cord_analysis.cc:53
absl::cord_internal::CordRep::Ref
static CordRep * Ref(CordRep *rep)
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:634
absl::cord_internal::CordRep::crc
CordRepCrc * crc()
Definition: cord_rep_crc.h:88
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::cord_internal::SkipCrcNode
CordRep * SkipCrcNode(CordRep *rep)
Definition: cord_rep_crc.h:70
absl::cord_internal::RefcountAndFlags::IsOne
bool IsOne()
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:144
absl::cord_internal::CordRepCrc::child
CordRep * child
Definition: cord_rep_crc.h:36


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:56