Skip to content

Instantly share code, notes, and snippets.

@msullivan
msullivan / reboot.c
Created June 20, 2016 22:18
code to reboot a pc using keyboard controller
void reboot(void)
{
while (1) { /* keep trying if it doesn't work */
/* wait for the output buffer to be empty */
while (inb(KEYBOARD_CMD_PORT) & KEYBOARD_OUTBUF_FULL_MASK)
;
/* send the command to have the keyboard controller reboot the machine.
* wtf. */
outb(KEYBOARD_CMD_PORT, KEYBOARD_CMD_REBOOT);
@msullivan
msullivan / clang-crash.cpp
Created April 13, 2016 20:02
clang parser segfault - I got it to crash on 3.6 and 3.7 on my machine
// clang++ --std=c++11 clang-crash.cpp
template<typename F>
auto foo(F f) -> decltype(f()) { }
void bar() {
foo([&]() { return *bogus; });
}
@msullivan
msullivan / notionstartup.sh
Created November 6, 2015 05:30
my notion startup script
#!/bin/sh
if pgrep unity-settings > /dev/null; then
exit
fi
xsetroot -solid black
# sleeps: evade, don't solve, concurrency problems
unity-settings-daemon& sleep 1;
wmdocker & sleep 1
@msullivan
msullivan / borrow-regression.rs
Created October 9, 2015 21:39
What seems to be a borrowchecker regression
// This compiles without error on stable and beta but gives a borrow
// error on nightly. It worked as of nightly 6e5a32547.
// Replacing the .by_ref() with explicitly taking a &mut ref fixes it.
// Other more dubious things fix it also.
pub type Session = i32;
pub struct StreamParser<'a, T: Iterator<Item=i32>> {
_tokens: T,
@msullivan
msullivan / fake-panel.py
Created September 17, 2015 03:16
Script to register a dummy panel so that multimedia keys work properly when using weirdo WMs
#!/usr/bin/env python3
# Michael J. Sullivan (http://www.msully.net/)
# Stupid little script to make gnome/unity media keys work when running
# weirdo window managers like xmonad and notion.
# Directions:
# With unity-settings-daemon running, run this script in the background.
# Tested on Ubuntu 15.04 using notion.
@msullivan
msullivan / vfork-nightmares.cpp
Created September 11, 2015 20:01
vfork testing
// This code is all kinds of illegal and tests whether other threads
// keep running while there is an outstanding vfork
#include <atomic>
#include <thread>
#include <iostream>
#include <unistd.h>
std::atomic<bool> child_running;
std::atomic<bool> child_signaled;
let time_execution func =
let start_time = Sys.time () in
let ret = func () in
let end_time = Sys.time () in
end_time -. start_time, ret
(* val time_execution : (unit -> 'a) -> float * 'a *)
let print_stuff () = Printf.printf "hello, world\n"
let compute_stuff () = 10 + 10
@msullivan
msullivan / typeof_fail.c
Created April 15, 2015 18:45
Copying values to a tmp with __typeof__ "doesn't" work
// Copying variables into a tmp using __typeof__ is a common
// pattern in hacky gcc C macros, but it can backfire and
// allow the value to be evaluated twice!
// This is because __typeof__ will evaluate its argument if
// the value has a variably sized type!
// gcc added __auto_type to get around this problem in its stdatomic.h, but I'm
// not losing any sleep over this.
// See http://patchwork.ozlabs.org/patch/290802/
@msullivan
msullivan / launder.c
Created March 5, 2015 20:33
"Launder" a value with inline asm
// Make a copy of a value that the compiler doesn't know anything about.
#define launder_value(v) \
({ \
__typeof__(v) __v = v; \
__asm__ __volatile__("" : "+r" (__v)::); \
__v; \
})
@msullivan
msullivan / MicroKanren.hs
Created February 26, 2015 22:54
MicroKanren (μKanren) in Haskell
import Control.Monad
type Var = Integer
type Subst = [(Var, Term)]
type State = (Subst, Integer)
type Program = State -> KList State
data Term = Atom String | Pair Term Term | Var Var deriving Show
-- Apply a substitution to the top level of a term