Skip to content

Instantly share code, notes, and snippets.

@firejox
firejox / wsl-interop-ac.path
Created April 15, 2024 11:55
WSL-Interop Access Control
[Unit]
Description=WSL-Interop Access Control
[Path]
PathChanged=/run/WSL
[Install]
WantedBy=multi-user.target
@firejox
firejox / sleep.sh
Created April 14, 2024 10:20
Alternative sleep implementation
#!/bin/sh
Help()
{
cat <<-EOF
Usage: $0 NUMBER[SUFFIX]...
or: $0 OPTION
Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),
'm' for minutes, 'h' for hours or 'd' for days. NUMBER need not be an
@firejox
firejox / CMakeLists.txt
Created May 24, 2023 13:55
LibTorch integrated with CMake on Windows Subsystem Linux
cmake_minimum_required(VERSION 3.21)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES "75")
endif()
set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc")
project(example-app)
@firejox
firejox / LL(1)-pg.cr
Created November 8, 2019 12:50
LL(1) parser generator
annotation ParserAttr
end
annotation TopSymbolAttr
end
annotation SymbolAttr
end
annotation Rule
@firejox
firejox / solution-1.cr
Last active June 8, 2020 05:47
The changes about prevent leaking container_of globaly
# channel.cr
class Channel(T)
struct Sender(T)
@link : Crystal::StaticList
end
Crystal::StaticList.container_of(ptr, Channel::Sender(T), @link)
end
# crystal/static_list.cr
@firejox
firejox / machine.cr
Last active December 21, 2019 13:18
State machine (pure inline)
struct Machine
enum State
Before
Run
After
Next
end
def initialize(@state : State, @line : Int32)
end
@firejox
firejox / rotate.cr
Created December 5, 2019 16:39
another array rotaion
class Array
def new_rotate!(n = 1)
return self if empty?
sz = @size
n %= sz
return self if n == 0
st = to_unsafe
ed = st + sz
while (sz > 1 && n != 0)
@firejox
firejox / benchmark.cr
Created December 5, 2019 04:04
Array rotation
require "benchmark"
class Array
def new_rotate!(n = 1)
return self if empty?
sz = self.size
n %= sz
return self if n == 0
tmp = Slice.new(to_unsafe, sz)
@firejox
firejox / static_list.cr
Created October 7, 2019 16:57
Circular Linked List
macro container_of(ptr, _type, member)
{% if _type.resolve < Reference %}
(({{ ptr }}).as(Void*) - offsetof({{ _type }}, {{ member }})).as({{ _type }})
{% else %}
(({{ ptr }}).as(Void*) - offsetof({{ _type }}, {{ member }})).as(Pointer({{ _type }}))
{% end %}
end
struct StaticList
@prev = uninitialized Pointer(StaticList)
@firejox
firejox / bench.cr
Created October 14, 2019 06:22
Channel ping-pong latency
ch = Channel(Int32).new
done = Channel(Time::Span).new
count = (ARGV[0]? || 1000).to_i
4.times do
spawn do
delay = Time::Span::ZERO
count.times do |i|