libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_quark.h
Go to the documentation of this file.
1/*! \file
2 * \brief ::GblQuark and string interning API
3 * \ingroup strings
4 *
5 * A GblQuark is an integral unique identifier associated with a given string.
6 * Two different C string pointers containing the same string value will have
7 * the same GblQuark. GblQuark is libGimbal's implementation of string interning.
8 *
9 * A GblQuark is highly useful for situations when string comparison speed is
10 * what matters the most, such as for hash tables. For example, libGimbal's
11 * property system internally represents property names as GblQuarks so that
12 * accessing a property by name is optimized.
13 *
14 * This string interning mechanism is implemented as a hash table
15 * mapping a given regular string to a GblQuark unique identifier. When a
16 * non-static string is registered, it is allocated within a paged static
17 * buffer, whose lifetime is that of the program.
18 *
19 * \note
20 * When working with string literals, the internal allocations can be
21 * elided by using GblQuark_fromStatic() or GblQuark_internStatic(),
22 * since a string literal's lifetime is global.
23 *
24 * \note
25 * All functions in this API are thread-safe.
26 *
27 * ### Example Usage
28 *
29 * // create a quark from a string literal
30 * GblQuark quark1 = GblQuark_fromStatic("String");
31 *
32 * // create a quark from a regular string buffer
33 * char buffer[] = "String";
34 * GblQuark quark2 = GblQuark_fromString(buffer);
35 *
36 * // the two values are equal
37 * GBL_ASSERT(quark1 == quark2);
38 *
39 * // convert them back to regular strings to show values are equal
40 * GBL_ASSERT(strcmp(GblQuark_toString(quark1),
41 * GblQuark_toString(quark2) == 0);
42 *
43 * \author 2023 Falco Girgis
44 * \copyright MIT License
45 */
46#ifndef GIMBAL_QUARK_H
47#define GIMBAL_QUARK_H
48
49#include "../core/gimbal_result.h"
50
51//! Value of an invalid or NULL ::GblQuark
52#define GBL_QUARK_INVALID ((GblQuark)0)
53
55
57
58/*! Uniquely identifiable interned string type
59 * \ingroup strings
60 *
61 * Two quarks may be directly compared to one another for equality.
62 *
63 * \note
64 * To test whether a given GblQuark is valid or not, it can be
65 * compared to #GBL_QUARK_INVALID.
66 */
67typedef uintptr_t GblQuark;
68
69/*! \name Static State
70 * \brief Methods for static initialization, finalization, and state
71 * @{
72 */
73//! Initializes the GblQuark registry with the given capacities (called automatically with defaults)
74GBL_EXPORT GBL_RESULT GblQuark_init (GblContext* pCtx,
75 size_t extraPageSize,
76 size_t initialEntries) GBL_NOEXCEPT;
77//! Finalizes the GblQuark registry, releasing all resources (called automatically upon shutdown)
79//! Returns a pointer to the GblContext object associated with the GblQuark registry
81//! @}
82
83/*! \name Statistics
84 * \brief Methods for querying and reporting usage stats
85 * @{
86 */
87//! Returns the total number of quarks maintained within the registry
89//! Returns the total number of allocation pages used by the registry
91//! Returns the size of each dynamically allocated page used by the registry
93//! Returns the total number of bytes used for string allocations by the registry
95//! Returns the total number of bytes remaining available on the current allocation page
97//! Returns the total number of bytes allocated (used or unused) for string storage
99//! Returns the total number of unused, unavailable, but allocated bytes for string storage
101//! Returns the utilization factor of total capacity vs bytes used (ranging 0.0-1.0)
103//! @}
104
105/*! \name String Interning
106 * \brief Methods for creating, retrieving, and converting GblQuarks
107 * @{
108 */
109//! Returns the GblQuark associated with the given string, adding a new entry to the registry if necessary
110GBL_EXPORT GblQuark GblQuark_fromString (const char* pStr, size_t len/*=0*/) GBL_NOEXCEPT;
111//! Returns the GblQuark associated with the given STATIC string, which can save an allocation when initially registering
113//! Returns the GblQuark associated with the given string, returning GBL_QUARK_INVALID if it was not previously registered
114GBL_EXPORT GblQuark GblQuark_tryString (const char* pStr, size_t len/*=0*/) GBL_NOEXCEPT;
115//! Returns the NULL-terminated interned C string associated with a given GblQuark
117//! Creates a GblQuark from the given string (if necessary), also returning its interned string
118GBL_EXPORT const char* GblQuark_internString (const char* pStr, size_t len/*=0*/) GBL_NOEXCEPT;
119//! Creates a GblQuark from the given STATIC string (if necessary, saving on allocating), also returning its interned string
120GBL_EXPORT const char* GblQuark_internStatic (const char* pString) GBL_NOEXCEPT;
121//! @}
122
124
125//! \cond
126#define GblQuark_fromString(...)
127 GblQuark_fromStringDefault_(__VA_ARGS__)
128#define GblQuark_fromStringDefault_(...)
129 GblQuark_fromStringDefault__(__VA_ARGS__, 0)
130#define GblQuark_fromStringDefault__(str, len, ...)
131 (GblQuark_fromString)(str, len)
132
133#define GblQuark_internString(...)
134 GblQuark_internStringDefault_(__VA_ARGS__)
135#define GblQuark_internStringDefault_(...)
136 GblQuark_internStringDefault__(__VA_ARGS__, 0)
137#define GblQuark_internStringDefault__(str, len, ...)
138 (GblQuark_internString)(str, len)
139
140#define GblQuark_tryString(...)
141 GblQuark_tryStringDefault_(__VA_ARGS__)
142#define GblQuark_tryStringDefault_(...)
143 GblQuark_tryStringDefault__(__VA_ARGS__, 0)
144#define GblQuark_tryStringDefault__(str, len, ...)
145 (GblQuark_tryString)(str, len)
146//! \endcond
147
148/*!
149 \fn GblQuark_init(GblContext* pCtx, size_t extraPageSize, size_t initialEntries)
150 \details
151 Initializes the GblQuark API with the given configuration parameters or
152 default if they're all set to NULL and 0.
153 \note
154 The API is already initialized internally as soon as it's used, so this is
155 typically not required.
156 \attention
157 This will invalidate any exiting ::GblQuark values!
158 \param pCtx context from which to allocate and log
159 \param extraPageSize size of each chunk allocation used beyond the builtin static initial page
160 \param initialEntries initial number of entries to preallocate in the hash table
161 \returns result code
162*/
163
164/*!
165 \fn GblQuark_final(void)
166 \details
167 Finalizes the GblQuark API, releasing all internally allocated paged data
168 and clearing the hash map.
169 \note
170 This will invalidate all existing GblQuark values and is typically
171 called when the application exits
172 \returns result code
173*/
174
175/*!
176 \fn GblQuark_pageSize(void)
177 \returns size of each additional page beyond the default static one for holding
178 interned strings
179*/
180
181/*!
182 \fn GblQuark_pageCount(void)
183 \returns number of pages which have been allocated to store interned strings
184*/
185
186/*!
187 \fn GblQuark_bytesUsed(void)
188 \returns total number of bytes used on all pages by interned strings
189*/
190
191/*!
192 \fn GblQuark_bytesAvailable(void)
193 \returns total number of bytes available on the current page
194*/
195
196/*!
197 \fn GblQuark_totalCapacity(void)
198 \returns total combined byte size of all active pages
199*/
200
201/*!
202 \fn GblQuark_fragmentedBytes(void)
203 \returns total number of leftover/wasted bytes at the end of a page that were too small to fill
204*/
205
206/*!
207 \fn GblQuark_utilization(void)
208 \returns Ratio of GblQuark_bytesUsed() over GblQuark_totalCapacity(),
209 representing what percentage of available resources is being utilized
210*/
211/*!
212 \fn GblQuark_count(void)
213 \returns total number of GblQuarks which have been created internally
214*/
215
216/*!
217 \fn GblQuark_context(void)
218 \returns GblContext object used for logging and allocation internally
219*/
220
221/*!
222 \fn GblQuark_fromStatic(const char* pString)
223 \details
224 Equivalent to GblQuark_fromString(), except for no internal storage
225 is allocated for the string, since its lifetime is known to be global.
226 \param pString string literal
227 \returns quark value or #GBL_QUARK_INVALID if the string is NULL
228 \sa GblQuark_fromString
229*/
230
231/*!
232 \fn GblQuark_toString(GblQuark quark)
233 \details
234 Retrieves the internal C string representation of the given quark
235 or NULL if the quark's value is #GBL_QUARK_INVALID
236 \param quark value of existing quark
237 \returns NULL-terminated C string or NULL
238*/
239
240/*!
241 \fn GblQuark_internStatic(const char* pString)
242 \details
243 Equivalent to GblQuark_internString() except that the
244 internal allocation of the string is ellided, because the string's
245 lifetime is known to be global. So the return value is the same as the
246 argument.
247 \param pString static or literal string or NULL
248 \returns pString
249 \sa GblQuark_internString
250*/
251
252#endif // GIMBAL_QUARK_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_EXPORT
size_t GblQuark_bytesAvailable(void)
Returns the total number of bytes remaining available on the current allocation page.
size_t GblQuark_bytesUsed(void)
Returns the total number of bytes used for string allocations by the registry.
size_t GblQuark_totalCapacity(void)
Returns the total number of bytes allocated (used or unused) for string storage.
float GblQuark_utilization(void)
Returns the utilization factor of total capacity vs bytes used (ranging 0.0-1.0)
const char * GblQuark_internStatic(const char *pString)
Creates a GblQuark from the given STATIC string (if necessary, saving on allocating),...
size_t GblQuark_fragmentedBytes(void)
Returns the total number of unused, unavailable, but allocated bytes for string storage.
size_t GblQuark_count(void)
Returns the total number of quarks maintained within the registry.
GBL_RESULT GblQuark_init(GblContext *pCtx, size_t extraPageSize, size_t initialEntries)
Initializes the GblQuark registry with the given capacities (called automatically with defaults)
const char * GblQuark_internString(const char *pStr, size_t len)
Creates a GblQuark from the given string (if necessary), also returning its interned string.
GblQuark GblQuark_fromStatic(const char *pSstring)
Returns the GblQuark associated with the given STATIC string, which can save an allocation when initi...
size_t GblQuark_pageCount(void)
Returns the total number of allocation pages used by the registry.
size_t GblQuark_pageSize(void)
Returns the size of each dynamically allocated page used by the registry.
const char * GblQuark_toString(GblQuark quark)
Returns the NULL-terminated interned C string associated with a given GblQuark.
GblContext * GblQuark_context(void)
Returns a pointer to the GblContext object associated with the GblQuark registry.
GblQuark GblQuark_fromString(const char *pStr, size_t len)
Returns the GblQuark associated with the given string, adding a new entry to the registry if necessar...
GBL_RESULT GblQuark_final(void)
Finalizes the GblQuark registry, releasing all resources (called automatically upon shutdown)
GblQuark GblQuark_tryString(const char *pStr, size_t len)
Returns the GblQuark associated with the given string, returning GBL_QUARK_INVALID if it was not prev...
uintptr_t GblQuark
Uniquely identifiable interned string type.