public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang] cli: replace memory allocation malloc -> arena
Date: Thu, 22 Feb 2024 20:09:57 +0100	[thread overview]
Message-ID: <20240222191007.78350-1-johnny@johnnyrichard.com> (raw)

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 <johnny@johnnyrichard.com>
---
 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 <https://www.gnu.org/licenses/>.
  */
+#include <assert.h>
 #include <errno.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#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


             reply	other threads:[~2024-02-22 18:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-22 19:09 Johnny Richard [this message]
2024-02-22 18:11 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-02-27 15:44 ` [PATCH olang] cli: replace memory allocation malloc -> arena Carlos Maniero
2024-02-27 17:04   ` Johnny Richard
2024-02-27 23:22     ` Carlos Maniero
2024-02-28 12:40       ` Johnny Richard
2024-02-27 17:12   ` Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240222191007.78350-1-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox