Ruby: prism/util/pm_constant_pool.h Source File

Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)

pm_constant_pool.h
Go to the documentation of this file.
1
10#ifndef PRISM_CONSTANT_POOL_H
11#define PRISM_CONSTANT_POOL_H
12
13#include "prism/defines.h"
14
15#include <assert.h>
16#include <stdbool.h>
17#include <stdint.h>
18#include <stdlib.h>
19#include <string.h>
20
26#define PM_CONSTANT_ID_UNSET 0
27
31 typedef uint32_t pm_constant_id_t;
32
36 typedef struct {
38 size_t size;
39
41 size_t capacity;
42
44 pm_constant_id_t *ids;
45} pm_constant_id_list_t;
46
52void pm_constant_id_list_init(pm_constant_id_list_t *list);
53
60void pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity);
61
70bool pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id);
71
79void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id);
80
88bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id);
89
95void pm_constant_id_list_free(pm_constant_id_list_t *list);
96
101 typedef unsigned int pm_constant_pool_bucket_type_t;
102
104 static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
105
107 static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
108
110 static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
111
113 typedef struct {
115 unsigned int id: 30;
116
118 pm_constant_pool_bucket_type_t type: 2;
119
121 uint32_t hash;
122} pm_constant_pool_bucket_t;
123
125 typedef struct {
127 const uint8_t *start;
128
130 size_t length;
131} pm_constant_t;
132
134 typedef struct {
136 pm_constant_pool_bucket_t *buckets;
137
139 pm_constant_t *constants;
140
142 uint32_t size;
143
145 uint32_t capacity;
146} pm_constant_pool_t;
147
155bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity);
156
164 pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
165
175 pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
176
186 pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
187
198 pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length);
199
209 pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
210
216void pm_constant_pool_free(pm_constant_pool_t *pool);
217
218#endif
assert.h
pm_constant_pool_find
pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length)
Find a constant in a constant pool.
Definition pm_constant_pool.c:205
PM_CONSTANT_POOL_BUCKET_DEFAULT
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT
By default, each constant is a slice of the source.
Definition pm_constant_pool.h:104
pm_constant_pool_init
bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity)
Initialize a new constant pool with a given capacity.
Definition pm_constant_pool.c:175
pm_constant_pool_insert_shared
pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length)
Insert a constant into a constant pool that is a slice of a source string.
Definition pm_constant_pool.c:296
pm_constant_id_list_init
void pm_constant_id_list_init(pm_constant_id_list_t *list)
Initialize a list of constant ids.
Definition pm_constant_pool.c:7
pm_constant_pool_bucket_type_t
unsigned int pm_constant_pool_bucket_type_t
The type of bucket in the constant pool hash map.
Definition pm_constant_pool.h:101
pm_constant_id_list_free
void pm_constant_id_list_free(pm_constant_id_list_t *list)
Free the memory associated with a list of constant ids.
Definition pm_constant_pool.c:68
pm_constant_pool_insert_constant
pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length)
Insert a constant into a constant pool from memory that is constant.
Definition pm_constant_pool.c:316
pm_constant_id_t
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
Definition pm_constant_pool.h:31
pm_constant_id_list_insert
void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id)
Insert a constant id into a list of constant ids at the specified index.
Definition pm_constant_pool.c:45
pm_constant_id_list_append
bool pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id)
Append a constant id to a list of constant ids.
Definition pm_constant_pool.c:30
pm_constant_pool_id_to_constant
pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id)
Return a pointer to the constant indicated by the given constant id.
Definition pm_constant_pool.c:195
pm_constant_pool_insert_owned
pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length)
Insert a constant into a constant pool from memory that is now owned by the constant pool.
Definition pm_constant_pool.c:306
PM_CONSTANT_POOL_BUCKET_OWNED
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED
An owned constant is one for which memory has been allocated.
Definition pm_constant_pool.h:107
pm_constant_id_list_init_capacity
void pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity)
Initialize a list of constant ids with a given capacity.
Definition pm_constant_pool.c:17
PM_CONSTANT_POOL_BUCKET_CONSTANT
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT
A constant constant is known at compile time.
Definition pm_constant_pool.h:110
pm_constant_id_list_includes
bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id)
Checks if the current constant id list includes the given constant id.
Definition pm_constant_pool.c:57
pm_constant_pool_free
void pm_constant_pool_free(pm_constant_pool_t *pool)
Free the memory associated with a constant pool.
Definition pm_constant_pool.c:324
defines.h
Macro definitions used throughout the prism library.
stdbool.h
C99 shim for <stdbool.h>
pm_constant_id_list_t
A list of constant IDs.
Definition pm_constant_pool.h:36
pm_constant_id_list_t::size
size_t size
The number of constant ids in the list.
Definition pm_constant_pool.h:38
pm_constant_id_list_t::capacity
size_t capacity
The number of constant ids that have been allocated in the list.
Definition pm_constant_pool.h:41
pm_constant_id_list_t::ids
pm_constant_id_t * ids
The constant ids in the list.
Definition pm_constant_pool.h:44
pm_constant_pool_bucket_t
A bucket in the hash map.
Definition pm_constant_pool.h:113
pm_constant_pool_bucket_t::hash
uint32_t hash
The hash of the bucket.
Definition pm_constant_pool.h:121
pm_constant_pool_bucket_t::id
unsigned int id
The incremental ID used for indexing back into the pool.
Definition pm_constant_pool.h:115
pm_constant_pool_bucket_t::type
pm_constant_pool_bucket_type_t type
The type of the bucket, which determines how to free it.
Definition pm_constant_pool.h:118
pm_constant_pool_t
The overall constant pool, which stores constants found while parsing.
Definition pm_constant_pool.h:134
pm_constant_pool_t::capacity
uint32_t capacity
The number of buckets that have been allocated in the hash map.
Definition pm_constant_pool.h:145
pm_constant_pool_t::buckets
pm_constant_pool_bucket_t * buckets
The buckets in the hash map.
Definition pm_constant_pool.h:136
pm_constant_pool_t::size
uint32_t size
The number of buckets in the hash map.
Definition pm_constant_pool.h:142
pm_constant_pool_t::constants
pm_constant_t * constants
The constants that are stored in the buckets.
Definition pm_constant_pool.h:139
pm_constant_t
A constant in the pool which effectively stores a string.
Definition pm_constant_pool.h:125
pm_constant_t::length
size_t length
The length of the string.
Definition pm_constant_pool.h:130
pm_constant_t::start
const uint8_t * start
A pointer to the start of the string.
Definition pm_constant_pool.h:127