Skip to content

Commit 7972b7b

Browse files
committed
move macros for short string implementation out of the header
1 parent 245f0e3 commit 7972b7b

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

stringdtype/stringdtype/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ _memory_usage(PyObject *NPY_UNUSED(self), PyObject *obj)
5959

6060
while (count--) {
6161
size_t size = npy_string_size(((npy_packed_static_string *)in));
62-
if (size > NPY_SHORT_STRING_MAX_SIZE) {
62+
// FIXME: add a way for a string to report its heap size usage
63+
if (size > (sizeof(npy_static_string) - 1)) {
6364
memory_usage += size;
6465
}
6566
in += stride;

stringdtype/stringdtype/src/static_string.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ typedef union _npy_static_string_u {
3939
_short_string_buffer direct_buffer;
4040
} _npy_static_string_u;
4141

42+
// room for two more flags with values 0x20 and 0x10
43+
#define NPY_STRING_MISSING 0x80 // 1000 0000
44+
#define NPY_STRING_SHORT 0x40 // 0100 0000
45+
46+
// short string sizes fit in a 4-bit integer
47+
#define NPY_SHORT_STRING_SIZE_MASK 0x0F // 0000 1111
48+
#define NPY_SHORT_STRING_MAX_SIZE \
49+
(sizeof(npy_static_string) - 1) // 15 or 7 depending on arch
50+
51+
// one byte in size is reserved for flags and small string optimization
52+
#define NPY_MAX_STRING_SIZE (1 << (sizeof(size_t) - 1)) - 1
53+
4254
// Since this has no flags set, technically this is a heap-allocated string
4355
// with size zero. Practically, that doesn't matter because we always do size
4456
// checks before accessing heap data, but that may be confusing. The nice part
@@ -106,7 +118,7 @@ int
106118
npy_string_newsize(const char *init, size_t size,
107119
npy_packed_static_string *to_init)
108120
{
109-
if (to_init == NULL || size > MAX_STRING_SIZE) {
121+
if (to_init == NULL || size > NPY_MAX_STRING_SIZE) {
110122
return -2;
111123
}
112124

@@ -145,7 +157,7 @@ npy_string_newsize(const char *init, size_t size,
145157
int
146158
npy_string_newemptysize(size_t size, npy_packed_static_string *out)
147159
{
148-
if (out == NULL || size > MAX_STRING_SIZE) {
160+
if (out == NULL || size > NPY_MAX_STRING_SIZE) {
149161
return -2;
150162
}
151163

stringdtype/stringdtype/src/static_string.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ typedef struct npy_static_string {
1313
const char *buf;
1414
} npy_static_string;
1515

16-
// room for two more flags with values 0x20 and 0x10
17-
#define NPY_STRING_MISSING 0x80 // 1000 0000
18-
#define NPY_STRING_SHORT 0x40 // 0100 0000
19-
20-
// short string sizes fit in a 4-bit integer
21-
#define NPY_SHORT_STRING_SIZE_MASK 0x0F // 0000 1111
22-
#define NPY_SHORT_STRING_MAX_SIZE \
23-
(sizeof(npy_static_string) - 1) // 15 or 7 depending on arch
24-
25-
// one byte in size is reserved for flags and small string optimization
26-
#define MAX_STRING_SIZE (1 << (sizeof(size_t) - 1)) - 1
27-
2816
// represents the empty string and can be passed safely to npy_static_string
2917
// API functions
3018
extern const npy_packed_static_string *NPY_EMPTY_STRING;

0 commit comments

Comments
 (0)