Ruby: prism/util/pm_string.h Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

pm_string.h
Go to the documentation of this file.
1
6#ifndef PRISM_STRING_H
7#define PRISM_STRING_H
8
9#include "prism/defines.h"
10
11#include <assert.h>
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdlib.h>
15#include <string.h>
16
17// The following headers are necessary to read files using demand paging.
18#ifdef _WIN32
19#include <windows.h>
20#else
21#include <fcntl.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#endif
26
30 typedef struct {
32 const uint8_t *source;
33
35 size_t length;
36
38 enum {
40 PM_STRING_CONSTANT,
41
43 PM_STRING_SHARED,
44
46 PM_STRING_OWNED,
47
49 PM_STRING_MAPPED
50 } type;
51} pm_string_t;
52
59 PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void);
60
65#define PM_STRING_EMPTY ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 })
66
74void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end);
75
83void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length);
84
92void pm_string_constant_init(pm_string_t *string, const char *source, size_t length);
93
109 PRISM_EXPORTED_FUNCTION bool pm_string_mapped_init(pm_string_t *string, const char *filepath);
110
117size_t pm_string_memsize(const pm_string_t *string);
118
125void pm_string_ensure_owned(pm_string_t *string);
126
133 PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string);
134
141 PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
142
148 PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string);
149
150#endif
assert.h
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:182
pm_string_ensure_owned
void pm_string_ensure_owned(pm_string_t *string)
Ensure the string is owned.
Definition pm_string.c:165
pm_string_constant_init
void pm_string_constant_init(pm_string_t *string, const char *source, size_t length)
Initialize a constant string that doesn't own its memory source.
Definition pm_string.c:42
pm_string_owned_init
void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length)
Initialize an owned string that is responsible for freeing allocated memory.
Definition pm_string.c:30
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:190
pm_string_shared_init
void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end)
Initialize a shared string that is based on initial input.
Definition pm_string.c:16
pm_string_memsize
size_t pm_string_memsize(const pm_string_t *string)
Returns the memory size associated with the string.
Definition pm_string.c:152
pm_string_mapped_init
PRISM_EXPORTED_FUNCTION bool 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:62
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:198
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
defines.h
Macro definitions used throughout the prism library.
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:32
stdbool.h
C99 shim for <stdbool.h>
pm_string_t
A generic string type that can have various ownership semantics.
Definition pm_string.h:30
pm_string_t::source
const uint8_t * source
A pointer to the start of the string.
Definition pm_string.h:32
pm_string_t::length
size_t length
The length of the string in bytes of memory.
Definition pm_string.h:35
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:46
pm_string_t::PM_STRING_CONSTANT
@ PM_STRING_CONSTANT
This string is a constant string, and should not be freed.
Definition pm_string.h:40
pm_string_t::PM_STRING_MAPPED
@ PM_STRING_MAPPED
This string is a memory-mapped file, and should be freed using pm_string_free.
Definition pm_string.h:49
pm_string_t::PM_STRING_SHARED
@ PM_STRING_SHARED
This is a slice of another string, and should not be freed.
Definition pm_string.h:43