Skip to content

Instantly share code, notes, and snippets.

@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
@msullivan
msullivan / lock-linking
Created February 25, 2015 19:15
Wrapper script to make commands that look like linking run in serial
#!/bin/sh
# Run a command with a lock if it looks like it is a linker command: that is,
# if none of its arguments are -c.
# Then you can do a build where compilation is parallel but linking is serial
# by doing something like (for llvm/clang):
# make -j4 'CXX=lock-linking /tmp/llvm-build-lock clang++'
LOCKFILE="$1"
@msullivan
msullivan / bsmake
Created February 25, 2015 18:37
Hacky wrapper to run make in parallel until linking starts
#!/bin/bash
# Terrible hack to run a build in parallel until it starts linking
# things and then switch to running it in serial to cut down on memory usage.
# This could be better and more robust in a *lot* of ways.
# I use this for building llvm/clang.
# The first argument gets passed to the initial make that runs until
# linking starts and should be -j4 or whatever. The rest of the
# arguments get passed to both makes.
@msullivan
msullivan / thin-air.c
Created December 15, 2014 20:12
Thin-air-read bad case
// Out of thin air writes are really scary.
// Example adapted from http://dl.acm.org/citation.cfm?id=2618134 to
// work under http://svr-pes20-cppmem.cl.cam.ac.uk/cppmem/index.html
// Can wind up with an = &b, bn = &a!
int main() {
int *p1, *p2;
atomic_int a, an, b, bn;
a = &an;
@msullivan
msullivan / indexed_list.agda
Created November 14, 2014 22:36
Agda vs. Coq
(* Indexed list indexing in Agda *)
data IList {a} (A : Set a) : Nat → Set a where
Nil : IList A 0
Cons : {n : Nat} -> A -> IList A n -> IList A (S n)
data Fin : Nat -> Set where
Z : {n : Nat} -> Fin (S n)
S : {n : Nat} -> Fin n -> Fin (S n)
@msullivan
msullivan / IO.hs
Last active August 29, 2015 14:08
Exploration of pre-monadic IO features
-- Exploration of pre monadic IO facilities and how they relate to
-- each other and with monadic IO.
-- See: http://research.microsoft.com/en-us/um/people/simonpj/papers/history-of-haskell/
-- and http://cpsc.yale.edu/sites/default/files/files/tr665.pdf
import System.IO
import System.IO.Unsafe
import System.IO.Error
import System.Exit
import Data.Functor