Ruby: prism/util/pm_constant_pool.h Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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
24 typedef uint32_t pm_constant_id_t;
25
29 typedef struct {
31 size_t size;
32
34 size_t capacity;
35
37 pm_constant_id_t *ids;
38} pm_constant_id_list_t;
39
45void pm_constant_id_list_init(pm_constant_id_list_t *list);
46
55bool pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id);
56
64bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id);
65
72size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list);
73
79void pm_constant_id_list_free(pm_constant_id_list_t *list);
80
85 typedef unsigned int pm_constant_pool_bucket_type_t;
86
88 static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
89
91 static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
92
94 static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
95
97 typedef struct {
99 unsigned int id: 30;
100
102 pm_constant_pool_bucket_type_t type: 2;
103
105 uint32_t hash;
106} pm_constant_pool_bucket_t;
107
109 typedef struct {
111 const uint8_t *start;
112
114 size_t length;
115} pm_constant_t;
116
118 typedef struct {
120 pm_constant_pool_bucket_t *buckets;
121
123 pm_constant_t *constants;
124
126 uint32_t size;
127
129 uint32_t capacity;
130} pm_constant_pool_t;
131
139bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity);
140
148 pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
149
159 pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
160
171 pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
172
182 pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
183
189void pm_constant_pool_free(pm_constant_pool_t *pool);
190
191#endif
assert.h
pm_constant_id_list_memsize
size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list)
Get the memory size of a list of constant ids.
Definition pm_constant_pool.c:44
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:88
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:159
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:255
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:85
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:52
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:274
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:24
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:18
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:179
pm_constant_pool_insert_owned
pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const 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:265
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:91
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:94
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:33
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:282
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:29
pm_constant_id_list_t::size
size_t size
The number of constant ids in the list.
Definition pm_constant_pool.h:31
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:34
pm_constant_id_list_t::ids
pm_constant_id_t * ids
The constant ids in the list.
Definition pm_constant_pool.h:37
pm_constant_pool_bucket_t
A bucket in the hash map.
Definition pm_constant_pool.h:97
pm_constant_pool_bucket_t::hash
uint32_t hash
The hash of the bucket.
Definition pm_constant_pool.h:105
pm_constant_pool_bucket_t::id
unsigned int id
The incremental ID used for indexing back into the pool.
Definition pm_constant_pool.h:99
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:102
pm_constant_pool_t
The overall constant pool, which stores constants found while parsing.
Definition pm_constant_pool.h:118
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:129
pm_constant_pool_t::buckets
pm_constant_pool_bucket_t * buckets
The buckets in the hash map.
Definition pm_constant_pool.h:120
pm_constant_pool_t::size
uint32_t size
The number of buckets in the hash map.
Definition pm_constant_pool.h:126
pm_constant_pool_t::constants
pm_constant_t * constants
The constants that are stored in the buckets.
Definition pm_constant_pool.h:123
pm_constant_t
A constant in the pool which effectively stores a string.
Definition pm_constant_pool.h:109
pm_constant_t::length
size_t length
The length of the string.
Definition pm_constant_pool.h:114
pm_constant_t::start
const uint8_t * start
A pointer to the start of the string.
Definition pm_constant_pool.h:111