@@ -26,10 +26,13 @@ extern const npy_packed_static_string *NPY_NULL_STRING;
2626// Handles heap allocations for static strings.
2727typedef struct npy_string_allocator npy_string_allocator ;
2828
29+ // Typedefs for allocator functions
2930typedef void * (* npy_string_malloc_func )(size_t size );
3031typedef void (* npy_string_free_func )(void * ptr );
3132
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
3336npy_string_allocator *
3437npy_string_new_allocator (npy_string_malloc_func m , npy_string_free_func f );
3538void
@@ -51,16 +54,16 @@ npy_string_newsize(const char *init, size_t size,
5154 npy_packed_static_string * to_init ,
5255 npy_string_allocator * allocator );
5356
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.
5659void
5760npy_string_free (npy_packed_static_string * str ,
5861 npy_string_allocator * allocator );
5962
6063// 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.
6467int
6568npy_string_dup (const npy_packed_static_string * in ,
6669 npy_packed_static_string * out , npy_string_allocator * allocator );
@@ -69,20 +72,19 @@ npy_string_dup(const npy_packed_static_string *in,
6972// *size* bytes of text. Does not do any initialization, the caller must
7073// initialize the string buffer after this function returns. Calling
7174// 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.
7982int
8083npy_string_newemptysize (size_t size , npy_packed_static_string * out ,
8184 npy_string_allocator * allocator );
8285
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.
8688int
8789npy_string_isnull (const npy_packed_static_string * in );
8890
@@ -91,10 +93,22 @@ npy_string_isnull(const npy_packed_static_string *in);
9193int
9294npy_string_cmp (const npy_static_string * s1 , const npy_static_string * s2 );
9395
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.
94105int
95106npy_load_string (const npy_packed_static_string * packed_string ,
96107 npy_static_string * unpacked_string );
97108
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.
98112size_t
99113npy_string_size (const npy_packed_static_string * packed_string );
100114
0 commit comments