added +vet for explicit allocators

This commit is contained in:
Simon Kellet 2026-06-02 22:25:24 +01:00
parent 3252771e56
commit b7e7357ef4

View File

@ -1,6 +1,6 @@
#+vet explicit-allocators
package main package main
import "core:fmt"
import "core:os" import "core:os"
import "core:strconv" import "core:strconv"
import "core:strings" import "core:strings"
@ -17,7 +17,6 @@ Ppm_Pixel :: struct {
} }
Ppm_file :: struct { Ppm_file :: struct {
file: os.File, //TODO: use this instead perhaps?
data: []u8, data: []u8,
magic_num: PPM_MAGIC_NUMBER, magic_num: PPM_MAGIC_NUMBER,
width: int, width: int,
@ -26,7 +25,14 @@ Ppm_file :: struct {
pixels: []Ppm_Pixel, pixels: []Ppm_Pixel,
} }
load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) { load_ppm :: proc(
ppm_file: ^Ppm_file,
filename: string,
allocator := context.allocator,
) -> (
bool,
os.Error,
) {
if ppm_file == nil || len(filename) == 0 { if ppm_file == nil || len(filename) == 0 {
return false, .Not_Exist return false, .Not_Exist
} }
@ -36,7 +42,7 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) {
//if file is valid, opemn but check error too! //if file is valid, opemn but check error too!
err: os.Error err: os.Error
ppm_file.data, err = os.read_entire_file_from_path(filename, context.temp_allocator) ppm_file.data, err = os.read_entire_file_from_path(filename, allocator)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -44,11 +50,11 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) {
} }
//TODO: handle errors and err return type (success: bool?, err: ErrorType?) //TODO: handle errors and err return type (success: bool?, err: ErrorType?)
ppm_parse :: proc(ppm_file: ^Ppm_file) { ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) {
magic_num_found := false magic_num_found := false
w_h_found := false w_h_found := false
max_col_val_found := false max_col_val_found := false
sb := strings.builder_make(allocator = context.temp_allocator) sb := strings.builder_make(allocator)
defer strings.builder_destroy(&sb) defer strings.builder_destroy(&sb)
it := string(ppm_file.data) it := string(ppm_file.data)
@ -66,20 +72,16 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
continue continue
} }
w_h_parts := strings.split(line, " ") w_h_parts := strings.split(line, " ", allocator)
if !w_h_found && len(w_h_parts) == 2 { if !w_h_found && len(w_h_parts) == 2 {
ppm_file.width, _ = strconv.parse_int(w_h_parts[0]) ppm_file.width, _ = strconv.parse_int(w_h_parts[0])
ppm_file.height, _ = strconv.parse_int(w_h_parts[1]) ppm_file.height, _ = strconv.parse_int(w_h_parts[1])
ppm_file.pixels = make( ppm_file.pixels = make([]Ppm_Pixel, ppm_file.width * ppm_file.height, allocator)
[]Ppm_Pixel,
ppm_file.width * ppm_file.height,
context.temp_allocator,
)
w_h_found = true w_h_found = true
continue continue
} }
max_val_part := strings.split(line, " ") max_val_part := strings.split(line, " ", allocator)
if !max_col_val_found && len(max_val_part) == 1 { if !max_col_val_found && len(max_val_part) == 1 {
ppm_file.max_col_val, _ = strconv.parse_int(max_val_part[0]) ppm_file.max_col_val, _ = strconv.parse_int(max_val_part[0])
max_col_val_found = true max_col_val_found = true
@ -87,7 +89,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
} }
if magic_num_found && w_h_found && max_col_val_found { if magic_num_found && w_h_found && max_col_val_found {
for value in strings.split(line, " ") { for value in strings.split(line, " ", allocator) {
strings.write_string(&sb, value) strings.write_string(&sb, value)
strings.write_string(&sb, " ") strings.write_string(&sb, " ")
} }
@ -97,7 +99,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
all_values := strings.to_string(sb) all_values := strings.to_string(sb)
index := 0 index := 0
pixel_values := strings.split(all_values, " ") pixel_values := strings.split(all_values, " ", allocator)
for i := 0; i < len(pixel_values); i += 3 { for i := 0; i < len(pixel_values); i += 3 {
if index < len(ppm_file.pixels) { if index < len(ppm_file.pixels) {