MongoDB  2.7.0
alignedbuilder.h
1 // @file alignedbuilder.h
2 
31 #pragma once
32 
33 #include "mongo/base/string_data.h"
34 
35 namespace mongo {
36 
39  public:
40  AlignedBuilder(unsigned init_size);
41  ~AlignedBuilder() { kill(); }
42 
44  void reset(unsigned sz);
45 
47  void reset();
48 
50  const char* buf() const { return _p._data; }
51 
55  size_t skip(unsigned n) {
56  unsigned l = len();
57  grow(n);
58  return l;
59  }
60 
62  char* atOfs(unsigned ofs) { return _p._data + ofs; }
63 
65  char* cur() { return _p._data + _len; }
66 
67  void appendChar(char j) {
68  *((char*)grow(sizeof(char))) = j;
69  }
70  void appendNum(char j) {
71  *((char*)grow(sizeof(char))) = j;
72  }
73  void appendNum(short j) {
74  *((short*)grow(sizeof(short))) = j;
75  }
76  void appendNum(int j) {
77  *((int*)grow(sizeof(int))) = j;
78  }
79  void appendNum(unsigned j) {
80  *((unsigned*)grow(sizeof(unsigned))) = j;
81  }
82  void appendNum(bool j) {
83  *((bool*)grow(sizeof(bool))) = j;
84  }
85  void appendNum(double j) {
86  *((double*)grow(sizeof(double))) = j;
87  }
88  void appendNum(long long j) {
89  *((long long*)grow(sizeof(long long))) = j;
90  }
91  void appendNum(unsigned long long j) {
92  *((unsigned long long*)grow(sizeof(unsigned long long))) = j;
93  }
94 
95  void appendBuf(const void *src, size_t len) { memcpy(grow((unsigned) len), src, len); }
96 
97  template<class T>
98  void appendStruct(const T& s) { appendBuf(&s, sizeof(T)); }
99 
100  void appendStr(const StringData &str , bool includeEOO = true ) {
101  const unsigned len = str.size() + ( includeEOO ? 1 : 0 );
102  verify( len < (unsigned) BSONObjMaxUserSize );
103  str.copyTo( grow(len), includeEOO );
104  }
105 
107  unsigned len() const { return _len; }
108 
109  private:
110  static const unsigned Alignment = 8192;
111 
113  inline char* grow(unsigned by) {
114  unsigned oldlen = _len;
115  _len += by;
116  if (MONGO_unlikely( _len > _p._size )) {
117  growReallocate(oldlen);
118  }
119  return _p._data + oldlen;
120  }
121 
122  void growReallocate(unsigned oldLenInUse);
123  void kill();
124  void mallocSelfAligned(unsigned sz);
125  void _malloc(unsigned sz);
126  void _realloc(unsigned newSize, unsigned oldLenInUse);
127  void _free(void*);
128 
129  struct AllocationInfo {
130  char *_data;
131  void *_allocationAddress;
132  unsigned _size;
133  } _p;
134  unsigned _len; // bytes in use
135  };
136 
137 }
void reset()
reset for a re-use.
Definition: alignedbuilder.cpp:46
size_t skip(unsigned n)
leave room for some stuff later
Definition: alignedbuilder.h:55
const char * buf() const
note this may be deallocated (realloced) if you keep writing or reset().
Definition: alignedbuilder.h:50
char * atOfs(unsigned ofs)
if buffer grows pointer no longer valid
Definition: alignedbuilder.h:62
char * cur()
if buffer grows pointer no longer valid
Definition: alignedbuilder.h:65
a page-aligned BufBuilder.
Definition: alignedbuilder.h:38
unsigned len() const
Definition: alignedbuilder.h:107