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