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