From 9ee1b32e2ddd6fa6aa35ce6139de423d79d8daf7 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 30 Sep 2024 01:10:42 +0300 Subject: [PATCH] engine: set malloc like and warn_unused_result attributes on common allocation functions, fs, image and soundlib --- engine/common/common.h | 80 ++++++++++++++++++------------- engine/common/filesystem_engine.c | 25 ++++++++++ engine/common/soundlib/snd_main.c | 2 +- engine/ref_api.h | 9 ++-- filesystem/filesystem.h | 6 ++- filesystem/fscallback.h | 8 +++- 6 files changed, 91 insertions(+), 39 deletions(-) diff --git a/engine/common/common.h b/engine/common/common.h index 59dae364..0d32d093 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -112,6 +112,9 @@ typedef enum #include "con_nprint.h" #include "crclib.h" #include "ref_api.h" +#define FSCALLBACK_OVERRIDE_OPEN +#define FSCALLBACK_OVERRIDE_LOADFILE +#define FSCALLBACK_OVERRIDE_MALLOC_LIKE #include "fscallback.h" // PERFORMANCE INFO @@ -362,12 +365,49 @@ extern host_parm_t host; typedef void (*xcommand_t)( void ); +// +// zone.c +// +void Memory_Init( void ); +void _Mem_Free( void *data, const char *filename, int fileline ); +void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) + ALLOC_CHECK( 3 ) WARN_UNUSED_RESULT; +void *_Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) + ALLOC_CHECK( 2 ) MALLOC_LIKE( _Mem_Free, 1 ) WARN_UNUSED_RESULT; +poolhandle_t _Mem_AllocPool( const char *name, const char *filename, int fileline ) + WARN_UNUSED_RESULT; +void _Mem_FreePool( poolhandle_t *poolptr, const char *filename, int fileline ); +void _Mem_EmptyPool( poolhandle_t poolptr, const char *filename, int fileline ); +void _Mem_Check( const char *filename, int fileline ); +qboolean Mem_IsAllocatedExt( poolhandle_t poolptr, void *data ); +void Mem_PrintList( size_t minallocationsize ); +void Mem_PrintStats( void ); + +#define Mem_Malloc( pool, size ) _Mem_Alloc( pool, size, false, __FILE__, __LINE__ ) +#define Mem_Calloc( pool, size ) _Mem_Alloc( pool, size, true, __FILE__, __LINE__ ) +#define Mem_Realloc( pool, ptr, size ) _Mem_Realloc( pool, ptr, size, true, __FILE__, __LINE__ ) +#define Mem_Free( mem ) _Mem_Free( mem, __FILE__, __LINE__ ) +#define Mem_AllocPool( name ) _Mem_AllocPool( name, __FILE__, __LINE__ ) +#define Mem_FreePool( pool ) _Mem_FreePool( pool, __FILE__, __LINE__ ) +#define Mem_EmptyPool( pool ) _Mem_EmptyPool( pool, __FILE__, __LINE__ ) +#define Mem_IsAllocated( mem ) Mem_IsAllocatedExt( NULL, mem ) +#define Mem_Check() _Mem_Check( __FILE__, __LINE__ ) + // // filesystem_engine.c // void FS_Init( const char *basedir ); void FS_Shutdown( void ); void *FS_GetNativeObject( const char *obj ); +int FS_Close( file_t *file ); +search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) + MALLOC_LIKE( _Mem_Free, 1 ) WARN_UNUSED_RESULT; +file_t *FS_Open( const char *filepath, const char *mode, qboolean gamedironly ) + MALLOC_LIKE( FS_Close, 1 ) WARN_UNUSED_RESULT; +byte *FS_LoadFile( const char *path, fs_offset_t *filesizeptr, qboolean gamedironly ) + MALLOC_LIKE( _Mem_Free, 1 ) WARN_UNUSED_RESULT; +byte *FS_LoadDirectFile( const char *path, fs_offset_t *filesizeptr ) + MALLOC_LIKE( _Mem_Free, 1 ) WARN_UNUSED_RESULT; // // cmd.c @@ -400,30 +440,6 @@ void Cmd_ExecuteString( const char *text ); void Cmd_ForwardToServer( void ); void Cmd_Escape( char *newCommand, const char *oldCommand, int len ); -// -// zone.c -// -void Memory_Init( void ); -void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 3 ); -void *_Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 2 ); -poolhandle_t _Mem_AllocPool( const char *name, const char *filename, int fileline ); -void _Mem_FreePool( poolhandle_t *poolptr, const char *filename, int fileline ); -void _Mem_EmptyPool( poolhandle_t poolptr, const char *filename, int fileline ); -void _Mem_Free( void *data, const char *filename, int fileline ); -void _Mem_Check( const char *filename, int fileline ); -qboolean Mem_IsAllocatedExt( poolhandle_t poolptr, void *data ); -void Mem_PrintList( size_t minallocationsize ); -void Mem_PrintStats( void ); - -#define Mem_Malloc( pool, size ) _Mem_Alloc( pool, size, false, __FILE__, __LINE__ ) -#define Mem_Calloc( pool, size ) _Mem_Alloc( pool, size, true, __FILE__, __LINE__ ) -#define Mem_Realloc( pool, ptr, size ) _Mem_Realloc( pool, ptr, size, true, __FILE__, __LINE__ ) -#define Mem_Free( mem ) _Mem_Free( mem, __FILE__, __LINE__ ) -#define Mem_AllocPool( name ) _Mem_AllocPool( name, __FILE__, __LINE__ ) -#define Mem_FreePool( pool ) _Mem_FreePool( pool, __FILE__, __LINE__ ) -#define Mem_EmptyPool( pool ) _Mem_EmptyPool( pool, __FILE__, __LINE__ ) -#define Mem_IsAllocated( mem ) Mem_IsAllocatedExt( NULL, mem ) -#define Mem_Check() _Mem_Check( __FILE__, __LINE__ ) // // imagelib @@ -434,10 +450,10 @@ void Image_Setup( void ); void Image_Init( void ); void Image_Shutdown( void ); void Image_AddCmdFlags( uint flags ); -rgbdata_t *FS_LoadImage( const char *filename, const byte *buffer, size_t size ); -qboolean FS_SaveImage( const char *filename, rgbdata_t *pix ); -rgbdata_t *FS_CopyImage( rgbdata_t *in ); void FS_FreeImage( rgbdata_t *pack ); +rgbdata_t *FS_LoadImage( const char *filename, const byte *buffer, size_t size ) MALLOC_LIKE( FS_FreeImage, 1 ) WARN_UNUSED_RESULT; +qboolean FS_SaveImage( const char *filename, rgbdata_t *pix ); +rgbdata_t *FS_CopyImage( rgbdata_t *in ) MALLOC_LIKE( FS_FreeImage, 1 ) WARN_UNUSED_RESULT; extern const bpc_desc_t PFDesc[]; // image get pixelformat qboolean Image_Process( rgbdata_t **pix, int width, int height, uint flags, float reserved ); void Image_PaletteHueReplace( byte *palSrc, int newHue, int start, int end, int pal_size ); @@ -492,14 +508,14 @@ typedef struct // void Sound_Init( void ); void Sound_Shutdown( void ); -wavdata_t *FS_LoadSound( const char *filename, const byte *buffer, size_t size ); void FS_FreeSound( wavdata_t *pack ); -stream_t *FS_OpenStream( const char *filename ); +void FS_FreeStream( stream_t *stream ); +wavdata_t *FS_LoadSound( const char *filename, const byte *buffer, size_t size ) MALLOC_LIKE( FS_FreeSound, 1 ) WARN_UNUSED_RESULT; +stream_t *FS_OpenStream( const char *filename ) MALLOC_LIKE( FS_FreeStream, 1 ) WARN_UNUSED_RESULT; wavdata_t *FS_StreamInfo( stream_t *stream ); int FS_ReadStream( stream_t *stream, int bytes, void *buffer ); int FS_SetStreamPos( stream_t *stream, int newpos ); int FS_GetStreamPos( stream_t *stream ); -void FS_FreeStream( stream_t *stream ); qboolean Sound_Process( wavdata_t **wav, int rate, int width, int channels, uint flags ); uint Sound_GetApproxWavePlayLen( const char *filepath ); qboolean Sound_SupportedFileFormat( const char *fileext ); @@ -564,7 +580,7 @@ qboolean SV_Active( void ); char *COM_MemFgets( byte *pMemFile, int fileSize, int *filePos, char *pBuffer, int bufferSize ); void COM_HexConvert( const char *pszInput, int nInputLength, byte *pOutput ); int COM_SaveFile( const char *filename, const void *data, int len ); -byte* COM_LoadFileForMe( const char *filename, int *pLength ); +byte *COM_LoadFileForMe( const char *filename, int *pLength ) MALLOC_LIKE( free, 1 ); qboolean COM_IsSafeFileToDownload( const char *filename ); cvar_t *pfnCVarGetPointer( const char *szVarName ); int pfnDrawConsoleString( int x, int y, char *string ); @@ -667,7 +683,7 @@ char *CL_Userinfo( void ); void CL_LegacyUpdateInfo( void ); void CL_CharEvent( int key ); qboolean CL_DisableVisibility( void ); -byte *COM_LoadFile( const char *filename, int usehunk, int *pLength ); +byte *COM_LoadFile( const char *filename, int usehunk, int *pLength ) MALLOC_LIKE( free, 1 ); struct cmd_s *Cmd_GetFirstFunctionHandle( void ); struct cmd_s *Cmd_GetNextFunctionHandle( struct cmd_s *cmd ); struct cmdalias_s *Cmd_AliasGetList( void ); diff --git a/engine/common/filesystem_engine.c b/engine/common/filesystem_engine.c index 909eedb3..c9c52835 100644 --- a/engine/common/filesystem_engine.c +++ b/engine/common/filesystem_engine.c @@ -27,6 +27,31 @@ fs_globals_t *FI; static pfnCreateInterface_t fs_pfnCreateInterface; static HINSTANCE fs_hInstance; +search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly ) +{ + return g_fsapi.Search( pattern, caseinsensitive, gamedironly ); +} + +int FS_Close( file_t *file ) +{ + return g_fsapi.Close( file ); +} + +file_t *FS_Open( const char *filepath, const char *mode, qboolean gamedironly ) +{ + return g_fsapi.Open( filepath, mode, gamedironly ); +} + +byte *FS_LoadFile( const char *path, fs_offset_t *filesizeptr, qboolean gamedironly ) +{ + return g_fsapi.LoadFile( path, filesizeptr, gamedironly ); +} + +byte *FS_LoadDirectFile( const char *path, fs_offset_t *filesizeptr ) +{ + return g_fsapi.LoadDirectFile( path, filesizeptr ); +} + static void COM_StripDirectorySlash( char *pname ) { size_t len; diff --git a/engine/common/soundlib/snd_main.c b/engine/common/soundlib/snd_main.c index fd2c25b6..a9ff2e87 100644 --- a/engine/common/soundlib/snd_main.c +++ b/engine/common/soundlib/snd_main.c @@ -30,7 +30,7 @@ static void Sound_Reset( void ) sound.size = 0; } -static wavdata_t *SoundPack( void ) +static MALLOC_LIKE( FS_FreeSound, 1 ) wavdata_t *SoundPack( void ) { wavdata_t *pack = Mem_Calloc( host.soundpool, sizeof( wavdata_t )); diff --git a/engine/ref_api.h b/engine/ref_api.h index 4e7d7656..fbb8521c 100644 --- a/engine/ref_api.h +++ b/engine/ref_api.h @@ -389,10 +389,13 @@ typedef struct ref_api_s int (*pfnGetStudioModelInterface)( int version, struct r_studio_interface_s **ppinterface, struct engine_studio_api_s *pstudio ); // memory - poolhandle_t (*_Mem_AllocPool)( const char *name, const char *filename, int fileline ); + poolhandle_t (*_Mem_AllocPool)( const char *name, const char *filename, int fileline ) + WARN_UNUSED_RESULT; void (*_Mem_FreePool)( poolhandle_t *poolptr, const char *filename, int fileline ); - void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 2 ); - void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 3 ); + void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) + ALLOC_CHECK( 2 ) WARN_UNUSED_RESULT; + void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) + ALLOC_CHECK( 3 ) WARN_UNUSED_RESULT; void (*_Mem_Free)( void *data, const char *filename, int fileline ); // library management diff --git a/filesystem/filesystem.h b/filesystem/filesystem.h index 5e11cba2..ecae3b47 100644 --- a/filesystem/filesystem.h +++ b/filesystem/filesystem.h @@ -217,8 +217,10 @@ typedef struct fs_interface_t // memory poolhandle_t (*_Mem_AllocPool)( const char *name, const char *filename, int fileline ); void (*_Mem_FreePool)( poolhandle_t *poolptr, const char *filename, int fileline ); - void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 2 ); - void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 3 ); + void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) + ALLOC_CHECK( 2 ) WARN_UNUSED_RESULT; + void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) + ALLOC_CHECK( 3 ) WARN_UNUSED_RESULT; void (*_Mem_Free)( void *data, const char *filename, int fileline ); // platform diff --git a/filesystem/fscallback.h b/filesystem/fscallback.h index 8a22962d..6fb70e5c 100644 --- a/filesystem/fscallback.h +++ b/filesystem/fscallback.h @@ -36,7 +36,9 @@ extern fs_globals_t *FI; #define FS_AllowDirectPaths (*g_fsapi.AllowDirectPaths) #define FS_AddGameDirectory (*g_fsapi.AddGameDirectory) #define FS_AddGameHierarchy (*g_fsapi.AddGameHierarchy) +#ifndef FSCALLBACK_OVERRIDE_MALLOC_LIKE #define FS_Search (*g_fsapi.Search) +#endif #define FS_SetCurrentDirectory (*g_fsapi.SetCurrentDirectory) #define FS_Path_f (*g_fsapi.Path_f) @@ -44,14 +46,16 @@ extern fs_globals_t *FI; #define FS_LoadGameInfo (*g_fsapi.LoadGameInfo) // file ops +#ifndef FSCALLBACK_OVERRIDE_MALLOC_LIKE #define FS_Open (*g_fsapi.Open) +#define FS_Close (*g_fsapi.Close) +#endif #define FS_Write (*g_fsapi.Write) #define FS_Read (*g_fsapi.Read) #define FS_Seek (*g_fsapi.Seek) #define FS_Tell (*g_fsapi.Tell) #define FS_Eof (*g_fsapi.Eof) #define FS_Flush (*g_fsapi.Flush) -#define FS_Close (*g_fsapi.Close) #define FS_Gets (*g_fsapi.Gets) #define FS_UnGetc (*g_fsapi.UnGetc) #define FS_Getc (*g_fsapi.Getc) @@ -62,8 +66,10 @@ extern fs_globals_t *FI; #define FS_FileCopy (*g_fsapi.FileCopy) // file buffer ops +#ifndef FSCALLBACK_OVERRIDE_MALLOC_LIKE #define FS_LoadFile (*g_fsapi.LoadFile) #define FS_LoadDirectFile (*g_fsapi.LoadDirectFile) +#endif #define FS_WriteFile (*g_fsapi.WriteFile) // file hashing