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-182.mta1.migadu.com (out-182.mta1.migadu.com [IPv6:2001:41d0:203:375::b6]) by mail-a.sr.ht (Postfix) with ESMTPS id 7DAD420279 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 22 Feb 2024 18:11:02 +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=1708625461; 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=fVnzXk53rg6ZMe6Rf6z8dzc5R9vEExzSwoRIkgVh+zk=; b=5CJKQI8m5RX0JjgNEX/AbIuQY9mGjdXj/OFEkJ9Q8V8owD3FpT7/TNSqKCtyYx2fLI0TTb hPk8CHQPqkVtP4MEXGsixEgAHK0H7HxYi6GBx2QLlLh2MZsQMLR+9gQntDSiXHXGSrxb+6 1FCF/U79QusMclL5loI9mDOJFdr7IMLAI8PlyFr+X5lsOxMgDpVDobZRNU75BIkAchFGRU Kjh8iQPXG4wXNDkhxPdVKkEs+ohf/r8EM+Xhy10A4oJUc2WHHoWhvC1mZPEvvxs5YcWz6/ t8VAIoG2hjU6NYfFhN7UK9RCHh2F9WspQIqBKYnOo2kICdKUjwWS83WMOTlvYw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang] cli: replace memory allocation malloc -> arena Date: Thu, 22 Feb 2024 20:09:57 +0100 Message-ID: <20240222191007.78350-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-TUID: bCtWZpY0FQwA 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 --- src/0c.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/0c.c b/src/0c.c index 978b770..f0ac974 100644 --- a/src/0c.c +++ b/src/0c.c @@ -14,15 +14,19 @@ * 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" +#define ARENA_CAPACITY (1024 * 1024) + typedef struct cli_args { int argc; @@ -45,7 +49,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 +77,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 +112,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 +127,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