8
8
// the high byte in vstring.size is reserved for flags
9
9
// SSSS SSSF
10
10
11
- typedef struct _npy_static_string_t {
11
+ typedef struct _npy_static_vstring_t {
12
12
size_t offset ;
13
- size_t size ;
14
- } _npy_static_string_t ;
13
+ size_t size_and_flags ;
14
+ } _npy_static_vstring_t ;
15
15
16
16
typedef struct _short_string_buffer {
17
- char buf [sizeof (_npy_static_string_t ) - 1 ];
18
- unsigned char flags_and_size ;
17
+ char buf [sizeof (_npy_static_vstring_t ) - 1 ];
18
+ unsigned char size_and_flags ;
19
19
} _short_string_buffer ;
20
20
21
21
#elif NPY_BYTE_ORDER == NPY_BIG_ENDIAN
22
22
23
23
// the high byte in vstring.size is reserved for flags
24
24
// FSSS SSSS
25
25
26
- typedef struct _npy_static_string_t {
26
+ typedef struct _npy_static_vstring_t {
27
27
size_t size ;
28
28
size_t offset ;
29
- } _npy_static_string_t ;
29
+ } _npy_static_vstring_t ;
30
30
31
31
typedef struct _short_string_buffer {
32
- unsigned char flags_and_size ;
33
- char buf [sizeof (npy_static_string_t ) - 1 ];
32
+ unsigned char size_and_flags ;
33
+ char buf [sizeof (npy_static_vstring_t ) - 1 ];
34
34
} _short_string_buffer ;
35
35
36
36
#endif
37
37
38
38
typedef union _npy_static_string_u {
39
- _npy_static_string_t vstring ;
39
+ _npy_static_vstring_t vstring ;
40
40
_short_string_buffer direct_buffer ;
41
41
} _npy_static_string_u ;
42
42
@@ -59,19 +59,19 @@ typedef union _npy_static_string_u {
59
59
// of this choice is a calloc'd array buffer (e.g. from np.empty) is filled
60
60
// with empty elements for free
61
61
const _npy_static_string_u empty_string_u = {
62
- .direct_buffer = {.flags_and_size = 0 , .buf = {0 }}};
62
+ .direct_buffer = {.size_and_flags = 0 , .buf = {0 }}};
63
63
const npy_packed_static_string * NPY_EMPTY_STRING =
64
64
(npy_packed_static_string * )& empty_string_u ;
65
65
// zero-filled, but with the NULL flag set to distinguish from empty string
66
66
const _npy_static_string_u null_string_u = {
67
- .direct_buffer = {.flags_and_size = NPY_STRING_MISSING , .buf = {0 }}};
67
+ .direct_buffer = {.size_and_flags = NPY_STRING_MISSING , .buf = {0 }}};
68
68
const npy_packed_static_string * NPY_NULL_STRING =
69
69
(npy_packed_static_string * )& null_string_u ;
70
70
71
71
#define VSTRING_FLAGS (string ) \
72
- string->direct_buffer.flags_and_size & ~NPY_SHORT_STRING_SIZE_MASK;
72
+ string->direct_buffer.size_and_flags & ~NPY_SHORT_STRING_SIZE_MASK;
73
73
#define HIGH_BYTE_MASK ((size_t)0XFF << 8 * (sizeof(size_t) - 1))
74
- #define VSTRING_SIZE (string ) (string->vstring.size & ~HIGH_BYTE_MASK)
74
+ #define VSTRING_SIZE (string ) (string->vstring.size_and_flags & ~HIGH_BYTE_MASK)
75
75
76
76
typedef struct npy_string_arena {
77
77
size_t cursor ;
@@ -89,9 +89,10 @@ struct npy_string_allocator {
89
89
void
90
90
set_vstring_size (_npy_static_string_u * str , size_t size )
91
91
{
92
- unsigned char current_flags = str -> direct_buffer .flags_and_size ;
93
- str -> vstring .size = size ;
94
- str -> direct_buffer .flags_and_size = current_flags ;
92
+ unsigned char * flags = & str -> direct_buffer .size_and_flags ;
93
+ unsigned char current_flags = * flags & ~NPY_SHORT_STRING_SIZE_MASK ;
94
+ str -> vstring .size_and_flags = size ;
95
+ str -> direct_buffer .size_and_flags = current_flags ;
95
96
}
96
97
97
98
char *
221
222
is_short_string (const npy_packed_static_string * s )
222
223
{
223
224
unsigned char high_byte =
224
- ((_npy_static_string_u * )s )-> direct_buffer .flags_and_size ;
225
+ ((_npy_static_string_u * )s )-> direct_buffer .size_and_flags ;
225
226
int has_short_flag = (high_byte & NPY_STRING_SHORT );
226
227
int has_on_heap_flag = (high_byte & NPY_STRING_ON_HEAP );
227
228
return has_short_flag && !has_on_heap_flag ;
@@ -230,7 +231,7 @@ is_short_string(const npy_packed_static_string *s)
230
231
int
231
232
is_medium_string (const _npy_static_string_u * s )
232
233
{
233
- unsigned char high_byte = s -> direct_buffer .flags_and_size ;
234
+ unsigned char high_byte = s -> direct_buffer .size_and_flags ;
234
235
int has_short_flag = (high_byte & NPY_STRING_SHORT );
235
236
int has_medium_flag = (high_byte & NPY_STRING_MEDIUM );
236
237
return (!has_short_flag && has_medium_flag );
240
241
npy_string_isnull (const npy_packed_static_string * s )
241
242
{
242
243
unsigned char high_byte =
243
- ((_npy_static_string_u * )s )-> direct_buffer .flags_and_size ;
244
+ ((_npy_static_string_u * )s )-> direct_buffer .size_and_flags ;
244
245
return (high_byte & NPY_STRING_MISSING ) == NPY_STRING_MISSING ;
245
246
}
246
247
@@ -270,7 +271,7 @@ npy_string_load(npy_string_allocator *allocator,
270
271
_npy_static_string_u * string_u = (_npy_static_string_u * )packed_string ;
271
272
272
273
if (is_short_string (packed_string )) {
273
- unsigned char high_byte = string_u -> direct_buffer .flags_and_size ;
274
+ unsigned char high_byte = string_u -> direct_buffer .size_and_flags ;
274
275
unpacked_string -> size = high_byte & NPY_SHORT_STRING_SIZE_MASK ;
275
276
unpacked_string -> buf = string_u -> direct_buffer .buf ;
276
277
}
@@ -300,7 +301,7 @@ heap_or_arena_allocate(npy_string_allocator *allocator,
300
301
_npy_static_string_u * to_init_u , size_t size ,
301
302
int * on_heap )
302
303
{
303
- unsigned char * flags = & to_init_u -> direct_buffer .flags_and_size ;
304
+ unsigned char * flags = & to_init_u -> direct_buffer .size_and_flags ;
304
305
if (* flags & NPY_STRING_SHORT ) {
305
306
// Have to heap allocate since there isn't a preexisting
306
307
// allocation. This leaves the NPY_STRING_SHORT flag set to indicate
357
358
heap_or_arena_deallocate (npy_string_allocator * allocator ,
358
359
_npy_static_string_u * str_u )
359
360
{
360
- unsigned char * flags = & str_u -> direct_buffer .flags_and_size ;
361
+ unsigned char * flags = & str_u -> direct_buffer .size_and_flags ;
361
362
if (* flags & NPY_STRING_ON_HEAP ) {
362
363
// It's a heap string (not in the arena buffer) so it needs to be
363
364
// deallocated with free(). For heap strings the offset is a raw
@@ -379,7 +380,7 @@ heap_or_arena_deallocate(npy_string_allocator *allocator,
379
380
return -1 ;
380
381
}
381
382
if (arena -> buffer != NULL ) {
382
- str_u -> direct_buffer .flags_and_size |= NPY_STRING_ARENA_FREED ;
383
+ str_u -> direct_buffer .size_and_flags |= NPY_STRING_ARENA_FREED ;
383
384
}
384
385
}
385
386
return 0 ;
@@ -425,11 +426,11 @@ npy_string_newemptysize(size_t size, npy_packed_static_string *out,
425
426
_npy_static_string_u * out_u = (_npy_static_string_u * )out ;
426
427
427
428
unsigned char flags =
428
- out_u -> direct_buffer .flags_and_size & ~NPY_SHORT_STRING_SIZE_MASK ;
429
+ out_u -> direct_buffer .size_and_flags & ~NPY_SHORT_STRING_SIZE_MASK ;
429
430
430
431
if (size == 0 ) {
431
432
* out = * NPY_EMPTY_STRING ;
432
- out_u -> direct_buffer .flags_and_size |= flags ;
433
+ out_u -> direct_buffer .size_and_flags |= flags ;
433
434
return 0 ;
434
435
}
435
436
@@ -458,7 +459,7 @@ npy_string_newemptysize(size_t size, npy_packed_static_string *out,
458
459
// In either case, the size data is in at most the least significant 4
459
460
// bits of the byte so it's safe to | with one of 0x10, 0x20, 0x40, or
460
461
// 0x80.
461
- out_u -> direct_buffer .flags_and_size = NPY_STRING_SHORT | flags | size ;
462
+ out_u -> direct_buffer .size_and_flags = NPY_STRING_SHORT | flags | size ;
462
463
}
463
464
464
465
return 0 ;
@@ -470,7 +471,7 @@ npy_string_free(npy_packed_static_string *str, npy_string_allocator *allocator)
470
471
_npy_static_string_u * str_u = (_npy_static_string_u * )str ;
471
472
if (is_not_a_vstring (str )) {
472
473
// zero out, keeping flags
473
- unsigned char * flags = & str_u -> direct_buffer .flags_and_size ;
474
+ unsigned char * flags = & str_u -> direct_buffer .size_and_flags ;
474
475
unsigned char current_flags = * flags & ~NPY_SHORT_STRING_SIZE_MASK ;
475
476
memcpy (str , NPY_EMPTY_STRING , sizeof (npy_packed_static_string ));
476
477
* flags |= current_flags ;
@@ -505,10 +506,10 @@ npy_string_dup(const npy_packed_static_string *in,
505
506
size_t size = VSTRING_SIZE (in_u );
506
507
if (size == 0 ) {
507
508
_npy_static_string_u * out_u = (_npy_static_string_u * )out ;
508
- unsigned char flags = out_u -> direct_buffer .flags_and_size &
509
+ unsigned char flags = out_u -> direct_buffer .size_and_flags &
509
510
~NPY_SHORT_STRING_SIZE_MASK ;
510
511
* out = * NPY_EMPTY_STRING ;
511
- out_u -> direct_buffer .flags_and_size |= flags ;
512
+ out_u -> direct_buffer .size_and_flags |= flags ;
512
513
return 0 ;
513
514
}
514
515
char * in_buf = NULL ;
@@ -566,7 +567,7 @@ npy_string_size(const npy_packed_static_string *packed_string)
566
567
_npy_static_string_u * string_u = (_npy_static_string_u * )packed_string ;
567
568
568
569
if (is_short_string (packed_string )) {
569
- return string_u -> direct_buffer .flags_and_size &
570
+ return string_u -> direct_buffer .size_and_flags &
570
571
NPY_SHORT_STRING_SIZE_MASK ;
571
572
}
572
573
0 commit comments