Skip to content

Instantly share code, notes, and snippets.

View lithdew's full-sized avatar

Kenta Iwasaki lithdew

View GitHub Profile
@lithdew
lithdew / main.zig
Last active July 22, 2020 19:03
libuv wip
const std = @import("std");
const c = @cImport(@cInclude("uv.h"));
const assert = std.debug.assert;
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub fn onClose(handle: ?*c.uv_handle_t) callconv(.C) void {
const allocator = &arena.allocator; // get allocator here
allocator.destroy(handle.?);
@lithdew
lithdew / build.zig
Created July 28, 2020 04:56
zig rocksdb
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) 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.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
@lithdew
lithdew / build.zig
Last active July 28, 2020 04:57
zig rocksdb
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) 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.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
@lithdew
lithdew / main.zig
Created July 28, 2020 09:45
zig stack/heap
const std = @import("std");
pub const KV = struct {
key: []const u8,
val: []const u8,
pub fn init(key: []const u8, val: []const u8) KV {
return KV{ .key = key, .val = val };
}
};
@lithdew
lithdew / main.zig
Created July 29, 2020 00:58
zig concat
const std = @import("std");
pub fn concat(bufs: []const []const u8) ![]const u8 {
// var buf: [11]u8 = undefined;
// var buffer = std.heap.FixedBufferAllocator.init(&buf);
return std.mem.concat(std.heap.c_allocator, u8, bufs);
// return std.mem.concat(&buffer.allocator, u8, bufs);
}
pub fn main() !void {
@lithdew
lithdew / gist:d01ab5beec771c8e51c29c66465842e8
Created July 30, 2020 10:05
zig: stack alloc recursive data structure
const std = @import("std");
const Node = struct {
left: ?*Node = null,
right: ?*Node = null,
pub fn new(allocator: *std.mem.Allocator) !Node {
return Node{
.left = try allocator.create(Node),
.right = try allocator.create(Node),
@lithdew
lithdew / main.zig
Created September 8, 2020 05:47
zig: graceful tcp client shutdown
const std = @import("std");
const os = std.os;
const File = std.fs.File;
pub fn shutdown(fd: os.fd_t, how: i32) !void {
const rc = os.system.shutdown(fd, how);
switch (os.errno(rc)) {
0 => return,
os.EBADF => unreachable,
@lithdew
lithdew / main.zig
Created September 14, 2020 09:30
zig: inheritance
pub const B = struct {
const Self = @This();
data: []const u8,
pub fn getParent(self: *Self) *A {
return @fieldParentPtr(A, "child", self);
}
};
@lithdew
lithdew / windows_client.zig
Created October 6, 2020 14:56
pike (windows): benchmark tcp client and server
const std = @import("std");
const os = std.os;
const net = std.net;
const windows = os.windows;
const ws2_32 = windows.ws2_32;
const pike = @import("pike.zig");
fn socket(address: net.Address, socket_type: u32) !os.fd_t {
@lithdew
lithdew / main.zig
Last active October 8, 2020 11:10
zig (windows): SetConsoleCtrlHandler() example
const std = @import("std");
const os = std.os;
const windows = os.windows;
pub const HANDLER_ROUTINE = fn(dwCtrlType: windows.DWORD) callconv(.Stdcall) windows.BOOL;
const funcs = struct {
extern "kernel32" fn SetConsoleCtrlHandler(HandlerRoutine: ?HANDLER_ROUTINE, Add: windows.BOOL) callconv(.Stdcall) windows.BOOL;
};