Skip to content

Instantly share code, notes, and snippets.

@jsimmons
jsimmons / wscript
Created July 22, 2010 10:29
waf example wscripts
#! /usr/bin/env python
# encoding: utf-8
# Joshua Simmons 2010
def configure(conf):
pass
def build(bld):
pong = bld(
features = 'c cprogram',
pub fn load_in_place<'a, T>(bytes: &'a [u8]) -> &'a T {
assert!(!bytes.is_empty());
assert!(validate::<T>(bytes));
unsafe { &*bytes.as_ptr().cast() }
}
#[cfg(test)]
@jsimmons
jsimmons / demo.c
Last active August 7, 2023 03:49
Minimal OpenGL X11 Demo Linux
#include <stdint.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <dlfcn.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <time.h>
@jsimmons
jsimmons / mpmc.c
Created July 9, 2012 02:58
Multiple Producer Multiple Consumer with two mutexes on FIFO
#include <pthread.h>
#include <stdio.h>
#define PRODUCER_COUNT 4
#define CONSUMER_COUNT 4
#define THREAD_COUNT (PRODUCER_COUNT + CONSUMER_COUNT)
#define WORK_ITERS 10000
#define WORK_COUNT 10000
#define QUEUE_SIZE 64
@jsimmons
jsimmons / link_map.c
Last active February 26, 2023 08:22
Linking The Hard Way
@jsimmons
jsimmons / ref_count.rs
Created April 28, 2022 07:49
Upgradable Reference Counting
use std::{
marker::PhantomData,
ops::Deref,
ptr::NonNull,
sync::atomic::{AtomicI32, Ordering},
};
struct Inner<T: ?Sized> {
// Number of strong references in addition to the current value.
// A negative value indicates a non-atomic reference count, counting up from i32::MIN
@jsimmons
jsimmons / frk.rs
Created January 2, 2022 22:30
Upgradable Atomic Ref Counting
/// Flexible Reference Kounting
use std::{
marker::PhantomData,
ops::Deref,
ptr::NonNull,
sync::atomic::{AtomicI32, Ordering},
};
struct Inner<T: ?Sized> {
// Number of strong references in addition to the current value.
@jsimmons
jsimmons / main.rs
Created April 3, 2021 09:51
Upgradable Rc / Arc
mod rc;
use rc::{BoxArc, BoxRc};
struct Widget {
value: i32,
}
impl Widget {
fn new(value: i32) -> Self {
@jsimmons
jsimmons / spsc.c
Created July 9, 2012 01:08
Single producer single consumer lock-free on FIFO
#include <pthread.h>
#include <stdio.h>
#define PRODUCER_COUNT 1
#define CONSUMER_COUNT 1
#define THREAD_COUNT (PRODUCER_COUNT + CONSUMER_COUNT)
#define QUEUE_SIZE 64
// Let's go the easy way and keep a gap between head and tail when full.
@jsimmons
jsimmons / ring-buffer.c
Created October 4, 2010 13:19
ringbuffer implementation
#include "ring-buffer.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
RingBuffer *rb_new(size_t size)
{
RingBuffer *buf = malloc(sizeof(*buf));
assert(buf != NULL);