Skip to content

Instantly share code, notes, and snippets.

@hraban
hraban / coroutines.lisp
Created May 29, 2011 12:05 — forked from ryepup/coroutines in lisp
lisp coroutines using chanl (threads)
(defmacro make-coroutine ((&key (coroutine-done-value :done)) &body body)
(alexandria:with-gensyms ((thrfn "thread body")
(c "channel"))
`(let* ((,c (make-instance 'chanl:bounded-channel))
(,thrfn (lambda ()
(flet ((yield (&optional n)
(chanl:send ,c n)))
,@body
(yield ,coroutine-done-value)))))
(let ((alive-p T) val thr)
@hraban
hraban / mylock.cs
Created March 20, 2012 09:12
Custom C# locking interface and implementation
using System;
using System.Threading;
namespace Locking
{
internal interface IWaitableLock
{
/// <summary>
/// Acquire the lock.
/// </summary>
@hraban
hraban / static-int-functions-O0.s
Created June 28, 2012 11:56
Initialize variables using static functions instead of macros
.file "test.c"
.text
.type a, @function
a:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
@hraban
hraban / gist:3017681
Last active August 15, 2016 12:25
My .emacs (hraban)
(add-to-list 'load-path "~/.emacs.d/plugins")
;; cl-* functions and macros
(require 'cl)
(cl-defmacro if-exists ((var fname) &rest body)
`(let ((,var ,fname))
(when (file-exists-p ,var)
,@body)))
.file "test.c"
.text
.p2align 4,,15
.globl my_test
.type my_test, @function
my_test:
.LFB11:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
@hraban
hraban / test-O3.s
Created July 4, 2012 23:05
Inlining through namespaces
.file "test.c"
.text
.p2align 4,,15
.globl inline_value_proper
.type inline_value_proper, @function
inline_value_proper:
.LFB7:
.cfi_startproc
movl $5000, %eax
ret
@hraban
hraban / prune-spaces.c
Created July 8, 2012 16:52
Example of a function that does not like overlapping buffers
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 256
/* source and destination buffers MUST NOT overlap */
char *
prune_spaces(char * dst, size_t dst_len, const char * src)
#!/usr/bin/env python
class Node(object):
def __init__(self, name, next=None):
self.next = next
self.name = name
self.l = 1 if next is None else next.l + 1
def __iter__(self,):
cur = self
@hraban
hraban / weakrefmainloop.go
Created November 5, 2013 22:53
poc to clean up a mainloop goroutine when the "public pointer" runs out and is garbage collected
package main
import (
"fmt"
"runtime"
"time"
"unsafe"
)
type foo struct {
@hraban
hraban / pobox.php
Created February 12, 2014 15:02
Write-only file uploads for PHP server
<?php
define("UPLOAD_DIR", "/tmp/pobox-uploads/");
define("CODE", "secretcodehere");
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($_POST["code"] !== CODE) {
die("upload denied: illegal code");
}
if (!(is_dir(UPLOAD_DIR) || mkdir(UPLOAD_DIR, 0700))) {