forked from rethinkdb/rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_buffer.hpp
More file actions
54 lines (39 loc) · 1.69 KB
/
printf_buffer.hpp
File metadata and controls
54 lines (39 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2010-2013 RethinkDB, all rights reserved.
#ifndef CONTAINERS_PRINTF_BUFFER_HPP_
#define CONTAINERS_PRINTF_BUFFER_HPP_
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
// Cannot include utils.hpp, we are included by utils.hpp.
#include "errors.hpp"
int64_t round_up_to_power_of_two(int64_t x);
// A static buffer for printing format strings, but it has a failsafe
// in case the buffer grows too large.
class printf_buffer_t {
public:
printf_buffer_t();
explicit printf_buffer_t(const char *format, ...) __attribute__((format (printf, 2, 3)));
printf_buffer_t(va_list ap, const char *format) __attribute__((format (printf, 3, 0)));
~printf_buffer_t();
// append and vappend become slow if the total size grows to N or
// greater. We still have std::vector-style amortized constant
// time growth, though.
void appendf(const char *format, ...) __attribute__((format (printf, 2, 3)));
void vappendf(const char *format, va_list ap) __attribute__((format (printf, 2, 0)));
char *data() const { return ptr_; }
const char *c_str() const { return ptr_; }
int size() const { return length_; }
static const int STATIC_DATA_SIZE = 1000;
private:
// The number of bytes that have been appended.
int64_t length_;
// Either a pointer to data_, or, if length_ >= N, a pointer to an
// array on the heap whose size is a power of two that is at least
// length_ + 1. Also, ptr_[length_] == 0, because we store a nul
// terminator.
char *ptr_;
// A data buffer for avoiding allocation in the common case.
char data_[STATIC_DATA_SIZE];
DISABLE_COPYING(printf_buffer_t);
};
#endif // CONTAINERS_PRINTF_BUFFER_HPP_