︙ | | |
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
-
+
|
* configure determined it is broken. In this case, we must make sure there is
* no name clash.
*/
# define asprintf asprintf_
# define vasprintf vasprintf_
#endif
struct context {
struct Context {
const char *format;
size_t formatLen;
char subformat[maxSubformatLen + 1];
size_t subformatLen;
va_list arguments;
char *buffer;
size_t bufferLen;
|
︙ | | |
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
-
+
-
+
-
+
-
+
|
va_end(arguments);
return ret;
}
#endif
static bool
appendString(struct context *ctx, const char *append, size_t appendLen)
appendString(struct Context *ctx, const char *append, size_t appendLen)
{
char *newBuf;
if (appendLen == 0)
return true;
if ((newBuf = realloc(ctx->buffer,
ctx->bufferLen + appendLen + 1)) == NULL)
return false;
memcpy(newBuf + ctx->bufferLen, append, appendLen);
ctx->buffer = newBuf;
ctx->bufferLen += appendLen;
return true;
}
static bool
appendSubformat(struct context *ctx, const char *subformat,
appendSubformat(struct Context *ctx, const char *subformat,
size_t subformatLen)
{
if (ctx->subformatLen + subformatLen > maxSubformatLen)
return false;
memcpy(ctx->subformat + ctx->subformatLen, subformat, subformatLen);
ctx->subformatLen += subformatLen;
ctx->subformat[ctx->subformatLen] = 0;
return true;
}
static bool
stringState(struct context *ctx)
stringState(struct Context *ctx)
{
if (ctx->format[ctx->i] == '%') {
if (ctx->i > 0)
if (!appendString(ctx, ctx->format + ctx->last,
ctx->i - ctx->last))
return false;
if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
return false;
ctx->last = ctx->i + 1;
ctx->state = stateFormatFlags;
}
return true;
}
static bool
formatFlagsState(struct context *ctx)
formatFlagsState(struct Context *ctx)
{
switch (ctx->format[ctx->i]) {
case '-':
case '+':
case ' ':
case '#':
case '0':
|
︙ | | |
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
-
+
-
+
|
break;
}
return true;
}
static bool
formatFieldWidthState(struct context *ctx)
formatFieldWidthState(struct Context *ctx)
{
if ((ctx->format[ctx->i] >= '0' && ctx->format[ctx->i] <= '9') ||
ctx->format[ctx->i] == '*' || ctx->format[ctx->i] == '.') {
if (!appendSubformat(ctx, ctx->format + ctx->i, 1))
return false;
} else {
ctx->state = stateFormatLengthModifier;
ctx->i--;
}
return true;
}
static bool
formatLengthModifierState(struct context *ctx)
formatLengthModifierState(struct Context *ctx)
{
/* Only one allowed */
switch (ctx->format[ctx->i]) {
case 'h': /* and also hh */
if (ctx->formatLen > ctx->i + 1 &&
ctx->format[ctx->i + 1] == 'h') {
if (!appendSubformat(ctx, ctx->format + ctx->i, 2))
|
︙ | | |
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
-
+
|
}
ctx->state = stateFormatConversionSpecifier;
return true;
}
static bool
formatConversionSpecifierState(struct context *ctx)
formatConversionSpecifierState(struct Context *ctx)
{
char *tmp = NULL;
int tmpLen = 0;
#ifndef HAVE_ASPRINTF_L
OFString *point;
#endif
|
︙ | | |
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
-
+
-
+
|
ctx->last = ctx->i + 1;
ctx->state = stateString;
return true;
}
static bool (*states[])(struct context *) = {
static bool (*states[])(struct Context *) = {
stringState,
formatFlagsState,
formatFieldWidthState,
formatLengthModifierState,
formatConversionSpecifierState
};
int
OFVASPrintF(char **string, const char *format, va_list arguments)
{
struct context ctx;
struct Context ctx;
ctx.format = format;
ctx.formatLen = strlen(format);
memset(ctx.subformat, 0, maxSubformatLen + 1);
ctx.subformatLen = 0;
va_copy(ctx.arguments, arguments);
ctx.bufferLen = 0;
|
︙ | | |
︙ | | |
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
|
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
|
-
-
+
+
-
-
-
+
+
+
-
-
-
+
+
+
|
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#if defined(HAVE_MMAP) && defined(HAVE_MLOCK) && defined(MAP_ANON)
static const size_t chunkSize = 16;
struct page {
struct page *next, *previous;
struct Page {
struct Page *next, *previous;
void *map;
unsigned char *page;
};
# if defined(OF_HAVE_COMPILER_TLS)
static thread_local struct page *firstPage = NULL;
static thread_local struct page *lastPage = NULL;
static thread_local struct page **preallocatedPages = NULL;
static thread_local struct Page *firstPage = NULL;
static thread_local struct Page *lastPage = NULL;
static thread_local struct Page **preallocatedPages = NULL;
static thread_local size_t numPreallocatedPages = 0;
# elif defined(OF_HAVE_THREADS)
static OFTLSKey firstPageKey, lastPageKey;
static OFTLSKey preallocatedPagesKey, numPreallocatedPagesKey;
# else
static struct page *firstPage = NULL;
static struct page *lastPage = NULL;
static struct page **preallocatedPages = NULL;
static struct Page *firstPage = NULL;
static struct Page *lastPage = NULL;
static struct Page **preallocatedPages = NULL;
static size_t numPreallocatedPages = 0;
# endif
static void *
mapPages(size_t numPages)
{
size_t pageSize = [OFSystemInfo pageSize];
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
if (numPages > SIZE_MAX / pageSize)
@throw [OFOutOfRangeException exception];
munlock(pointer, numPages * pageSize);
munmap(pointer, numPages * pageSize);
}
static struct page *
static struct Page *
addPage(bool allowPreallocated)
{
size_t pageSize = [OFSystemInfo pageSize];
size_t mapSize = OFRoundUpToPowerOf2(CHAR_BIT, pageSize / chunkSize) /
CHAR_BIT;
struct page *page;
struct Page *page;
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
struct page *lastPage;
struct Page *lastPage;
# endif
if (allowPreallocated) {
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
uintptr_t numPreallocatedPages =
(uintptr_t)OFTLSKeyGet(numPreallocatedPagesKey);
# endif
if (numPreallocatedPages > 0) {
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
struct page **preallocatedPages =
struct Page **preallocatedPages =
OFTLSKeyGet(preallocatedPagesKey);
# endif
numPreallocatedPages--;
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
OFEnsure(OFTLSKeySet(numPreallocatedPagesKey,
(void *)numPreallocatedPages) == 0);
|
︙ | | |
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
-
+
|
OFEnsure(OFTLSKeySet(firstPageKey, page) == 0);
# endif
return page;
}
static void
removePageIfEmpty(struct page *page)
removePageIfEmpty(struct Page *page)
{
unsigned char *map = page->map;
size_t pageSize = [OFSystemInfo pageSize];
size_t mapSize = OFRoundUpToPowerOf2(CHAR_BIT, pageSize / chunkSize) /
CHAR_BIT;
for (size_t i = 0; i < mapSize; i++)
|
︙ | | |
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
-
+
|
OFEnsure(OFTLSKeySet(lastPageKey, page->previous) == 0);
# endif
OFFreeMemory(page);
}
static void *
allocateMemory(struct page *page, size_t bytes)
allocateMemory(struct Page *page, size_t bytes)
{
size_t chunks, chunksLeft, pageSize, i, firstChunk;
bytes = OFRoundUpToPowerOf2(chunkSize, bytes);
chunks = chunksLeft = bytes / chunkSize;
firstChunk = 0;
pageSize = [OFSystemInfo pageSize];
|
︙ | | |
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
-
+
|
return page->page + (chunkSize * firstChunk);
}
return NULL;
}
static void
freeMemory(struct page *page, void *pointer, size_t bytes)
freeMemory(struct Page *page, void *pointer, size_t bytes)
{
size_t chunks, chunkIndex;
bytes = OFRoundUpToPowerOf2(chunkSize, bytes);
chunks = bytes / chunkSize;
chunkIndex = ((uintptr_t)pointer - (uintptr_t)page->page) / chunkSize;
|
︙ | | |
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
-
+
-
+
|
+ (void)preallocateUnswappableMemoryWithSize: (size_t)size
{
#if defined(HAVE_MMAP) && defined(HAVE_MLOCK) && defined(MAP_ANON)
size_t pageSize = [OFSystemInfo pageSize];
size_t numPages = OFRoundUpToPowerOf2(pageSize, size) / pageSize;
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
struct page **preallocatedPages = OFTLSKeyGet(preallocatedPagesKey);
struct Page **preallocatedPages = OFTLSKeyGet(preallocatedPagesKey);
size_t numPreallocatedPages;
# endif
size_t i;
if (preallocatedPages != NULL)
@throw [OFInvalidArgumentException exception];
preallocatedPages = OFAllocZeroedMemory(numPages, sizeof(struct page));
preallocatedPages = OFAllocZeroedMemory(numPages, sizeof(struct Page));
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
OFEnsure(OFTLSKeySet(preallocatedPagesKey, preallocatedPages) == 0);
# endif
@try {
for (i = 0; i < numPages; i++)
preallocatedPages[i] = addPage(false);
|
︙ | | |
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
-
+
-
+
|
memset(_items, 0, count * itemSize);
#if defined(HAVE_MMAP) && defined(HAVE_MLOCK) && defined(MAP_ANON)
} else if (count * itemSize >= pageSize)
_items = mapPages(OFRoundUpToPowerOf2(pageSize,
count * itemSize) / pageSize);
else {
# if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
struct page *lastPage = OFTLSKeyGet(lastPageKey);
struct Page *lastPage = OFTLSKeyGet(lastPageKey);
# endif
for (struct page *page = lastPage; page != NULL;
for (struct Page *page = lastPage; page != NULL;
page = page->previous) {
_items = allocateMemory(page, count * itemSize);
if (_items != NULL) {
_page = page;
break;
}
|
︙ | | |