OLD | NEW |
(Empty) | |
| 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/os.h" |
| 11 #include "vm/zone.h" |
| 12 |
| 13 namespace dart { |
| 14 |
| 15 ZoneTextBuffer::ZoneTextBuffer(Zone* zone, intptr_t initial_capacity) |
| 16 : zone_(zone), buffer_(NULL), length_(0), capacity_(0) { |
| 17 ASSERT(initial_capacity > 0); |
| 18 buffer_ = reinterpret_cast<char*>(zone->Alloc<char>(initial_capacity)); |
| 19 capacity_ = initial_capacity; |
| 20 buffer_[length_] = '\0'; |
| 21 } |
| 22 |
| 23 |
| 24 intptr_t ZoneTextBuffer::Printf(const char* format, ...) { |
| 25 va_list args; |
| 26 va_start(args, format); |
| 27 intptr_t remaining = capacity_ - length_; |
| 28 ASSERT(remaining >= 0); |
| 29 intptr_t len = OS::VSNPrint(buffer_ + length_, remaining, format, args); |
| 30 va_end(args); |
| 31 if (len >= remaining) { |
| 32 EnsureCapacity(len); |
| 33 remaining = capacity_ - length_; |
| 34 ASSERT(remaining > len); |
| 35 va_list args2; |
| 36 va_start(args2, format); |
| 37 intptr_t len2 = OS::VSNPrint(buffer_ + length_, remaining, format, args2); |
| 38 va_end(args2); |
| 39 ASSERT(len == len2); |
| 40 } |
| 41 length_ += len; |
| 42 buffer_[length_] = '\0'; |
| 43 return len; |
| 44 } |
| 45 |
| 46 |
| 47 void ZoneTextBuffer::AddString(const char* s) { |
| 48 Printf("%s", s); |
| 49 } |
| 50 |
| 51 |
| 52 void ZoneTextBuffer::EnsureCapacity(intptr_t len) { |
| 53 intptr_t remaining = capacity_ - length_; |
| 54 if (remaining <= len) { |
| 55 const int kBufferSpareCapacity = 64; // Somewhat arbitrary. |
| 56 // TODO(turnidge): do we need to guard against overflow or other |
| 57 // security issues here? Text buffers are used by the debugger |
| 58 // to send user-controlled data (e.g. values of string variables) to |
| 59 // the debugger front-end. |
| 60 intptr_t new_capacity = capacity_ + len + kBufferSpareCapacity; |
| 61 buffer_ = zone_->Realloc<char>(buffer_, capacity_, new_capacity); |
| 62 capacity_ = new_capacity; |
| 63 } |
| 64 } |
| 65 |
| 66 } // namespace dart |
OLD | NEW |