88// the high byte in vstring.size is reserved for flags
99// SSSS SSSF
1010
11- typedef struct _npy_static_string_t {
11+ typedef struct _npy_static_vstring_t {
1212 size_t offset ;
13- size_t size ;
14- } _npy_static_string_t ;
13+ size_t size_and_flags ;
14+ } _npy_static_vstring_t ;
1515
1616typedef 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 ;
1919} _short_string_buffer ;
2020
2121#elif NPY_BYTE_ORDER == NPY_BIG_ENDIAN
2222
2323// the high byte in vstring.size is reserved for flags
2424// FSSS SSSS
2525
26- typedef struct _npy_static_string_t {
26+ typedef struct _npy_static_vstring_t {
2727 size_t size ;
2828 size_t offset ;
29- } _npy_static_string_t ;
29+ } _npy_static_vstring_t ;
3030
3131typedef 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 ];
3434} _short_string_buffer ;
3535
3636#endif
3737
3838typedef union _npy_static_string_u {
39- _npy_static_string_t vstring ;
39+ _npy_static_vstring_t vstring ;
4040 _short_string_buffer direct_buffer ;
4141} _npy_static_string_u ;
4242
@@ -59,19 +59,19 @@ typedef union _npy_static_string_u {
5959// of this choice is a calloc'd array buffer (e.g. from np.empty) is filled
6060// with empty elements for free
6161const _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 }}};
6363const npy_packed_static_string * NPY_EMPTY_STRING =
6464 (npy_packed_static_string * )& empty_string_u ;
6565// zero-filled, but with the NULL flag set to distinguish from empty string
6666const _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 }}};
6868const npy_packed_static_string * NPY_NULL_STRING =
6969 (npy_packed_static_string * )& null_string_u ;
7070
7171#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;
7373#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)
7575
7676typedef struct npy_string_arena {
7777 size_t cursor ;
@@ -89,9 +89,10 @@ struct npy_string_allocator {
8989void
9090set_vstring_size (_npy_static_string_u * str , size_t size )
9191{
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 ;
9596}
9697
9798char *
221222is_short_string (const npy_packed_static_string * s )
222223{
223224 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 ;
225226 int has_short_flag = (high_byte & NPY_STRING_SHORT );
226227 int has_on_heap_flag = (high_byte & NPY_STRING_ON_HEAP );
227228 return has_short_flag && !has_on_heap_flag ;
@@ -230,7 +231,7 @@ is_short_string(const npy_packed_static_string *s)
230231int
231232is_medium_string (const _npy_static_string_u * s )
232233{
233- unsigned char high_byte = s -> direct_buffer .flags_and_size ;
234+ unsigned char high_byte = s -> direct_buffer .size_and_flags ;
234235 int has_short_flag = (high_byte & NPY_STRING_SHORT );
235236 int has_medium_flag = (high_byte & NPY_STRING_MEDIUM );
236237 return (!has_short_flag && has_medium_flag );
240241npy_string_isnull (const npy_packed_static_string * s )
241242{
242243 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 ;
244245 return (high_byte & NPY_STRING_MISSING ) == NPY_STRING_MISSING ;
245246}
246247
@@ -270,7 +271,7 @@ npy_string_load(npy_string_allocator *allocator,
270271 _npy_static_string_u * string_u = (_npy_static_string_u * )packed_string ;
271272
272273 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 ;
274275 unpacked_string -> size = high_byte & NPY_SHORT_STRING_SIZE_MASK ;
275276 unpacked_string -> buf = string_u -> direct_buffer .buf ;
276277 }
@@ -300,7 +301,7 @@ heap_or_arena_allocate(npy_string_allocator *allocator,
300301 _npy_static_string_u * to_init_u , size_t size ,
301302 int * on_heap )
302303{
303- unsigned char * flags = & to_init_u -> direct_buffer .flags_and_size ;
304+ unsigned char * flags = & to_init_u -> direct_buffer .size_and_flags ;
304305 if (* flags & NPY_STRING_SHORT ) {
305306 // Have to heap allocate since there isn't a preexisting
306307 // allocation. This leaves the NPY_STRING_SHORT flag set to indicate
357358heap_or_arena_deallocate (npy_string_allocator * allocator ,
358359 _npy_static_string_u * str_u )
359360{
360- unsigned char * flags = & str_u -> direct_buffer .flags_and_size ;
361+ unsigned char * flags = & str_u -> direct_buffer .size_and_flags ;
361362 if (* flags & NPY_STRING_ON_HEAP ) {
362363 // It's a heap string (not in the arena buffer) so it needs to be
363364 // deallocated with free(). For heap strings the offset is a raw
@@ -379,7 +380,7 @@ heap_or_arena_deallocate(npy_string_allocator *allocator,
379380 return -1 ;
380381 }
381382 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 ;
383384 }
384385 }
385386 return 0 ;
@@ -425,11 +426,11 @@ npy_string_newemptysize(size_t size, npy_packed_static_string *out,
425426 _npy_static_string_u * out_u = (_npy_static_string_u * )out ;
426427
427428 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 ;
429430
430431 if (size == 0 ) {
431432 * out = * NPY_EMPTY_STRING ;
432- out_u -> direct_buffer .flags_and_size |= flags ;
433+ out_u -> direct_buffer .size_and_flags |= flags ;
433434 return 0 ;
434435 }
435436
@@ -458,7 +459,7 @@ npy_string_newemptysize(size_t size, npy_packed_static_string *out,
458459 // In either case, the size data is in at most the least significant 4
459460 // bits of the byte so it's safe to | with one of 0x10, 0x20, 0x40, or
460461 // 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 ;
462463 }
463464
464465 return 0 ;
@@ -470,7 +471,7 @@ npy_string_free(npy_packed_static_string *str, npy_string_allocator *allocator)
470471 _npy_static_string_u * str_u = (_npy_static_string_u * )str ;
471472 if (is_not_a_vstring (str )) {
472473 // 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 ;
474475 unsigned char current_flags = * flags & ~NPY_SHORT_STRING_SIZE_MASK ;
475476 memcpy (str , NPY_EMPTY_STRING , sizeof (npy_packed_static_string ));
476477 * flags |= current_flags ;
@@ -505,10 +506,10 @@ npy_string_dup(const npy_packed_static_string *in,
505506 size_t size = VSTRING_SIZE (in_u );
506507 if (size == 0 ) {
507508 _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 &
509510 ~NPY_SHORT_STRING_SIZE_MASK ;
510511 * out = * NPY_EMPTY_STRING ;
511- out_u -> direct_buffer .flags_and_size |= flags ;
512+ out_u -> direct_buffer .size_and_flags |= flags ;
512513 return 0 ;
513514 }
514515 char * in_buf = NULL ;
@@ -566,7 +567,7 @@ npy_string_size(const npy_packed_static_string *packed_string)
566567 _npy_static_string_u * string_u = (_npy_static_string_u * )packed_string ;
567568
568569 if (is_short_string (packed_string )) {
569- return string_u -> direct_buffer .flags_and_size &
570+ return string_u -> direct_buffer .size_and_flags &
570571 NPY_SHORT_STRING_SIZE_MASK ;
571572 }
572573
0 commit comments