diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-03-06 12:29:17 +0100 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-03-06 12:29:17 +0100 |
| commit | ce057ff755d2fe852cb83e4025686d9efd3053d7 (patch) | |
| tree | fdec119d9833660a6c06c26de049fc833ff41ed3 /core/container/bit_array/bit_array.odin | |
| parent | ad719e7c3a258c84079581c7a9be71e0434c98cd (diff) | |
[bit_array] Really fix the leak.
Diffstat (limited to 'core/container/bit_array/bit_array.odin')
| -rw-r--r-- | core/container/bit_array/bit_array.odin | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/core/container/bit_array/bit_array.odin b/core/container/bit_array/bit_array.odin index 0016ca105..98ef4b542 100644 --- a/core/container/bit_array/bit_array.odin +++ b/core/container/bit_array/bit_array.odin @@ -16,15 +16,16 @@ INDEX_MASK :: 63 NUM_BITS :: 64 Bit_Array :: struct { - bits: [dynamic]u64, - bias: int, - max_index: int, + bits: [dynamic]u64, + bias: int, + max_index: int, + free_pointer: bool, } Bit_Array_Iterator :: struct { - array: ^Bit_Array, + array: ^Bit_Array, word_idx: int, - bit_idx: uint, + bit_idx: uint, } /* @@ -187,7 +188,7 @@ set :: proc(ba: ^Bit_Array, #any_int index: uint, allocator := context.allocator /* A helper function to create a Bit Array with optional bias, in case your smallest index is non-zero (including negative). */ -create :: proc(max_index: int, min_index := 0, allocator := context.allocator) -> (res: Bit_Array, ok: bool) #optional_ok { +create :: proc(max_index: int, min_index := 0, allocator := context.allocator) -> (res: ^Bit_Array, ok: bool) #optional_ok { context.allocator = allocator size_in_bits := max_index - min_index @@ -195,11 +196,11 @@ create :: proc(max_index: int, min_index := 0, allocator := context.allocator) - legs := size_in_bits >> INDEX_SHIFT - res = Bit_Array{ - bias = min_index, - max_index = max_index, - } - return res, resize_if_needed(&res, legs) + res = new(Bit_Array) + res.bias = min_index + res.max_index = max_index + res.free_pointer = true + return res, resize_if_needed(res, legs) } /* @@ -216,6 +217,9 @@ clear :: proc(ba: ^Bit_Array) { destroy :: proc(ba: ^Bit_Array) { if ba == nil { return } delete(ba.bits) + if ba.free_pointer { // Only free if this Bit_Array was created using `create`, not when on the stack. + free(ba) + } } /* |