Ruby 4.1.0dev (2026-02-28 revision 85b04616269660e2d06441e2a833edad4491564d)
pm_string.c
2
3static const uint8_t empty_source[] = "";
4
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
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 const size_t path_size = sizeof(WCHAR) * ((size_t) length);
75 handle->path = xmalloc(path_size);
76 if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
77 xfree_sized(handle->path, path_size);
79 }
80
81 handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
82 if (handle->file == INVALID_HANDLE_VALUE) {
84
85 if (GetLastError() == ERROR_ACCESS_DENIED) {
86 DWORD attributes = GetFileAttributesW(handle->path);
87 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
89 }
90 }
91
92 xfree_sized(handle->path, path_size);
93 return result;
94 }
95
97}
98
102static void
103pm_string_file_handle_close(pm_string_file_handle_t *handle) {
104 xfree(handle->path);
105 CloseHandle(handle->file);
106}
107#endif
108
121pm_string_mapped_init(pm_string_t *string, const char *filepath) {
122#ifdef _WIN32
123 // Open the file for reading.
124 pm_string_file_handle_t handle;
125 pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
126 if (result != PM_STRING_INIT_SUCCESS) return result;
127
128 // Get the file size.
129 DWORD file_size = GetFileSize(handle.file, NULL);
130 if (file_size == INVALID_FILE_SIZE) {
131 pm_string_file_handle_close(&handle);
133 }
134
135 // If the file is empty, then we don't need to do anything else, we'll set
136 // the source to a constant empty string and return.
137 if (file_size == 0) {
138 pm_string_file_handle_close(&handle);
139 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
141 }
142
143 // Create a mapping of the file.
144 HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
145 if (mapping == NULL) {
146 pm_string_file_handle_close(&handle);
148 }
149
150 // Map the file into memory.
151 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
152 CloseHandle(mapping);
153 pm_string_file_handle_close(&handle);
154
155 if (source == NULL) {
157 }
158
159 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
161#elif defined(_POSIX_MAPPED_FILES)
162 // Open the file for reading
163 int fd = open(filepath, O_RDONLY);
164 if (fd == -1) {
166 }
167
168 // Stat the file to get the file size
169 struct stat sb;
170 if (fstat(fd, &sb) == -1) {
171 close(fd);
173 }
174
175 // Ensure it is a file and not a directory
176 if (S_ISDIR(sb.st_mode)) {
177 close(fd);
179 }
180
181 // mmap the file descriptor to virtually get the contents
182 size_t size = (size_t) sb.st_size;
183 uint8_t *source = NULL;
184
185 if (size == 0) {
186 close(fd);
187 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
189 }
190
191 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
192 if (source == MAP_FAILED) {
193 close(fd);
195 }
196
197 close(fd);
198 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
200#else
201 return pm_string_file_init(string, filepath);
202#endif
203}
204
211pm_string_file_init(pm_string_t *string, const char *filepath) {
212#ifdef _WIN32
213 // Open the file for reading.
214 pm_string_file_handle_t handle;
215 pm_string_init_result_t result = pm_string_file_handle_open(&handle, filepath);
216 if (result != PM_STRING_INIT_SUCCESS) return result;
217
218 // Get the file size.
219 const DWORD file_size = GetFileSize(handle.file, NULL);
220 if (file_size == INVALID_FILE_SIZE) {
221 pm_string_file_handle_close(&handle);
223 }
224
225 // If the file is empty, then we don't need to do anything else, we'll set
226 // the source to a constant empty string and return.
227 if (file_size == 0) {
228 pm_string_file_handle_close(&handle);
229 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
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);
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);
245 }
246
247 // Check the number of bytes read
248 if (bytes_read != file_size) {
249 xfree_sized(source, file_size);
250 pm_string_file_handle_close(&handle);
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 };
257#elif defined(PRISM_HAS_FILESYSTEM)
258 // Open the file for reading
259 int fd = open(filepath, O_RDONLY);
260 if (fd == -1) {
262 }
263
264 // Stat the file to get the file size
265 struct stat sb;
266 if (fstat(fd, &sb) == -1) {
267 close(fd);
269 }
270
271 // Ensure it is a file and not a directory
272 if (S_ISDIR(sb.st_mode)) {
273 close(fd);
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 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
283 }
284
285 const size_t length = (size_t) size;
286 uint8_t *source = xmalloc(length);
287 if (source == NULL) {
288 close(fd);
290 }
291
292 long bytes_read = (long) read(fd, source, length);
293 close(fd);
294
295 if (bytes_read == -1) {
296 xfree_sized(source, length);
298 }
299
300 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
302#else
303 (void) string;
304 (void) filepath;
305 perror("pm_string_file_init is not implemented for this platform");
307#endif
308}
309
314void
315pm_string_ensure_owned(pm_string_t *string) {
316 if (string->type == PM_STRING_OWNED) return;
317
318 size_t length = pm_string_length(string);
319 const uint8_t *source = pm_string_source(string);
320
321 uint8_t *memory = xmalloc(length);
322 if (!memory) return;
323
324 pm_string_owned_init(string, memory, length);
325 memcpy((void *) string->source, source, length);
326}
327
334int
335pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
336 size_t left_length = pm_string_length(left);
337 size_t right_length = pm_string_length(right);
338
339 if (left_length < right_length) {
340 return -1;
341 } else if (left_length > right_length) {
342 return 1;
343 }
344
345 return memcmp(pm_string_source(left), pm_string_source(right), left_length);
346}
347
353 return string->length;
354}
355
359PRISM_EXPORTED_FUNCTION const uint8_t *
361 return string->source;
362}
363
369 void *memory = (void *) string->source;
370
371 if (string->type == PM_STRING_OWNED) {
372 xfree(memory);
373#ifdef PRISM_HAS_MMAP
374 } else if (string->type == PM_STRING_MAPPED && string->length) {
375#if defined(_WIN32)
376 UnmapViewOfFile(memory);
377#elif defined(_POSIX_MAPPED_FILES)
378 munmap(memory, string->length);
379#endif
380#endif /* PRISM_HAS_MMAP */
381 }
382}
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string)
Returns the length associated with the string.
Definition pm_string.c:352
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:360
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:121
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
Definition pm_string.c:368
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:211
A generic string type that can have various ownership semantics.
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
Indicates that the string was successfully initialized.
Definition pm_string.h:107
@ 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
Indicates that the file that was attempted to be opened was a directory.
Definition pm_string.h:116
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:53
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
const uint8_t * source
A pointer to the start of the string.
Definition pm_string.h:35
size_t length
The length of the string in bytes of memory.
Definition pm_string.h:38
enum pm_string_t::@105 type
The type of the string.