Skip to content

Instantly share code, notes, and snippets.

View andrewrk's full-sized avatar

Andrew Kelley andrewrk

View GitHub Profile
@andrewrk
andrewrk / bar.zig
Created December 19, 2023 00:26
demo of specifying different target CPU features per module. context: https://github.com/ziglang/zig/pull/18160
pub fn add(a: i32, b: i32) i32 {
return a + b;
}
@andrewrk
andrewrk / output.txt
Created April 21, 2017 02:52
zig test --test-filter lets you test things while you have broken code elsewhere
[nix-shell:~/dev/zig/build]$ ./zig test ../std/os/path.zig --test-filter resolve
Test 1/1 os.path.resolve...OK
[nix-shell:~/dev/zig/build]$ ./zig test ../std/os/path.zig 
/home/andy/dev/zig/std/buffer.zig:105:24: error: container 'Buffer' has no member called 'initFromBuffer'
    var buf2 = %%Buffer.initFromBuffer(&buf);
                       ^
/home/andy/dev/zig/std/buffer.zig:106:15: error: no member named 'eqlBuffer' in 'Buffer'
    assert(buf.eqlBuffer(&buf2));
              ^
@andrewrk
andrewrk / gist:1883543
Created February 22, 2012 09:20
function to count trailing zeros

Given an unsigned integer, return the number of trailing zero bits

Test framework:

#include <stdio.h>
#include <assert.h>

unsigned trailing_zeros(unsigned n) {
    // fill in this function
@andrewrk
andrewrk / main.zig
Created December 1, 2023 21:40
advent of code 2023 day 1 part 1 in zig
const std = @import("std");
const example =
\\1abc2
\\pqr3stu8vwx
\\a1b2c3d4e5f
\\treb7uchet
;
pub fn main() !void {
@andrewrk
andrewrk / strace_c.txt
Last active November 22, 2023 17:47
overhead of hello world in C, C++, Zig
andy@ark ~/tmp> gcc -o hello hello.c
andy@ark ~/tmp> strace ./hello
execve("./hello", ["./hello"], 0x7ffe96b5ddb0 /* 62 vars */) = 0
brk(NULL) = 0x2129000
access("/etc/ld-nix.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/nix/store/xg6ilb9g9zhi2zg1dpi4zcp288rhnvns-glibc-2.30/lib/tls/haswell/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/xg6ilb9g9zhi2zg1dpi4zcp288rhnvns-glibc-2.30/lib/tls/haswell/x86_64", 0x7fff2bd55cc0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/nix/store/xg6ilb9g9zhi2zg1dpi4zcp288rhnvns-glibc-2.30/lib/tls/haswell/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/xg6ilb9g9zhi2zg1dpi4zcp288rhnvns-glibc-2.30/lib/tls/haswell", 0x7fff2bd55cc0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/nix/store/xg6ilb9g9zhi2zg1dpi4zcp288rhnvns-glibc-2.30/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or dire
@andrewrk
andrewrk / bootloader.zig
Created February 4, 2023 21:42
example of embedding a generated executable inside another one
pub export fn _start() void {}
@andrewrk
andrewrk / build.zig
Last active September 30, 2023 19:01
zig project with the main function being in C
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
@andrewrk
andrewrk / README.md
Created August 28, 2023 19:00
exfiltrate groove basin database to a JSON file
  1. drop db2json.js into your groovebasin instance
  2. stop your groove basin instance so that it does not write to the db while you are dumping its data
  3. node db2json.js (use the same node.js version as groove basin is using, and it also wants to use the same leveldown dependency you already have installed). It hard-codes the input as groovebasin.db.
  4. groovebasin.db.json is created which is 100% of the information from the database, in JSON format. It is one giant map of every key-value pair.
@andrewrk
andrewrk / 1-brute-force.c
Created May 1, 2019 21:58
cleanup code techniques
int main(int argc, char **argv) {
struct SoundIo *soundio = soundio_create();
if (!soundio) {
fprintf(stderr, "out of memory\n");
return 1;
}
int err;
if ((err = soundio_connect(soundio))) {
fprintf(stderr, "unable to connect: %s\n", soundio_strerror(err));
soundio_destroy(soundio);
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBFv8SrUBEADCku6WktTc1g+iyE9ZCtMv4kWqSHyQxFaEV8V5J2EAkjAzgr6w
NLmHGmNmXm8EzCWnwn/KfHJCeXTcgma/FtIF7hJfWB0xktA7WENUVc3qtT0cY9z3
9jh6J3TW3m9hcN7szSyEqGvPMVCvd5pERZXfof9OaRqtNak3GBOcklHYrVJ0KCtA
quR0t9NYrdOQikmBy4c9GaDsq/6H39LPuuj/vm7M+MHrw5dlKh+HPeUP9jMbFoXU
ohz97RSy8T2lUQDQx1EisAJNvdpU3mzAlWy2pEH+pKCBs5L0vPV/tvH1J5Pd489s
7VcdM9AolIuHvV0qCDAG7fcWujV5R5w48vznvfi6R3DN8O2iVrYdOWn2Bm60HdGm
XxGQswb6/MfThpFzQUNQpvnXxdbt2vefUTmM4suid6ki/jLfsiY1rqcNdEcriYFx
J6ma4SvZOB7OB2DG9bjWSItDIa2HqW37o//FYoFHJO0L+v5qjemYx5QrpL2wCpnY