| 1 | /** |
| 2 | * @file pm_string.h |
| 3 | * |
| 4 | * A generic string type that can have various ownership semantics. |
| 5 | */ |
| 6 | #ifndef PRISM_STRING_H |
| 7 | #define PRISM_STRING_H |
| 8 | |
| 9 | #include "prism/defines.h" |
| 10 | |
| 11 | #include <assert.h> |
| 12 | #include <errno.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stddef.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | // The following headers are necessary to read files using demand paging. |
| 19 | #ifdef _WIN32 |
| 20 | #include <windows.h> |
| 21 | #elif defined(_POSIX_MAPPED_FILES) |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #elif defined(PRISM_HAS_FILESYSTEM) |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/stat.h> |
| 28 | #endif |
| 29 | |
| 30 | /** |
| 31 | * A generic string type that can have various ownership semantics. |
| 32 | */ |
| 33 | typedef struct { |
| 34 | /** A pointer to the start of the string. */ |
| 35 | const uint8_t *source; |
| 36 | |
| 37 | /** The length of the string in bytes of memory. */ |
| 38 | size_t length; |
| 39 | |
| 40 | /** The type of the string. This field determines how the string should be freed. */ |
| 41 | enum { |
| 42 | /** This string is a constant string, and should not be freed. */ |
| 43 | PM_STRING_CONSTANT, |
| 44 | |
| 45 | /** This is a slice of another string, and should not be freed. */ |
| 46 | PM_STRING_SHARED, |
| 47 | |
| 48 | /** This string owns its memory, and should be freed using `pm_string_free()`. */ |
| 49 | PM_STRING_OWNED, |
| 50 | |
| 51 | #ifdef PRISM_HAS_MMAP |
| 52 | /** This string is a memory-mapped file, and should be freed using `pm_string_free()`. */ |
| 53 | PM_STRING_MAPPED |
| 54 | #endif |
| 55 | } type; |
| 56 | } pm_string_t; |
| 57 | |
| 58 | /** |
| 59 | * Returns the size of the pm_string_t struct. This is necessary to allocate the |
| 60 | * correct amount of memory in the FFI backend. |
| 61 | * |
| 62 | * @return The size of the pm_string_t struct. |
| 63 | */ |
| 64 | PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void); |
| 65 | |
| 66 | /** |
| 67 | * Defines an empty string. This is useful for initializing a string that will |
| 68 | * be filled in later. |
| 69 | */ |
| 70 | #define PM_STRING_EMPTY ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 }) |
| 71 | |
| 72 | /** |
| 73 | * Initialize a shared string that is based on initial input. |
| 74 | * |
| 75 | * @param string The string to initialize. |
| 76 | * @param start The start of the string. |
| 77 | * @param end The end of the string. |
| 78 | */ |
| 79 | void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end); |
| 80 | |
| 81 | /** |
| 82 | * Initialize an owned string that is responsible for freeing allocated memory. |
| 83 | * |
| 84 | * @param string The string to initialize. |
| 85 | * @param source The source of the string. |
| 86 | * @param length The length of the string. |
| 87 | */ |
| 88 | void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length); |
| 89 | |
| 90 | /** |
| 91 | * Initialize a constant string that doesn't own its memory source. |
| 92 | * |
| 93 | * @param string The string to initialize. |
| 94 | * @param source The source of the string. |
| 95 | * @param length The length of the string. |
| 96 | */ |
| 97 | void pm_string_constant_init(pm_string_t *string, const char *source, size_t length); |
| 98 | |
| 99 | /** |
| 100 | * Represents the result of calling pm_string_mapped_init or |
| 101 | * pm_string_file_init. We need this additional information because there is |
| 102 | * not a platform-agnostic way to indicate that the file that was attempted to |
| 103 | * be opened was a directory. |
| 104 | */ |
| 105 | typedef enum { |
| 106 | /** Indicates that the string was successfully initialized. */ |
| 107 | PM_STRING_INIT_SUCCESS = 0, |
| 108 | /** |
| 109 | * Indicates a generic error from a string_*_init function, where the type |
| 110 | * of error should be read from `errno` or `GetLastError()`. |
| 111 | */ |
| 112 | PM_STRING_INIT_ERROR_GENERIC = 1, |
| 113 | /** |
| 114 | * Indicates that the file that was attempted to be opened was a directory. |
| 115 | */ |
| 116 | PM_STRING_INIT_ERROR_DIRECTORY = 2 |
| 117 | } pm_string_init_result_t; |
| 118 | |
| 119 | /** |
| 120 | * Read the file indicated by the filepath parameter into source and load its |
| 121 | * contents and size into the given `pm_string_t`. The given `pm_string_t` |
| 122 | * should be freed using `pm_string_free` when it is no longer used. |
| 123 | * |
| 124 | * We want to use demand paging as much as possible in order to avoid having to |
| 125 | * read the entire file into memory (which could be detrimental to performance |
| 126 | * for large files). This means that if we're on windows we'll use |
| 127 | * `MapViewOfFile`, on POSIX systems that have access to `mmap` we'll use |
| 128 | * `mmap`, and on other POSIX systems we'll use `read`. |
| 129 | * |
| 130 | * @param string The string to initialize. |
| 131 | * @param filepath The filepath to read. |
| 132 | * @return The success of the read, indicated by the value of the enum. |
| 133 | * |
| 134 | * \public \memberof pm_string_t |
| 135 | */ |
| 136 | PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_t *string, const char *filepath); |
| 137 | |
| 138 | /** |
| 139 | * Read the file indicated by the filepath parameter into source and load its |
| 140 | * contents and size into the given `pm_string_t`. The given `pm_string_t` |
| 141 | * should be freed using `pm_string_free` when it is no longer used. |
| 142 | * |
| 143 | * @param string The string to initialize. |
| 144 | * @param filepath The filepath to read. |
| 145 | * @return The success of the read, indicated by the value of the enum. |
| 146 | * |
| 147 | * \public \memberof pm_string_t |
| 148 | */ |
| 149 | PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath); |
| 150 | |
| 151 | /** |
| 152 | * Ensure the string is owned. If it is not, then reinitialize it as owned and |
| 153 | * copy over the previous source. |
| 154 | * |
| 155 | * @param string The string to ensure is owned. |
| 156 | */ |
| 157 | void pm_string_ensure_owned(pm_string_t *string); |
| 158 | |
| 159 | /** |
| 160 | * Compare the underlying lengths and bytes of two strings. Returns 0 if the |
| 161 | * strings are equal, a negative number if the left string is less than the |
| 162 | * right string, and a positive number if the left string is greater than the |
| 163 | * right string. |
| 164 | * |
| 165 | * @param left The left string to compare. |
| 166 | * @param right The right string to compare. |
| 167 | * @return The comparison result. |
| 168 | */ |
| 169 | int pm_string_compare(const pm_string_t *left, const pm_string_t *right); |
| 170 | |
| 171 | /** |
| 172 | * Returns the length associated with the string. |
| 173 | * |
| 174 | * @param string The string to get the length of. |
| 175 | * @return The length of the string. |
| 176 | * |
| 177 | * \public \memberof pm_string_t |
| 178 | */ |
| 179 | PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string); |
| 180 | |
| 181 | /** |
| 182 | * Returns the start pointer associated with the string. |
| 183 | * |
| 184 | * @param string The string to get the start pointer of. |
| 185 | * @return The start pointer of the string. |
| 186 | * |
| 187 | * \public \memberof pm_string_t |
| 188 | */ |
| 189 | PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string); |
| 190 | |
| 191 | /** |
| 192 | * Free the associated memory of the given string. |
| 193 | * |
| 194 | * @param string The string to free. |
| 195 | * |
| 196 | * \public \memberof pm_string_t |
| 197 | */ |
| 198 | PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string); |
| 199 | |
| 200 | #endif |