1 // Copyright 2020 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 #include "absl/strings/internal/cord_internal.h"
15 
16 #include <atomic>
17 #include <cassert>
18 #include <memory>
19 
20 #include "absl/base/internal/raw_logging.h"
21 #include "absl/container/inlined_vector.h"
22 #include "absl/strings/internal/cord_rep_btree.h"
23 #include "absl/strings/internal/cord_rep_crc.h"
24 #include "absl/strings/internal/cord_rep_flat.h"
25 #include "absl/strings/internal/cord_rep_ring.h"
26 #include "absl/strings/str_cat.h"
27 
28 namespace absl {
29 ABSL_NAMESPACE_BEGIN
30 namespace cord_internal {
31 
32 ABSL_CONST_INIT std::atomic<bool> cord_ring_buffer_enabled(
33     kCordEnableRingBufferDefault);
34 ABSL_CONST_INIT std::atomic<bool> shallow_subcords_enabled(
35     kCordShallowSubcordsDefault);
36 ABSL_CONST_INIT std::atomic<bool> cord_btree_exhaustive_validation(false);
37 
LogFatalNodeType(CordRep * rep)38 void LogFatalNodeType(CordRep* rep) {
39   ABSL_INTERNAL_LOG(FATAL, absl::StrCat("Unexpected node type: ",
40                                         static_cast<int>(rep->tag)));
41 }
42 
Destroy(CordRep * rep)43 void CordRep::Destroy(CordRep* rep) {
44   assert(rep != nullptr);
45 
46   while (true) {
47     assert(!rep->refcount.IsImmortal());
48     if (rep->tag == BTREE) {
49       CordRepBtree::Destroy(rep->btree());
50       return;
51     } else if (rep->tag == RING) {
52       CordRepRing::Destroy(rep->ring());
53       return;
54     } else if (rep->tag == EXTERNAL) {
55       CordRepExternal::Delete(rep);
56       return;
57     } else if (rep->tag == SUBSTRING) {
58       CordRepSubstring* rep_substring = rep->substring();
59       rep = rep_substring->child;
60       delete rep_substring;
61       if (rep->refcount.Decrement()) {
62         return;
63       }
64     } else if (rep->tag == CRC) {
65       CordRepCrc::Destroy(rep->crc());
66       return;
67     } else {
68       assert(rep->IsFlat());
69       CordRepFlat::Delete(rep);
70       return;
71     }
72   }
73 }
74 
75 }  // namespace cord_internal
76 ABSL_NAMESPACE_END
77 }  // namespace absl
78