-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstatic_string.c
More file actions
593 lines (523 loc) · 17.2 KB
/
Copy pathstatic_string.c
File metadata and controls
593 lines (523 loc) · 17.2 KB
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
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
212
213
214
215
216
217
218
219
220
221
222
223
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "static_string.h"
#include <stdint.h>
#include <string.h>
#if NPY_BYTE_ORDER == NPY_LITTLE_ENDIAN
// the high byte in vstring.size is reserved for flags
// SSSS SSSF
typedef struct _npy_static_vstring_t {
size_t offset;
size_t size_and_flags;
} _npy_static_vstring_t;
typedef struct _short_string_buffer {
char buf[sizeof(_npy_static_vstring_t) - 1];
unsigned char size_and_flags;
} _short_string_buffer;
#elif NPY_BYTE_ORDER == NPY_BIG_ENDIAN
// the high byte in vstring.size is reserved for flags
// FSSS SSSS
typedef struct _npy_static_vstring_t {
size_t size;
size_t offset;
} _npy_static_vstring_t;
typedef struct _short_string_buffer {
unsigned char size_and_flags;
char buf[sizeof(_npy_static_vstring_t) - 1];
} _short_string_buffer;
#endif
typedef union _npy_static_string_u {
_npy_static_vstring_t vstring;
_short_string_buffer direct_buffer;
} _npy_static_string_u;;
#define NPY_STRING_MISSING 0x80 // 1000 0000
#define NPY_STRING_SHORT 0x40 // 0100 0000
#define NPY_STRING_ARENA_FREED 0x20 // 0010 0000
#define NPY_STRING_ON_HEAP 0x10 // 0001 0000
#define NPY_STRING_MEDIUM 0x08 // 0000 1000
#define NPY_STRING_FLAG_MASK 0xF8 // 1111 1000
// short string sizes fit in a 4-bit integer
#define NPY_SHORT_STRING_SIZE_MASK 0x0F // 0000 1111
#define NPY_SHORT_STRING_MAX_SIZE \
(sizeof(_npy_static_string) - 1) // 15 or 7 depending on arch
#define NPY_MEDIUM_STRING_MAX_SIZE 0xFF // 256
// Since this has no flags set, technically this is a heap-allocated string
// with size zero. Practically, that doesn't matter because we always do size
// checks before accessing heap data, but that may be confusing. The nice part
// of this choice is a calloc'd array buffer (e.g. from np.empty) is filled
// with empty elements for free
const _npy_static_string_u empty_string_u = {
.direct_buffer = {.size_and_flags = 0, .buf = {0}}};
#define VSTRING_FLAGS(string) \
string->direct_buffer.size_and_flags & ~NPY_SHORT_STRING_SIZE_MASK;
#define HIGH_BYTE_MASK ((size_t)0XFF << 8 * (sizeof(size_t) - 1))
#define VSTRING_SIZE(string) (string->vstring.size_and_flags & ~HIGH_BYTE_MASK)
typedef struct npy_string_arena {
size_t cursor;
size_t size;
char *buffer;
} npy_string_arena;
struct npy_string_allocator {
npy_string_malloc_func malloc;
npy_string_free_func free;
npy_string_realloc_func realloc;
npy_string_arena arena;
};
void
set_vstring_size(_npy_static_string_u *str, size_t size)
{
unsigned char current_flags = str->direct_buffer.size_and_flags;
str->vstring.size_and_flags = size;
str->direct_buffer.size_and_flags = current_flags;
}
char *
vstring_buffer(npy_string_arena *arena, _npy_static_string_u *string)
{
char flags = VSTRING_FLAGS(string);
if (flags & NPY_STRING_ON_HEAP) {
return (char *)string->vstring.offset;
}
if (arena->buffer == NULL) {
return NULL;
}
return (char *)((size_t)arena->buffer + string->vstring.offset);
}
#define ARENA_EXPAND_FACTOR 1.25
char *
arena_malloc(npy_string_arena *arena, npy_string_realloc_func r, size_t size)
{
// one extra size_t to store the size of the allocation
size_t string_storage_size;
if (size <= NPY_MEDIUM_STRING_MAX_SIZE) {
string_storage_size = size + sizeof(unsigned char);
}
else {
string_storage_size = size + sizeof(size_t);
}
if ((arena->size - arena->cursor) <= string_storage_size) {
// realloc the buffer so there is enough room
// first guess is to double the size of the buffer
size_t newsize;
if (arena->size == 0) {
newsize = string_storage_size;
}
else if (((ARENA_EXPAND_FACTOR * arena->size) - arena->cursor) >
string_storage_size) {
newsize = ARENA_EXPAND_FACTOR * arena->size;
}
else {
newsize = arena->size + string_storage_size;
}
if ((arena->cursor + size) >= newsize) {
// need extra room beyond the expansion factor, leave some padding
newsize = ARENA_EXPAND_FACTOR * (arena->cursor + size);
}
// passing a NULL buffer to realloc is the same as malloc
char *newbuf = r(arena->buffer, newsize);
if (newbuf == NULL) {
return NULL;
}
memset(newbuf + arena->cursor, 0, newsize - arena->cursor);
arena->buffer = newbuf;
arena->size = newsize;
}
char *ret;
if (size <= NPY_MEDIUM_STRING_MAX_SIZE) {
unsigned char *size_loc =
(unsigned char *)&arena->buffer[arena->cursor];
*size_loc = size;
ret = &arena->buffer[arena->cursor + sizeof(char)];
}
else {
char *size_ptr = (char *)&arena->buffer[arena->cursor];
memcpy(size_ptr, &size, sizeof(size_t));
ret = &arena->buffer[arena->cursor + sizeof(size_t)];
}
arena->cursor += string_storage_size;
return ret;
}
int
arena_free(npy_string_arena *arena, _npy_static_string_u *str)
{
if (arena->size == 0 && arena->cursor == 0 && arena->buffer == NULL) {
// empty arena, nothing to do
return 0;
}
char *ptr = vstring_buffer(arena, str);
if (ptr == NULL) {
return -1;
}
size_t size = VSTRING_SIZE(str);
uintptr_t buf_start = (uintptr_t)arena->buffer;
uintptr_t ptr_loc = (uintptr_t)ptr;
uintptr_t end_loc = ptr_loc + size;
uintptr_t buf_end = buf_start + arena->size;
if (ptr_loc < buf_start || ptr_loc > buf_end || end_loc > buf_end) {
return -1;
}
memset(ptr, 0, size);
return 0;
}
static npy_string_arena NEW_ARENA = {0, 0, NULL};
npy_string_allocator *
_NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f,
npy_string_realloc_func r)
{
npy_string_allocator *allocator = m(sizeof(npy_string_allocator));
if (allocator == NULL) {
return NULL;
}
allocator->malloc = m;
allocator->free = f;
allocator->realloc = r;
// arena buffer gets allocated in arena_malloc
allocator->arena = NEW_ARENA;
return allocator;
}
void
_NpyString_free_allocator(npy_string_allocator *allocator)
{
npy_string_free_func f = allocator->free;
if (allocator->arena.buffer != NULL) {
f(allocator->arena.buffer);
}
f(allocator);
}
int
is_short_string(const npy_packed_static_string *s)
{
unsigned char high_byte =
((_npy_static_string_u *)s)->direct_buffer.size_and_flags;
int has_short_flag = (high_byte & NPY_STRING_SHORT);
int has_on_heap_flag = (high_byte & NPY_STRING_ON_HEAP);
return has_short_flag && !has_on_heap_flag;
}
int
is_medium_string(const _npy_static_string_u *s)
{
unsigned char high_byte = s->direct_buffer.size_and_flags;
int has_short_flag = (high_byte & NPY_STRING_SHORT);
int has_medium_flag = (high_byte & NPY_STRING_MEDIUM);
return (!has_short_flag && has_medium_flag);
}
int
_NpyString_isnull(const npy_packed_static_string *s)
{
unsigned char high_byte =
((_npy_static_string_u *)s)->direct_buffer.size_and_flags;
return (high_byte & NPY_STRING_MISSING) == NPY_STRING_MISSING;
}
int
is_not_a_vstring(const npy_packed_static_string *s)
{
return is_short_string(s) || _NpyString_isnull(s);
}
int
is_a_vstring(const npy_packed_static_string *s)
{
return !is_not_a_vstring(s);
}
int
_NpyString_load(npy_string_allocator *allocator,
const npy_packed_static_string *packed_string,
_npy_static_string *unpacked_string)
{
if (_NpyString_isnull(packed_string)) {
unpacked_string->size = 0;
unpacked_string->buf = NULL;
return 1;
}
_npy_static_string_u *string_u = (_npy_static_string_u *)packed_string;
if (is_short_string(packed_string)) {
unsigned char high_byte = string_u->direct_buffer.size_and_flags;
unpacked_string->size = high_byte & NPY_SHORT_STRING_SIZE_MASK;
unpacked_string->buf = string_u->direct_buffer.buf;
}
else {
size_t size = VSTRING_SIZE(string_u);
char *buf = NULL;
if (size > 0) {
npy_string_arena *arena = &allocator->arena;
if (arena == NULL) {
return -1;
}
buf = vstring_buffer(arena, string_u);
if (buf == NULL) {
return -1;
}
}
unpacked_string->size = size;
unpacked_string->buf = buf;
}
return 0;
}
char *
heap_or_arena_allocate(npy_string_allocator *allocator,
_npy_static_string_u *to_init_u, size_t size,
int *on_heap)
{
unsigned char *flags = &to_init_u->direct_buffer.size_and_flags;
if (*flags & NPY_STRING_SHORT) {
// Have to heap allocate since there isn't a preexisting
// allocation. This leaves the NPY_STRING_SHORT flag set to indicate
// that there is no room in the arena buffer for strings in this
// entry
*flags |= NPY_STRING_ON_HEAP;
*on_heap = 1;
return allocator->malloc(sizeof(char) * size);
}
npy_string_arena *arena = &allocator->arena;
if (arena == NULL) {
return NULL;
}
if (*flags & NPY_STRING_ARENA_FREED) {
// Check if there's room for the new string in the existing
// allocation. The size is stored one size_t "behind" the beginning of
// the allocation.
char *buf = vstring_buffer(arena, to_init_u);
if (buf == NULL) {
return NULL;
}
size_t alloc_size;
if (is_medium_string(to_init_u)) {
// stored in a char so direct access is OK
alloc_size = (size_t) * (buf - 1);
}
else {
// not necessarily memory-aligned, so need to use memcpy
size_t *size_loc = (size_t *)((uintptr_t)buf - sizeof(size_t));
memcpy(&alloc_size, size_loc, sizeof(size_t));
}
if (size <= alloc_size) {
// we have room!
*flags &= ~NPY_STRING_ARENA_FREED;
return buf;
}
else {
// No room, resort to a heap allocation.
*flags = NPY_STRING_ON_HEAP;
*on_heap = 1;
return allocator->malloc(sizeof(char) * size);
}
}
// string isn't previously allocated, so add to existing arena allocation
char *ret = arena_malloc(arena, allocator->realloc, sizeof(char) * size);
if (size < NPY_MEDIUM_STRING_MAX_SIZE) {
*flags |= NPY_STRING_MEDIUM;
}
return ret;
}
int
heap_or_arena_deallocate(npy_string_allocator *allocator,
_npy_static_string_u *str_u)
{
unsigned char *flags = &str_u->direct_buffer.size_and_flags;
if (*flags & NPY_STRING_ON_HEAP) {
// It's a heap string (not in the arena buffer) so it needs to be
// deallocated with free(). For heap strings the offset is a raw
// address so this cast is safe.
allocator->free((char *)str_u->vstring.offset);
if (*flags & NPY_STRING_SHORT) {
*flags = 0 | NPY_STRING_SHORT;
}
else {
*flags &= ~NPY_STRING_ON_HEAP;
}
}
else if (VSTRING_SIZE(str_u) != 0) {
npy_string_arena *arena = &allocator->arena;
if (arena == NULL) {
return -1;
}
if (arena_free(arena, str_u) < 0) {
return -1;
}
if (arena->buffer != NULL) {
str_u->direct_buffer.size_and_flags |= NPY_STRING_ARENA_FREED;
}
}
return 0;
}
int
_NpyString_newsize(const char *init, size_t size,
npy_packed_static_string *to_init,
npy_string_allocator *allocator)
{
if (_NpyString_newemptysize(size, to_init, allocator) < 0) {
return -1;
}
if (size == 0) {
return 0;
}
_npy_static_string_u *to_init_u = ((_npy_static_string_u *)to_init);
char *buf = NULL;
if (size > NPY_SHORT_STRING_MAX_SIZE) {
buf = vstring_buffer(&allocator->arena, to_init_u);
}
else {
buf = to_init_u->direct_buffer.buf;
}
memcpy(buf, init, size);
return 0;
}
int
_NpyString_newemptysize(size_t size, npy_packed_static_string *out,
npy_string_allocator *allocator)
{
if (size > NPY_MAX_STRING_SIZE) {
return -1;
}
_npy_static_string_u *out_u = (_npy_static_string_u *)out;
unsigned char flags =
out_u->direct_buffer.size_and_flags & ~NPY_SHORT_STRING_SIZE_MASK;
if (size == 0) {
memcpy(out_u, &empty_string_u, sizeof(_npy_static_string_u));
out_u->direct_buffer.size_and_flags |= flags;
return 0;
}
if (size > NPY_SHORT_STRING_MAX_SIZE) {
int on_heap = 0;
char *buf = heap_or_arena_allocate(allocator, out_u, size, &on_heap);
if (buf == NULL) {
return -1;
}
if (on_heap) {
out_u->vstring.offset = (size_t)buf;
}
else {
npy_string_arena *arena = &allocator->arena;
if (arena == NULL) {
return -1;
}
out_u->vstring.offset = (size_t)buf - (size_t)arena->buffer;
}
set_vstring_size(out_u, size);
}
else {
// Size can be no larger than 7 or 15, depending on CPU architecture.
// In either case, the size data is in at most the least significant 4
// bits of the byte so it's safe to | with one of 0x10, 0x20, 0x40, or
// 0x80.
out_u->direct_buffer.size_and_flags = NPY_STRING_SHORT | flags | size;
}
return 0;
}
int
_NpyString_free(npy_packed_static_string *str, npy_string_allocator *allocator)
{
_npy_static_string_u *str_u = (_npy_static_string_u *)str;
if (is_not_a_vstring(str)) {
// zero out, keeping flags
unsigned char *flags = &str_u->direct_buffer.size_and_flags;
unsigned char current_flags = *flags & ~NPY_SHORT_STRING_SIZE_MASK;
memcpy(str_u, &empty_string_u, sizeof(_npy_static_string_u));
*flags |= current_flags;
}
else {
if (VSTRING_SIZE(str_u) == 0) {
// empty string is a vstring but nothing to deallocate
return 0;
}
if (heap_or_arena_deallocate(allocator, str_u) < 0) {
return -1;
}
}
return 0;
}
int
_NpyString_dup(const npy_packed_static_string *in,
npy_packed_static_string *out,
npy_string_allocator *in_allocator,
npy_string_allocator *out_allocator)
{
if (_NpyString_isnull(in)) {
return _NpyString_pack_null(out_allocator, out);
}
if (is_short_string(in)) {
memcpy(out, in, sizeof(_npy_static_string_u));
return 0;
}
_npy_static_string_u *in_u = (_npy_static_string_u *)in;
size_t size = VSTRING_SIZE(in_u);
if (size == 0) {
_npy_static_string_u *out_u = (_npy_static_string_u *)out;
unsigned char flags = out_u->direct_buffer.size_and_flags &
~NPY_SHORT_STRING_SIZE_MASK;
memcpy(out_u, &empty_string_u, sizeof(_npy_static_string_u));
out_u->direct_buffer.size_and_flags |= flags;
return 0;
}
char *in_buf = NULL;
npy_string_arena *arena = &in_allocator->arena;
if (arena->buffer == NULL) {
return -1;
}
int used_malloc = 0;
if (in_allocator == out_allocator && is_a_vstring(in)) {
in_buf = in_allocator->malloc(size);
memcpy(in_buf, vstring_buffer(arena, in_u), size);
used_malloc = 1;
}
else {
in_buf = vstring_buffer(arena, in_u);
}
int ret =
_NpyString_newsize(in_buf, VSTRING_SIZE(in_u), out, out_allocator);
if (used_malloc) {
in_allocator->free(in_buf);
}
return ret;
}
int
_NpyString_cmp(const _npy_static_string *s1, const _npy_static_string *s2)
{
size_t minsize = s1->size < s2->size ? s1->size : s2->size;
int cmp = 0;
if (minsize != 0) {
cmp = strncmp(s1->buf, s2->buf, minsize);
}
if (cmp == 0) {
if (s1->size > minsize) {
return 1;
}
if (s2->size > minsize) {
return -1;
}
}
return cmp;
}
size_t
_NpyString_size(const npy_packed_static_string *packed_string)
{
if (_NpyString_isnull(packed_string)) {
return 0;
}
_npy_static_string_u *string_u = (_npy_static_string_u *)packed_string;
if (is_short_string(packed_string)) {
return string_u->direct_buffer.size_and_flags &
NPY_SHORT_STRING_SIZE_MASK;
}
return VSTRING_SIZE(string_u);
}
int
_NpyString_pack(npy_string_allocator *allocator,
npy_packed_static_string *packed_string, const char *buf,
size_t size)
{
if (_NpyString_free(packed_string, allocator) < 0) {
return -1;
}
return _NpyString_newsize(buf, size, packed_string, allocator);
}
int
_NpyString_pack_null(npy_string_allocator *allocator,
npy_packed_static_string *packed_string)
{
_npy_static_string_u *str_u = (_npy_static_string_u *)packed_string;
unsigned char *flags = &str_u->direct_buffer.size_and_flags;
unsigned char current_flags = *flags & ~NPY_SHORT_STRING_SIZE_MASK;
if (_NpyString_free(packed_string, allocator) < 0) {
return -1;
}
memcpy(str_u, &empty_string_u, sizeof(_npy_static_string_u));
*flags = current_flags | NPY_STRING_MISSING;
return 0;
}