From 847f7a7569f90052eec2335f1b666b01c8ed5f57 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 11 Aug 2015 21:19:55 -0400 Subject: [PATCH] Automatic ReBuffer<> garbage collection of unused buffers --- src/IOThread.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/IOThread.h b/src/IOThread.h index 0b06f0a..734b37a 100644 --- a/src/IOThread.h +++ b/src/IOThread.h @@ -37,6 +37,8 @@ protected: }; +#define REBUFFER_GC_LIMIT 100 + template class ReBuffer { @@ -44,11 +46,23 @@ public: BufferType *getBuffer() { BufferType* buf = NULL; for (outputBuffersI = outputBuffers.begin(); outputBuffersI != outputBuffers.end(); outputBuffersI++) { - if ((*outputBuffersI)->getRefCount() <= 0) { - return (*outputBuffersI); + if (!buf && (*outputBuffersI)->getRefCount() <= 0) { + buf = (*outputBuffersI); + (*outputBuffersI)->setRefCount(0); + } else if ((*outputBuffersI)->getRefCount() <= 0) { + (*outputBuffersI)->decRefCount(); } } + if (buf) { + if (outputBuffers.back()->getRefCount() < -REBUFFER_GC_LIMIT) { + BufferType *ref = outputBuffers.back(); + outputBuffers.pop_back(); + delete ref; + } + return buf; + } + buf = new BufferType(); outputBuffers.push_back(buf);