-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTLPointerArray.c
177 lines (144 loc) · 6.14 KB
/
TLPointerArray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* TLPointerArray.c
* Mercatalog
*
* Created by Nathan Vander Wilt on 8/26/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/
#include "TLPointerArray.h"
#pragma mark Custom allocator for callback wrapping
static void* TLArrayAllocateCB(CFIndex allocSize, CFOptionFlags hint, void *info) {
(void)hint;
(void)info;
return malloc(allocSize);
}
static void* TLArrayReallocateCB(void *ptr, CFIndex newsize, CFOptionFlags hint, void *info) {
(void)hint;
(void)info;
if (!ptr || !(newsize>0)) return NULL;
return realloc(ptr, newsize);
}
static void TLArrayDeallocateCB(void *ptr, void *info) {
(void)info;
free(ptr);
}
typedef struct {
TLPointerArrayItemRetainCallback retainItem;
TLPointerArrayItemReleaseCallback releaseItem;
} TLPointerArrayAllocatorInfo;
static TLPointerArrayAllocatorInfo* TLPointerArrayCreateAllocatorInfoRef(TLPointerArrayItemRetainCallback retainCallback,
TLPointerArrayItemReleaseCallback releaseCallback)
{
TLPointerArrayAllocatorInfo* allocatorInfoRef = (TLPointerArrayAllocatorInfo*)malloc(sizeof(TLPointerArrayAllocatorInfo));
allocatorInfoRef->retainItem = retainCallback;
allocatorInfoRef->releaseItem = releaseCallback;
return allocatorInfoRef;
}
static void TLPointerArrayDestroyAllocatorInfoRef(const void* context) {
free((TLPointerArrayAllocatorInfo*)context);
}
static CFAllocatorRef TLPointerArrayCreateAllocatorForCallbacks(TLPointerArrayItemRetainCallback retainCallback,
TLPointerArrayItemReleaseCallback releaseCallback)
{
TLPointerArrayAllocatorInfo* allocatorInfo = TLPointerArrayCreateAllocatorInfoRef(retainCallback, releaseCallback);
CFAllocatorContext context =
{
.version = 0,
.info = allocatorInfo,
.retain = NULL,
.release = TLPointerArrayDestroyAllocatorInfoRef,
.copyDescription = NULL,
.allocate = TLArrayAllocateCB,
.reallocate = TLArrayReallocateCB,
.deallocate = TLArrayDeallocateCB,
.preferredSize = NULL
};
return CFAllocatorCreate(kCFAllocatorDefault, &context);
}
#pragma mark Array callback wrappers
static TLPointerItem TLPointerArrayRetainCallbackWrapper(CFAllocatorRef allocator, TLPointerItem item) {
CFAllocatorContext context;
CFAllocatorGetContext(allocator, &context);
TLPointerArrayAllocatorInfo* allocatorInfoRef = (TLPointerArrayAllocatorInfo*)context.info;
return allocatorInfoRef->retainItem(item);
}
static void TLPointerArrayReleaseCallbackWrapper(CFAllocatorRef allocator, TLPointerItem item) {
CFAllocatorContext context;
CFAllocatorGetContext(allocator, &context);
TLPointerArrayAllocatorInfo* allocatorInfoRef = (TLPointerArrayAllocatorInfo*)context.info;
allocatorInfoRef->releaseItem(item);
}
static inline CFArrayCallBacks TLPointerArrayGetCallbacks() {
CFArrayCallBacks callbacks = {
.version = 0,
.retain = TLPointerArrayRetainCallbackWrapper,
.release = TLPointerArrayReleaseCallbackWrapper,
.copyDescription = NULL,
.equal = NULL
};
return callbacks;
}
#pragma mark Array wrappers
TLPointerArrayRef TLPointerArrayRetain(TLPointerArrayRef array) {
return (TLPointerArrayRef)CFRetain((CFArrayRef)array);
}
void TLPointerArrayRelease(TLPointerArrayRef array) {
if (!array) return;
CFRelease((CFArrayRef)array);
}
TLPointerArrayRef TLPointerArrayCreateCopy(TLPointerArrayRef pointerArray) {
CFAllocatorRef arrayAllocator = CFGetAllocator((CFArrayRef)pointerArray);
return (TLPointerArrayRef)CFArrayCreateCopy(arrayAllocator, (CFArrayRef)pointerArray);
}
tl_uint_t TLPointerArrayGetCount(TLPointerArrayRef pointerArray) {
return CFArrayGetCount((CFArrayRef)pointerArray);
}
TLPointerItem TLPointerArrayGetItemAtIndex(TLPointerArrayRef pointerArray, tl_uint_t itemIndex) {
return CFArrayGetValueAtIndex((CFArrayRef)pointerArray, itemIndex);
}
#pragma mark Mutable array wrappers
TLMutablePointerArrayRef TLPointerArrayCreateMutable(tl_uint_t countLimit,
TLPointerArrayItemRetainCallback retainCallback,
TLPointerArrayItemReleaseCallback releaseCallback)
{
if (countLimit > TLCFIndexMax) return NULL;
CFAllocatorRef arrayAllocator = TLPointerArrayCreateAllocatorForCallbacks(retainCallback, releaseCallback);
CFArrayCallBacks callbacks = TLPointerArrayGetCallbacks();
CFMutableArrayRef mutableArray = CFArrayCreateMutable(arrayAllocator, countLimit, &callbacks);
CFRelease(arrayAllocator);
return (TLMutablePointerArrayRef)mutableArray;
}
TLMutablePointerArrayRef TLPointerArrayCreateMutableCopy(TLPointerArrayRef pointerArray, tl_uint_t countLimit) {
if (countLimit > TLCFIndexMax) return NULL;
CFAllocatorRef arrayAllocator = CFGetAllocator((CFArrayRef)pointerArray);
return (TLMutablePointerArrayRef)CFArrayCreateMutableCopy(arrayAllocator, countLimit, (CFArrayRef)pointerArray);
}
void TLPointerArrayAppendItem(TLMutablePointerArrayRef pointerArray, TLPointerItem item) {
CFArrayAppendValue((CFMutableArrayRef)pointerArray, item);
}
void TLPointerArrayAppendArray(TLMutablePointerArrayRef pointerArray, TLPointerArrayRef otherArray) {
CFRange otherRange = CFRangeMake(0, TLPointerArrayGetCount(otherArray));
CFArrayAppendArray((CFMutableArrayRef)pointerArray, (CFArrayRef)otherArray, otherRange);
}
void TLPointerArrayRemoveItemAtIndex(TLMutablePointerArrayRef pointerArray, tl_uint_t itemIndex) {
CFArrayRemoveValueAtIndex((CFMutableArrayRef)pointerArray, itemIndex);
}
typedef struct {
TLPointerArraySortCallback realCallback;
void* realContext;
} TLPointerArrayCompareContext;
static CFIndex TLPointerArraySortCallbackWrapper(TLPointerItem item1, TLPointerItem item2, void* context) {
TLPointerArrayCompareContext* wrappedContext = (TLPointerArrayCompareContext*)context;
TLPointerArraySortCallback compareFunction = wrappedContext->realCallback;
void* compareContext = wrappedContext->realContext;
return (CFIndex)compareFunction(item1, item2, compareContext);
}
void TLPointerArraySort(TLMutablePointerArrayRef pointerArray, TLPointerArraySortCallback compareFunction, void* context) {
CFRange sortRange = CFRangeMake(0, TLPointerArrayGetCount(pointerArray));
TLPointerArrayCompareContext wrappedContext = {
.realCallback = compareFunction,
.realContext = context
};
CFArraySortValues((CFMutableArrayRef)pointerArray, sortRange, TLPointerArraySortCallbackWrapper, &wrappedContext);
}