Ruby: prism/util/pm_string.c Source File

Ruby 3.4.9p82 (2026-03-11 revision 76cca827ab52ab1d346a728f068d5b8da3e2952b)

pm_string.c
1#include "prism/util/pm_string.h"
2
3static const uint8_t empty_source[] = "";
4
9 PRISM_EXPORTED_FUNCTION size_t
10pm_string_sizeof(void) {
11 return sizeof(pm_string_t);
12}
13
17void
18pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) {
19 assert(start <= end);
20
21 *string = (pm_string_t) {
22 .type = PM_STRING_SHARED,
23 .source = start,
24 .length = (size_t) (end - start)
25 };
26}
27
31void
32pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length) {
33 *string = (pm_string_t) {
34 .type = PM_STRING_OWNED,
35 .source = source,
36 .length = length
37 };
38}
39
43void
44pm_string_constant_init(pm_string_t *string, const char *source, size_t length) {
45 *string = (pm_string_t) {
46 .type = PM_STRING_CONSTANT,
47 .source = (const uint8_t *) source,
48 .length = length
49 };
50}
51
52#ifdef _WIN32
57typedef struct {
59 WCHAR *path;
60
62 HANDLE file;
63} pm_string_file_handle_t;
64
69static pm_string_init_result_t
70pm_string_file_handle_open(pm_string_file_handle_t *handle, const char *filepath) {
71 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
72 if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
73
74 handle->path = xmalloc(sizeof(WCHAR) * ((size_t) length));
75 if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
76 xfree(handle->path);
77 return PM_STRING_INIT_ERROR_GENERIC;
78 }
79
80 handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
81 if (handle->file == INVALID_HANDLE_VALUE) {
82 pm_string_init_result_t result = PM_STRING_INIT_ERROR_GENERIC;
83
84 if (GetLastError() == ERROR_ACCESS_DENIED) {
85 DWORD attributes = GetFileAttributesW(handle->path);
86 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
87 result = PM_STRING_INIT_ERROR_DIRECTORY;
88 }
89 }
90
91 xfree(handle->path);
92 return result;
93 }
94
95 return PM_STRING_INIT_SUCCESS;
96}
97
101static void
102pm_string_file_handle_close(pm_string_file_handle_t *handle) {
103 xfree(handle->path);
104 CloseHandle(handle->file);
105}
106#endif
107
119 PRISM_EXPORTED_FUNCTION pm_string_init_result_t
120pm_string_mapped_init(pm_string_t *string, const char *filepath) {
121#ifdef _WIN32
122 // Open the file for reading.
123 pm_string_file_handle_t handle;
124 pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
125 if (result != PM_STRING_INIT_SUCCESS) return result;
126
127 // Get the file size.
128 DWORD file_size = GetFileSize(handle.file, NULL);
129 if (file_size == INVALID_FILE_SIZE) {
130 pm_string_file_handle_close(&handle);
131 return PM_STRING_INIT_ERROR_GENERIC;
132 }
133
134 // If the file is empty, then we don't need to do anything else, we'll set
135 // the source to a constant empty string and return.
136 if (file_size == 0) {
137 pm_string_file_handle_close(&handle);
138 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
139 return PM_STRING_INIT_SUCCESS;
140 }
141
142 // Create a mapping of the file.
143 HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
144 if (mapping == NULL) {
145 pm_string_file_handle_close(&handle);
146 return PM_STRING_INIT_ERROR_GENERIC;
147 }
148
149 // Map the file into memory.
150 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
151 CloseHandle(mapping);
152 pm_string_file_handle_close(&handle);
153
154 if (source == NULL) {
155 return PM_STRING_INIT_ERROR_GENERIC;
156 }
157
158 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
159 return PM_STRING_INIT_SUCCESS;
160#elif defined(_POSIX_MAPPED_FILES)
161 // Open the file for reading
162 int fd = open(filepath, O_RDONLY);
163 if (fd == -1) {
164 return PM_STRING_INIT_ERROR_GENERIC;
165 }
166
167 // Stat the file to get the file size
168 struct stat sb;
169 if (fstat(fd, &sb) == -1) {
170 close(fd);
171 return PM_STRING_INIT_ERROR_GENERIC;
172 }
173
174 // Ensure it is a file and not a directory
175 if (S_ISDIR(sb.st_mode)) {
176 close(fd);
177 return PM_STRING_INIT_ERROR_DIRECTORY;
178 }
179
180 // mmap the file descriptor to virtually get the contents
181 size_t size = (size_t) sb.st_size;
182 uint8_t *source = NULL;
183
184 if (size == 0) {
185 close(fd);
186 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
187 return PM_STRING_INIT_SUCCESS;
188 }
189
190 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
191 if (source == MAP_FAILED) {
192 close(fd);
193 return PM_STRING_INIT_ERROR_GENERIC;
194 }
195
196 close(fd);
197 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
198 return PM_STRING_INIT_SUCCESS;
199#else
200 return pm_string_file_init(string, filepath);
201#endif
202}
203
209 PRISM_EXPORTED_FUNCTION pm_string_init_result_t
210pm_string_file_init(pm_string_t *string, const char *filepath) {
211#ifdef _WIN32
212 // Open the file for reading.
213 pm_string_file_handle_t handle;
214 pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
215 if (result != PM_STRING_INIT_SUCCESS) return result;
216
217 // Get the file size.
218 DWORD file_size = GetFileSize(handle.file, NULL);
219 if (file_size == INVALID_FILE_SIZE) {
220 pm_string_file_handle_close(&handle);
221 return PM_STRING_INIT_ERROR_GENERIC;
222 }
223
224 // If the file is empty, then we don't need to do anything else, we'll set
225 // the source to a constant empty string and return.
226 if (file_size == 0) {
227 pm_string_file_handle_close(&handle);
228 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
229 return PM_STRING_INIT_SUCCESS;
230 }
231
232 // Create a buffer to read the file into.
233 uint8_t *source = xmalloc(file_size);
234 if (source == NULL) {
235 pm_string_file_handle_close(&handle);
236 return PM_STRING_INIT_ERROR_GENERIC;
237 }
238
239 // Read the contents of the file
240 DWORD bytes_read;
241 if (!ReadFile(handle.file, source, file_size, &bytes_read, NULL)) {
242 pm_string_file_handle_close(&handle);
243 return PM_STRING_INIT_ERROR_GENERIC;
244 }
245
246 // Check the number of bytes read
247 if (bytes_read != file_size) {
248 xfree(source);
249 pm_string_file_handle_close(&handle);
250 return PM_STRING_INIT_ERROR_GENERIC;
251 }
252
253 pm_string_file_handle_close(&handle);
254 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size };
255 return PM_STRING_INIT_SUCCESS;
256#elif defined(PRISM_HAS_FILESYSTEM)
257 // Open the file for reading
258 int fd = open(filepath, O_RDONLY);
259 if (fd == -1) {
260 return PM_STRING_INIT_ERROR_GENERIC;
261 }
262
263 // Stat the file to get the file size
264 struct stat sb;
265 if (fstat(fd, &sb) == -1) {
266 close(fd);
267 return PM_STRING_INIT_ERROR_GENERIC;
268 }
269
270 // Ensure it is a file and not a directory
271 if (S_ISDIR(sb.st_mode)) {
272 close(fd);
273 return PM_STRING_INIT_ERROR_DIRECTORY;
274 }
275
276 // Check the size to see if it's empty
277 size_t size = (size_t) sb.st_size;
278 if (size == 0) {
279 close(fd);
280 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
281 return PM_STRING_INIT_SUCCESS;
282 }
283
284 size_t length = (size_t) size;
285 uint8_t *source = xmalloc(length);
286 if (source == NULL) {
287 close(fd);
288 return PM_STRING_INIT_ERROR_GENERIC;
289 }
290
291 long bytes_read = (long) read(fd, source, length);
292 close(fd);
293
294 if (bytes_read == -1) {
295 xfree(source);
296 return PM_STRING_INIT_ERROR_GENERIC;
297 }
298
299 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
300 return PM_STRING_INIT_SUCCESS;
301#else
302 (void) string;
303 (void) filepath;
304 perror("pm_string_file_init is not implemented for this platform");
305 return PM_STRING_INIT_ERROR_GENERIC;
306#endif
307}
308
313void
314pm_string_ensure_owned(pm_string_t *string) {
315 if (string->type == PM_STRING_OWNED) return;
316
317 size_t length = pm_string_length(string);
318 const uint8_t *source = pm_string_source(string);
319
320 uint8_t *memory = xmalloc(length);
321 if (!memory) return;
322
323 pm_string_owned_init(string, memory, length);
324 memcpy((void *) string->source, source, length);
325}
326
333int
334pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
335 size_t left_length = pm_string_length(left);
336 size_t right_length = pm_string_length(right);
337
338 if (left_length < right_length) {
339 return -1;
340 } else if (left_length > right_length) {
341 return 1;
342 }
343
344 return memcmp(pm_string_source(left), pm_string_source(right), left_length);
345}
346
350 PRISM_EXPORTED_FUNCTION size_t
351pm_string_length(const pm_string_t *string) {
352 return string->length;
353}
354
358 PRISM_EXPORTED_FUNCTION const uint8_t *
359pm_string_source(const pm_string_t *string) {
360 return string->source;
361}
362
366 PRISM_EXPORTED_FUNCTION void
367pm_string_free(pm_string_t *string) {
368 void *memory = (void *) string->source;
369
370 if (string->type == PM_STRING_OWNED) {
371 xfree(memory);
372#ifdef PRISM_HAS_MMAP
373 } else if (string->type == PM_STRING_MAPPED && string->length) {
374#if defined(_WIN32)
375 UnmapViewOfFile(memory);
376#elif defined(_POSIX_MAPPED_FILES)
377 munmap(memory, string->length);
378#endif
379#endif /* PRISM_HAS_MMAP */
380 }
381}
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
xmalloc
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
pm_string.h
A generic string type that can have various ownership semantics.
pm_string_init_result_t
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
Definition pm_string.h:105
PM_STRING_INIT_SUCCESS
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
Definition pm_string.h:107
PM_STRING_INIT_ERROR_GENERIC
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
Definition pm_string.h:112
PM_STRING_INIT_ERROR_DIRECTORY
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
Definition pm_string.h:116
pm_string_sizeof
PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void)
Returns the size of the pm_string_t struct.
Definition pm_string.c:10
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:53
pm_string_t
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
pm_string_t::pm_string_length
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string)
Returns the length associated with the string.
Definition pm_string.c:351
pm_string_t::source
const uint8_t * source
A pointer to the start of the string.
Definition pm_string.h:35
pm_string_t::length
size_t length
The length of the string in bytes of memory.
Definition pm_string.h:38
pm_string_t::pm_string_source
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string)
Returns the start pointer associated with the string.
Definition pm_string.c:359
pm_string_t::pm_string_mapped_init
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_t *string, const char *filepath)
Read the file indicated by the filepath parameter into source and load its contents and size into the...
Definition pm_string.c:120
pm_string_t::pm_string_free
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
Definition pm_string.c:367
pm_string_t::type
enum pm_string_t::@346265266332060241255337121126133217326336224105 type
The type of the string.
pm_string_t::PM_STRING_OWNED
@ PM_STRING_OWNED
This string owns its memory, and should be freed using pm_string_free().
Definition pm_string.h:49
pm_string_t::PM_STRING_CONSTANT
@ PM_STRING_CONSTANT
This string is a constant string, and should not be freed.
Definition pm_string.h:43
pm_string_t::pm_string_file_init
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath)
Read the file indicated by the filepath parameter into source and load its contents and size into the...
Definition pm_string.c:210