From 83e25cfcfc6bc6d8ee7bbbb753feb411beda270c Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Wed, 26 Feb 2025 23:02:23 +0500 Subject: [PATCH] [main.c] Fix memory issues. --- include/commonlib.h | 23 +++++++++++++++++------ main.c | 10 +++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/commonlib.h b/include/commonlib.h index 7af1d7b..1604c4f 100644 --- a/include/commonlib.h +++ b/include/commonlib.h @@ -9,18 +9,13 @@ #include #include -// Memory allocation -#define C_MALLOC malloc -#define C_FREE free -#define C_REALLOC realloc -#define C_MEMCPY memcpy - // Remove Prefix #ifdef COMMONLIB_REMOVE_PREFIX #define ASSERT c_ASSERT #define ARRAY_LEN c_ARRAY_LEN #define da_append c_da_append +#define da_free c_da_free #define DYNAMIC_ARRAY_INITIAL_CAPACITY c_DYNAMIC_ARRAY_INITIAL_CAPACITY // #define c_DYNAMIC_ARRAY_INITIAL_CAPACITY @@ -80,6 +75,21 @@ #endif // COMMONLIB_REMOVE_PREFIX +// Memory allocation +#ifndef C_MALLOC +#define C_MALLOC malloc +#endif +#ifndef C_FREE +#define C_FREE free +#endif +#ifndef C_REALLOC +#define C_REALLOC realloc +#endif +#ifndef C_MEMCPY +#define C_MEMCPY memcpy +#endif + + // typedefs typedef unsigned int uint; typedef uint8_t uint8; @@ -164,6 +174,7 @@ typedef struct c_Arena c_Arena; } while (0) #define c_da_pop_front(da) (c_ASSERT(da.count > 0, "Array is empty"), da.count--, *da.items++) +#define c_da_free(da) C_FREE(da.items) // // OS diff --git a/main.c b/main.c index 2ad0d04..6d7269e 100644 --- a/main.c +++ b/main.c @@ -61,6 +61,10 @@ Lexer make_lexer(const char *filename) { return l; } +void free_lexer(Lexer *l) { + free(l->src.data); +} + bool eof(Lexer *l) { return l->cur >= l->src.count; } @@ -180,8 +184,12 @@ int main(int argc, char **argv) { Lexer l = make_lexer(filename); - lex(&l); + Tokens tokens = lex(&l); info("OK"); + + free_lexer(&l); + + da_free(tokens); return 0; } -- 2.39.5