From 193c2c8366b85b35f307331d4d2cb43d9e10dfb5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sun, 31 May 2009 22:46:15 +0000 Subject: [PATCH] * add function to shrink empty StrBufs so they don't permanently alloc unneeded sizes. --- libcitadel/lib/libcitadel.h | 1 + libcitadel/lib/stringbuf.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) 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 -- 2.39.2