* [PATCH olang] cli: replace memory allocation malloc -> arena
@ 2024-02-22 19:09 Johnny Richard
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
0 siblings, 2 replies; 7+ messages in thread
From: Johnny Richard @ 2024-02-22 19:09 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [olang/patches/.build.yml] build success
2024-02-22 19:09 [PATCH olang] cli: replace memory allocation malloc -> arena Johnny Richard
@ 2024-02-22 18:11 ` builds.sr.ht
2024-02-27 15:44 ` [PATCH olang] cli: replace memory allocation malloc -> arena Carlos Maniero
1 sibling, 0 replies; 7+ messages in thread
From: builds.sr.ht @ 2024-02-22 18:11 UTC (permalink / raw)
To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel
olang/patches/.build.yml: SUCCESS in 37s
[cli: replace memory allocation malloc -> arena][0] from [Johnny Richard][1]
[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/49749
[1]: mailto:johnny@johnnyrichard.com
✓ #1155615 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1155615
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH olang] cli: replace memory allocation malloc -> arena
2024-02-22 19:09 [PATCH olang] cli: replace memory allocation malloc -> arena Johnny Richard
2024-02-22 18:11 ` [olang/patches/.build.yml] build success builds.sr.ht
@ 2024-02-27 15:44 ` Carlos Maniero
2024-02-27 17:04 ` Johnny Richard
2024-02-27 17:12 ` Johnny Richard
1 sibling, 2 replies; 7+ messages in thread
From: Carlos Maniero @ 2024-02-27 15:44 UTC (permalink / raw)
To: Johnny Richard, ~johnnyrichard/olang-devel
> diff --git a/src/0c.c b/src/0c.c
> index 978b770..f0ac974 100644
> --- a/src/0c.c
> +++ b/src/0c.c
0c.c no longer exists
> +#define ARENA_CAPACITY (1024 * 1024)
Can you add a TODO comment to us to remember to solve this issue in the
feature? We can both make a algorithm to predict how much memory we
gonna need or make arena expandable.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH olang] cli: replace memory allocation malloc -> arena
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-27 17:12 ` Johnny Richard
1 sibling, 1 reply; 7+ messages in thread
From: Johnny Richard @ 2024-02-27 17:04 UTC (permalink / raw)
To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel
On Tue, Feb 27, 2024 at 12:44:47PM -0300, Carlos Maniero wrote:
> > diff --git a/src/0c.c b/src/0c.c
> > index 978b770..f0ac974 100644
> > --- a/src/0c.c
> > +++ b/src/0c.c
> 0c.c no longer exists
>
> > +#define ARENA_CAPACITY (1024 * 1024)
> Can you add a TODO comment to us to remember to solve this issue in the
> feature? We can both make a algorithm to predict how much memory we
> gonna need or make arena expandable.
Do you think the following fixup is good enough?
------->8-------
Subject: [PATCH olang] fixup! cli: replace memory allocation malloc -> arena
diff --git a/src/main.c b/src/main.c
index f0ac974..2b2f12a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,6 +25,7 @@
#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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH olang] cli: replace memory allocation malloc -> arena
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 17:12 ` Johnny Richard
1 sibling, 0 replies; 7+ messages in thread
From: Johnny Richard @ 2024-02-27 17:12 UTC (permalink / raw)
To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel
On Tue, Feb 27, 2024 at 12:44:47PM -0300, Carlos Maniero wrote:
> > diff --git a/src/0c.c b/src/0c.c
> > index 978b770..f0ac974 100644
> > --- a/src/0c.c
> > +++ b/src/0c.c
> 0c.c no longer exists
missed the reply for this comment...
Sure, since you've applied the rename patch first, I will fixup the file
name as well. The next patch I will do that.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-02-28 11:40 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-22 19:09 [PATCH olang] cli: replace memory allocation malloc -> arena Johnny Richard
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
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