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/container/inlined_vector.h" 21 #include "absl/strings/internal/cord_rep_btree.h" 22 #include "absl/strings/internal/cord_rep_flat.h" 23 #include "absl/strings/internal/cord_rep_ring.h" 24 25 namespace absl { 26 ABSL_NAMESPACE_BEGIN 27 namespace cord_internal { 28 29 ABSL_CONST_INIT std::atomic<bool> cord_btree_enabled(kCordEnableBtreeDefault); 30 ABSL_CONST_INIT std::atomic<bool> cord_ring_buffer_enabled( 31 kCordEnableRingBufferDefault); 32 ABSL_CONST_INIT std::atomic<bool> shallow_subcords_enabled( 33 kCordShallowSubcordsDefault); 34 ABSL_CONST_INIT std::atomic<bool> cord_btree_exhaustive_validation(false); 35 Destroy(CordRep * rep)36void CordRep::Destroy(CordRep* rep) { 37 assert(rep != nullptr); 38 39 absl::InlinedVector<CordRep*, Constants::kInlinedVectorSize> pending; 40 while (true) { 41 assert(!rep->refcount.IsImmortal()); 42 if (rep->tag == CONCAT) { 43 CordRepConcat* rep_concat = rep->concat(); 44 CordRep* right = rep_concat->right; 45 if (!right->refcount.Decrement()) { 46 pending.push_back(right); 47 } 48 CordRep* left = rep_concat->left; 49 delete rep_concat; 50 rep = nullptr; 51 if (!left->refcount.Decrement()) { 52 rep = left; 53 continue; 54 } 55 } else if (rep->tag == BTREE) { 56 CordRepBtree::Destroy(rep->btree()); 57 rep = nullptr; 58 } else if (rep->tag == RING) { 59 CordRepRing::Destroy(rep->ring()); 60 rep = nullptr; 61 } else if (rep->tag == EXTERNAL) { 62 CordRepExternal::Delete(rep); 63 rep = nullptr; 64 } else if (rep->tag == SUBSTRING) { 65 CordRepSubstring* rep_substring = rep->substring(); 66 CordRep* child = rep_substring->child; 67 delete rep_substring; 68 rep = nullptr; 69 if (!child->refcount.Decrement()) { 70 rep = child; 71 continue; 72 } 73 } else { 74 CordRepFlat::Delete(rep); 75 rep = nullptr; 76 } 77 78 if (!pending.empty()) { 79 rep = pending.back(); 80 pending.pop_back(); 81 } else { 82 break; 83 } 84 } 85 } 86 87 } // namespace cord_internal 88 ABSL_NAMESPACE_END 89 } // namespace absl 90