From 428c9a77e91859ce295b8f775dcf16450b3e8725 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Sat, 11 Sep 2010 15:02:26 +0200 Subject: [PATCH] * NewStrBufDupAppendFlush(): if you have an IO/Buf, and a maybe yet created or still NULL target buffer, this is your friend. Depending on bufferlength it will choose to swap buffers or copy them over, so your target buffer won't become that huge for a short string. --- libcitadel/lib/libcitadel.h | 1 + libcitadel/lib/stringbuf.c | 91 ++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 029145f12..d297385bc 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -189,6 +189,7 @@ 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); +void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, int KeepOriginal); 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 fcbd1daa7..61a688c13 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -215,6 +215,23 @@ void dbg_Init(StrBuf *Buf) #endif +/** + * @ingroup StrBuf + * @brief swaps the contents of two StrBufs + * this is to be used to have cheap switched between a work-buffer and a target buffer + * @param A First one + * @param B second one + */ +static inline void SwapBuffers(StrBuf *A, StrBuf *B) +{ + StrBuf C; + + memcpy(&C, A, sizeof(*A)); + memcpy(A, B, sizeof(*B)); + memcpy(B, &C, sizeof(C)); + +} + /** * @ingroup StrBuf_Cast * @brief Cast operator to Plain String @@ -372,6 +389,64 @@ StrBuf* NewStrBufDup(const StrBuf *CopyMe) return NewBuf; } +/** + * @ingroup StrBuf_DeConstructors + * @brief Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards. + * @param CopyFlushMe Buffer to faxmilate if KeepOriginal, or to move into CreateRelpaceMe if !KeepOriginal. + * @param CreateRelpaceMe If NULL, will be created, else Flushed and filled CopyFlushMe + * @param KeepOriginal should CopyFlushMe remain intact? or may we Steal its buffer? + * @returns the new stringbuffer + */ +void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, int KeepOriginal) +{ + StrBuf *NewBuf; + + if (CreateRelpaceMe == NULL) + return; + if (CopyFlushMe == NULL) + { + if (*CreateRelpaceMe != NULL) + FlushStrBuf(*CreateRelpaceMe); + else + *CreateRelpaceMe = NewStrBuf(); + return; + } + + /* + * Randomly Chosen: bigger than 64 chars is cheaper to swap the buffers instead of copying. + * else *CreateRelpaceMe may use more memory than needed in a longer term, CopyFlushMe might + * be a big IO-Buffer... + */ + if (KeepOriginal || (StrLength(CopyFlushMe) > 64)) + { + if (*CreateRelpaceMe == NULL) + { + *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed + 1); + dbg_Init(NewBuf); + } + else + { + NewBuf = *CreateRelpaceMe; + FlushStrBuf(NewBuf); + } + StrBufAppendBuf(NewBuf, CopyFlushMe, 0); + } + else + { + if (*CreateRelpaceMe == NULL) + { + *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufSize); + dbg_Init(NewBuf); + } + else + NewBuf = *CreateRelpaceMe; + SwapBuffers (NewBuf, CopyFlushMe); + } + if (!KeepOriginal) + FlushStrBuf(CopyFlushMe); + return; +} + /** * @ingroup StrBuf_DeConstructors * @brief create a new Buffer using an existing c-string @@ -2725,22 +2800,6 @@ static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr) return end; } -/** - * @ingroup StrBuf - * @brief swaps the contents of two StrBufs - * this is to be used to have cheap switched between a work-buffer and a target buffer - * @param A First one - * @param B second one - */ -static inline void SwapBuffers(StrBuf *A, StrBuf *B) -{ - StrBuf C; - - memcpy(&C, A, sizeof(*A)); - memcpy(A, B, sizeof(*B)); - memcpy(B, &C, sizeof(C)); - -} /** -- 2.39.2