From: NaitLee Date: Sun, 16 Jul 2023 08:53:05 +0000 (+0800) Subject: (fix #378) Implement `FS_LoadFileExtraBuffer` in `fs.c` to avoid reallocation X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=048e9e8a519473e447879a99d328bc3c8d73c4b2;p=xonotic%2Fdarkplaces.git (fix #378) Implement `FS_LoadFileExtraBuffer` in `fs.c` to avoid reallocation Signed-off-by: NaitLee --- diff --git a/cmd.c b/cmd.c index 684be183..6d5797d3 100644 --- a/cmd.c +++ b/cmd.c @@ -618,7 +618,7 @@ static void Cmd_Exec(cmd_state_t *cmd, const char *filename) return; // don't execute config.cfg } - f = (char *)FS_LoadFile (filename, tempmempool, false, &fsize); + f = (char *)FS_LoadFileExtraBuffer (filename, tempmempool, false, 2, &fsize); if (!f) { Con_Printf("couldn't exec %s\n",filename); @@ -628,7 +628,6 @@ static void Cmd_Exec(cmd_state_t *cmd, const char *filename) if (f[fsize - 1] != '\n') { - f = (char *)Mem_Realloc(cbuf_mempool, f, fsize + 2); f[fsize] = '\n'; f[fsize + 1] = '\0'; ++fsize; diff --git a/fs.c b/fs.c index 25789e02..5a17a7e1 100644 --- a/fs.c +++ b/fs.c @@ -3382,9 +3382,10 @@ FS_LoadAndCloseQFile Loads full content of a qfile_t and closes it. Always appends a 0 byte. +You may specify to allocate extra bytes for the file content buffer, by using the `extrabytes` parameter. ============ */ -static unsigned char *FS_LoadAndCloseQFile (qfile_t *file, const char *path, mempool_t *pool, qbool quiet, fs_offset_t *filesizepointer) +static unsigned char *FS_LoadAndCloseQFile (qfile_t *file, const char *path, mempool_t *pool, qbool quiet, unsigned long extrabytes, fs_offset_t *filesizepointer) { unsigned char *buf = NULL; fs_offset_t filesize = 0; @@ -3399,7 +3400,7 @@ static unsigned char *FS_LoadAndCloseQFile (qfile_t *file, const char *path, mem return NULL; } - buf = (unsigned char *)Mem_Alloc (pool, filesize + 1); + buf = (unsigned char *)Mem_Alloc (pool, filesize + 1 + extrabytes); buf[filesize] = '\0'; FS_Read (file, buf, filesize); FS_Close (file); @@ -3424,7 +3425,21 @@ Always appends a 0 byte. unsigned char *FS_LoadFile (const char *path, mempool_t *pool, qbool quiet, fs_offset_t *filesizepointer) { qfile_t *file = FS_OpenVirtualFile(path, quiet); - return FS_LoadAndCloseQFile(file, path, pool, quiet, filesizepointer); + return FS_LoadAndCloseQFile(file, path, pool, quiet, 0, filesizepointer); +} + +/* +============ +FS_LoadFileExtraBuffer + +Filename are relative to the quake directory. +Always appends a 0 byte, and optionally extra bytes. +============ +*/ +unsigned char *FS_LoadFileExtraBuffer (const char *path, mempool_t *pool, qbool quiet, unsigned long extrabytes, fs_offset_t *filesizepointer) +{ + qfile_t *file = FS_OpenVirtualFile(path, quiet); + return FS_LoadAndCloseQFile(file, path, pool, quiet, extrabytes, filesizepointer); } @@ -3439,7 +3454,7 @@ Always appends a 0 byte. unsigned char *FS_SysLoadFile (const char *path, mempool_t *pool, qbool quiet, fs_offset_t *filesizepointer) { qfile_t *file = FS_SysOpen(path, "rb", false); - return FS_LoadAndCloseQFile(file, path, pool, quiet, filesizepointer); + return FS_LoadAndCloseQFile(file, path, pool, quiet, 0, filesizepointer); } diff --git a/fs.h b/fs.h index 7b7b18f9..f7275739 100644 --- a/fs.h +++ b/fs.h @@ -118,6 +118,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet, const void FS_FreeSearch(fssearch_t *search); unsigned char *FS_LoadFile (const char *path, mempool_t *pool, qbool quiet, fs_offset_t *filesizepointer); +unsigned char *FS_LoadFileExtraBuffer (const char *path, mempool_t *pool, qbool quiet, unsigned long extrabytes, fs_offset_t *filesizepointer); unsigned char *FS_SysLoadFile (const char *path, mempool_t *pool, qbool quiet, fs_offset_t *filesizepointer); qbool FS_WriteFileInBlocks (const char *filename, const void *const *data, const fs_offset_t *len, size_t count); qbool FS_WriteFile (const char *filename, const void *data, fs_offset_t len);