Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:13

0001 // Copyright 2021 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
0016 #define ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
0017 
0018 #include <cassert>
0019 #include <cstdint>
0020 
0021 #include "absl/base/config.h"
0022 #include "absl/base/optimization.h"
0023 #include "absl/crc/internal/crc_cord_state.h"
0024 #include "absl/strings/internal/cord_internal.h"
0025 
0026 namespace absl {
0027 ABSL_NAMESPACE_BEGIN
0028 namespace cord_internal {
0029 
0030 // CordRepCrc is a CordRep node intended only to appear at the top level of a
0031 // cord tree.  It associates an "expected CRC" with the contained data, to allow
0032 // for easy passage of checksum data in Cord data flows.
0033 //
0034 // From Cord's perspective, the crc value has no semantics; any validation of
0035 // the contained checksum is the user's responsibility.
0036 struct CordRepCrc : public CordRep {
0037   CordRep* child;
0038   absl::crc_internal::CrcCordState crc_cord_state;
0039 
0040   // Consumes `child` and returns a CordRepCrc prefixed tree containing `child`.
0041   // If the specified `child` is itself a CordRepCrc node, then this method
0042   // either replaces the existing node, or directly updates the crc state in it
0043   // depending on the node being shared or not, i.e.: refcount.IsOne().
0044   // `child` must only be null if the Cord is empty. Never returns null.
0045   static CordRepCrc* New(CordRep* child, crc_internal::CrcCordState state);
0046 
0047   // Destroys (deletes) the provided node. `node` must not be null.
0048   static void Destroy(CordRepCrc* node);
0049 };
0050 
0051 // Consumes `rep` and returns a CordRep* with any outer CordRepCrc wrapper
0052 // removed.  This is usually a no-op (returning `rep`), but this will remove and
0053 // unref an outer CordRepCrc node.
0054 inline CordRep* RemoveCrcNode(CordRep* rep) {
0055   assert(rep != nullptr);
0056   if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
0057     CordRep* child = rep->crc()->child;
0058     if (rep->refcount.IsOne()) {
0059       delete rep->crc();
0060     } else {
0061       CordRep::Ref(child);
0062       CordRep::Unref(rep);
0063     }
0064     return child;
0065   }
0066   return rep;
0067 }
0068 
0069 // Returns `rep` if it is not a CordRepCrc node, or its child if it is.
0070 // Does not consume or create a reference on `rep` or the returned value.
0071 inline CordRep* SkipCrcNode(CordRep* rep) {
0072   assert(rep != nullptr);
0073   if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
0074     return rep->crc()->child;
0075   } else {
0076     return rep;
0077   }
0078 }
0079 
0080 inline const CordRep* SkipCrcNode(const CordRep* rep) {
0081   assert(rep != nullptr);
0082   if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
0083     return rep->crc()->child;
0084   } else {
0085     return rep;
0086   }
0087 }
0088 
0089 inline CordRepCrc* CordRep::crc() {
0090   assert(IsCrc());
0091   return static_cast<CordRepCrc*>(this);
0092 }
0093 
0094 inline const CordRepCrc* CordRep::crc() const {
0095   assert(IsCrc());
0096   return static_cast<const CordRepCrc*>(this);
0097 }
0098 
0099 }  // namespace cord_internal
0100 ABSL_NAMESPACE_END
0101 }  // namespace absl
0102 
0103 #endif  // ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_