From 343d7f7d269be96fbc82be6b421bdc657fa79979 Mon Sep 17 00:00:00 2001 From: sbird Date: Fri, 23 Sep 2022 04:02:30 +0200 Subject: [PATCH] [debug] do not use heap allocation for MgbaPrintf --- include/gba/isagbprint.h | 10 ++++++-- src/libisagbprn.c | 50 +++------------------------------------- 2 files changed, 11 insertions(+), 49 deletions(-) diff --git a/include/gba/isagbprint.h b/include/gba/isagbprint.h index 59518ecdf..91aa9f214 100644 --- a/include/gba/isagbprint.h +++ b/include/gba/isagbprint.h @@ -3,6 +3,12 @@ #include "gba/types.h" +#define MGBA_LOG_FATAL (0) +#define MGBA_LOG_ERROR (1) +#define MGBA_LOG_WARN (2) +#define MGBA_LOG_INFO (3) +#define MGBA_LOG_DEBUG (4) + #ifdef NDEBUG #define DebugPrintf(pBuf, ...) #define MgbaOpen() @@ -13,7 +19,7 @@ bool32 MgbaOpen(void); void MgbaClose(void); -void MgbaPrintf(const char *pBuf, ...); +void MgbaPrintf(s32 level, const char *pBuf, ...); void MgbaAssert(const char *pFile, s32 nLine, const char *pExpression, bool32 nStopProgram); void NoCashGBAPrintf(const char *pBuf, ...); void NoCashGBAAssert(const char *pFile, s32 nLine, const char *pExpression, bool32 nStopProgram); @@ -23,7 +29,7 @@ void AGBPrintInit(void); #if (LOG_HANDLER == LOG_HANDLER_MGBA_PRINT) -#define DebugPrintf(pBuf, ...) MgbaPrintf(pBuf, __VA_ARGS__) +#define DebugPrintf(pBuf, ...) MgbaPrintf(MGBA_LOG_INFO, pBuf, __VA_ARGS__) #define DebugAssert(pFile, nLine, pExpression, nStopProgram) MgbaAssert(pFile, nLine, pExpression, nStopProgram) #elif (LOG_HANDLER == LOG_HANDLER_NOCASH_PRINT) diff --git a/src/libisagbprn.c b/src/libisagbprn.c index 0dbaec157..fab9b5990 100644 --- a/src/libisagbprn.c +++ b/src/libisagbprn.c @@ -211,14 +211,6 @@ void NoCashGBAAssert(const char *pFile, s32 nLine, const char *pExpression, bool // mgba print functions #if (LOG_HANDLER == LOG_HANDLER_MGBA_PRINT) -#define MGBA_PRINTF_BUFFER_SIZE (4096) - -#define MGBA_LOG_FATAL (0) -#define MGBA_LOG_ERROR (1) -#define MGBA_LOG_WARN (2) -#define MGBA_LOG_INFO (3) -#define MGBA_LOG_DEBUG (4) - #define MGBA_REG_DEBUG_MAX (256) bool32 MgbaOpen(void) @@ -232,7 +224,7 @@ void MgbaClose(void) *REG_DEBUG_ENABLE = 0; } -static void MgbaPrintfBounded(s32 level, const char* ptr, ...) +void MgbaPrintf(s32 level, const char* ptr, ...) { va_list args; @@ -249,52 +241,16 @@ static void MgbaPrintfBounded(s32 level, const char* ptr, ...) *REG_DEBUG_FLAGS = level | 0x100; } -void MgbaPrintf(const char* ptr, ...) -{ - va_list args; - u32 offset = 0; - u32 n = 0; - u32 i; - char *buffer = Alloc(MGBA_PRINTF_BUFFER_SIZE); - AGB_ASSERT(buffer != NULL); - - va_start(args, ptr); - #if (PRETTY_PRINT_HANDLER == PRETTY_PRINT_MINI_PRINTF) - n = mini_vsnprintf(buffer, MGBA_PRINTF_BUFFER_SIZE, ptr, args); - #elif (PRETTY_PRINT_HANDLER == PRETTY_PRINT_LIBC) - n = vsnprintf(buffer, MGBA_PRINTF_BUFFER_SIZE, ptr, args); - #else - #error "unspecified pretty printing handler." - #endif - va_end(args); - - AGB_ASSERT(n < MGBA_PRINTF_BUFFER_SIZE); - - do - { - for (i = 0; i < MGBA_REG_DEBUG_MAX; ++i) - { - REG_DEBUG_STRING[i] = buffer[offset + i]; - if (buffer[offset + i] == 0) - break; - } - offset += i; - *REG_DEBUG_FLAGS = MGBA_LOG_INFO | 0x100; - } while ((i == MGBA_REG_DEBUG_MAX) && (buffer[offset] != '\0')); - - Free(buffer); -} - void MgbaAssert(const char *pFile, s32 nLine, const char *pExpression, bool32 nStopProgram) { if (nStopProgram) { - MgbaPrintfBounded(MGBA_LOG_ERROR, "ASSERTION FAILED FILE=[%s] LINE=[%d] EXP=[%s]", pFile, nLine, pExpression); + MgbaPrintf(MGBA_LOG_ERROR, "ASSERTION FAILED FILE=[%s] LINE=[%d] EXP=[%s]", pFile, nLine, pExpression); asm(".hword 0xEFFF"); } else { - MgbaPrintfBounded(MGBA_LOG_WARN, "WARING FILE=[%s] LINE=[%d] EXP=[%s]", pFile, nLine, pExpression); + MgbaPrintf(MGBA_LOG_WARN, "WARING FILE=[%s] LINE=[%d] EXP=[%s]", pFile, nLine, pExpression); } } #endif