Ruby 4.1.0dev (2026-03-23 revision f8459601271ebbc5e1efb101387da955ed1faabb)
constant_pool.h
1#ifndef PRISM_INTERNAL_CONSTANT_POOL_H
2#define PRISM_INTERNAL_CONSTANT_POOL_H
3
5
6#include "prism/arena.h"
7
8#include <stdbool.h>
9
10/* A constant in the pool which effectively stores a string. */
12 /* A pointer to the start of the string. */
13 const uint8_t *start;
14
15 /* The length of the string. */
16 size_t length;
17};
18
19/*
20 * The type of bucket in the constant pool hash map. This determines how the
21 * bucket should be freed.
22 */
23typedef unsigned int pm_constant_pool_bucket_type_t;
24
25/* By default, each constant is a slice of the source. */
26static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
27
28/* An owned constant is one for which memory has been allocated. */
29static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
30
31/* A constant constant is known at compile time. */
32static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
33
34/* A bucket in the hash map. */
35typedef struct {
36 /* The incremental ID used for indexing back into the pool. */
37 unsigned int id: 30;
38
39 /* The type of the bucket, which determines how to free it. */
40 pm_constant_pool_bucket_type_t type: 2;
41
42 /* The hash of the bucket. */
43 uint32_t hash;
44
45 /*
46 * A pointer to the start of the string, stored directly in the bucket to
47 * avoid a pointer chase to the constants array during probing.
48 */
49 const uint8_t *start;
50
51 /* The length of the string. */
52 size_t length;
54
55/* The overall constant pool, which stores constants found while parsing. */
57 /* The buckets in the hash map. */
59
60 /* The constants that are stored in the buckets. */
61 pm_constant_t *constants;
62
63 /* The number of buckets in the hash map. */
64 uint32_t size;
65
66 /* The number of buckets that have been allocated in the hash map. */
67 uint32_t capacity;
68};
69
70/*
71 * When we allocate constants into the pool, we reserve 0 to mean that the slot
72 * is not yet filled. This constant is reused in other places to indicate the
73 * lack of a constant id.
74 */
75#define PM_CONSTANT_ID_UNSET 0
76
77/* Initialize a list of constant ids with a given capacity. */
78void pm_constant_id_list_init_capacity(pm_arena_t *arena, pm_constant_id_list_t *list, size_t capacity);
79
80/* Insert a constant id into a list of constant ids at the specified index. */
81void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id);
82
83/* Checks if the current constant id list includes the given constant id. */
84bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id);
85
86/* Initialize a new constant pool with a given capacity. */
87void pm_constant_pool_init(pm_arena_t *arena, pm_constant_pool_t *pool, uint32_t capacity);
88
89/* Return a pointer to the constant indicated by the given constant id. */
90pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
91
92/*
93 * Find a constant in a constant pool. Returns the id of the constant, or 0 if
94 * the constant is not found.
95 */
96pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
97
98/*
99 * Insert a constant into a constant pool that is a slice of a source string.
100 * Returns the id of the constant, or 0 if any potential calls to resize fail.
101 */
102pm_constant_id_t pm_constant_pool_insert_shared(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
103
104/*
105 * Insert a constant into a constant pool from memory that is now owned by the
106 * constant pool. Returns the id of the constant, or 0 if any potential calls to
107 * resize fail.
108 */
109pm_constant_id_t pm_constant_pool_insert_owned(pm_arena_t *arena, pm_constant_pool_t *pool, uint8_t *start, size_t length);
110
111/*
112 * Insert a constant into a constant pool from memory that is constant. Returns
113 * the id of the constant, or 0 if any potential calls to resize fail.
114 */
115pm_constant_id_t pm_constant_pool_insert_constant(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
116
117#endif
A bump allocator for the prism parser.
A data structure that stores a set of strings.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
C99 shim for <stdbool.h>
A list of constant IDs.