From eaa6e52fbe831f4112a0bccf89194c9b403df13c Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Tue, 30 Apr 2024 22:00:54 +0100 Subject: [PATCH] Switch from using Allocator to Arena --- src/image/image.c | 30 +++++------------------------- src/image/image.h | 6 ++---- src/main.c | 19 +++++++++---------- src/tiff/tiffread.c | 26 ++++++++------------------ src/tiff/tiffread.h | 3 ++- 5 files changed, 26 insertions(+), 58 deletions(-) diff --git a/src/image/image.c b/src/image/image.c index f6de50b..c1c049f 100644 --- a/src/image/image.c +++ b/src/image/image.c @@ -1,22 +1,17 @@ #include "image.h" -#include "mem_allocator.h" -#include "mem_libc.h" +#include "mem_arena.h" #include #include -Image *create_image(u64 width, u64 height, Pixel *data, - const Allocator *allocator) { - Allocator alloc; - if (!allocator) { - alloc = wapp_mem_libc_allocator(); - } else { - alloc = *allocator; +Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) { + if (!arena) { + return NULL; } u64 buf_length = width * height; u64 total_size = sizeof(Image) + sizeof(Pixel) * buf_length; - Image *img = wapp_mem_allocator_alloc(&alloc, total_size); + Image *img = wapp_mem_arena_alloc(arena, total_size); if (!img) { return NULL; } @@ -28,18 +23,3 @@ Image *create_image(u64 width, u64 height, Pixel *data, return img; } - -void destroy_image(Image **img, const Allocator *allocator) { - if (!img || !(*img)) { - return; - } - - Allocator alloc; - if (!allocator) { - alloc = wapp_mem_libc_allocator(); - } else { - alloc = *allocator; - } - - wapp_mem_allocator_free(&alloc, (void **)img); -} diff --git a/src/image/image.h b/src/image/image.h index 433f47a..e071838 100644 --- a/src/image/image.h +++ b/src/image/image.h @@ -1,7 +1,7 @@ #ifndef IMAGE_H #define IMAGE_H -#include "mem_allocator.h" +#include "mem_arena.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus @@ -24,9 +24,7 @@ struct image { Pixel data[]; }; -Image *create_image(u64 width, u64 height, Pixel *data, - const Allocator *allocator); -void destroy_image(Image **img, const Allocator *allocator); +Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena); #ifdef __cplusplus } diff --git a/src/main.c b/src/main.c index df79ff8..940add2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,6 @@ #include "aliases.h" #include "image.h" -#include "mem_allocator.h" -#include "mem_ctx.h" +#include "mem_arena.h" #include "tiffread.h" #include #include @@ -35,17 +34,17 @@ Point point_from_index(u32 index, u32 w); int main(int argc, char *argv[]) { int exit_code = EXIT_SUCCESS; - wapp_mem_ctx_init(10 * 1024 * 1024, 2 * 1024 * 1024); - Allocator ctx_main_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_MAIN); - Allocator ctx_temp_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_TEMP); - (void)(ctx_temp_allocator); + Arena *arena = NULL; + if (!wapp_mem_arena_init(&arena, 10 * 1024 * 1024)) { + return EXIT_FAILURE; + } const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif"; - Image *img = read_baseline_tiff(file_to_open, &ctx_main_allocator); + Image *img = read_baseline_tiff(file_to_open, arena); if (!img) { exit_code = EXIT_FAILURE; - goto MAIN_FREE_CONTEXT; + goto MAIN_DESTROY_ARENA; } FILE *out = fopen("test.ppm", "wb"); @@ -158,8 +157,8 @@ int main(int argc, char *argv[]) { // MAIN_QUIT_SDL: // SDL_Quit(); -MAIN_FREE_CONTEXT: - wapp_mem_ctx_free(); +MAIN_DESTROY_ARENA: + wapp_mem_arena_destroy(&arena); return exit_code; } diff --git a/src/tiff/tiffread.c b/src/tiff/tiffread.c index 760f702..ec5ebc9 100644 --- a/src/tiff/tiffread.c +++ b/src/tiff/tiffread.c @@ -2,10 +2,7 @@ #include "aliases.h" #include "endianness.h" #include "image.h" -#include "mem_allocator.h" #include "mem_arena.h" -#include "mem_ctx.h" -#include "mem_libc.h" #include #include #include @@ -39,12 +36,11 @@ // clang-format on TiffHdr read_tiff_header(FILE *fp); -TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, - const Allocator *allocator); +TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena); TiffImage read_fields(FILE *fp, const TiffHdr *header, const TiffIFD *ifd); void read_from_file_with_offset(FILE *fp, void *dst, u64 count, u64 offset); -Image *read_baseline_tiff(const char *file, const Allocator *allocator) { +Image *read_baseline_tiff(const char *file, Arena *arena) { Image *img_out = NULL; if (!file) { @@ -79,14 +75,7 @@ Image *read_baseline_tiff(const char *file, const Allocator *allocator) { goto READ_BASELINE_FILE_CLEANUP; } - Allocator alloc; - if (!allocator) { - alloc = wapp_mem_libc_allocator(); - } else { - alloc = *allocator; - } - - TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, &alloc); + TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, arena); read_fields(fp, &header, &ifd); @@ -133,8 +122,7 @@ TiffHdr read_tiff_header(FILE *fp) { return header; } -TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, - const Allocator *allocator) { +TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena) { if (!fp || !header) { return NULL_TIFF_IFD; } @@ -161,8 +149,10 @@ TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, } u64 field_byte_count = sizeof(TiffField) * ifd.count; - ifd.fields = - (TiffField *)wapp_mem_allocator_alloc(allocator, field_byte_count); + ifd.fields = (TiffField *)wapp_mem_arena_alloc(arena, field_byte_count); + if (!(ifd.fields)) { + return NULL_TIFF_IFD; + } fread(ifd.fields, field_byte_count, 1, fp); fread(&(ifd.next_ifd), sizeof(ifd.next_ifd), 1, fp); diff --git a/src/tiff/tiffread.h b/src/tiff/tiffread.h index d86c831..0123af0 100644 --- a/src/tiff/tiffread.h +++ b/src/tiff/tiffread.h @@ -1,6 +1,7 @@ #ifndef TIFFREAD_H #define TIFFREAD_H +#include "mem_arena.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus @@ -124,7 +125,7 @@ struct tiff_image { bool extra_samples_offset; }; -Image *read_baseline_tiff(const char *file, const Allocator *allocator); +Image *read_baseline_tiff(const char *file, Arena *arena); #ifdef __cplusplus }