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 #include "absl/strings/internal/cord_rep_crc.h" 16 17 #include <cassert> 18 #include <cstdint> 19 20 #include "absl/base/config.h" 21 #include "absl/strings/internal/cord_internal.h" 22 23 namespace absl { 24 ABSL_NAMESPACE_BEGIN 25 namespace cord_internal { 26 New(CordRep * child,uint32_t crc)27CordRepCrc* CordRepCrc::New(CordRep* child, uint32_t crc) { 28 assert(child != nullptr); 29 if (child->IsCrc()) { 30 if (child->refcount.IsOne()) { 31 child->crc()->crc = crc; 32 return child->crc(); 33 } 34 CordRep* old = child; 35 child = old->crc()->child; 36 CordRep::Ref(child); 37 CordRep::Unref(old); 38 } 39 auto* new_cordrep = new CordRepCrc; 40 new_cordrep->length = child->length; 41 new_cordrep->tag = cord_internal::CRC; 42 new_cordrep->child = child; 43 new_cordrep->crc = crc; 44 return new_cordrep; 45 } 46 Destroy(CordRepCrc * node)47void CordRepCrc::Destroy(CordRepCrc* node) { 48 CordRep::Unref(node->child); 49 delete node; 50 } 51 52 } // namespace cord_internal 53 ABSL_NAMESPACE_END 54 } // namespace absl 55