Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: ppapi/native_client/src/untrusted/nacl_ppapi_util/string_buffer.cc

Issue 10914053: Relocating files in the nacl repo that belong in chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include <stdarg.h>
8
9 #include "native_client/src/untrusted/nacl_ppapi_util/string_buffer.h"
10
11 namespace nacl {
12
13 StringBuffer::StringBuffer() {
14 nbytes_ = 1024;
15 insert_ = 0;
16 buffer_ = reinterpret_cast<char *>(malloc(nbytes_));
17 if (NULL == buffer_) {
18 perror("StringBuffer Ctor malloc");
19 abort();
20 }
21 buffer_[0] = '\0';
22 }
23
24 void StringBuffer::DiscardOutput() {
25 insert_ = 0;
26 buffer_[0] = '\0';
27 }
28
29 StringBuffer::~StringBuffer() {
30 nbytes_ = 0;
31 insert_ = 0;
32 free(buffer_);
33 buffer_ = NULL;
34 }
35
36 void StringBuffer::Printf(char const *fmt, ...) {
37 size_t space;
38 char *insert_pt;
39 va_list ap;
40 size_t written = 0;
41 char *new_buffer;
42
43 for (;;) {
44 space = nbytes_ - insert_;
45 insert_pt = buffer_ + insert_;
46 va_start(ap, fmt);
47 written = vsnprintf(insert_pt, space, fmt, ap);
48 va_end(ap);
49 if (written < space) {
50 insert_ += written;
51 break;
52 }
53 // insufficient space -- grow the buffer
54 new_buffer = reinterpret_cast<char *>(realloc(buffer_, 2 * nbytes_));
55 if (NULL == new_buffer) {
56 // give up
57 fprintf(stderr, "StringBufferPrintf: no memory\n");
58 break;
59 }
60 nbytes_ *= 2;
61 buffer_ = new_buffer;
62 }
63 }
64
65 } // namespace nacl
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698