fix: clean up all memory leaks. use tracking allocator in example

This commit is contained in:
2025-05-25 22:44:14 +02:00
parent 8982186b72
commit 3175b3a23b
3 changed files with 48 additions and 17 deletions
+9 -11
View File
@@ -61,9 +61,7 @@ base83_decode_str :: proc(str: string) -> int {
base83_decode :: proc{base83_decode_str, base83_decode_byte}
@(private)
base83_encode :: proc(value, length: int, allocator := context.allocator) -> string {
b := strings.builder_make_none(allocator)
base83_encode :: proc(b: ^strings.Builder, value, length: int, allocator := context.allocator) {
pow :: proc(base, exp: int) -> int {
base := base
exp := exp
@@ -80,10 +78,8 @@ base83_encode :: proc(value, length: int, allocator := context.allocator) -> str
for i in 1..=length {
digit := (value / pow(83, length - i)) % 83
strings.write_byte(&b, ENC_TABLE[digit])
strings.write_byte(b, ENC_TABLE[digit])
}
return strings.to_string(b)
}
@(private)
@@ -168,6 +164,7 @@ decode :: proc(blurhash: string, width, height: int, punch := 1.0, allocator :=
colors_size := num_x * num_y
colors := make([][3]f32, colors_size)
defer delete(colors)
for iter in 0..<colors_size {
if iter == 0 {
@@ -266,6 +263,7 @@ encode :: proc(pixels: []byte, x_components, y_components, width, height, bytes_
builder := strings.builder_make_len_cap(0, 2 + 4 + (9 * 9 - 1) * 2)
factors := make([dynamic][3]f32, 0, x_components * y_components)
defer delete(factors)
for y in 0..<y_components {
for x in 0..<x_components {
@@ -278,7 +276,7 @@ encode :: proc(pixels: []byte, x_components, y_components, width, height, bytes_
ac := factors[1:]
size_flag := (x_components - 1) + (y_components - 1) * 9
strings.write_string(&builder, base83_encode(size_flag, 1))
base83_encode(&builder, size_flag, 1)
max_value: f32
@@ -290,16 +288,16 @@ encode :: proc(pixels: []byte, x_components, y_components, width, height, bytes_
quantized_max_value := int(max(0, min(82, math.floor(actual_max_value * 166 - 0.5))))
max_value = f32(quantized_max_value + 1) / 166.0
strings.write_string(&builder, base83_encode(quantized_max_value, 1))
base83_encode(&builder, quantized_max_value, 1)
} else {
max_value = 1
strings.write_string(&builder, base83_encode(0, 1))
base83_encode(&builder, 0, 1)
}
strings.write_string(&builder, base83_encode(encode_dc(dc), 4))
base83_encode(&builder, encode_dc(dc), 4)
for factor in ac {
strings.write_string(&builder, base83_encode(encode_ac(factor, max_value), 2))
base83_encode(&builder, encode_ac(factor, max_value), 2)
}
return strings.to_string(builder), nil