Ruby: prism/util/pm_string.c Source File

Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)

pm_string.c
1#include "prism/util/pm_string.h"
2
7 PRISM_EXPORTED_FUNCTION size_t
8pm_string_sizeof(void) {
9 return sizeof(pm_string_t);
10}
11
15void
16pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) {
17 assert(start <= end);
18
19 *string = (pm_string_t) {
20 .type = PM_STRING_SHARED,
21 .source = start,
22 .length = (size_t) (end - start)
23 };
24}
25
29void
30pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length) {
31 *string = (pm_string_t) {
32 .type = PM_STRING_OWNED,
33 .source = source,
34 .length = length
35 };
36}
37
41void
42pm_string_constant_init(pm_string_t *string, const char *source, size_t length) {
43 *string = (pm_string_t) {
44 .type = PM_STRING_CONSTANT,
45 .source = (const uint8_t *) source,
46 .length = length
47 };
48}
49
50#ifdef _WIN32
55typedef struct {
57 WCHAR *path;
58
60 HANDLE file;
61} pm_string_file_handle_t;
62
67static pm_string_init_result_t
68pm_string_file_handle_open(pm_string_file_handle_t *handle, const char *filepath) {
69 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
70 if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
71
72 handle->path = xmalloc(sizeof(WCHAR) * ((size_t) length));
73 if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
74 xfree(handle->path);
75 return PM_STRING_INIT_ERROR_GENERIC;
76 }
77
78 handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
79 if (handle->file == INVALID_HANDLE_VALUE) {
80 pm_string_init_result_t result = PM_STRING_INIT_ERROR_GENERIC;
81
82 if (GetLastError() == ERROR_ACCESS_DENIED) {
83 DWORD attributes = GetFileAttributesW(handle->path);
84 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
85 result = PM_STRING_INIT_ERROR_DIRECTORY;
86 }
87 }
88
89 xfree(handle->path);
90 return result;
91 }
92
93 return PM_STRING_INIT_SUCCESS;
94}
95
99static void
100pm_string_file_handle_close(pm_string_file_handle_t *handle) {
101 xfree(handle->path);
102 CloseHandle(handle->file);
103}
104#endif
105
117 PRISM_EXPORTED_FUNCTION pm_string_init_result_t
118pm_string_mapped_init(pm_string_t *string, const char *filepath) {
119#ifdef _WIN32
120 // Open the file for reading.
121 pm_string_file_handle_t handle;
122 pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
123 if (result != PM_STRING_INIT_SUCCESS) return result;
124
125 // Get the file size.
126 DWORD file_size = GetFileSize(handle.file, NULL);
127 if (file_size == INVALID_FILE_SIZE) {
128 pm_string_file_handle_close(&handle);
129 return PM_STRING_INIT_ERROR_GENERIC;
130 }
131
132 // If the file is empty, then we don't need to do anything else, we'll set
133 // the source to a constant empty string and return.
134 if (file_size == 0) {
135 pm_string_file_handle_close(&handle);
136 const uint8_t source[] = "";
137 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
138 return PM_STRING_INIT_SUCCESS;
139 }
140
141 // Create a mapping of the file.
142 HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
143 if (mapping == NULL) {
144 pm_string_file_handle_close(&handle);
145 return PM_STRING_INIT_ERROR_GENERIC;
146 }
147
148 // Map the file into memory.
149 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
150 CloseHandle(mapping);
151 pm_string_file_handle_close(&handle);
152
153 if (source == NULL) {
154 return PM_STRING_INIT_ERROR_GENERIC;
155 }
156
157 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
158 return PM_STRING_INIT_SUCCESS;
159#elif defined(_POSIX_MAPPED_FILES)
160 // Open the file for reading
161 int fd = open(filepath, O_RDONLY);
162 if (fd == -1) {
163 return PM_STRING_INIT_ERROR_GENERIC;
164 }
165
166 // Stat the file to get the file size
167 struct stat sb;
168 if (fstat(fd, &sb) == -1) {
169 close(fd);
170 return PM_STRING_INIT_ERROR_GENERIC;
171 }
172
173 // Ensure it is a file and not a directory
174 if (S_ISDIR(sb.st_mode)) {
175 close(fd);
176 return PM_STRING_INIT_ERROR_DIRECTORY;
177 }
178
179 // mmap the file descriptor to virtually get the contents
180 size_t size = (size_t) sb.st_size;
181 uint8_t *source = NULL;
182
183 if (size == 0) {
184 close(fd);
185 const uint8_t source[] = "";
186 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = 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 const uint8_t source[] = "";
229 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
230 return PM_STRING_INIT_SUCCESS;
231 }
232
233 // Create a buffer to read the file into.
234 uint8_t *source = xmalloc(file_size);
235 if (source == NULL) {
236 pm_string_file_handle_close(&handle);
237 return PM_STRING_INIT_ERROR_GENERIC;
238 }
239
240 // Read the contents of the file
241 DWORD bytes_read;
242 if (!ReadFile(handle.file, source, file_size, &bytes_read, NULL)) {
243 pm_string_file_handle_close(&handle);
244 return PM_STRING_INIT_ERROR_GENERIC;
245 }
246
247 // Check the number of bytes read
248 if (bytes_read != file_size) {
249 xfree(source);
250 pm_string_file_handle_close(&handle);
251 return PM_STRING_INIT_ERROR_GENERIC;
252 }
253
254 pm_string_file_handle_close(&handle);
255 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size };
256 return PM_STRING_INIT_SUCCESS;
257#elif defined(PRISM_HAS_FILESYSTEM)
258 // Open the file for reading
259 int fd = open(filepath, O_RDONLY);
260 if (fd == -1) {
261 return PM_STRING_INIT_ERROR_GENERIC;
262 }
263
264 // Stat the file to get the file size
265 struct stat sb;
266 if (fstat(fd, &sb) == -1) {
267 close(fd);
268 return PM_STRING_INIT_ERROR_GENERIC;
269 }
270
271 // Ensure it is a file and not a directory
272 if (S_ISDIR(sb.st_mode)) {
273 close(fd);
274 return PM_STRING_INIT_ERROR_DIRECTORY;
275 }
276
277 // Check the size to see if it's empty
278 size_t size = (size_t) sb.st_size;
279 if (size == 0) {
280 close(fd);
281 const uint8_t source[] = "";
282 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
283 return PM_STRING_INIT_SUCCESS;
284 }
285
286 size_t length = (size_t) size;
287 uint8_t *source = xmalloc(length);
288 if (source == NULL) {
289 close(fd);
290 return PM_STRING_INIT_ERROR_GENERIC;
291 }
292
293 long bytes_read = (long) read(fd, source, length);
294 close(fd);
295
296 if (bytes_read == -1) {
297 xfree(source);
298 return PM_STRING_INIT_ERROR_GENERIC;
299 }
300
301 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
302 return PM_STRING_INIT_SUCCESS;
303#else
304 (void) string;
305 (void) filepath;
306 perror("pm_string_file_init is not implemented for this platform");
307 return PM_STRING_INIT_ERROR_GENERIC;
308#endif
309}
310
315void
316pm_string_ensure_owned(pm_string_t *string) {
317 if (string->type == PM_STRING_OWNED) return;
318
319 size_t length = pm_string_length(string);
320 const uint8_t *source = pm_string_source(string);
321
322 uint8_t *memory = xmalloc(length);
323 if (!memory) return;
324
325 pm_string_owned_init(string, memory, length);
326 memcpy((void *) string->source, source, length);
327}
328
335int
336pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
337 size_t left_length = pm_string_length(left);
338 size_t right_length = pm_string_length(right);
339
340 if (left_length < right_length) {
341 return -1;
342 } else if (left_length > right_length) {
343 return 1;
344 }
345
346 return memcmp(pm_string_source(left), pm_string_source(right), left_length);
347}
348
352 PRISM_EXPORTED_FUNCTION size_t
353pm_string_length(const pm_string_t *string) {
354 return string->length;
355}
356
360 PRISM_EXPORTED_FUNCTION const uint8_t *
361pm_string_source(const pm_string_t *string) {
362 return string->source;
363}
364
368 PRISM_EXPORTED_FUNCTION void
369pm_string_free(pm_string_t *string) {
370 void *memory = (void *) string->source;
371
372 if (string->type == PM_STRING_OWNED) {
373 xfree(memory);
374#ifdef PRISM_HAS_MMAP
375 } else if (string->type == PM_STRING_MAPPED && string->length) {
376#if defined(_WIN32)
377 UnmapViewOfFile(memory);
378#elif defined(_POSIX_MAPPED_FILES)
379 munmap(memory, string->length);
380#endif
381#endif /* PRISM_HAS_MMAP */
382 }
383}
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:8
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:50
pm_string_t
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
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::type
enum pm_string_t::@346265266332060241255337121126133217326336224105 type
The type of the string.