Skip to content

Instantly share code, notes, and snippets.

View pmj's full-sized avatar

Phil Dennis-Jordan pmj

View GitHub Profile
/**
* Runs the given function while locking (synchronising on) the specified object.
*
* This uses Rhino's sync() function, but is a lot more flexible, allowing
* unbound functions and closures. May of course be nested, but beware of deadlocks.
* @param {Object} obj JavaScript object to be locked.
* @param {Function} fn The function to call from within a synchronised block.
* @param [args...] Any number of additional arguments to pass to fn.
* @returns The value returned from calling fn.
*
#include <stdio.h>
class A
{
int answer;
public:
A(int answer) :
answer(answer)
{
printf("%i\n", answer);
// macro for declaring members with C++ types in Objective-C++ classes which should be #import-able by Objective-C
#ifdef __cplusplus
#define CPPCLASS(ns, cname) ns :: cname
#else
#define CPPCLASS(ns, cname) struct CPP_ ## ns ## _ ## cname
#endif
@pmj
pmj / gist:655493
Created October 30, 2010 16:44
memoization with invalidation in clojure
(defn memoize*
"Like clojure.core/memoize, but the returned function's metadata contains an :invalidate function which completely resets the memory in the 0-ary form, or accepts the argument sequence for which to invalidate as its only argument."
[f]
(let [memo (atom {})]
(with-meta
(fn memoized [& args]
(if-let [e (find @memo args)]
(val e)
(let [ret (apply f args)]
(swap! memo assoc args ret)
@pmj
pmj / gist:976273
Created May 17, 2011 10:36
iOS developer program license agreement section 3.3.2
An Application may not download or install executable code. Interpreted code may only
be used in an Application if all scripts, code and interpreters are packaged in the Application and
not downloaded. The only exception to the foregoing is scripts and code downloaded and run by
Apple's built-in WebKit framework.
@pmj
pmj / gist:2660790
Created May 11, 2012 16:31
64-bit * 64-bit -> 128-bit unsigned integer long multiplication
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
struct big64
{
uint32_t v[2]; /* num = v[0] + (v[1] << 32) - "little endian" */
};
typedef struct big64 big64_t;
@pmj
pmj / gist:2944868
Created June 17, 2012 15:36
Safe Objective-C cast with superclass type check
#define SSDC_CAST(TYPE, EXPR) \
({ \
__typeof(EXPR) _cast_expr_tmp = (EXPR); \
__typeof(EXPR) _super_check_ref __attribute__((unused)) = (TYPE*)nil; \
([_cast_expr_tmp isKindOfClass:[TYPE class]]) ? (TYPE*)_cast_expr_tmp : nil; \
})
@pmj
pmj / gist:2944912
Created June 17, 2012 15:46
Super safe container_of macro for structs
/** genc_container_of(obj, cont_type, member_name)
* Get pointer to struct object from a pointer to one of its members.
* obj - pointer to a struct member, or NULL
* cont_type - structure type of the containing object
* member_name - name of the member of cont_type referenced by obj
*
* Similar to Linux's container_of macro, except it also handles NULL pointers
* correctly, which is to say it evaluates to NULL when the obj is NULL. We also
* try to support non-GCC compilers which don't support the ({ }) expression
* syntax.
@pmj
pmj / gist:3075688
Created July 9, 2012 10:34
WIP NSDictionary typed destructuring bind.
// To be used something like this:
SSDC_BIND_DICTIONARY_TYPED(some_dictionary,
NSNumber, count, @"count_key",
NSArray, array_data, kSomeArrayKey,
NSString, a_string, stringkey);
/* this produces the variables count, array_data, and a_string, which
* have the specified static AND dynamic types (i.e. if objectForKey:stringKey
* is not actually an NSString, a_string will contain nil, not the object that
* will throw exceptions when used as a string.
@pmj
pmj / gist:3235684
Created August 2, 2012 09:10
Diffstat of xnu-1699.26.8 (OSX 10.7.4 kernel) and xnu-2050.7.9 (OSX 10.8 kernel)
xnu-1699.26.8/EXTERNAL_HEADERS/mach-o/kld.h |only
xnu-1699.26.8/bsd/crypto/aes |only
xnu-1699.26.8/bsd/crypto/des |only
xnu-1699.26.8/bsd/crypto/sha2 |only
xnu-1699.26.8/bsd/kern/kern_callout.c |only
xnu-1699.26.8/bsd/net/dlil_pvt.h |only
xnu-1699.26.8/bsd/net/pf_mtag.h |only
xnu-1699.26.8/bsd/sys/kern_callout.h |only
xnu-1699.26.8/libkern/crypto/md5.c |only
xnu-1699.26.8/libkern/crypto/sha1.c |only