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 {
26 ABSL_NAMESPACE_BEGIN
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 {
36 CordRep* child;
37 uint32_t crc;
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.
44 static CordRepCrc* New(CordRep* child, uint32_t crc);
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.
RemoveCrcNode(CordRep * rep)53 inline CordRep* RemoveCrcNode(CordRep* rep) {
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 {
60 CordRep::Ref(child);
61 CordRep::Unref(rep);
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.
SkipCrcNode(CordRep * rep)70 inline CordRep* SkipCrcNode(CordRep* rep) {
71 assert(rep != nullptr);
72 if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
73 return rep->crc()->child;
74 } else {
75 return rep;
76 }
77 }
78
SkipCrcNode(const CordRep * rep)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
crc()88 inline CordRepCrc* CordRep::crc() {
89 assert(IsCrc());
90 return static_cast<CordRepCrc*>(this);
91 }
92
crc()93 inline const CordRepCrc* CordRep::crc() const {
94 assert(IsCrc());
95 return static_cast<const CordRepCrc*>(this);
96 }
97
98 } // namespace cord_internal
99 ABSL_NAMESPACE_END
100 } // namespace absl
101
102 #endif // ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
103