Switch from using Allocator to Arena
This commit is contained in:
parent
3c6e5defb9
commit
eaa6e52fbe
@ -1,22 +1,17 @@
|
|||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_arena.h"
|
||||||
#include "mem_libc.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
Image *create_image(u64 width, u64 height, Pixel *data,
|
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) {
|
||||||
const Allocator *allocator) {
|
if (!arena) {
|
||||||
Allocator alloc;
|
return NULL;
|
||||||
if (!allocator) {
|
|
||||||
alloc = wapp_mem_libc_allocator();
|
|
||||||
} else {
|
|
||||||
alloc = *allocator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 buf_length = width * height;
|
u64 buf_length = width * height;
|
||||||
u64 total_size = sizeof(Image) + sizeof(Pixel) * buf_length;
|
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) {
|
if (!img) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -28,18 +23,3 @@ Image *create_image(u64 width, u64 height, Pixel *data,
|
|||||||
|
|
||||||
return img;
|
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);
|
|
||||||
}
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef IMAGE_H
|
#ifndef IMAGE_H
|
||||||
#define IMAGE_H
|
#define IMAGE_H
|
||||||
|
|
||||||
#include "mem_allocator.h"
|
#include "mem_arena.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
@ -24,9 +24,7 @@ struct image {
|
|||||||
Pixel data[];
|
Pixel data[];
|
||||||
};
|
};
|
||||||
|
|
||||||
Image *create_image(u64 width, u64 height, Pixel *data,
|
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena);
|
||||||
const Allocator *allocator);
|
|
||||||
void destroy_image(Image **img, const Allocator *allocator);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
19
src/main.c
19
src/main.c
@ -1,7 +1,6 @@
|
|||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_arena.h"
|
||||||
#include "mem_ctx.h"
|
|
||||||
#include "tiffread.h"
|
#include "tiffread.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_events.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 main(int argc, char *argv[]) {
|
||||||
int exit_code = EXIT_SUCCESS;
|
int exit_code = EXIT_SUCCESS;
|
||||||
|
|
||||||
wapp_mem_ctx_init(10 * 1024 * 1024, 2 * 1024 * 1024);
|
Arena *arena = NULL;
|
||||||
Allocator ctx_main_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_MAIN);
|
if (!wapp_mem_arena_init(&arena, 10 * 1024 * 1024)) {
|
||||||
Allocator ctx_temp_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_TEMP);
|
return EXIT_FAILURE;
|
||||||
(void)(ctx_temp_allocator);
|
}
|
||||||
|
|
||||||
const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif";
|
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) {
|
if (!img) {
|
||||||
exit_code = EXIT_FAILURE;
|
exit_code = EXIT_FAILURE;
|
||||||
goto MAIN_FREE_CONTEXT;
|
goto MAIN_DESTROY_ARENA;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *out = fopen("test.ppm", "wb");
|
FILE *out = fopen("test.ppm", "wb");
|
||||||
@ -158,8 +157,8 @@ int main(int argc, char *argv[]) {
|
|||||||
// MAIN_QUIT_SDL:
|
// MAIN_QUIT_SDL:
|
||||||
// SDL_Quit();
|
// SDL_Quit();
|
||||||
|
|
||||||
MAIN_FREE_CONTEXT:
|
MAIN_DESTROY_ARENA:
|
||||||
wapp_mem_ctx_free();
|
wapp_mem_arena_destroy(&arena);
|
||||||
|
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,7 @@
|
|||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "endianness.h"
|
#include "endianness.h"
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "mem_allocator.h"
|
|
||||||
#include "mem_arena.h"
|
#include "mem_arena.h"
|
||||||
#include "mem_ctx.h"
|
|
||||||
#include "mem_libc.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -39,12 +36,11 @@
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
TiffHdr read_tiff_header(FILE *fp);
|
TiffHdr read_tiff_header(FILE *fp);
|
||||||
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset,
|
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena);
|
||||||
const Allocator *allocator);
|
|
||||||
TiffImage read_fields(FILE *fp, const TiffHdr *header, const TiffIFD *ifd);
|
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);
|
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;
|
Image *img_out = NULL;
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
@ -79,14 +75,7 @@ Image *read_baseline_tiff(const char *file, const Allocator *allocator) {
|
|||||||
goto READ_BASELINE_FILE_CLEANUP;
|
goto READ_BASELINE_FILE_CLEANUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator alloc;
|
TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, arena);
|
||||||
if (!allocator) {
|
|
||||||
alloc = wapp_mem_libc_allocator();
|
|
||||||
} else {
|
|
||||||
alloc = *allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, &alloc);
|
|
||||||
|
|
||||||
read_fields(fp, &header, &ifd);
|
read_fields(fp, &header, &ifd);
|
||||||
|
|
||||||
@ -133,8 +122,7 @@ TiffHdr read_tiff_header(FILE *fp) {
|
|||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset,
|
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena) {
|
||||||
const Allocator *allocator) {
|
|
||||||
if (!fp || !header) {
|
if (!fp || !header) {
|
||||||
return NULL_TIFF_IFD;
|
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;
|
u64 field_byte_count = sizeof(TiffField) * ifd.count;
|
||||||
ifd.fields =
|
ifd.fields = (TiffField *)wapp_mem_arena_alloc(arena, field_byte_count);
|
||||||
(TiffField *)wapp_mem_allocator_alloc(allocator, field_byte_count);
|
if (!(ifd.fields)) {
|
||||||
|
return NULL_TIFF_IFD;
|
||||||
|
}
|
||||||
|
|
||||||
fread(ifd.fields, field_byte_count, 1, fp);
|
fread(ifd.fields, field_byte_count, 1, fp);
|
||||||
fread(&(ifd.next_ifd), sizeof(ifd.next_ifd), 1, fp);
|
fread(&(ifd.next_ifd), sizeof(ifd.next_ifd), 1, fp);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef TIFFREAD_H
|
#ifndef TIFFREAD_H
|
||||||
#define TIFFREAD_H
|
#define TIFFREAD_H
|
||||||
|
|
||||||
|
#include "mem_arena.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
@ -124,7 +125,7 @@ struct tiff_image {
|
|||||||
bool extra_samples_offset;
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user