From: Wilfried Göesgens Date: Sun, 31 May 2009 22:46:15 +0000 (+0000) Subject: * add function to shrink empty StrBufs so they don't permanently alloc unneeded sizes. X-Git-Tag: v7.86~1113 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=193c2c8366b85b35f307331d4d2cb43d9e10dfb5;p=citadel.git * add function to shrink empty StrBufs so they don't permanently alloc unneeded sizes. --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 4c378b9fa..132c49f09 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -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); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 3759ecccc..dc180176e 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -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