From 06f1eaa1538279a82b6a24ed7ee17c1dea1e1293 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Fri, 30 Apr 2021 09:35:43 +0200 Subject: [PATCH] Use regular allocator from png+gzip's `load_from_file`. I would've used `os.stream_from_handle`, but: - Parts of it seem to be implemented for Windows only at the moment. - PNG's `peek_data` using that stream didn't manage to rewind and thus tried to parse the data after the header as the header. Two things must happen: - The `os.stream_from_handle` implementation needs to be fixed. - PNG and GZIP's parsers need to be able to handle streams that can't rewind or seek (backward). Those fixes are on my TODO list but are exceed the scope of this patch. --- core/compress/gzip/gzip.odin | 4 +++- core/image/png/png.odin | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/compress/gzip/gzip.odin b/core/compress/gzip/gzip.odin index 3278d7c1d..457b32bd3 100644 --- a/core/compress/gzip/gzip.odin +++ b/core/compress/gzip/gzip.odin @@ -108,7 +108,9 @@ load_from_slice :: proc(slice: ^[]u8, buf: ^bytes.Buffer, allocator := context.a } load_from_file :: proc(filename: string, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) { - data, ok := os.read_entire_file(filename, context.temp_allocator); + data, ok := os.read_entire_file(filename, allocator); + defer delete(data); + if ok { err = load_from_slice(&data, buf, allocator); return; diff --git a/core/image/png/png.odin b/core/image/png/png.odin index a25fed1ec..5ea9979a8 100644 --- a/core/image/png/png.odin +++ b/core/image/png/png.odin @@ -366,11 +366,9 @@ load_from_slice :: proc(slice: ^[]u8, options: Options = {}, allocator := contex } load_from_file :: proc(filename: string, options: Options = {}, allocator := context.allocator) -> (img: ^Image, err: Error) { - load_file :: proc(filename: string) -> (res: []u8, ok: bool) { - return os.read_entire_file(filename, context.temp_allocator); - } + data, ok := os.read_entire_file(filename, allocator); + defer delete(data); - data, ok := load_file(filename); if ok { img, err = load_from_slice(&data, options, allocator); return;