libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
|
Go to the source code of this file.
Macros | |
#define | GBL_QUARK_INVALID |
Typedefs | |
typedef uintptr_t | GblQuark |
Functions | |
Static State | |
Methods for static initialization, finalization, and state | |
GBL_RESULT | GblQuark_init (GblContext *pCtx, size_t extraPageSize, size_t initialEntries) |
GBL_RESULT | GblQuark_final (void) |
GblContext * | GblQuark_context (void) |
Statistics | |
Methods for querying and reporting usage stats | |
size_t | GblQuark_count (void) |
size_t | GblQuark_pageCount (void) |
size_t | GblQuark_pageSize (void) |
size_t | GblQuark_bytesUsed (void) |
size_t | GblQuark_bytesAvailable (void) |
size_t | GblQuark_totalCapacity (void) |
size_t | GblQuark_fragmentedBytes (void) |
float | GblQuark_utilization (void) |
String Interning | |
Methods for creating, retrieving, and converting GblQuarks | |
GblQuark | GblQuark_fromString (const char *pStr, size_t len) |
GblQuark | GblQuark_fromStatic (const char *pSstring) |
GblQuark | GblQuark_tryString (const char *pStr, size_t len) |
const char * | GblQuark_toString (GblQuark quark) |
const char * | GblQuark_internString (const char *pStr, size_t len) |
const char * | GblQuark_internStatic (const char *pString) |
GblQuark and string interning API
A GblQuark is an integral unique identifier associated with a given string. Two different C string pointers containing the same string value will have the same GblQuark. GblQuark is libGimbal's implementation of string interning.
A GblQuark is highly useful for situations when string comparison speed is what matters the most, such as for hash tables. For example, libGimbal's property system internally represents property names as GblQuarks so that accessing a property by name is optimized.
This string interning mechanism is implemented as a hash table mapping a given regular string to a GblQuark unique identifier. When a non-static string is registered, it is allocated within a paged static buffer, whose lifetime is that of the program.
// create a quark from a string literal GblQuark quark1 = GblQuark_fromStatic("String"); // create a quark from a regular string buffer char buffer[] = "String"; GblQuark quark2 = GblQuark_fromString(buffer); // the two values are equal GBL_ASSERT(quark1 == quark2); // convert them back to regular strings to show values are equal GBL_ASSERT(strcmp(GblQuark_toString(quark1), GblQuark_toString(quark2) == 0);
Definition in file gimbal_quark.h.
#define GBL_QUARK_INVALID |
Value of an invalid or NULL GblQuark.
Definition at line 52 of file gimbal_quark.h.
GblQuark_init | ( | GblContext * | pCtx, |
size_t | extraPageSize, | ||
size_t | initialEntries | ||
) |
Initializes the GblQuark registry with the given capacities (called automatically with defaults)
Initializes the GblQuark API with the given configuration parameters or default if they're all set to NULL and 0.
pCtx | context from which to allocate and log |
extraPageSize | size of each chunk allocation used beyond the builtin static initial page |
initialEntries | initial number of entries to preallocate in the hash table |
GblQuark_final | ( | void | ) |
Finalizes the GblQuark registry, releasing all resources (called automatically upon shutdown)
Finalizes the GblQuark API, releasing all internally allocated paged data and clearing the hash map.
GblQuark_context | ( | void | ) |
Returns a pointer to the GblContext object associated with the GblQuark registry.
GblQuark_count | ( | void | ) |
Returns the total number of quarks maintained within the registry.
GblQuark_pageCount | ( | void | ) |
Returns the total number of allocation pages used by the registry.
GblQuark_pageSize | ( | void | ) |
Returns the size of each dynamically allocated page used by the registry.
GblQuark_bytesUsed | ( | void | ) |
Returns the total number of bytes used for string allocations by the registry.
GblQuark_bytesAvailable | ( | void | ) |
Returns the total number of bytes remaining available on the current allocation page.
GblQuark_totalCapacity | ( | void | ) |
Returns the total number of bytes allocated (used or unused) for string storage.
GblQuark_fragmentedBytes | ( | void | ) |
Returns the total number of unused, unavailable, but allocated bytes for string storage.
GblQuark_utilization | ( | void | ) |
Returns the utilization factor of total capacity vs bytes used (ranging 0.0-1.0)
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 necessary.
GblQuark_fromStatic | ( | const char * | pString | ) |
Returns the GblQuark associated with the given STATIC string, which can save an allocation when initially registering.
Equivalent to GblQuark_fromString(), except for no internal storage is allocated for the string, since its lifetime is known to be global.
pString | string literal |
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 previously registered.
GblQuark_toString | ( | GblQuark | quark | ) |
Returns the NULL-terminated interned C string associated with a given GblQuark.
Retrieves the internal C string representation of the given quark or NULL if the quark's value is GBL_QUARK_INVALID
quark | value of existing quark |
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_internStatic | ( | const char * | pString | ) |
Creates a GblQuark from the given STATIC string (if necessary, saving on allocating), also returning its interned string.
Equivalent to GblQuark_internString() except that the internal allocation of the string is ellided, because the string's lifetime is known to be global. So the return value is the same as the argument.
pString | static or literal string or NULL |