Ruby 3.3.2p78 (2024-05-30 revision e5a195edf62fe1bf7146a191da13fa1c4fecbd71)
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
30typedef struct {
32 const uint8_t *source;
33
35 size_t length;
36
38 enum {
41
44
47
49 PM_STRING_MAPPED
50 } type;
52
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
109PRISM_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
126
134
141PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
142
149
150#endif
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
void pm_string_ensure_owned(pm_string_t *string)
Ensure the string is owned.
Definition pm_string.c:165
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
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
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
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
size_t pm_string_memsize(const pm_string_t *string)
Returns the memory size associated with the string.
Definition pm_string.c:152
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
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
Definition pm_string.c:198
PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void)
Returns the size of the pm_string_t struct.
Definition pm_string.c:8
Macro definitions used throughout the prism library.
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:32
C99 shim for <stdbool.h>
A generic string type that can have various ownership semantics.
Definition pm_string.h:30
const uint8_t * source
A pointer to the start of the string.
Definition pm_string.h:32
@ PM_STRING_OWNED
This string owns its memory, and should be freed using pm_string_free.
Definition pm_string.h:46
@ PM_STRING_CONSTANT
This string is a constant string, and should not be freed.
Definition pm_string.h:40
@ PM_STRING_SHARED
This is a slice of another string, and should not be freed.
Definition pm_string.h:43
size_t length
The length of the string in bytes of memory.
Definition pm_string.h:35