0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00
mongodb/util/alignedbuilder.h

124 lines
3.7 KiB
C
Raw Normal View History

2010-11-29 04:49:17 +01:00
// @file alignedbuilder.h
/**
* Copyright (C) 2009 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../bson/stringdata.h"
2010-11-29 04:49:17 +01:00
2011-01-04 06:40:41 +01:00
namespace mongo {
2010-11-29 04:49:17 +01:00
/** a page-aligned BufBuilder. */
class AlignedBuilder {
public:
AlignedBuilder(unsigned init_size);
2010-11-29 04:49:17 +01:00
~AlignedBuilder() { kill(); }
/** reset for a re-use. shrinks if > 128MB */
void reset() {
_len = 0;
const unsigned sizeCap = 128*1024*1024;
if (_p._size > sizeCap)
_realloc(sizeCap, _len);
}
2010-11-29 04:49:17 +01:00
/** note this may be deallocated (realloced) if you keep writing or reset(). */
2010-12-01 17:17:50 +01:00
const char* buf() const { return _p._data; }
2010-11-29 04:49:17 +01:00
2011-01-04 06:40:41 +01:00
/** leave room for some stuff later
2010-12-01 15:48:29 +01:00
@return offset in the buffer that was our current position
*/
2011-01-04 06:40:41 +01:00
size_t skip(unsigned n) {
2010-12-01 15:48:29 +01:00
unsigned l = len();
grow(n);
return l;
}
2010-12-01 17:17:50 +01:00
char* atOfs(unsigned ofs) { return _p._data + ofs; }
2010-12-01 15:48:29 +01:00
2011-01-04 06:40:41 +01:00
void appendChar(char j) {
2010-11-29 04:49:17 +01:00
*((char*)grow(sizeof(char))) = j;
}
2011-01-04 06:40:41 +01:00
void appendNum(char j) {
2010-11-29 04:49:17 +01:00
*((char*)grow(sizeof(char))) = j;
}
void appendNum(short j) {
*((short*)grow(sizeof(short))) = j;
}
void appendNum(int j) {
*((int*)grow(sizeof(int))) = j;
}
void appendNum(unsigned j) {
*((unsigned*)grow(sizeof(unsigned))) = j;
}
void appendNum(bool j) {
*((bool*)grow(sizeof(bool))) = j;
}
void appendNum(double j) {
*((double*)grow(sizeof(double))) = j;
}
void appendNum(long long j) {
*((long long*)grow(sizeof(long long))) = j;
}
void appendNum(unsigned long long j) {
*((unsigned long long*)grow(sizeof(unsigned long long))) = j;
}
void appendBuf(const void *src, size_t len) { memcpy(grow((unsigned) len), src, len); }
2010-11-29 04:49:17 +01:00
template<class T>
void appendStruct(const T& s) { appendBuf(&s, sizeof(T)); }
2010-11-29 04:49:17 +01:00
void appendStr(const StringData &str , bool includeEOO = true ) {
const unsigned len = str.size() + ( includeEOO ? 1 : 0 );
2010-12-06 03:30:58 +01:00
assert( len < (unsigned) BSONObjMaxUserSize );
2010-11-29 04:49:17 +01:00
memcpy(grow(len), str.data(), len);
}
/** @return the in-use length */
unsigned len() const { return _len; }
private:
static const unsigned Alignment = 8192;
2010-11-29 04:49:17 +01:00
/** returns the pre-grow write position */
inline char* grow(unsigned by) {
unsigned oldlen = _len;
_len += by;
2010-12-01 17:17:50 +01:00
if ( _len > _p._size ) {
growReallocate(oldlen);
2010-11-29 04:49:17 +01:00
}
2010-12-01 17:17:50 +01:00
return _p._data + oldlen;
2010-11-29 04:49:17 +01:00
}
2010-12-01 17:17:50 +01:00
void growReallocate(unsigned oldLenInUse);
void kill();
2010-12-01 17:17:50 +01:00
void mallocSelfAligned(unsigned sz);
void _malloc(unsigned sz);
void _realloc(unsigned newSize, unsigned oldLenInUse);
2011-01-04 06:40:41 +01:00
void _free(void*);
2010-11-29 04:49:17 +01:00
2010-12-01 17:17:50 +01:00
struct AllocationInfo {
char *_data;
void *_allocationAddress;
unsigned _size;
} _p;
unsigned _len; // bytes in use
2010-11-29 04:49:17 +01:00
};
}