]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/stringbuf.c
* add function to shrink empty StrBufs so they don't permanently alloc unneeded sizes.
[citadel.git] / libcitadel / lib / stringbuf.c
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