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