From 19f30f1562238a0bbbb23acc249311a17361e36d Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 4 Apr 2022 13:40:09 -0500 Subject: [PATCH] freeing packets and commands --- example/udp_client.onyx | 2 +- example/udp_server.onyx | 4 ++-- src/packet.onyx | 8 ++++++++ src/peer.onyx | 17 +++++++++++++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/example/udp_client.onyx b/example/udp_client.onyx index 507545a..cd943c0 100644 --- a/example/udp_client.onyx +++ b/example/udp_client.onyx @@ -1,5 +1,5 @@ #load "core/std" -#load "src/module" +#load "./../src/module" use package core onyx_net :: package onyx_net diff --git a/example/udp_server.onyx b/example/udp_server.onyx index 184127a..184b9f8 100644 --- a/example/udp_server.onyx +++ b/example/udp_server.onyx @@ -1,5 +1,5 @@ #load "core/std" -#load "src/module" +#load "./../src/module" use package core onyx_net :: package onyx_net @@ -36,7 +36,7 @@ main :: (args) => { if it.data == "What's Up?" { packet := new(onyx_net.Packet); packet.flags |= .Reliable; - packet.data = #file_contents "src/host.onyx"; + packet.data = #file_contents "./../src/host.onyx"; onyx_net.peer_send(it.peer, 0, packet); } diff --git a/src/packet.onyx b/src/packet.onyx index ace4186..a2a7e58 100644 --- a/src/packet.onyx +++ b/src/packet.onyx @@ -26,6 +26,14 @@ Packet :: struct { flags: Flags; data: [] u8; + + // Set this to true to have the internal code + // free the data when it is done with the packet. + free_data := false; + + // The allocator used for the data. Only used if + // free_data is set to true. + data_allocator := context.allocator; } Acknowledgement :: struct { diff --git a/src/peer.onyx b/src/peer.onyx index e40d929..6a2b1c4 100644 --- a/src/peer.onyx +++ b/src/peer.onyx @@ -216,6 +216,8 @@ peer_flush_outgoing_commands :: (peer: ^Peer) -> i32 { if (it.command.command & .Flag_Acknowledge) != 0 { peer.sent_reliable_commands << it; + } else { + peer_free_outgoing_command(peer, it); } } @@ -284,13 +286,20 @@ peer_remove_sent_reliable_command :: (peer: ^Peer, seq_num: u16, channel: Channe if command == null do return .None; - defer { - cfree(command.command); - cfree(command); - } + defer peer_free_outgoing_command(peer, command); return command_get_effective(command.command.command); } +peer_free_outgoing_command :: (peer: ^Peer, command: ^Outgoing_Command) { + if command.packet.free_data { + raw_free(command.packet.data_allocator, command.packet.data.data); + } + + if command.packet != null do cfree(command.packet); + cfree(command.command); + cfree(command); +} + Channel_ID :: u8; -- 2.25.1