@@ -26,10 +26,13 @@ extern const npy_packed_static_string *NPY_NULL_STRING;
26
26
// Handles heap allocations for static strings.
27
27
typedef struct npy_string_allocator npy_string_allocator ;
28
28
29
+ // Typedefs for allocator functions
29
30
typedef void * (* npy_string_malloc_func )(size_t size );
30
31
typedef void (* npy_string_free_func )(void * ptr );
31
32
32
- // Use these functions to create a new allocator and destroy allocators
33
+ // Use these functions to create and destroy string allocators, normally
34
+ // users won't use this directly and will use an allocator already
35
+ // attached to a dtype instance
33
36
npy_string_allocator *
34
37
npy_string_new_allocator (npy_string_malloc_func m , npy_string_free_func f );
35
38
void
@@ -51,16 +54,16 @@ npy_string_newsize(const char *init, size_t size,
51
54
npy_packed_static_string * to_init ,
52
55
npy_string_allocator * allocator );
53
56
54
- // Sets len to 0 and if the internal buffer is not already NULL, frees it if
55
- // it is allocated on the heap and sets it to NULL. Cannot fail.
57
+ // Zeroes out the packed string and frees any heap allocated data. Cannot
58
+ // fail.
56
59
void
57
60
npy_string_free (npy_packed_static_string * str ,
58
61
npy_string_allocator * allocator );
59
62
60
63
// Copies the contents of *in* into *out*. Allocates a new string buffer for
61
- // *out* and assumes that *out* is uninitialized. Returns -1 if malloc fails
62
- // and -2 if *out* is not initialized. npy_string_free *must* be called before
63
- // this is called if *in* points to an existing string. Returns 0 on success.
64
+ // *out*, npy_string_free *must* be called before this is called if *out*
65
+ // points to an existing string. Returns -1 if malloc fails. Returns 0 on
66
+ // success.
64
67
int
65
68
npy_string_dup (const npy_packed_static_string * in ,
66
69
npy_packed_static_string * out , npy_string_allocator * allocator );
@@ -69,20 +72,19 @@ npy_string_dup(const npy_packed_static_string *in,
69
72
// *size* bytes of text. Does not do any initialization, the caller must
70
73
// initialize the string buffer after this function returns. Calling
71
74
// npy_string_free on *to_init* before calling this function on an existing
72
- // string or copying the contents of NPY_EMPTY_STRING into *to_init* is
73
- // sufficient to initialize it. Does not check if *to_init* is NULL or if the
74
- // internal buffer is non-NULL, undefined behavior or memory leaks are
75
- // possible if this function is passed a pointer to an unintialized struct,
76
- // a NULL pointer, or an existing heap-allocated string. Returns 0 on
77
- // success. Returns -1 if allocating the string would exceed the maximum
78
- // allowed string size or exhaust available memory. Returns 0 on success.
75
+ // string or initializing a new string with the contents of NPY_EMPTY_STRING
76
+ // is sufficient to initialize it. Does not check if *to_init* has already
77
+ // been initialized or if the internal buffer is non-NULL, undefined behavior
78
+ // or memory leaks are possible if this function is passed a NULL pointer or
79
+ // an existing heap-allocated string. Returns 0 on success. Returns -1 if
80
+ // allocating the string would exceed the maximum allowed string size or
81
+ // exhaust available memory. Returns 0 on success.
79
82
int
80
83
npy_string_newemptysize (size_t size , npy_packed_static_string * out ,
81
84
npy_string_allocator * allocator );
82
85
83
- // Determine if *in* corresponds to a NULL npy_static_string struct (e.g. len
84
- // is zero and buf is NULL. Returns 1 if this is the case and zero otherwise.
85
- // Cannot fail.
86
+ // Determine if *in* corresponds to a NULL npy_static_string struct. Returns 1
87
+ // if this is the case and zero otherwise. Cannot fail.
86
88
int
87
89
npy_string_isnull (const npy_packed_static_string * in );
88
90
@@ -91,10 +93,22 @@ npy_string_isnull(const npy_packed_static_string *in);
91
93
int
92
94
npy_string_cmp (const npy_static_string * s1 , const npy_static_string * s2 );
93
95
96
+ // Extract the packed contents of *packed_string* into *unpacked_string*. A
97
+ // useful pattern is to define a stack-allocated npy_static_string instance
98
+ // initialized to {0, NULL} and pass a pointer to that string to unpack the
99
+ // contents of a packed string. The *unpacked_string* is a read-only view onto
100
+ // the *packed_string* data and should not be used to modify the string
101
+ // data. If *packed_string* is the null string, sets *unpacked_string* to the
102
+ // NULL pointer. Returns 1 if *packed_string* is the null string and 0
103
+ // otherwise so this function can be used to simultaneously unpack a string
104
+ // and determine if it is a null string.
94
105
int
95
106
npy_load_string (const npy_packed_static_string * packed_string ,
96
107
npy_static_string * unpacked_string );
97
108
109
+ // Returns the size of the string data in the packed string. Useful in
110
+ // situations where only the string size is needed and determing if this is a
111
+ // null or unpacking the string is unnecessary.
98
112
size_t
99
113
npy_string_size (const npy_packed_static_string * packed_string );
100
114
0 commit comments