Skip to content

Instantly share code, notes, and snippets.

@Tetralux
Tetralux / test_serialize_atom_strings_to_stream.odin
Last active December 10, 2024 17:16
A way to serialise strings along with a blob
/*
A short demonstration of how atomized strings (strings turned into single ints to make comparison faster and storage simpler) can be serialised into a stream (or file),
alongside the blobs that refer to them, without doing anything more than just memcpy'ing everything into the stream.
It achieves this using the following techniques:
1) Atoms are stored in the Person structs only. They are i32s.
2) Atomizer turns strings into i32s by copying their length (u32le) into a buffer, followed by the string data. The current offset into the buffer is the atom for that string.
3) Both the slice of Persons and the atomizer's buffer can be directly written into a stream without alteration. See the comment in main() of the output.
Written by Tetralux 2024-10-12.
@Tetralux
Tetralux / linked_list_example.odin
Last active July 28, 2024 14:25
A basic linked list implementation that doesn't allocate nodes individually.
/*
A simple example of a linked list implementation that does NOT allocate the nodes individually.
Written by TetraluxOnPC, 2024-07-28.
*/
package linked_list_example
import "core:mem"
import "core:mem/virtual"
import "core:fmt"
@Tetralux
Tetralux / strict_allocator.odin
Last active August 17, 2024 20:56
Allocator which maps each allocation to a separate page, and locks that page immediately on free
package util
import "base:runtime"
import "base:intrinsics"
import "core:mem"
import "core:mem/virtual"
import "core:os"
import "core:fmt"
@Tetralux
Tetralux / interface_example_allocators.zig
Last active January 18, 2024 13:54
Zig interface example (Allocator)
const Ally = struct {
data: ?*anyopaque,
proc: *const fn(
ally_data: ?*anyopaque,
mode: Mode,
count: usize,
alignment: usize,
old_memory: []u8,
) error{OutOfMemory}![]u8,
@Tetralux
Tetralux / stream_server_error_handling.zig
Last active November 15, 2023 23:56
A simple illustration of a std.net.StreamServer that handles each client with a thread pool, with simple error handling
// A simple illustration of a stream server that handles each client with a thread pool, and
// if any worker returns an error then it prints out the errorReturnTrace.
//
// Untested.
//
// Written by Tetralux, 2023-11-15.
const std = @import("std");
const Data = struct {
@Tetralux
Tetralux / ini.zig
Last active October 21, 2023 02:03
Rudimentary INI parser in Zig
const std = @import("std");
const string = []const u8;
const Allocator = std.mem.Allocator;
/// Given an entire INI string, parses it into a map of key-value pairs.
/// The pairs are all freed by a call to `Parser.deinit`, and retrieved via `Parser.get`.
/// If you need them to live beyond that, use `some_other_allocator.dupe(u8, value)`.
pub const Parser = struct {
data: std.StringArrayHashMap(string),
@Tetralux
Tetralux / simple_exec.zig
Last active October 13, 2023 18:13
A simple example program that runs a program with the given arguments by searching the PATH environment variable.
//
// A simple example program that runs a program with the given arguments by searching the PATH environment variable.
//
// Tetralux, 2023-10-13.
//
const std = @import("std");
const builtin = @import("builtin");
const string = []const u8;
@Tetralux
Tetralux / counting_allocator.odin
Last active March 26, 2023 10:36
Back of the envolope counting allocator implementation
import "core:mem"
import "core:runtime"
Counting_Allocator :: struct {
total_used: uint,
backing_ally: mem.Allocator,
}
counting_allocator :: proc(ca: ^Counting_Allocator) -> mem.Allocator {
return {
@Tetralux
Tetralux / tiny.zig
Last active March 11, 2023 10:37
A simple example of a calculator programming language that compiles to native code!
//
// A simple example of a calculator programming language, that compiles to native code!
//
// Written by Tetralux <[email protected]>, 2023-03-09.
//
// Programs are a string that you pass as an argument in the form of a mathmetical expression.
// e.g: '10 + 4 - 1 + 7'.
// This program will generate some Zig code that computes the answer, and prints it to stdout.
// It then will invoke the Zig compiler as a subprocess to compile and run this program.
//
@Tetralux
Tetralux / popen_test.odin
Last active September 29, 2022 06:32
Example of how to bind and use popen from libc
package process
import "core:c"
import "core:c/libc"
import "core:strings"
when ODIN_OS == .Windows {
foreign import libc_obj "system:libucrt.lib"
POPEN_NAME :: "_popen"
PCLOSE_NAME :: "_pclose"