* add function to shrink empty StrBufs so they don't permanently alloc unneeded sizes.
authorWilfried Göesgens <willi@citadel.org>
Sun, 31 May 2009 22:46:15 +0000 (22:46 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 31 May 2009 22:46:15 +0000 (22:46 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 4c378b9fa61668de8230ce8772be0b972dca95d1..132c49f091c0ac1bf00ad4ede23dcf327fd6b3e5 100644 (file)
@@ -223,6 +223,7 @@ StrBuf* NewStrBuf(void);
 StrBuf* NewStrBufDup(const StrBuf *CopyMe);
 StrBuf* NewStrBufPlain(const char* ptr, int nChars);
 long StrBufShrinkToFit(StrBuf *Buf, int Force);
+void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize);
 
 int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars);
 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant);
index 3759eccccee1e97e1366309b65feda5cee631978..dc180176e5e60ec298a1c80e09be8b14f7083e6b 100644 (file)
@@ -138,6 +138,22 @@ static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
        return Buf->BufSize;
 }
 
+/**
+ * \brief shrink an _EMPTY_ buffer if its Buffer superseeds threshhold to NewSize. Buffercontent is thoroughly ignored and flushed.
+ * \param Buf Buffer to shrink (has to be empty)
+ * \param ThreshHold if the buffer is bigger then this, its readjusted
+ * \param NewSize if we Shrink it, how big are we going to be afterwards?
+ */
+void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize)
+{
+       if (Buf->BufUsed > ThreshHold) {
+               free(Buf->buf);
+               Buf->buf = (char*) malloc(NewSize);
+               Buf->BufUsed = 0;
+               Buf->BufSize = NewSize;
+       }
+}
+
 /**
  * \brief shrink long term buffers to their real size so they don't waste memory
  * \param Buf buffer to shrink