| 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
|---|---|
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "vm/zone_text_buffer.h" |
| 6 | |
| 7 | #include "platform/assert.h" |
| 8 | #include "platform/globals.h" |
| 9 | #include "platform/utils.h" |
| 10 | #include "vm/object.h" |
| 11 | #include "vm/os.h" |
| 12 | #include "vm/zone.h" |
| 13 | |
| 14 | namespace dart { |
| 15 | |
| 16 | ZoneTextBuffer::ZoneTextBuffer(Zone* zone, intptr_t initial_capacity) |
| 17 | : zone_(zone) { |
| 18 | ASSERT(initial_capacity > 0); |
| 19 | buffer_ = reinterpret_cast<char*>(zone->Alloc<char>(initial_capacity)); |
| 20 | capacity_ = initial_capacity; |
| 21 | buffer_[length_] = '\0'; |
| 22 | } |
| 23 | |
| 24 | void ZoneTextBuffer::Clear() { |
| 25 | const intptr_t initial_capacity = 64; |
| 26 | buffer_ = reinterpret_cast<char*>(zone_->Alloc<char>(initial_capacity)); |
| 27 | capacity_ = initial_capacity; |
| 28 | length_ = 0; |
| 29 | buffer_[length_] = '\0'; |
| 30 | } |
| 31 | |
| 32 | bool ZoneTextBuffer::EnsureCapacity(intptr_t len) { |
| 33 | intptr_t remaining = capacity_ - length_; |
| 34 | if (remaining <= len) { |
| 35 | intptr_t new_capacity = capacity_ + Utils::Maximum(capacity_, len); |
| 36 | buffer_ = zone_->Realloc<char>(buffer_, capacity_, new_capacity); |
| 37 | capacity_ = new_capacity; |
| 38 | } |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | } // namespace dart |
| 43 |