diff --git a/TODOS.md b/TODOS.md index 072f62f..b6f1eb0 100644 --- a/TODOS.md +++ b/TODOS.md @@ -18,11 +18,9 @@ 9. Update `read_wire_string` to use a slice. -10. Pass allocator to findr? +10. Consider getting rid of color global. -11. Consider getting rid of color global. - -12. `write_flags_table` should never return false. +11. `write_flags_table` should never return false. ## Double-check AI output diff --git a/config.odin b/config.odin index 556c174..eabd26b 100644 --- a/config.odin +++ b/config.odin @@ -201,8 +201,7 @@ find_git_roots :: proc( ok: bool, ) { paths := search_paths(cfg, allocator) - // TODO: Pass allocator to findr - findr.find_repos(paths[:], &roots, os.get_processor_core_count()) + findr.find_repos(paths[:], &roots, os.get_processor_core_count(), allocator) ok = true return } @@ -214,6 +213,7 @@ search_paths :: proc(cfg: Config, allocator := context.allocator) -> [dynamic]st } paths := new_clone(cfg.scan_config.include, allocator) + defer free(paths, allocator) for &include in paths { expanded, _ := strings.replace(include, "~", home, 1, allocator) @@ -227,7 +227,8 @@ search_paths :: proc(cfg: Config, allocator := context.allocator) -> [dynamic]st } } } - return paths^ + result := paths^ + return result } envr_dir :: proc(config_path: string) -> string { diff --git a/config_test.odin b/config_test.odin index c48cf9f..d5777aa 100644 --- a/config_test.odin +++ b/config_test.odin @@ -2,6 +2,7 @@ package main import "core:fmt" +import "core:mem" import "core:os" import "core:path/filepath" import "core:strings" @@ -194,3 +195,14 @@ test_search_paths_expands_tilde :: proc(t: ^testing.T) { } } +@(test) +test_search_paths_no_leak :: proc(t: ^testing.T) { + cfg := Config { + scan_config = ScanConfig{include = make([dynamic]string, 0, 1)}, + } + defer delete(cfg.scan_config.include) + append(&cfg.scan_config.include, "/tmp") + + _ = search_paths(cfg, context.allocator) +} + diff --git a/db.odin b/db.odin index 4f71aec..e23c6bd 100644 --- a/db.odin +++ b/db.odin @@ -491,7 +491,7 @@ db_persist :: proc(db: ^Db, f: ^EnvFile, old_path: string) -> bool { } try_move_dir :: proc(db: ^Db, f: ^EnvFile, allocator: mem.Allocator) -> (bool, SyncError) { - roots, ok := find_git_roots(db.cfg) + roots, ok := find_git_roots(db.cfg, context.allocator) if !ok { return false, .GitRootFailed } diff --git a/findr/repos.odin b/findr/repos.odin index 197354e..edef045 100644 --- a/findr/repos.odin +++ b/findr/repos.odin @@ -16,7 +16,14 @@ RepoPool :: struct { threads: []^thread.Thread, } -find_repos :: proc(roots: []string, results: ^[dynamic]string, thread_count: int) { +find_repos :: proc( + roots: []string, + results: ^[dynamic]string, + thread_count: int, + allocator := context.allocator, +) { + // TODO: This may be a code smell + context.allocator = allocator if len(roots) == 0 do return pool := new(RepoPool) @@ -126,3 +133,4 @@ process_repo_dir :: proc(pool: ^RepoPool, dir_path: string) { } } } + diff --git a/findr/walker.odin b/findr/walker.odin index e0c476a..928f4c3 100644 --- a/findr/walker.odin +++ b/findr/walker.odin @@ -78,7 +78,15 @@ collect_worker :: proc(t: ^thread.Thread) { } } -walk :: proc(roots: []string, results: ^[dynamic]string, opts: WalkOptions, thread_count: int) { +walk :: proc( + roots: []string, + results: ^[dynamic]string, + opts: WalkOptions, + thread_count: int, + allocator := context.allocator, +) { + // TODO: This may be a code smell + context.allocator = allocator if len(roots) == 0 do return ch, _ := chan.create(chan.Chan([]u8), max(2 * thread_count, 2), context.allocator)