diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-02-10 21:21:54 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-02-10 21:44:07 +0000 |
commit | 626d0f456c3b678a8cc247ec76b3ad9a63dff7fb (patch) | |
tree | 7f48e58f66985e2fb20eddb5e75fac931f952807 /vespamalloc | |
parent | b35ecd28e9f90b12ac95f48f71e71d827230a472 (diff) |
Remove template argument
Diffstat (limited to 'vespamalloc')
18 files changed, 171 insertions, 155 deletions
diff --git a/vespamalloc/src/tests/allocfree/linklist.cpp b/vespamalloc/src/tests/allocfree/linklist.cpp index ea6eab44555..1bffb76b2ee 100644 --- a/vespamalloc/src/tests/allocfree/linklist.cpp +++ b/vespamalloc/src/tests/allocfree/linklist.cpp @@ -20,18 +20,18 @@ template <size_t MinSizeClassC, size_t MaxSizeClassMultiAllocC> class MemBlockT : public vespamalloc::CommonT<MinSizeClassC> { public: - typedef vespamalloc::StackEntry<vespamalloc::StackReturnEntry> Stack; + typedef vespamalloc::StackEntry Stack; enum { MaxSizeClassMultiAlloc = MaxSizeClassMultiAllocC, SizeClassSpan = (MaxSizeClassMultiAllocC-MinSizeClassC) }; - MemBlockT() : _ptr(NULL) { } + MemBlockT() : _ptr(nullptr) { } MemBlockT(void * p) : _ptr(p) { } MemBlockT(void * p, size_t /*sz*/) : _ptr(p) { } void *ptr() { return _ptr; } const void *ptr() const { return _ptr; } - bool validAlloc() const { return _ptr != NULL; } - bool validFree() const { return _ptr != NULL; } + bool validAlloc() const { return _ptr != nullptr; } + bool validFree() const { return _ptr != nullptr; } void setExact(size_t ) { } void alloc(bool ) { } void threadId(int ) { } @@ -40,7 +40,7 @@ public: bool allocated() const { return false; } int threadId() const { return 0; } void info(FILE *, unsigned level=0) const { (void) level; } - Stack * callStack() { return NULL; } + Stack * callStack() { return nullptr; } size_t callStackLen() const { return 0; } static size_t adjustSize(size_t sz) { return sz; } @@ -138,7 +138,7 @@ int Test::Main() { ASSERT_TRUE((l >= &globalList[0]) && (l < &globalList[NumBlocks])); } List *n = List::linkOut(sharedList); - ASSERT_TRUE(n == NULL); + ASSERT_TRUE(n == nullptr); List::HeadPtr tmp(sharedList.load()); tmp._tag = 1; @@ -156,12 +156,12 @@ int Test::Main() { LinkInOutAndIn pc1(sharedList, 16, false); LinkInOutAndIn pc2(sharedList, 16, true); - ASSERT_TRUE(pool.NewThread(&c1, NULL) != NULL); - ASSERT_TRUE(pool.NewThread(&c2, NULL) != NULL); - ASSERT_TRUE(pool.NewThread(&p1, NULL) != NULL); - ASSERT_TRUE(pool.NewThread(&p2, NULL) != NULL); - ASSERT_TRUE(pool.NewThread(&pc1, NULL) != NULL); - ASSERT_TRUE(pool.NewThread(&pc2, NULL) != NULL); + ASSERT_TRUE(pool.NewThread(&c1, nullptr) != nullptr); + ASSERT_TRUE(pool.NewThread(&c2, nullptr) != nullptr); + ASSERT_TRUE(pool.NewThread(&p1, nullptr) != nullptr); + ASSERT_TRUE(pool.NewThread(&p2, nullptr) != nullptr); + ASSERT_TRUE(pool.NewThread(&pc1, nullptr) != nullptr); + ASSERT_TRUE(pool.NewThread(&pc2, nullptr) != nullptr); for (; duration > 0; --duration) { LOG(info, "%d seconds left...", duration); @@ -182,6 +182,6 @@ int Test::Main() { ASSERT_TRUE((l >= &globalList[0]) && (l < &globalList[NumBlocks])); } n = List::linkOut(sharedList); - ASSERT_TRUE(n == NULL); + ASSERT_TRUE(n == nullptr); TEST_DONE(); } diff --git a/vespamalloc/src/tests/test2/testgraph.cpp b/vespamalloc/src/tests/test2/testgraph.cpp index 9aa3020f0be..7a79756f7fb 100644 --- a/vespamalloc/src/tests/test2/testgraph.cpp +++ b/vespamalloc/src/tests/test2/testgraph.cpp @@ -23,7 +23,7 @@ public: asciistream os; os << ' ' << node; _string += os.c_str(); - if (node.callers() == NULL) { + if (node.callers() == nullptr) { printf("%s\n", _string.c_str()); } } @@ -75,7 +75,7 @@ void testaggregator() { callGraph.addStack(s4, 3); Aggregator agg; DumpGraph<CallGraphT::Node> dump(&agg, "{ ", " }"); - callGraph.traverseDepth(dump);; + callGraph.traverseDepth(dump); asciistream ost; ost << agg; printf("%s\n", ost.c_str()); diff --git a/vespamalloc/src/vespamalloc/malloc/common.cpp b/vespamalloc/src/vespamalloc/malloc/common.cpp index eab023d12fa..4d97aa13b05 100644 --- a/vespamalloc/src/vespamalloc/malloc/common.cpp +++ b/vespamalloc/src/vespamalloc/malloc/common.cpp @@ -1,5 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "common.h" +#include <vespamalloc/util/callstack.h> #include <pthread.h> namespace vespamalloc { @@ -30,7 +31,7 @@ void Mutex::quit() void Mutex::init() { if (!_use && ! _stopRecursion) { - pthread_mutex_init(&_mutex, NULL); + pthread_mutex_init(&_mutex, nullptr); _use = true; } } @@ -43,6 +44,34 @@ Guard::Guard(Mutex & m) : MallocRecurseOnSuspend(true); } +FILE * _G_logFile = stderr; +size_t _G_bigBlockLimit = 0x80000000; + +void +logStackTrace() { + StackEntry st[32]; + size_t count = StackEntry::fillStack(st, NELEMS(st)); + st[4].info(_G_logFile); + fprintf(_G_logFile, "\n"); + for(size_t i=1; (i < count) && (i < NELEMS(st)); i++) { + const auto & s = st[i]; + if (s.valid()) { + s.info(_G_logFile); + fprintf(_G_logFile, " from "); + } + } + fprintf(_G_logFile, "\n"); +} + +void +logBigBlock(const void *ptr, size_t exact, size_t adjusted, size_t gross) +{ + size_t sz(exact); + if (std::max(std::max(sz, adjusted), gross) > _G_bigBlockLimit) { + fprintf(_G_logFile, "validating %p(%ld, %ld, %ld) ", ptr, sz, adjusted, gross); + logStackTrace(); + } +} } diff --git a/vespamalloc/src/vespamalloc/malloc/common.h b/vespamalloc/src/vespamalloc/malloc/common.h index 421575ac587..4b0af3199a1 100644 --- a/vespamalloc/src/vespamalloc/malloc/common.h +++ b/vespamalloc/src/vespamalloc/malloc/common.h @@ -122,6 +122,11 @@ public: }; void info(); +void logBigBlock(const void * ptr, size_t exact, size_t adjusted, size_t gross) __attribute__((noinline)); +void logStackTrace() __attribute__((noinline)); + +extern FILE * _G_logFile; +extern size_t _G_bigBlockLimit; } diff --git a/vespamalloc/src/vespamalloc/malloc/globalpool.hpp b/vespamalloc/src/vespamalloc/malloc/globalpool.hpp index 65e9af2fe52..ae9791f37b2 100644 --- a/vespamalloc/src/vespamalloc/malloc/globalpool.hpp +++ b/vespamalloc/src/vespamalloc/malloc/globalpool.hpp @@ -127,7 +127,7 @@ AllocPoolT<MemBlockPtrT>::exactAlloc(size_t exactSize, SizeClassT sc, csl->add(mem); ChunkSList * ncsl = csl; USE_STAT2(_stat[sc]._exactAlloc.fetch_add(1, std::memory_order_relaxed)); - mem.logBigBlock(exactSize, mem.adjustSize(exactSize), MemBlockPtrT::classSize(sc)); + logBigBlock(mem.ptr(), exactSize, mem.adjustSize(exactSize), MemBlockPtrT::classSize(sc)); PARANOID_CHECK1( if (ncsl->empty() || (ncsl->count() > ChunkSList::NumBlocks)) { *(int*)0 = 0; } ); return ncsl; } @@ -143,7 +143,7 @@ AllocPoolT<MemBlockPtrT>::returnMemory(SizeClassT sc, typename AllocPoolT<MemBlo for(; !csl->empty(); ) { MemBlockPtrT mem; csl->sub(mem); - mem.logBigBlock(mem.size(), mem.adjustSize(mem.size()), MemBlockPtrT::classSize(sc)); + logBigBlock(mem.ptr(), mem.size(), mem.adjustSize(mem.size()), MemBlockPtrT::classSize(sc)); _dataSegment.returnBlock(mem.rawPtr()); } completelyEmpty = csl; diff --git a/vespamalloc/src/vespamalloc/malloc/malloc.cpp b/vespamalloc/src/vespamalloc/malloc/malloc.cpp index 75c06c04859..36912e80768 100644 --- a/vespamalloc/src/vespamalloc/malloc/malloc.cpp +++ b/vespamalloc/src/vespamalloc/malloc/malloc.cpp @@ -26,7 +26,7 @@ template <> void MemBlock:: dumpInfo(size_t level) { - _GmemP->info(_logFile, level); + _GmemP->info(_G_logFile, level); } } diff --git a/vespamalloc/src/vespamalloc/malloc/malloc.h b/vespamalloc/src/vespamalloc/malloc/malloc.h index 449dce29896..2fb0f81826d 100644 --- a/vespamalloc/src/vespamalloc/malloc/malloc.h +++ b/vespamalloc/src/vespamalloc/malloc/malloc.h @@ -123,18 +123,7 @@ template <typename MemBlockPtrT, typename ThreadListT> void MemoryManager<MemBlockPtrT, ThreadListT>::crash() { fprintf(stderr, "vespamalloc detected unrecoverable error.\n"); -#if 0 - if (_invalidMemLogLevel > 0) { - static size_t numRecurse=0; - if (numRecurse++ == 0) { - MemBlockPtrT::dumpInfo(_invalidMemLogLevel); - } - numRecurse--; - } - sleep(1); -#else abort(); -#endif } template <typename MemBlockPtrT, typename ThreadListT> @@ -211,7 +200,7 @@ void MemoryManager<MemBlockPtrT, ThreadListT>::freeSC(void *ptr, SizeClassT sc) template <typename MemBlockPtrT, typename ThreadListT> void * MemoryManager<MemBlockPtrT, ThreadListT>::realloc(void *oldPtr, size_t sz) { - void *ptr(nullptr); + void *ptr; if (oldPtr) { MemBlockPtrT mem(oldPtr); mem.readjustAlignment(_segment); diff --git a/vespamalloc/src/vespamalloc/malloc/memblock.h b/vespamalloc/src/vespamalloc/malloc/memblock.h index 7a7ee7c71f0..1d0b0e3da00 100644 --- a/vespamalloc/src/vespamalloc/malloc/memblock.h +++ b/vespamalloc/src/vespamalloc/malloc/memblock.h @@ -3,7 +3,7 @@ #include <vespamalloc/util/callstack.h> #include <vespamalloc/malloc/common.h> -#include <stdio.h> +#include <cstdio> namespace vespamalloc { @@ -12,7 +12,7 @@ class MemBlockT : public CommonT<MinSizeClassC> { public: using Parent = CommonT<MinSizeClassC>; - using Stack = StackEntry<StackReturnEntry>; + using Stack = StackEntry; enum { MaxSizeClassMultiAlloc = MaxSizeClassMultiAllocC, SizeClassSpan = (MaxSizeClassMultiAllocC-MinSizeClassC) @@ -43,14 +43,11 @@ public: Stack * callStack() { return nullptr; } size_t callStackLen() const { return 0; } void fillMemory(size_t) { } - void logBigBlock(size_t exact, size_t adjusted, size_t gross) const __attribute__((noinline)); static size_t adjustSize(size_t sz) { return sz; } static size_t adjustSize(size_t sz, std::align_val_t) { return sz; } static size_t unAdjustSize(size_t sz) { return sz; } static void dumpInfo(size_t level); - static void dumpFile(FILE * fp) { _logFile = fp; } - static void bigBlockLimit(size_t lim); static void setFill(uint8_t ) { } static bool verifySizeClass(int sc) { (void) sc; return true; } static size_t getMinSizeForAlignment(size_t align, size_t sz) { @@ -60,11 +57,9 @@ public: } private: void * _ptr; - static FILE *_logFile; - static size_t _bigBlockLimit; }; -typedef MemBlockT<5, 20> MemBlock; +using MemBlock = MemBlockT<5, 20>; template <> void MemBlock::dumpInfo(size_t level); extern template class MemBlockT<5, 20>; diff --git a/vespamalloc/src/vespamalloc/malloc/memblock.hpp b/vespamalloc/src/vespamalloc/malloc/memblock.hpp index ffd27d2dfbc..c4bef6de99d 100644 --- a/vespamalloc/src/vespamalloc/malloc/memblock.hpp +++ b/vespamalloc/src/vespamalloc/malloc/memblock.hpp @@ -5,40 +5,5 @@ namespace vespamalloc { -template <size_t MinSizeClassC, size_t MaxSizeClassMultiAllocC> -void -MemBlockT<MinSizeClassC, MaxSizeClassMultiAllocC>::logBigBlock(size_t exact, size_t adjusted, size_t gross) const -{ - size_t sz(exact); - if (std::max(std::max(sz, adjusted), gross) > _bigBlockLimit) { - Stack st[32]; - size_t count = Stack::fillStack(st, NELEMS(st)); - fprintf(_logFile, "validating %p(%ld, %ld, %ld)", - ptr(), sz, adjusted, gross); - st[3].info(_logFile); - fprintf(_logFile, "\n"); - for(size_t i=1; (i < count) && (i < NELEMS(st)); i++) { - const Stack & s = st[i]; - if (s.valid()) { - s.info(_logFile); - fprintf(_logFile, " from "); - } - } - fprintf(_logFile, "\n"); - } -} - -template <size_t MinSizeClassC, size_t MaxSizeClassMultiAllocC> -void -MemBlockT<MinSizeClassC, MaxSizeClassMultiAllocC>::bigBlockLimit(size_t lim) -{ - _bigBlockLimit = lim; -} - -template <size_t MinSizeClassC, size_t MaxSizeClassMultiAllocC> -FILE * MemBlockT<MinSizeClassC, MaxSizeClassMultiAllocC>::_logFile = stderr; -template <size_t MinSizeClassC, size_t MaxSizeClassMultiAllocC> -size_t MemBlockT<MinSizeClassC, MaxSizeClassMultiAllocC>::_bigBlockLimit = 0x80000000; - } diff --git a/vespamalloc/src/vespamalloc/malloc/memblockboundscheck.h b/vespamalloc/src/vespamalloc/malloc/memblockboundscheck.h index 8c734f6dbd3..d6421b9a7dd 100644 --- a/vespamalloc/src/vespamalloc/malloc/memblockboundscheck.h +++ b/vespamalloc/src/vespamalloc/malloc/memblockboundscheck.h @@ -9,7 +9,7 @@ namespace vespamalloc { class MemBlockBoundsCheckBaseTBase : public CommonT<5> { public: - typedef StackEntry<StackReturnEntry> Stack; + using Stack = StackEntry; void * rawPtr() { return _ptr; } void *ptr() { char *p((char*)_ptr); diff --git a/vespamalloc/src/vespamalloc/malloc/memorywatcher.h b/vespamalloc/src/vespamalloc/malloc/memorywatcher.h index 242dd5d9fcc..88229aca77a 100644 --- a/vespamalloc/src/vespamalloc/malloc/memorywatcher.h +++ b/vespamalloc/src/vespamalloc/malloc/memorywatcher.h @@ -194,14 +194,14 @@ template <typename T, typename S> void MemoryWatcher<T, S>::activateOptions() { activateLogFile(_params[Params::logfile].value()); - T::dumpFile(_logFile); + _G_logFile = _logFile; this->setupSegmentLog(_params[Params::bigsegment_loglevel].valueAsLong(), _params[Params::bigsegment_limit].valueAsLong(), _params[Params::bigsegment_increment].valueAsLong(), _params[Params::allocs2show].valueAsLong()); this->setupLog(_params[Params::pralloc_loglimit].valueAsLong()); this->setParams(_params[Params::threadcachelimit].valueAsLong()); - T::bigBlockLimit(_params[Params::bigblocklimit].valueAsLong()); + _G_bigBlockLimit = _params[Params::bigblocklimit].valueAsLong(); T::setFill(_params[Params::fillvalue].valueAsLong()); } diff --git a/vespamalloc/src/vespamalloc/util/callgraph.h b/vespamalloc/src/vespamalloc/util/callgraph.h index 94df3bd5e92..5dffe7f6a31 100644 --- a/vespamalloc/src/vespamalloc/util/callgraph.h +++ b/vespamalloc/src/vespamalloc/util/callgraph.h @@ -11,7 +11,7 @@ template<typename T, typename AddSub> class CallGraphNode { public: - CallGraphNode() : _callers(NULL), _next(NULL), _content(), _count(0) { } + CallGraphNode() : _callers(nullptr), _next(nullptr), _content(), _count(0) { } const CallGraphNode *next() const { return _next; } const CallGraphNode *callers() const { return _callers; } const T & content() const { return _content; } @@ -45,9 +45,9 @@ bool CallGraphNode<T, AddSub>::addStack(T * stack, size_t nelem, Store & store) } else if (_content == stack[0]) { _count++; if (nelem > 1) { - if (_callers == NULL) { + if (_callers == nullptr) { _callers = store.alloc(); - if (_callers != NULL) { + if (_callers != nullptr) { _callers->content(stack[1]); } } @@ -58,9 +58,9 @@ bool CallGraphNode<T, AddSub>::addStack(T * stack, size_t nelem, Store & store) retval = true; } } else { - if (_next == NULL) { + if (_next == nullptr) { _next = store.alloc(); - if (_next != NULL) { + if (_next != nullptr) { _next->content(stack[0]); } } @@ -102,7 +102,7 @@ class ArrayStore { public: ArrayStore() : _used(0) { } - T * alloc() { return (_used < MaxElem) ? &_array[_used++] : NULL; } + T * alloc() { return (_used < MaxElem) ? &_array[_used++] : nullptr; } AddSub size() const { return _used; } private: AddSub _used; @@ -113,15 +113,15 @@ template <typename Content, size_t MaxElems, typename AddSub> class CallGraph { public: - typedef CallGraphNode<Content, AddSub> Node; + using Node = CallGraphNode<Content, AddSub>; CallGraph() : - _root(NULL), - _nodeStore(new NodeStore()) + _root(nullptr), + _nodeStore(std::make_unique<NodeStore>()) { } CallGraph(Content root) : - _root(NULL), - _nodeStore(new NodeStore()) + _root(nullptr), + _nodeStore(std::make_unique<NodeStore>()) { checkOrSetRoot(root); } @@ -143,14 +143,14 @@ private: CallGraph(const CallGraph &); CallGraph & operator = (const CallGraph &); bool checkOrSetRoot(const Content & root) { - if (_root == NULL) { + if (_root == nullptr) { _root = _nodeStore->alloc(); _root->content(root); } - return (_root != NULL); + return (_root != nullptr); } typedef ArrayStore<Node, MaxElems, AddSub> NodeStore; - Node * _root; + Node * _root; std::unique_ptr<NodeStore> _nodeStore; }; diff --git a/vespamalloc/src/vespamalloc/util/callstack.cpp b/vespamalloc/src/vespamalloc/util/callstack.cpp index 9be2aa8133e..629f6427ab3 100644 --- a/vespamalloc/src/vespamalloc/util/callstack.cpp +++ b/vespamalloc/src/vespamalloc/util/callstack.cpp @@ -5,7 +5,8 @@ namespace vespamalloc { -const char * dlAddr(const void * func) { +const char * +dlAddr(const void * func) { static const char * _unknown = "UNKNOWN"; const char * funcName = _unknown; Dl_info info; @@ -16,25 +17,29 @@ const char * dlAddr(const void * func) { return funcName; } -static void verifyAndCopy(const void * addr, char *v, size_t sz) -{ +namespace { +void +verifyAndCopy(const void *addr, char *v, size_t sz) { size_t pos(0); - const char * sym = dlAddr(addr); - for (;sym && (sym[pos] != '\0') && (pos < sz-1); pos++) { + const char *sym = dlAddr(addr); + for (; sym && (sym[pos] != '\0') && (pos < sz - 1); pos++) { char c(sym[pos]); v[pos] = isprint(c) ? c : '.'; } v[pos] = '\0'; } +} -void StackReturnEntry::info(FILE * os) const +void +StackReturnEntry::info(FILE * os) const { static char tmp[0x400]; verifyAndCopy(_return, tmp, sizeof(tmp)); fprintf(os, "%s(%p)", tmp, _return); } -asciistream & operator << (asciistream & os, const StackReturnEntry & v) +asciistream & +operator << (asciistream & os, const StackReturnEntry & v) { static char tmp[0x100]; static char t[0x200]; @@ -43,4 +48,30 @@ asciistream & operator << (asciistream & os, const StackReturnEntry & v) return os << t; } +const void * StackEntry::_stopAddr = nullptr; + +size_t +StackEntry::fillStack(StackEntry *stack, size_t nelems) +{ + void * retAddr[nelems]; + int sz = backtrace(retAddr, nelems); + if ((sz > 0) && (size_t(sz) <= nelems)) { + for(int i(1); i < sz; i++) { + StackEntry entry(retAddr[i], nullptr); + if (entry.valid()) { + stack[i-1] = entry; + } else { + sz = i; + } + } + sz -= 1; // Do not count self + } else { + sz = 0; + } + if (size_t(sz) < nelems) { + stack[sz] = StackEntry(); + } + return sz; +} + } diff --git a/vespamalloc/src/vespamalloc/util/callstack.h b/vespamalloc/src/vespamalloc/util/callstack.h index 89feb01c4b2..3a2986a0a89 100644 --- a/vespamalloc/src/vespamalloc/util/callstack.h +++ b/vespamalloc/src/vespamalloc/util/callstack.h @@ -13,8 +13,8 @@ const char * dlAddr(const void * addr); class StackReturnEntry { public: - StackReturnEntry(const void * returnAddress = NULL, - const void * stack=NULL) + StackReturnEntry(const void * returnAddress = nullptr, + const void * stack=nullptr) : _return(returnAddress) { (void) stack; @@ -23,7 +23,7 @@ public: return (size_t(_return) - size_t(b._return)); } void info(FILE * os) const; - bool valid() const { return _return != NULL; } + bool valid() const { return _return != nullptr; } bool valid(const void * stopAddr) const { return valid() && (_return != stopAddr); } bool valid(const void * stopAddrMin, const void * stopAddrMax) const { return valid() && ! ((stopAddrMin <= _return) && (_return < stopAddrMax)); } private: @@ -31,11 +31,10 @@ private: const void * _return; }; -template <typename StackRep> class StackEntry { public: - StackEntry(const void * returnAddress = NULL, - const void * stack = NULL) + StackEntry(const void * returnAddress = nullptr, + const void * stack = nullptr) : _stackRep(returnAddress, stack) { } bool operator == (const StackEntry & b) const { return cmp(b) == 0; } @@ -47,39 +46,12 @@ public: static void setStopAddress(const void * stopAddr) { _stopAddr = stopAddr; } private: int cmp(const StackEntry & b) const { return _stackRep.cmp(b._stackRep); } - friend asciistream & operator << (asciistream & os, const StackEntry<StackRep> & v) { + friend asciistream & operator << (asciistream & os, const StackEntry & v) { return os << v._stackRep; } - StackRep _stackRep; + StackReturnEntry _stackRep; static const void * _stopAddr; }; -template <typename StackRep> -const void * StackEntry<StackRep>::_stopAddr = NULL; - -template <typename StackRep> -size_t StackEntry<StackRep>::fillStack(StackEntry<StackRep> *stack, size_t nelems) -{ - void * retAddr[nelems]; - int sz = backtrace(retAddr, nelems); - if ((sz > 0) && (size_t(sz) <= nelems)) { - for(int i(1); i < sz; i++) { - StackEntry<StackRep> entry(retAddr[i], NULL); - if (entry.valid()) { - stack[i-1] = entry; - } else { - sz = i; - } - } - sz -= 1; // Do not count self - } else { - sz = 0; - } - if (size_t(sz) < nelems) { - stack[sz] = StackEntry<StackRep>(); - } - return sz; -} - } diff --git a/vespamalloc/src/vespamalloc/util/stream.cpp b/vespamalloc/src/vespamalloc/util/stream.cpp index 4bf2e9aed9e..4af6ab56aba 100644 --- a/vespamalloc/src/vespamalloc/util/stream.cpp +++ b/vespamalloc/src/vespamalloc/util/stream.cpp @@ -5,18 +5,50 @@ namespace vespamalloc { -asciistream::asciistream() : - _rPos(0), - _wPos(0), - _buffer(static_cast<char *>(malloc(1024))), - _sz(1024) +asciistream::asciistream() + : _rPos(0), + _wPos(0), + _buffer(static_cast<char *>(malloc(1024))), + _sz(1024) { } asciistream::~asciistream() { - free(_buffer); - _buffer = nullptr; + if (_buffer != nullptr) { + free(_buffer); + _buffer = nullptr; + } +} + +asciistream::asciistream(asciistream && rhs) noexcept + : _rPos(rhs._rPos), + _wPos(rhs._wPos), + _buffer(rhs._buffer), + _sz(rhs._sz) +{ + rhs._rPos = 0; + rhs._wPos = 0; + rhs._sz = 0; + rhs._buffer = nullptr; +} + +asciistream & +asciistream::operator = (asciistream && rhs) noexcept +{ + if (this != &rhs) { + if (_buffer) free(_buffer); + + _rPos = rhs._rPos; + _wPos = rhs._wPos; + _buffer = rhs._buffer; + _sz = rhs._sz; + rhs._rPos = 0; + rhs._wPos = 0; + rhs._sz = 0; + rhs._buffer = nullptr; + } + return *this; } asciistream::asciistream(const asciistream & rhs) : @@ -29,7 +61,8 @@ asciistream::asciistream(const asciistream & rhs) : _buffer[_wPos] = 0; } -asciistream & asciistream::operator = (const asciistream & rhs) +asciistream & +asciistream::operator = (const asciistream & rhs) { if (this != &rhs) { asciistream newStream(rhs); @@ -38,6 +71,7 @@ asciistream & asciistream::operator = (const asciistream & rhs) return *this; } + void asciistream::swap(asciistream & rhs) { std::swap(_rPos, rhs._rPos); diff --git a/vespamalloc/src/vespamalloc/util/stream.h b/vespamalloc/src/vespamalloc/util/stream.h index 5d9750be827..34af28486dd 100644 --- a/vespamalloc/src/vespamalloc/util/stream.h +++ b/vespamalloc/src/vespamalloc/util/stream.h @@ -11,6 +11,8 @@ class asciistream public: asciistream(); ~asciistream(); + asciistream(asciistream && rhs) noexcept; + asciistream & operator = (asciistream && ) noexcept; asciistream(const asciistream & rhs); asciistream & operator = (const asciistream & rhs); void swap(asciistream & rhs); diff --git a/vespamalloc/src/vespamalloc/util/traceutil.cpp b/vespamalloc/src/vespamalloc/util/traceutil.cpp index 49cde544732..36698125f97 100644 --- a/vespamalloc/src/vespamalloc/util/traceutil.cpp +++ b/vespamalloc/src/vespamalloc/util/traceutil.cpp @@ -4,13 +4,9 @@ namespace vespamalloc { -Aggregator::Aggregator() -{ -} +Aggregator::Aggregator() = default; -Aggregator::~Aggregator() -{ -} +Aggregator::~Aggregator() = default; struct CmpGraph { @@ -23,8 +19,8 @@ asciistream & operator << (asciistream & os, const Aggregator & v) { Aggregator::Map map(v._map); std::sort(map.begin(), map.end(), CmpGraph()); - for (Aggregator::Map::const_iterator it=map.begin(); it != map.end(); it++) { - os << it->first << " : " << it->second.c_str() << '\n'; + for (const auto & e : map) { + os << e.first << " : " << e.second.c_str() << '\n'; } return os; } diff --git a/vespamalloc/src/vespamalloc/util/traceutil.h b/vespamalloc/src/vespamalloc/util/traceutil.h index 15fb8e221c8..fe424d7259e 100644 --- a/vespamalloc/src/vespamalloc/util/traceutil.h +++ b/vespamalloc/src/vespamalloc/util/traceutil.h @@ -13,7 +13,7 @@ namespace vespamalloc { -typedef StackEntry<StackReturnEntry> StackElem; +using StackElem = StackEntry; typedef CallGraph<StackElem, 0x10000, Index> CallGraphT; class Aggregator @@ -21,7 +21,7 @@ class Aggregator public: Aggregator(); ~Aggregator(); - void push_back(size_t num, const string & s) { _map.push_back(Map::value_type(num, s)); } + void push_back(size_t num, const string & s) { _map.emplace_back(num, s); } friend asciistream & operator << (asciistream & os, const Aggregator & v); private: typedef std::vector< std::pair<size_t, string> > Map; @@ -57,9 +57,7 @@ DumpGraph<N>::DumpGraph(Aggregator * aggregator, const char * start, const char } template<typename N> -DumpGraph<N>::~DumpGraph() -{ -} +DumpGraph<N>::~DumpGraph() = default; template<typename N> void DumpGraph<N>::handle(const N & node) @@ -71,7 +69,7 @@ void DumpGraph<N>::handle(const N & node) asciistream os; os << ' ' << node; _string += os.c_str(); - if (node.callers() == NULL) { + if (node.callers() == nullptr) { _string += _endString; _aggregator->push_back(_min, _string); } |