diff options
author | dwight <dwight@10gen.com> | 2010-11-15 22:58:09 -0500 |
---|---|---|
committer | dwight <dwight@10gen.com> | 2010-11-15 22:58:09 -0500 |
commit | 62206631c02076f6c0537e4f6b63c4c836a8458c (patch) | |
tree | 6eaf8f8cb09b2996ac6b540cad3c1e5bd32ea77f | |
parent | 80f2d635e05eb93d9d6907e3278606ede384f6b6 (diff) | |
download | mongo-62206631c02076f6c0537e4f6b63c4c836a8458c.tar.gz |
const
-rw-r--r-- | util/alignedbuilder.h | 292 | ||||
-rw-r--r-- | util/logfile.cpp | 2 |
2 files changed, 147 insertions, 147 deletions
diff --git a/util/alignedbuilder.h b/util/alignedbuilder.h index bc663212ed1..a6e09938f2a 100644 --- a/util/alignedbuilder.h +++ b/util/alignedbuilder.h @@ -1,148 +1,148 @@ // @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 - -namespace mongo { - - /** a page-aligned BufBuilder. */ - class AlignedBuilder { - public: - AlignedBuilder(unsigned init_size) : _size(init_size) { - _data = (char *) _malloc(_size); - if( _data == 0 ) - msgasserted(13523, "out of memory AlignedBuilder"); - _len = 0; - } - ~AlignedBuilder() { kill(); } - - /** reset for a re-use */ - void reset() { _len = 0; } - - /** leave room for some stuff later */ - char* skip(unsigned n) { return grow(n); } - - /** note this may be deallocated (realloced) if you keep writing or reset(). */ - const char* buf() const { return _data; } - - void appendChar(char j){ - *((char*)grow(sizeof(char))) = j; - } - void appendNum(char j){ - *((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); - } - - template<class T> - void appendStruct(const T& s) { - appendBuf(&s, sizeof(T)); - } - - void appendStr(const StringData &str , bool includeEOO = true ) { - const unsigned len = str.size() + ( includeEOO ? 1 : 0 ); - memcpy(grow(len), str.data(), len); - } - - /** @return the in-use length */ - unsigned len() const { return _len; } - - private: - /** returns the pre-grow write position */ - inline char* grow(unsigned by) { - unsigned oldlen = _len; - _len += by; - if ( _len > _size ) { - grow_reallocate(); - } - return _data + oldlen; - } - - /* "slow" portion of 'grow()' */ - void NOINLINE_DECL grow_reallocate(){ - unsigned a = _size * 2; - assert( a ); - if ( _len > a ) - a = _len + 16 * 1024; - assert( a < 0x20000000 ); - _data = (char *) _realloc(_data, a, _len); - _size = a; - } - - static const unsigned Alignment = 8192; - static void _free(void *p) { -#if defined(_WIN32) - VirtualFree(p, 0, MEM_RELEASE); -#else - free(p); -#endif - } - static void* _malloc(unsigned sz) { -#if defined(_WIN32) - void *p = VirtualAlloc(0, sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); -#elif defined(_POSIX_VERSION) - void *p = 0; - int res = posix_memalign(&p, Alignment, sz); - massert(13524, "out of memory AlignedBuilder", res == 0); - cout << "temp: _malloc " << p << ' ' << sz << endl; -#else - void *p = malloc(sz); - assert( ((size_t) p) % Alignment == 0 ); -#endif - return p; - } - static void* _realloc(void *ptr, unsigned newSize, unsigned oldSize) { - void *p = _malloc(newSize); - memcpy(p, ptr, oldSize); - _free(ptr); - return p; - } - void kill() { - _free(_data); - _data = 0; - } - - char *_data; - unsigned _len; - unsigned _size; - }; - -} +/**
+* 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
+
+namespace mongo {
+
+ /** a page-aligned BufBuilder. */
+ class AlignedBuilder {
+ public:
+ AlignedBuilder(unsigned init_size) : _size(init_size) {
+ _data = (char *) _malloc(_size);
+ if( _data == 0 )
+ msgasserted(13523, "out of memory AlignedBuilder");
+ _len = 0;
+ }
+ ~AlignedBuilder() { kill(); }
+
+ /** reset for a re-use */
+ void reset() { _len = 0; }
+
+ /** leave room for some stuff later */
+ char* skip(unsigned n) { return grow(n); }
+
+ /** note this may be deallocated (realloced) if you keep writing or reset(). */
+ const char* buf() const { return _data; }
+
+ void appendChar(char j){
+ *((char*)grow(sizeof(char))) = j;
+ }
+ void appendNum(char j){
+ *((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);
+ }
+
+ template<class T>
+ void appendStruct(const T& s) {
+ appendBuf(&s, sizeof(T));
+ }
+
+ void appendStr(const StringData &str , bool includeEOO = true ) {
+ const unsigned len = str.size() + ( includeEOO ? 1 : 0 );
+ memcpy(grow(len), str.data(), len);
+ }
+
+ /** @return the in-use length */
+ unsigned len() const { return _len; }
+
+ private:
+ /** returns the pre-grow write position */
+ inline char* grow(unsigned by) {
+ unsigned oldlen = _len;
+ _len += by;
+ if ( _len > _size ) {
+ grow_reallocate();
+ }
+ return _data + oldlen;
+ }
+
+ /* "slow" portion of 'grow()' */
+ void NOINLINE_DECL grow_reallocate(){
+ unsigned a = _size * 2;
+ assert( a );
+ if ( _len > a )
+ a = _len + 16 * 1024;
+ assert( a < 0x20000000 );
+ _data = (char *) _realloc(_data, a, _len);
+ _size = a;
+ }
+
+ static const unsigned Alignment = 8192;
+ static void _free(void *p) {
+#if defined(_WIN32)
+ VirtualFree(p, 0, MEM_RELEASE);
+#else
+ free(p);
+#endif
+ }
+ static void* _malloc(unsigned sz) {
+#if defined(_WIN32)
+ void *p = VirtualAlloc(0, sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
+#elif defined(_POSIX_VERSION)
+ void *p = 0;
+ int res = posix_memalign(&p, Alignment, sz);
+ massert(13524, "out of memory AlignedBuilder", res == 0);
+ cout << "temp: _malloc " << p << ' ' << sz << endl;
+#else
+ void *p = malloc(sz);
+ assert( ((size_t) p) % Alignment == 0 );
+#endif
+ return p;
+ }
+ static void* _realloc(void *ptr, unsigned newSize, unsigned oldSize) {
+ void *p = _malloc(newSize);
+ memcpy(p, ptr, oldSize);
+ _free(ptr);
+ return p;
+ }
+ void kill() {
+ _free(_data);
+ _data = 0;
+ }
+
+ char *_data;
+ unsigned _len;
+ unsigned _size;
+ };
+
+}
diff --git a/util/logfile.cpp b/util/logfile.cpp index 9a11ae29bc1..5629fd0b51f 100644 --- a/util/logfile.cpp +++ b/util/logfile.cpp @@ -77,7 +77,7 @@ namespace mongo { CloseHandle(_fd); } - void LogFile::synchronousAppend(void *buf, size_t len) { + void LogFile::synchronousAppend(const void *buf, size_t len) { assert(_fd); DWORD written; if( !WriteFile(_fd, buf, len, &written, NULL) ) { |