Switch from using Allocator to Arena

This commit is contained in:
Abdelrahman Said 2024-04-30 22:00:54 +01:00
parent 3c6e5defb9
commit eaa6e52fbe
5 changed files with 26 additions and 58 deletions

View File

@ -1,22 +1,17 @@
#include "image.h"
#include "mem_allocator.h"
#include "mem_libc.h"
#include "mem_arena.h"
#include <stddef.h>
#include <string.h>
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);
}

View File

@ -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
}

View File

@ -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 <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
@ -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;
}

View File

@ -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 <math.h>
#include <netinet/in.h>
#include <stdbool.h>
@ -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);

View File

@ -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
}