diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2016-10-02 19:59:41 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2016-10-03 16:52:36 +0000 |
commit | 6a0ea2d5fce1967927cf1f4f319fb0209ac2eb1e (patch) | |
tree | cdf844359ac298e85d5ffc4e15f467964a8c7593 /staging_vespalib/src | |
parent | 74d9d289d9b9ffd5f9f427a1fb7abf176bae4abe (diff) |
Checkpoint 1
Diffstat (limited to 'staging_vespalib/src')
7 files changed, 39 insertions, 56 deletions
diff --git a/staging_vespalib/src/tests/array/allocinarray_benchmark.cpp b/staging_vespalib/src/tests/array/allocinarray_benchmark.cpp index 9f365ff937c..426cb7ee3b8 100644 --- a/staging_vespalib/src/tests/array/allocinarray_benchmark.cpp +++ b/staging_vespalib/src/tests/array/allocinarray_benchmark.cpp @@ -60,7 +60,7 @@ private: typedef TreeNode<long> N; typedef RefTreeNode<long> R; -typedef AllocInArray<R, vespalib::Array<R, MMapAlloc> > Store; +typedef AllocInArray<R, vespalib::Array<R> > Store; void populate(Store & store, uint32_t parent, size_t depth) { diff --git a/staging_vespalib/src/tests/array/allocinarray_test.cpp b/staging_vespalib/src/tests/array/allocinarray_test.cpp index 3a40a71f288..de6e5792b35 100644 --- a/staging_vespalib/src/tests/array/allocinarray_test.cpp +++ b/staging_vespalib/src/tests/array/allocinarray_test.cpp @@ -26,7 +26,7 @@ Test::Main() TEST_INIT("allocinarray_test"); testAllocInArray<int64_t, vespalib::Array<int64_t> >(); - testAllocInArray<int64_t, vespalib::Array<int64_t, vespalib::DefaultAlloc> >(); + testAllocInArray<int64_t, vespalib::Array<int64_t> >(); testAllocInArray<int64_t, std::vector<int64_t> >(); testAllocInArray<int64_t, std::deque<int64_t> >(); diff --git a/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp b/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp index 26a7eac824f..e046415e8e8 100644 --- a/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp +++ b/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp @@ -12,21 +12,20 @@ size_t padbefore(size_t alignment, const char *buf) { } } -template <typename T> -DataBufferT<T>::DataBufferT(size_t len, size_t alignment) +DataBuffer::DataBuffer(size_t len, size_t alignment, const Alloc & initial) : _alignment(alignment), _externalBuf(NULL), _bufstart(NULL), _bufend(NULL), _datapt(NULL), _freept(NULL), - _buffer() + _buffer(initial.create(0)) { assert(_alignment > 0); if (len > 0) { // avoid very small buffers for performance reasons: size_t bufsize = std::max(256ul, roundUp2inN(len + (_alignment - 1))); - T newBuf(bufsize); + Alloc newBuf(initial.create(bufsize)); _bufstart = static_cast<char *>(newBuf.get()); _buffer.swap(newBuf); @@ -38,18 +37,16 @@ DataBufferT<T>::DataBufferT(size_t len, size_t alignment) } -template <typename T> void -DataBufferT<T>::moveFreeToData(size_t len) +DataBuffer::moveFreeToData(size_t len) { assert(getFreeLen() >= len); _freept += len; } -template <typename T> void -DataBufferT<T>::moveDeadToData(size_t len) +DataBuffer::moveDeadToData(size_t len) { assert(getDeadLen() >= len); _datapt -= len; @@ -59,18 +56,16 @@ DataBufferT<T>::moveDeadToData(size_t len) } -template <typename T> void -DataBufferT<T>::moveDataToFree(size_t len) +DataBuffer::moveDataToFree(size_t len) { assert(getDataLen() >= len); _freept -= len; } -template <typename T> bool -DataBufferT<T>::shrink(size_t newsize) +DataBuffer::shrink(size_t newsize) { if (getBufSize() <= newsize || getDataLen() > newsize) { return false; @@ -78,7 +73,7 @@ DataBufferT<T>::shrink(size_t newsize) char *newbuf = NULL; char *newdata = NULL; newsize += (_alignment - 1); - T newBuf(newsize); + Alloc newBuf(_buffer.create(newsize)); if (newsize != 0) { newbuf = static_cast<char *>(newBuf.get()); newdata = newbuf + padbefore(_alignment, newbuf); @@ -93,9 +88,8 @@ DataBufferT<T>::shrink(size_t newsize) } -template <typename T> void -DataBufferT<T>::pack(size_t needbytes) +DataBuffer::pack(size_t needbytes) { needbytes += (_alignment - 1); size_t dataLen = getDataLen(); @@ -104,7 +98,7 @@ DataBufferT<T>::pack(size_t needbytes) (getDeadLen() + getFreeLen()) * 4 < dataLen) { size_t bufsize = std::max(256ul, roundUp2inN(needbytes+dataLen)); - T newBuf(bufsize); + Alloc newBuf(_buffer.create(bufsize)); char *newbuf = static_cast<char *>(newBuf.get()); char *newdata = newbuf + padbefore(_alignment, newbuf); memcpy(newdata, _datapt, dataLen); @@ -122,9 +116,8 @@ DataBufferT<T>::pack(size_t needbytes) } -template <typename T> bool -DataBufferT<T>::equals(DataBufferT *other) +DataBuffer::equals(DataBuffer *other) { if (getDataLen() != other->getDataLen()) return false; @@ -132,9 +125,8 @@ DataBufferT<T>::equals(DataBufferT *other) } -template <typename T> void -DataBufferT<T>::hexDump() +DataBuffer::hexDump() { char *pt = _datapt; printf("*** DataBuffer HexDump BEGIN ***\n"); @@ -150,9 +142,8 @@ DataBufferT<T>::hexDump() } -template <typename T> void -DataBufferT<T>::swap(DataBufferT &other) +DataBuffer::swap(DataBuffer &other) { _buffer.swap(other._buffer); std::swap(_alignment, other._alignment); @@ -163,9 +154,8 @@ DataBufferT<T>::swap(DataBufferT &other) std::swap(_freept, other._freept); } -template <typename T> -T -DataBufferT<T>::stealBuffer() +vespalib::alloc::Alloc +DataBuffer::stealBuffer() { assert( ! referencesExternalData() ); _externalBuf = nullptr; @@ -176,15 +166,9 @@ DataBufferT<T>::stealBuffer() return std::move(_buffer); } -template <typename T> bool -DataBufferT<T>::referencesExternalData() const { +DataBuffer::referencesExternalData() const { return (_externalBuf == _bufstart) && (getBufSize() > 0); } -template class DataBufferT<HeapAlloc>; -template class DataBufferT<MMapAlloc>; -template class DataBufferT<DefaultAlloc>; - - } // namespace vespalib diff --git a/staging_vespalib/src/vespa/vespalib/data/databuffer.h b/staging_vespalib/src/vespa/vespalib/data/databuffer.h index 7db17808eae..76a604732f5 100644 --- a/staging_vespalib/src/vespa/vespalib/data/databuffer.h +++ b/staging_vespalib/src/vespa/vespalib/data/databuffer.h @@ -30,23 +30,23 @@ namespace vespalib { * the data will be relocated within the buffer and/or a bigger buffer * will be allocated. **/ -template <typename T> -class DataBufferT +class DataBuffer { private: + using Alloc = vespalib::alloc::Alloc; size_t _alignment; char *_externalBuf; char *_bufstart; char *_bufend; char *_datapt; char *_freept; - T _buffer; + Alloc _buffer; - DataBufferT(const DataBufferT &); - DataBufferT &operator=(const DataBufferT &); + DataBuffer(const DataBuffer &); + DataBuffer &operator=(const DataBuffer &); public: - typedef std::unique_ptr<DataBufferT<T>> UP; + typedef std::unique_ptr<DataBuffer> UP; /** * Construct a databuffer. @@ -54,7 +54,7 @@ public: * @param len the initial size of the buffer. * @param alignment required memory alignment for data start **/ - DataBufferT(size_t len = 1024, size_t alignment = 1); + DataBuffer(size_t len = 1024, size_t alignment = 1, const Alloc & initial = vespalib::DefaultAlloc::create(0)); /** * Construct a databuffer using externally allocated memory. Note @@ -64,22 +64,24 @@ public: * @param buf pointer to preallocated memory * @param len length of preallocated memory **/ - DataBufferT(char *buf, size_t len) : + DataBuffer(char *buf, size_t len) : _alignment(1), _externalBuf(buf), _bufstart(buf), _bufend(buf + len), _datapt(_bufstart), - _freept(_bufstart) + _freept(_bufstart), + _buffer(vespalib::DefaultAlloc::create(0)) { } - DataBufferT(const char *buf, size_t len) : + DataBuffer(const char *buf, size_t len) : _alignment(1), _externalBuf(const_cast<char *>(buf)), _bufstart(_externalBuf), _bufend(_bufstart + len), _datapt(_bufstart), - _freept(_bufend) + _freept(_bufend), + _buffer(vespalib::DefaultAlloc::create(0)) { } /** @@ -570,7 +572,7 @@ public: * @return true(equal)/false(not equal) * @param other the other buffer. **/ - bool equals(DataBufferT *other); + bool equals(DataBuffer *other); /** * Print a human-readable representation of this buffer to @@ -601,13 +603,10 @@ public: * * @param other the other buffer. **/ - void swap(DataBufferT &other); + void swap(DataBuffer &other); - T stealBuffer(); + Alloc stealBuffer(); }; -typedef DataBufferT<DefaultAlloc> DataBuffer; -typedef DataBufferT<MMapAlloc> MMapDataBuffer; - } // namespace vespalib diff --git a/staging_vespalib/src/vespa/vespalib/util/allocinarray.h b/staging_vespalib/src/vespa/vespalib/util/allocinarray.h index a6a89c4d41a..755d1885eee 100644 --- a/staging_vespalib/src/vespa/vespalib/util/allocinarray.h +++ b/staging_vespalib/src/vespa/vespalib/util/allocinarray.h @@ -16,7 +16,7 @@ namespace vespalib { * - when the AllocInArray goes out of scope. * - on an explicit clear. **/ -template <typename T, typename V=vespalib::Array<T, HeapAlloc> > +template <typename T, typename V=vespalib::Array<T> > class AllocInArray { public: typedef uint32_t Index; diff --git a/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.cpp b/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.cpp index 96e74ecf007..b17accddc5d 100644 --- a/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.cpp +++ b/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.cpp @@ -5,7 +5,7 @@ using namespace vespalib; GrowableByteBuffer::GrowableByteBuffer(uint32_t initialLen) : - _buffer(initialLen), + _buffer(DefaultAlloc::create(initialLen)), _position(0) { } @@ -16,7 +16,7 @@ GrowableByteBuffer::allocate(uint32_t len) size_t need(_position + len); if (need > _buffer.size()) { uint32_t newSize = vespalib::roundUp2inN(need); - DefaultAlloc newBuf(newSize); + Alloc newBuf(DefaultAlloc::create(newSize)); memcpy(newBuf.get(), _buffer.get(), _position); _buffer.swap(newBuf); } diff --git a/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.h b/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.h index 61baa250e40..fd65f0134bc 100644 --- a/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.h +++ b/staging_vespalib/src/vespa/vespalib/util/growablebytebuffer.h @@ -85,8 +85,8 @@ public: private: void putReverse(const char* buffer, uint32_t length); - - DefaultAlloc _buffer; + using Alloc = vespalib::alloc::Alloc; + Alloc _buffer; uint32_t _position; double _growFactor; |