From 7e3762a4423203562575cebb52cde7fdca937898 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 3 Aug 2026 21:11:02 +0200 Subject: [PATCH] Fix leaks --- src/file/file.odin | 12 +++---- src/file/list_dir.odin | 12 +++++-- src/file/read_contents.odin | 62 ------------------------------------ src/file/write_metadata.odin | 1 + src/main.odin | 1 + tests/blabla.odin | 7 ++++ 6 files changed, 24 insertions(+), 71 deletions(-) delete mode 100644 src/file/read_contents.odin create mode 100644 tests/blabla.odin diff --git a/src/file/file.odin b/src/file/file.odin index 6858b52..076edcd 100644 --- a/src/file/file.odin +++ b/src/file/file.odin @@ -3,14 +3,12 @@ package file import "core:fmt" import "core:os" -// TODO: -// - list files in directory -// - get relative path -// - read file contents -// - write file contents +// ----------------------------------------- working_dir: string +// ----------------------------------------- + store_working_dir :: proc(allocator := context.allocator) { wd, err := os.get_working_directory(allocator) if err != nil { @@ -20,9 +18,9 @@ store_working_dir :: proc(allocator := context.allocator) { working_dir = wd } -remove_working_dir :: proc() { +remove_working_dir :: proc(allocator := context.allocator) { if working_dir != "" { - delete(working_dir) + delete(working_dir, allocator) working_dir = "" } } diff --git a/src/file/list_dir.odin b/src/file/list_dir.odin index e8e0195..a6ae847 100644 --- a/src/file/list_dir.odin +++ b/src/file/list_dir.odin @@ -31,6 +31,14 @@ list_dir_recursive_by_path :: proc( return list_dir_recursive_by_path_impl(path, allocator) } +// Frees every owning relpath string then the dynamic array itself. +delete_entries :: proc(entries: ^[dynamic]FileEntry) { + for entry in entries { + delete(entry.relpath, entries.allocator) + } + delete(entries^) +} + @(private) list_dir_recursive_by_path_impl :: proc( path: string, @@ -51,7 +59,7 @@ list_dir_recursive_by_path_impl :: proc( list_dir_queue(&queue, &result, working_dir, allocator) } - return result + return result // owning [dynamic] } @(private) @@ -78,7 +86,7 @@ list_dir_queue :: proc( fmt.eprintln("error: read directory failed:", read_err) os.exit(1) } - defer delete(entries, allocator) + defer os.file_info_slice_delete(entries, allocator) // Reserve space for new entries, minor waste as directories are also counted. reserve(result, len(result) + len(entries)) diff --git a/src/file/read_contents.odin b/src/file/read_contents.odin deleted file mode 100644 index 9a0651d..0000000 --- a/src/file/read_contents.odin +++ /dev/null @@ -1,62 +0,0 @@ -package file - -import "core:fmt" -import "core:os" -import "core:strings" - -// ----------------------------------------- - -Asset :: struct #all_or_none { - relpath: string, - size: i64, - data: []u8, -} - -// ----------------------------------------- - -read_by_path :: proc(path: string) -> []u8 { - f, open_err := os.open(path, {.Read}) - if open_err != nil { - fmt.eprintln("error: open failed:", open_err) - os.exit(1) - } - defer os.close(f) - - if !os.is_file(path) { - fmt.eprintln("error: path is not a file:", path) - os.exit(1) - } - - path := path // shadow parameter - path = strings.clone(path) - - size, size_err := os.file_size(f) - if size_err != nil { - fmt.eprintln("error: get file size failed:", size_err) - os.exit(1) - } - - data := make([]u8, size) - - n, read_err := os.read_full(f, data) - if read_err != nil { - delete(data) - fmt.eprintln( - "error: file read failed:", - read_err, - "got", - n, - "of", - size, - "bytes", - ) - os.exit(1) - } - - return data -} - -get_asset_by_path :: proc(path: string) -> Asset { - data := read_by_path(path) - return Asset{relpath = path, size = cast(i64)len(data), data = data} -} diff --git a/src/file/write_metadata.odin b/src/file/write_metadata.odin index bb9ac34..849b10b 100644 --- a/src/file/write_metadata.odin +++ b/src/file/write_metadata.odin @@ -74,6 +74,7 @@ write_asset_index :: proc( // ---------------------------------------- index_bytes := make([]u8, index_size, allocator) + defer delete(index_bytes) for f, i in entries { diff --git a/src/main.odin b/src/main.odin index 09afdf9..5603f48 100644 --- a/src/main.odin +++ b/src/main.odin @@ -18,6 +18,7 @@ main :: proc() { // fmt.println("size:", opts.size) entries := file.list_dir_recursive(opts.input) + defer file.delete_entries(&entries) file.compute_metadata_offsets(cast(u64)len(entries)) file.write_assets(opts.output, entries[:], opts.size, opts.compression) diff --git a/tests/blabla.odin b/tests/blabla.odin new file mode 100644 index 0000000..7ec2495 --- /dev/null +++ b/tests/blabla.odin @@ -0,0 +1,7 @@ +package tests + +import "core:testing" + +@(test) +foo :: proc(_: ^testing.T) { +}