From a03d388a0cf9467c0f3528ff46d74eb01a47c6d1 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Fri, 19 Jun 2026 07:50:05 -0400 Subject: [PATCH] refactor: Allocations now use the temp_allocator more frequently. --- cli.odin | 1 + cmd_sync.odin | 2 +- config.odin | 11 +++++++---- crypto.odin | 37 ++++++++++++++++++++----------------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cli.odin b/cli.odin index a13c54c..9995cce 100644 --- a/cli.odin +++ b/cli.odin @@ -75,6 +75,7 @@ parse_args :: proc(args: []string, out: io.Stream, err: io.Stream) -> (cmd: Comm cmd.flags = make(map[string]string) cmd.bool_set = make(map[string]bool) + // TODO: Optimize loop? i := 2 for i < len(args) { arg := args[i] diff --git a/cmd_sync.odin b/cmd_sync.odin index c3838b0..2cd78b7 100644 --- a/cmd_sync.odin +++ b/cmd_sync.odin @@ -64,7 +64,7 @@ cmd_sync :: proc(cmd: ^Command) { render_table(cmd.out, headers, table_rows[:]) } else { - data, marshal_err := json.marshal(results[:]) + data, marshal_err := json.marshal(results[:], allocator = context.temp_allocator) if marshal_err != nil { fmt.wprintf(cmd.err, "Error marshaling JSON: %v\n", marshal_err, flush = false) return diff --git a/config.odin b/config.odin index 238e340..91f05b8 100644 --- a/config.odin +++ b/config.odin @@ -84,9 +84,9 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool { } if os.exists(cfg.config_path) && !force { - info, stat_err := os.stat(cfg.config_path, context.allocator) + info, stat_err := os.stat(cfg.config_path, context.temp_allocator) if stat_err == nil { - defer os.file_info_delete(info, context.allocator) + defer os.file_info_delete(info, context.temp_allocator) if info.size > 0 { fmt.println("Config file already exists. Run again with --force to reinitialize.") return false @@ -94,12 +94,15 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool { } } - data, marshal_err := json.marshal(cfg, {pretty = true, use_spaces = true, spaces = 2}) + data, marshal_err := json.marshal( + cfg, + {pretty = true, use_spaces = true, spaces = 2}, + context.temp_allocator, + ) if marshal_err != nil { fmt.printf("Error marshaling config: %v\n", marshal_err) return false } - defer delete(data) write_err := os.write_entire_file(cfg.config_path, data) if write_err != nil { diff --git a/crypto.odin b/crypto.odin index 9f12e67..214fbf2 100644 --- a/crypto.odin +++ b/crypto.odin @@ -1,5 +1,6 @@ package main +import "core:crypto/_fiat/field_p384r1" import "core:fmt" import "core:mem" import "core:os" @@ -33,12 +34,12 @@ init_sodium :: proc "contextless" () { } } +// TODO: Optimize performance encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: bool) { - x25519_pairs, pairs_ok := ssh_to_x25519(keys) + x25519_pairs, pairs_ok := ssh_to_x25519(keys, context.temp_allocator) if !pairs_ok { return } - defer delete(x25519_pairs) sym_key: [CRYPTO_SECRETBOX_KEY_BYTES]u8 randombytes_buf(&sym_key[0], CRYPTO_SECRETBOX_KEY_BYTES) @@ -47,7 +48,7 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b randombytes_buf(&main_nonce[0], CRYPTO_SECRETBOX_NONCE_BYTES) ct_len := len(plaintext) + CRYPTO_SECRETBOX_MAC_BYTES - secret_ct := make([]u8, ct_len) + secret_ct := make([]u8, ct_len, context.temp_allocator) pt_ptr: [^]u8 if len(plaintext) > 0 { pt_ptr = &plaintext[0] @@ -66,7 +67,7 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b } num_recipients := u32(len(x25519_pairs)) - entries := make([]RecipientEntry, num_recipients) + entries := make([]RecipientEntry, num_recipients, context.temp_allocator) for i in 0 ..< len(x25519_pairs) { for j in 0 ..< CRYPTO_BOX_PUBLICKEY_BYTES { @@ -126,8 +127,6 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b mem.copy(&ciphertext[pos], &secret_ct[0], ct_len) - delete(entries) - delete(secret_ct) ok = true return } @@ -176,11 +175,10 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b enc_nonce: [CRYPTO_BOX_NONCE_BYTES]u8 enc_pub: [CRYPTO_BOX_PUBLICKEY_BYTES]u8 - x25519_pairs, pairs_ok := ssh_to_x25519(keys) + x25519_pairs, pairs_ok := ssh_to_x25519(keys, context.temp_allocator) if !pairs_ok { return } - defer delete(x25519_pairs) found := false matched_pi := 0 @@ -272,33 +270,39 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b return } -ssh_to_x25519 :: proc(keys: []SshKeyPair) -> (pairs: []X25519Keypair, ok: bool) { +ssh_to_x25519 :: proc( + keys: []SshKeyPair, + allocator := context.temp_allocator, +) -> ( + []X25519Keypair, + bool, +) { if len(keys) == 0 { - return + return {}, false } - pairs = make([]X25519Keypair, len(keys)) + pairs := make([]X25519Keypair, len(keys), allocator) for i in 0 ..< len(keys) { ssh_kp, parse_ok := parse_ssh_private_key(keys[i].Private) if !parse_ok { fmt.printf("Error: failed to parse SSH private key: %s\n", keys[i].Private) delete(pairs) - return + return pairs, false } ssh_pub, pub_ok := parse_ssh_public_key(keys[i].Public) if !pub_ok { fmt.printf("Error: failed to parse SSH public key: %s\n", keys[i].Public) delete(pairs) - return + return pairs, false } pk_rc := crypto_sign_ed25519_pk_to_curve25519(&pairs[i].Public[0], &ssh_pub[0]) if pk_rc != 0 { fmt.println("Error: failed to convert ed25519 public key to curve25519") delete(pairs) - return + return pairs, false } ed25519_sk: [64]u8 @@ -313,11 +317,10 @@ ssh_to_x25519 :: proc(keys: []SshKeyPair) -> (pairs: []X25519Keypair, ok: bool) if sk_rc != 0 { fmt.println("Error: failed to convert ed25519 private key to curve25519") delete(pairs) - return + return pairs, false } } - ok = true - return + return pairs, true }