Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created November 4, 2011 21:51
Show Gist options
  • Save slembcke/1340578 to your computer and use it in GitHub Desktop.
Save slembcke/1340578 to your computer and use it in GitHub Desktop.
#if __has_feature(objc_arc)
typedef __strong id CCARRAY_ID;
#define CCARRAY_RETAIN(obj) obj
#define CCARRAY_RELEASE(obj) (void)obj
#else
typedef id CCARRAY_ID;
#define CCARRAY_RETAIN(obj) [obj retain]
#define CCARRAY_RELEASE(obj) [obj release]
#endif
typedef struct ccArray {
NSUInteger num, max;
CCARRAY_ID *arr;
} ccArray;
/** Allocates and initializes a new array with specified capacity */
static inline ccArray* ccArrayNew(NSUInteger capacity) {
if (capacity == 0)
capacity = 1;
ccArray *arr = (ccArray*)malloc( sizeof(ccArray) );
arr->num = 0;
arr->arr = (CCARRAY_ID *) malloc( capacity * sizeof(id) );
arr->max = capacity;
return arr;
}
/** Removes all objects from arr */
static inline void ccArrayRemoveAllObjects(ccArray *arr)
{
while( arr->num > 0 )
CCARRAY_RELEASE(arr->arr[--arr->num]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment