Skip to content

Instantly share code, notes, and snippets.

View kulp's full-sized avatar
💬
ceci n'est pas un statut

Darren Kulp kulp

💬
ceci n'est pas un statut
View GitHub Profile
@kulp
kulp / tree_macro.pl
Created March 30, 2010 22:54
Generates CPP macros for mapping arbitrary integer sets to small integer sets
#!/usr/bin/env perl
use strict;
use List::Util qw(max);
my $name = "b";
# for each significant bit position, where the sequence through the bit
# positions is chosen such that each bit chosen divides the remaining
# search space as nearly evenly as possible, construct an array that
# looks like this:
@mmalex
mmalex / pngencode.cpp
Created April 7, 2011 17:48 — forked from anonymous/pngencode.cpp
bug fix png encoder
// by alex evans, 2011. released into the public domain.
// based on a first ever reading of the png spec, it occurs to me that a minimal png encoder should be quite simple.
// this is a first stab - may be buggy! the only external dependency is zlib and some basic typedefs (u32, u8)
//
// VERSION 0.02! now using zlib's crc rather than my own, and avoiding a memcpy and memory scribbler in the old one
// by passing the zero byte at the start of the scanline to zlib first, then the original scanline in place. WIN!
//
// more context at http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
//
// follow me on twitter @mmalex http://twitter.com/mmalex
@kulp
kulp / kv_int.h
Created April 7, 2011 18:20
A lightweight integer-keyed key-value store
/**
* @file
* Provides a lightweight integer-keyed key-value store.
*
* The key must be an integral type (default @c int), but can be any integral
* type if @c KV_KEY_TYPE is defined before @c #include'ing this file.
*/
#ifndef KV_INT_H_
#define KV_INT_H_
@kulp
kulp / pmvg.c
Created September 21, 2011 23:57
Poor man's valgrind
// "Poor man's valgrind"
#include "pmvg.h"
#include <stdlib.h>
#include <search.h>
static void *track_tree;
struct track_pair {
void *addr; size_t size;
};
static long bytes_lost;
@kulp
kulp / buddy.c
Last active December 21, 2016 02:05
A little buddy allocator
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#define RANK_0_LOG 5
#define RANK_0_SIZE (1ul << RANK_0_LOG)
@kulp
kulp / check_range.c
Created December 12, 2012 20:45
Recursively find the bounding box of mismatched bytes between two arrays
unsigned check_range(const char *bad, const char *good, size_t len, size_t bounds[2])
{
if (len < 2) {
if (len)
return *bad != *good;
return 0;
} else if (memcmp(bad, good, len)) {
size_t subbounds[2][2] = { { 0 } };
int leftbad = check_range(&bad[0 ], &good[0 ], len / 2, subbounds[0]);
int rightbad = check_range(&bad[len / 2], &good[len / 2], len - len / 2, subbounds[1]);
@JohnEarnest
JohnEarnest / Garbage.fs
Created January 13, 2013 03:21
A compact, simple implementation of a garbage-collected cons-pair heap utilizing Cheney's algorithm.
\
\ Garbage.fs
\
\ A compact, simple implementation of a garbage-collected
\ cons-pair heap utilizing Cheney's algorithm.
\ Pointers to pairs are identified by a pattern in the
\ high-order bits of a value, chosen not to collide
\ with the constants "true" or "false".
\
@forestbelton
forestbelton / Env.pm
Last active June 26, 2021 10:49
A basic LISP interpreter in Perl
#!/usr/bin/env perl
use strict;
use warnings;
package Env;
sub new {
my $class = shift;
my $names = shift;
@kulp
kulp / huff_decode.c
Last active December 17, 2015 04:09
Huffman encoder
#include "huffman.h"
int huff_decode(const dict_entry *dict, char *out, const huff_word **in,
size_t *len, size_t *bits, size_t *off)
{
const dict_entry *n = dict;
int found = 0;
while (!found && *len && *off < *bits) {
unsigned char bit = (**in >> *off) & 1;
@kulp
kulp / ansi16_to_ansi256.pl
Last active August 29, 2015 14:01
Map ansi16 colours to ansi256 colours
#!/usr/bin/env perl
# export PAGER="ansi16_to_ansi256.pl | ${PAGER:-less -R}"
use strict;
use Color::ANSI::Util qw(ansi16_to_rgb rgb_to_ansi256);
my $esc = qr/\x1b\[/;
my $fg = qr/3\d/;
my $bg = qr/4\d/;
my $seq = qr/$esc((?:$fg|$bg)(?:;(?:$fg|$bg))*)m/o;