From 536075b00a3d36973b123b5d54f32cc2b7ee3b58 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Tue, 13 May 2025 22:28:29 +0200 Subject: [PATCH] Add string buffer library --- src/Sourcefile | 1 + src/strbuf.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/strbuf.h | 29 ++++++++++++++++++++++++++ src/typedef.h | 3 +++ 4 files changed, 88 insertions(+) create mode 100644 src/strbuf.c create mode 100644 src/strbuf.h diff --git a/src/Sourcefile b/src/Sourcefile index 36b9a0e79..e8e6eb5f7 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -1,5 +1,6 @@ qs22j.c string.c +strbuf.c d_main.cpp d_clisrv.c d_net.c diff --git a/src/strbuf.c b/src/strbuf.c new file mode 100644 index 000000000..a38db9aad --- /dev/null +++ b/src/strbuf.c @@ -0,0 +1,55 @@ +// BLANKART +//----------------------------------------------------------------------------- +// Copyright (C) 2025 by Team BlanKart. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file strbuf.c +/// \brief string buffer library, for making chunks of many small strings + +#include "doomdef.h" +#include "strbuf.h" +#include "z_zone.h" + +#define STRBUF_BLOCKSIZE 16384 // 16K pages are the future, son! + +// allocates and returns a new string buffer +// free with Z_Free +strbuf_t *strbuf_alloc(void) +{ + strbuf_t *buf = Z_Malloc(STRBUF_BLOCKSIZE, PU_STATIC, NULL); + if (!buf) + I_Error("Failed to allocate string buffer"); + buf->size = sizeof(UINT32); + return buf; +} + +// appends a string to the buffer +// returns an offset to the newly appended string +UINT32 strbuf_append(strbuf_t **strbuf, const char *str) +{ + strbuf_t *buf = *strbuf; + size_t addbytes = strlen(str) + 1; + UINT32 oldsize = buf->size; + + buf->size += addbytes; + if ((oldsize % STRBUF_BLOCKSIZE) + addbytes >= STRBUF_BLOCKSIZE) { + UINT32 newsize = buf->size + STRBUF_BLOCKSIZE - (buf->size % STRBUF_BLOCKSIZE); + if (newsize <= oldsize) + I_Error("String buffer size wrapped around!?"); + buf = *strbuf = Z_Realloc(buf, newsize, PU_STATIC, NULL); + if (!buf) + I_Error("Failed to allocate string buffer"); + } + + strcpy(buf->buf - sizeof(UINT32) + oldsize, str); + return oldsize; +} + +// returns the string at the given offset +char *strbuf_get(strbuf_t *strbuf, UINT32 ofs) +{ + return strbuf->buf - sizeof(UINT32) + ofs; +} diff --git a/src/strbuf.h b/src/strbuf.h new file mode 100644 index 000000000..238e5bf7f --- /dev/null +++ b/src/strbuf.h @@ -0,0 +1,29 @@ +// BLANKART +//----------------------------------------------------------------------------- +// Copyright (C) 2025 by Team BlanKart. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file strbuf.h +/// \brief string buffer library, for making chunks of many small strings + +#ifdef __cplusplus +extern "C" { +#endif + +// for simplicity's sake, the header is included in the size (keeps the allocation aligned!) +struct strbuf_t +{ + UINT32 size; + char buf[]; +}; + +strbuf_t *strbuf_alloc(void); +UINT32 strbuf_append(strbuf_t **strbuf, const char *str); +char *strbuf_get(strbuf_t *strbuf, UINT32 ofs); + +#ifdef __cplusplus +} +#endif diff --git a/src/typedef.h b/src/typedef.h index 467352019..312eaff03 100644 --- a/src/typedef.h +++ b/src/typedef.h @@ -393,6 +393,9 @@ TYPEDEF (vmode_t); // sounds.h TYPEDEF (sfxinfo_t); +// strbuf.h +TYPEDEF (strbuf_t); + // taglist.h TYPEDEF (taglist_t); TYPEDEF (taggroup_t);