Skip to content

Instantly share code, notes, and snippets.

/*
* Copyright (c) 2000-2016 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
@joshlf
joshlf / lib.rs
Created August 9, 2017 20:24
malloc_bind crate
//! Bindings for the C `malloc` API to Rust allocators.
//!
//! This crate provides a mechanism to construct a C allocator - an implementation of `malloc`,
//! `free`, and related functions - that is backed by a Rust allocator (an implementation of the
//! `Alloc` trait).
//!
//! In order to create bindings, two things must be provided: an implementation of the `Alloc`
//! trait, and an implementation of the `LayoutFinder` trait (defined in this crate). Since the C
//! API does not provide size or alignment on `free`, but the Rust `Alloc` API requires both size
//! and alignment on `dealloc`, a mapping must be maintained between allocated objects and those
@joshlf
joshlf / doer.go
Created October 2, 2016 17:05
A demonstration of a self-cleaning worker
// This is an example of a self-cleaning worker. A common pattern in Go
// is to have a type which, when created with NewX, spawns one or more
// goroutines in the background to perform work. It is usually the
// caller's responsibility to call Close() or Stop() in order to shut
// these workers down and not leak goroutines and the memory resources
// that the goroutines have references to.
//
// The idea behind a self-cleaning worker is to leverage the runtime's
// ability to set finalizers on objects in order to detect when an object
// with a still-live worker goroutine has gone out of scope. The type
@joshlf
joshlf / binary.go
Created August 22, 2016 23:14
Bug when building source with binary-only-package
// +build !build_source
//go:binary-only-package
package bug
package sort
import (
"math/rand"
"sort"
"testing"
)
func TestSort(t *testing.T) {
for i := 0; i < 100; i++ {
package sort
import (
"fmt"
"reflect"
"sort"
"sync"
"unsafe"
)
@joshlf
joshlf / udp-stream.go
Created August 28, 2014 00:48
UDP Streaming at 16 kbps
package main
import (
"fmt"
"io"
"net"
"os"
"github.com/synful/rate"
)
@joshlf
joshlf / loop.c
Created August 15, 2014 11:43
Infinite loops.
int main() {
long *i;
long arr[64];
for (i = arr; i < arr + (64 * sizeof(arr)); i++) {
*i = (long)arr;
}
}
@joshlf
joshlf / instance.go
Created August 6, 2014 09:35
Randomly generate instances of arbitrary types
/*
The *Rand type here is essentially equivalent to the standard library's
math/rand.Rand, albeit with a few additions for types (to randomly
generate strings, bools, etc).
*/
package rand
import (
"reflect"
@joshlf
joshlf / reflection.go
Created January 5, 2014 20:17
"go build" can be passed linker flags. If the "-s" flag is passed to remove debugging information, runtime reflection can break. Try building with 'go build' and with 'go build -ldflags "-s"' separately.
package main
import (
"fmt"
"reflect"
"runtime"
)
func main() {
fmt.Println(runtime.FuncForPC(reflect.ValueOf(main).Pointer()).Name())