Debian Codegraph @

include/prism/util/pm_string.h

raw symbols SCIP
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_Hcxx . . $ `include/prism/util/pm_string.h:7:9`!
8
9 #include "prism/defines.h"cxx . . $ `<file>/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_FILEScxx . . $ `<external>/4533468968537266721/posix_opt.h:41:9`!)
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 structcxx . . $ $anonymous_type_cbb08f63caf075c2_0# {
34 /** A pointer to the start of the string. */
35 const uint8_tcxx . . $ uint8_t# *sourcecxx . . $ $anonymous_type_cbb08f63caf075c2_0#source.;
36
37 /** The length of the string in bytes of memory. */
38 size_tcxx . . $ size_t# lengthcxx . . $ $anonymous_type_cbb08f63caf075c2_0#length.;
39
40 /** The type of the string. This field determines how the string should be freed. */
41 enumcxx . . $ $anonymous_type_cbb08f63caf075c2_0#$anonymous_type_1# {
42 /** This string is a constant string, and should not be freed. */
43 PM_STRING_CONSTANTcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_CONSTANT.,
44
45 /** This is a slice of another string, and should not be freed. */
46 PM_STRING_SHAREDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_SHARED.,
47
48 /** This string owns its memory, and should be freed using `pm_string_free()`. */
49 PM_STRING_OWNEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_OWNED.,
50
51 #ifdef PRISM_HAS_MMAPcxx . . $ `include/prism/defines.h:129:16`!
52 /** This string is a memory-mapped file, and should be freed using `pm_string_free()`. */
53 PM_STRING_MAPPEDcxx . . $ $anonymous_type_cbb08f63caf075c2_0#PM_STRING_MAPPED.
54 #endif
55 } typecxx . . $ $anonymous_type_cbb08f63caf075c2_0#type.;
56 } pm_string_tcxx . . $ 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_FUNCTIONcxx . . $ `include/prism/defines.h:50:19`! size_tcxx . . $ size_t# pm_string_sizeofcxx . . $ pm_string_sizeof(f708dce9a8856c70).(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_EMPTYcxx . . $ `include/prism/util/pm_string.h:70:9`! ((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_initcxx . . $ pm_string_shared_init(3931a8f5f74a1622).(pm_string_tcxx . . $ pm_string_t# *string, const uint8_tcxx . . $ uint8_t# *start, const uint8_tcxx . . $ 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_initcxx . . $ pm_string_owned_init(15e507396f0067cc).(pm_string_tcxx . . $ pm_string_t# *string, uint8_tcxx . . $ uint8_t# *source, size_tcxx . . $ 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_initcxx . . $ pm_string_constant_init(501226d9fb5d86b1).(pm_string_tcxx . . $ pm_string_t# *string, const char *source, size_tcxx . . $ 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 enumcxx . . $ $anonymous_type_cbb08f63caf075c2_2# {
106 /** Indicates that the string was successfully initialized. */
107 PM_STRING_INIT_SUCCESScxx . . $ 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_GENERICcxx . . $ 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_DIRECTORYcxx . . $ PM_STRING_INIT_ERROR_DIRECTORY. = 2
117 } pm_string_init_result_tcxx . . $ 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_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! pm_string_init_result_tcxx . . $ pm_string_init_result_t# pm_string_mapped_initcxx . . $ pm_string_mapped_init(16ac81ad46c85296).(pm_string_tcxx . . $ 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_FUNCTIONcxx . . $ `include/prism/defines.h:50:19`! pm_string_init_result_tcxx . . $ pm_string_init_result_t# pm_string_file_initcxx . . $ pm_string_file_init(16ac81ad46c85296).(pm_string_tcxx . . $ 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_ownedcxx . . $ pm_string_ensure_owned(c15d128c68b0cb72).(pm_string_tcxx . . $ 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_comparecxx . . $ pm_string_compare(89247a2b4ba54e22).(const pm_string_tcxx . . $ pm_string_t# *left, const pm_string_tcxx . . $ 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_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! size_tcxx . . $ size_t# pm_string_lengthcxx . . $ pm_string_length(576a13ceb66f8a15).(const pm_string_tcxx . . $ 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_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! const uint8_tcxx . . $ uint8_t# * pm_string_sourcecxx . . $ pm_string_source(37c9bd456e9b3712).(const pm_string_tcxx . . $ 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_FUNCTIONcxx . . $ `include/prism/defines.h:53:16`! void pm_string_freecxx . . $ pm_string_free(c15d128c68b0cb72).(pm_string_tcxx . . $ pm_string_t# *string);
199
200 #endif