Skip to content

Instantly share code, notes, and snippets.

@mikeash
mikeash / gist:1267596
Created October 6, 2011 14:52
ARC and objc_getClassList do not get along
// clang -framework Foundation -fobjc-arc test.m
#import <objc/runtime.h>
#import <stdio.h>
#import <stdlib.h>
int main(int argc, char **argv)
{
@autoreleasepool
objc[3120]: Do not call -_isDeallocating.
(gdb) bt
#0 0x31e57048 in _objc_trap ()
#1 0x31e570ac in _objc_fatal ()
#2 0x31e570ac in _objc_fatal ()
#3 0x31e608a2 in _objc_rootIsDeallocating ()
#4 0x33608754 in -[NSProxy _isDeallocating] ()
#5 0x3360878c in -[NSProxy allowsWeakReference] ()
@mikeash
mikeash / gist:1290455
Created October 16, 2011 03:05
hash-based lookup table for classes that don't support native ZWR
void *_MAZeroingWeakRefClassPresentToken = &_MAZeroingWeakRefClassPresentToken;
void *_MAZeroingWeakRefClassNativeWeakReferenceNotAllowedTable[256] = {
[0x3] = (void *[256]) { // 1
[0x5b] = (void *[256]) { // 2
[0x81] = (void *[256]) { // 3
[0x42] = (void *[256]) { // 4
[0xa2] = (void *[256]) { // 5
[0xe] = (void *[256]) { // 6
[0x20] = (void *[256]) { // 7
[0xd7] = (void *[256]) { // 8
@mikeash
mikeash / blockforward.m
Created October 21, 2011 02:38
NSInvocation works for blocks too!
#import <dlfcn.h>
#import <Foundation/Foundation.h>
struct BlockDescriptor
{
unsigned long reserved;
unsigned long size;
void *rest[1];
@mikeash
mikeash / gist:1355671
Created November 10, 2011 18:28
Multiple return
// clang -W -Wall -Wno-unused-parameter -framework Foundation -fobjc-arc test.m
#import <Foundation/Foundation.h>
#define IDARRAY(...) ((id[]){ __VA_ARGS__ })
#define IDCOUNT(...) (sizeof(IDARRAY(__VA_ARGS__)) / sizeof(id))
typedef id (^Tuple)(int);
@mikeash
mikeash / switcher.ino
Created December 24, 2011 01:18
Arduino driver for automatic audio switch
int inPins[2] = { 0, 1 };
int lastValues[2] = { 0, 0 };
float averages[2] = { 0, 0 };
float delta = 0.0;
float decay = 0.02;
int outPin = 12;
@mikeash
mikeash / gist:1522021
Created December 26, 2011 20:03
Randomness
// generator returns values in range [0, 1)
typedef double (^CWRandomGenerator)(void);
CWRandomGenerator CWMakeRandomGenerator(void);
// generate a normally-distributed number with the given mean and standard deviation
// see http://en.wikipedia.org/wiki/Normal_distribution
double CWNormalDistribution(CWRandomGenerator rnd, double mean, double stddev);
@mikeash
mikeash / gist:1522029
Created December 26, 2011 20:11
Units
//
// CWUnits.h
// cow
//
// Created by Michael Ash on 4/19/11.
// Copyright 2011 Two Pastry Eaters. All rights reserved.
//
#import <Foundation/Foundation.h>
@mikeash
mikeash / gist:1571661
Created January 6, 2012 17:58
mirrored ring buffer allocator
#include <mach/mach.h>
#include <mach/vm_map.h>
#include <stdio.h>
#include <stdlib.h>
void *allocate_pair(size_t howmuch)
{
#define CHECK_ERR do { \
@mikeash
mikeash / gist:1667482
Created January 24, 2012 02:48
lisp.m
// clang -framework Foundation lisp.m
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
@interface Node : NSObject
- (id)evaluate: (NSArray *)arguments;
- (id)eval;
@end