From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=johnnyrichard.com header.i=@johnnyrichard.com Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [IPv6:2001:41d0:1004:224b::b9]) by mail-a.sr.ht (Postfix) with ESMTPS id 1D2A320345 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 28 Feb 2024 11:38:16 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1709120295; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=I/zMJ5YpJdKnunOdul30Q6aoTgSJu7bY/cjmMIhd+DU=; b=NOfydHHU7/ClBi0Nn3bhW2oJTart8/azGFQROtY+NO/AZXXzDs0MwBiatDduXX1fvtZxPU AVxRZNLV8mGpd/dCyZaiMTmKFOQIDz+RniLV95Jlv+o9vaZ/0od8ZQxmbAbftY8khE+Jt/ CTZVmncLqFrR7ALBZYTN9T+a8m3a5qtgnV8Z/eR8voX9baZW2m9Th7jOTyb4ilORdvJwYZ f28ed/4j/l5wUXONWRt/2AWM88HCzigy9VLhWJ+tlXIkKqV9ytz/6njMFIzb7bxukcG3Fa 7dunHkTgUl09kJewp81hyA5HxTYtc2AZN5bp+Q3RcirjYODJGqU4htQhQt4VEg== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2] cli: replace memory allocation malloc -> arena Date: Wed, 28 Feb 2024 13:37:22 +0100 Message-ID: <20240228123810.44195-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-TUID: s29fLcI8UHSa This code assumes that 1MB will be enough space to accommodate the entire source code and the AST. If the source code is greater than half of the arena capacity, the program will crash. Signed-off-by: Johnny Richard --- v2: add TODO comment to ajust the arena capacity in the furure src/main.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 978b770..2b2f12a 100644 --- a/src/main.c +++ b/src/main.c @@ -14,15 +14,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include #include #include #include #include #include +#include "arena.h" #include "lexer.h" #include "string_view.h" +// TODO: find a better solution to define the arena capacity +#define ARENA_CAPACITY (1024 * 1024) + typedef struct cli_args { int argc; @@ -45,7 +50,7 @@ static void print_token(char *file_path, token_t *token); string_view_t -read_entire_file(char *file_path); +read_entire_file(char *file_path, arena_t *arena); int main(int argc, char **argv) @@ -73,7 +78,8 @@ main(int argc, char **argv) return EXIT_FAILURE; } - string_view_t file_content = read_entire_file(opts.file_path); + arena_t arena = arena_new(ARENA_CAPACITY); + string_view_t file_content = read_entire_file(opts.file_path, &arena); lexer_t lexer = { 0 }; lexer_init(&lexer, file_content); @@ -107,7 +113,7 @@ print_usage(FILE *stream, char *prog) } string_view_t -read_entire_file(char *file_path) +read_entire_file(char *file_path, arena_t *arena) { FILE *stream = fopen(file_path, "rb"); @@ -122,7 +128,9 @@ read_entire_file(char *file_path) file_content.size = ftell(stream); fseek(stream, 0, SEEK_SET); - file_content.chars = (char *)malloc(file_content.size); + assert(file_content.size * 2 < ARENA_CAPACITY); + + file_content.chars = (char *)arena_alloc(arena, (size_t)file_content.size); if (file_content.chars == NULL) { fprintf(stderr, "Could not read file %s: %s\n", file_path, strerror(errno)); -- 2.43.2