Skip to content

Instantly share code, notes, and snippets.

View matu3ba's full-sized avatar

Jan Ph. H. matu3ba

View GitHub Profile
@matu3ba
matu3ba / pipe.zig
Last active May 16, 2022 22:21
Pipe in Zig. Notice that EOF can only be read once child or parent close the pipe. Doesnt matter if we dup or not.
const std = @import("std");
const os = std.os;
pub fn main() !void {
const alloc = std.testing.allocator;
const pipe = try os.pipe(); // read end [0], write end [1]
const read_dup = try os.dup(pipe[0]);
const write_dup = try os.dup(pipe[1]); // for simplicity
const pid_result = try os.fork();
if (pid_result == 0) {
@matu3ba
matu3ba / canonicalize_test_unix.zig
Created May 5, 2022 14:43
path that requires canonicalization for unix and wasi
test "canonicalize path Unix" {
if (builtin.os.tag == .windows) // symlinks are not portable
return error.SkipZigTest;
// TODO testing partial solution to https://github.com/ziglang/zig/issues/4812
const alloc = testing.allocator;
var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE
defer tmp.cleanup();
const tmpdirpath = try tmp.getFullPath(alloc);
defer alloc.free(tmpdirpath);
var cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; // maximum size of a UTF-8 encoded file path
@matu3ba
matu3ba / mem_consistency.cpp
Created April 24, 2022 18:01
notes on memory consistency
// Memory concistency and atomicity
#include <atomic>
#include <cassert>
#include <iostream>
#include <mutex>
#include <thread>
// assume: init of data = 0, flag != SET
// hint: L1 and B1 can repeat many times
@matu3ba
matu3ba / output
Last active April 19, 2022 22:54
access of inactive union field
!zig test union.zig
1/1 test "testme123"... thread 571747 panic: access of inactive union field
/home/user/dev/git/zig/tryzig/union.zig:40:27: 0x208295 in TcpIo.setup (test)
tcpio.ctrl.client = try tcp.Client.init(.ip, .{ .close_on_exec = true });
^
@matu3ba
matu3ba / instructions.sh
Created April 11, 2022 10:31
minimal instructions to install+use nixos with qemu
#qemu-img create -f qcow2 NOME.img XG
qemu-img create -f qcow2 nixos-test.img 20G
#qemu-system-x86_64 -boot d -cdrom image.iso -m 512 -hda mydisk.img
qemu-system-x86_64 -enable-kvm -boot d \
-cdrom nixos-minimal-21.11-x86_64-linux.iso \
-m 2G -cpu host -smp 2 -hda nixos-test.img
#usage
qemu-system-x86_64 -enable-kvm -boot d \
-m 2G -cpu host -smp 2 -hda nixos-test.img
@matu3ba
matu3ba / client.zig
Created April 10, 2022 22:28
2 tcp connections between parent and child process
//! client started by server as subprocess
const std = @import("std");
const builtin = @import("builtin");
const net = std.x.net;
const os = std.x.os;
const testing = std.testing;
const tcp = net.tcp;
const ip = net.ip;
@matu3ba
matu3ba / client.zig
Created April 10, 2022 13:13
child_process with tcp connection for portability eventually be used as test_runner
//! client started by server as subprocess
const std = @import("std");
const builtin = @import("builtin");
const net = std.x.net;
const os = std.x.os;
const testing = std.testing;
const tcp = net.tcp;
const ip = net.ip;
@matu3ba
matu3ba / parse_float.patch
Created March 17, 2022 22:29
prase_float128
diff --git a/lib/std/fmt/parse_float.zig b/lib/std/fmt/parse_float.zig
index 585c23ed5..585400587 100644
--- a/lib/std/fmt/parse_float.zig
+++ b/lib/std/fmt/parse_float.zig
@@ -1,36 +1,1111 @@
-// Adapted from https://github.com/grzegorz-kraszewski/stringtofloat.
+// Code ported from musl libc 8f12c4e110acb3bbbdc8abfb3a552c3ced718039
+// and then modified to use softfloat and to assume f128 for everything
-// MIT License
@matu3ba
matu3ba / output
Last active March 8, 2022 22:03
subprocess testing
:!/usr/bin/time -v zig test %
1/1 test "build and call child_process"... cwd_str: /home/misterspoon/dev/git/zig/zig/pipe
tmpDir: vCWMmOLRT0RiWAMe
child_zig: zig-cache/tmp/vCWMmOLRT0RiWAMe/child.zig
emit_bin: -femit-bin=zig-cache/tmp/vCWMmOLRT0RiWAMe/child
child_path: zig-cache/tmp/vCWMmOLRT0RiWAMe/child
OK
All 1 tests passed.
Command being timed: "zig test tst_childprocess.zig"
@matu3ba
matu3ba / subprocess.zig
Last active March 7, 2022 00:21
write+compile child executable and call it as subprocess in Zig with checks
const std = @import("std");
const childstr =
\\ const std = @import("std");
\\ const builtin = @import("builtin");
\\ const os = std.os;
\\ pub fn main() !void {
\\ var arena_inst = std.heap.ArenaAllocator.init(std.heap.page_allocator);
\\ defer arena_inst.deinit();
\\ const arena = arena_inst.allocator();
\\ var it = try std.process.argsWithAllocator(arena);