Skip to content

Commit

Permalink
Few updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirek Rusin authored and Mirek Rusin committed Feb 16, 2011
1 parent c0cd916 commit fb6fc15
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
41 changes: 38 additions & 3 deletions CoreJSON/CoreJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ inline __JSONStackEntryRef __JSONStackEntryCreate(CFAllocatorRef allocator, CFIn
__JSONStackEntryRef entry = CFAllocatorAllocate(allocator, sizeof(__JSONStackEntry), 0);
if (entry) {
entry->allocator = allocator;
entry->retainCount = 1;
entry->index = index;
entry->valuesIndex = 0;
if ((entry->valuesLength = valuesLength))
Expand All @@ -67,11 +68,12 @@ inline __JSONStackEntryRef __JSONStackEntryRetain(__JSONStackEntryRef entry) {
}

inline CFIndex __JSONStackEntryRelease(__JSONStackEntryRef entry) {
// TODO: Deallocate stack entry arrays
return --entry->retainCount;
}

inline __JSONStackEntryRef __JSONStackEntryReleaseRef(__JSONStackEntryRef *entry) {
if (__JSONStackEntryRelease(*entry))
if (0 == __JSONStackEntryRelease(*entry))
entry = NULL;
return *entry;
}
Expand All @@ -90,6 +92,7 @@ inline __JSONStackRef __JSONStackCreate(CFAllocatorRef allocator, CFIndex maxDep
__JSONStackRef stack = CFAllocatorAllocate(allocator, sizeof(__JSONStack), 0);
if (stack) {
stack->allocator = allocator;
stack->retainCount = 1;
stack->size = maxDepth;
stack->index = 0;
stack->stack = CFAllocatorAllocate(stack->allocator, sizeof(__JSONStackEntryRef) * stack->size, 0);
Expand All @@ -98,6 +101,11 @@ inline __JSONStackRef __JSONStackCreate(CFAllocatorRef allocator, CFIndex maxDep
return stack;
}

inline CFIndex __JSONStackRelease(__JSONStackRef stack) {
// TODO: Stack deallocator
return --stack->retainCount;
}

inline __JSONStackEntryRef __JSONStackGetTop(__JSONStackRef stack) {
if (stack->index > 0)
return stack->stack[stack->index - 1];
Expand All @@ -112,7 +120,7 @@ inline bool __JSONStackPush(__JSONStackRef stack, __JSONStackEntryRef entry) {
stack->stack[stack->index++] = __JSONStackEntryRetain(entry);
success = 1;
} else {
// TODO: Out of bounds
// TODO: Out of bounds, reallocate more space
}
}
return success;
Expand All @@ -121,10 +129,11 @@ inline bool __JSONStackPush(__JSONStackRef stack, __JSONStackEntryRef entry) {
inline __JSONStackEntryRef __JSONStackPop(__JSONStackRef stack) {
__JSONStackEntryRef entry = NULL;
if (stack) {
// TODO: __JSONStackEntryRelease(stack->stack[stack->index])
if (--stack->index >= 0) {
entry = stack->stack[stack->index];
} else {
// TODO: Out of bounds
// TODO: Out of bounds, this is error, should never happen
}
}
return entry;
Expand Down Expand Up @@ -318,6 +327,32 @@ inline CoreJSONRef JSONCreate(CFAllocatorRef allocator) {
return json;
}

inline CFIndex JSONRelease(CoreJSONRef json) {
CFIndex retainCount = -1;
if (json) {
if (json->retainCount > 0) {
if ((retainCount = --json->retainCount) == 0) {
CFAllocatorRef allocator = json->allocator;

yajl_free(json->yajlParser);
// yajl_free(json->yajlGenerator);

CFRelease(json->elements);
__JSONStackRelease(json->stack);

CFAllocatorDeallocate(allocator, json);
}
}
}
return retainCount;
}

inline CoreJSONRef JSONReleaseRef(CoreJSONRef *json) {
if (0 == JSONRelease(*json))
json = NULL;
return *json;
}

inline void JSONParseWithString(CoreJSONRef json, CFStringRef string) {

// Let's make sure we've got a clean plate first
Expand Down
28 changes: 16 additions & 12 deletions CoreJSON/CoreJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ void __JSONUTF8StringDestroy (__JSONUTF8String utf8String
// maps by a single create call.
typedef struct {
CFAllocatorRef allocator;
CFIndex retainCount;
CFIndex retainCount;

CFIndex index;
CFIndex index;

CFTypeRef *values;
CFIndex valuesIndex;
CFIndex valuesLength;
CFTypeRef *values;
CFIndex valuesIndex;
CFIndex valuesLength;

CFTypeRef *keys;
CFIndex keysIndex;
CFIndex keysLength;
CFTypeRef *keys;
CFIndex keysIndex;
CFIndex keysLength;
} __JSONStackEntry;

typedef __JSONStackEntry *__JSONStackEntryRef;
Expand All @@ -88,6 +88,7 @@ void __JSONStackEntryAppendKey (__JSONStackEntryRef entry, CFTy

typedef struct {
CFAllocatorRef allocator;
CFIndex retainCount;
__JSONStackEntryRef *stack;
CFIndex index;
CFIndex size;
Expand All @@ -96,6 +97,7 @@ typedef struct {
typedef __JSONStack *__JSONStackRef;

__JSONStackRef __JSONStackCreate (CFAllocatorRef allocator, CFIndex maxDepth);
CFIndex __JSONStackRelease (__JSONStackRef stack);
__JSONStackEntryRef __JSONStackGetTop (__JSONStackRef stack);
bool __JSONStackPush (__JSONStackRef stack, __JSONStackEntryRef entry);
__JSONStackEntryRef __JSONStackPop (__JSONStackRef stack);
Expand All @@ -118,8 +120,8 @@ int __JSONParserAppendArrayEnd (void *context);
#pragma Public API

typedef struct {
CFAllocatorRef allocator;
CFIndex retainCount;
CFAllocatorRef allocator;
CFIndex retainCount;

yajl_handle yajlParser;
yajl_parser_config yajlParserConfig;
Expand All @@ -139,5 +141,7 @@ typedef CoreJSON *CoreJSONRef;

extern CoreJSONRef JSONCreate (CFAllocatorRef allocator);
extern CoreJSONRef JSONCreateWithString (CFAllocatorRef allocator, CFStringRef string);
extern void JSONParseWithString (CoreJSONRef json, CFStringRef string);
extern CFTypeRef JSONGetObject (CoreJSONRef json);
extern void JSONParseWithString (CoreJSONRef json, CFStringRef string);
extern CFTypeRef JSONGetObject (CoreJSONRef json);
extern CFIndex JSONRelease (CoreJSONRef json);
inline CoreJSONRef JSONReleaseRef (CoreJSONRef *json);

0 comments on commit fb6fc15

Please sign in to comment.