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 #include <utility>
20 
21 #include "absl/base/config.h"
22 #include "absl/strings/internal/cord_internal.h"
23 
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 namespace cord_internal {
27 
New(CordRep * child,crc_internal::CrcCordState state)28 CordRepCrc* CordRepCrc::New(CordRep* child, crc_internal::CrcCordState state) {
29   if (child != nullptr && child->IsCrc()) {
30     if (child->refcount.IsOne()) {
31       child->crc()->crc_cord_state = std::move(state);
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 != nullptr ? child->length : 0;
41   new_cordrep->tag = cord_internal::CRC;
42   new_cordrep->child = child;
43   new_cordrep->crc_cord_state = std::move(state);
44   return new_cordrep;
45 }
46 
Destroy(CordRepCrc * node)47 void CordRepCrc::Destroy(CordRepCrc* node) {
48   if (node->child != nullptr) {
49     CordRep::Unref(node->child);
50   }
51   delete node;
52 }
53 
54 }  // namespace cord_internal
55 ABSL_NAMESPACE_END
56 }  // namespace absl
57