Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)
pm_constant_pool.h File Reference

A data structure that stores a set of strings. More...

#include "prism/defines.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  pm_constant_id_list_t
 A list of constant IDs. More...
 
struct  pm_constant_pool_bucket_t
 A bucket in the hash map. More...
 
struct  pm_constant_t
 A constant in the pool which effectively stores a string. More...
 
struct  pm_constant_pool_t
 The overall constant pool, which stores constants found while parsing. More...
 

Typedefs

typedef uint32_t pm_constant_id_t
 A constant id is a unique identifier for a constant in the constant pool.
 
typedef unsigned int pm_constant_pool_bucket_type_t
 The type of bucket in the constant pool hash map.
 

Functions

void pm_constant_id_list_init (pm_constant_id_list_t *list)
 Initialize a list of constant ids.
 
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.
 
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.
 
size_t pm_constant_id_list_memsize (pm_constant_id_list_t *list)
 Get the memory size of a list of constant ids.
 
void pm_constant_id_list_free (pm_constant_id_list_t *list)
 Free the memory associated with a list of constant ids.
 
bool pm_constant_pool_init (pm_constant_pool_t *pool, uint32_t capacity)
 Initialize a new constant pool with a given capacity.
 
pm_constant_tpm_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.
 
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.
 
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.
 
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.
 
void pm_constant_pool_free (pm_constant_pool_t *pool)
 Free the memory associated with a constant pool.
 

Variables

static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0
 By default, each constant is a slice of the source.
 
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1
 An owned constant is one for which memory has been allocated.
 
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2
 A constant constant is known at compile time.
 

Detailed Description

A data structure that stores a set of strings.

Each string is assigned a unique id, which can be used to compare strings for equality. This comparison ends up being much faster than strcmp, since it only requires a single integer comparison.

Definition in file pm_constant_pool.h.

Typedef Documentation

◆ pm_constant_id_t

typedef uint32_t pm_constant_id_t

A constant id is a unique identifier for a constant in the constant pool.

Definition at line 24 of file pm_constant_pool.h.

◆ pm_constant_pool_bucket_type_t

typedef unsigned int pm_constant_pool_bucket_type_t

The type of bucket in the constant pool hash map.

This determines how the bucket should be freed.

Definition at line 85 of file pm_constant_pool.h.

Function Documentation

◆ 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.

Returns false if any potential reallocations fail.

Parameters
listThe list to append to.
idThe id to append.
Returns
Whether the append succeeded.

Returns false if any potential reallocations fail.

Definition at line 18 of file pm_constant_pool.c.

◆ 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.

Parameters
listThe list to free.

Definition at line 52 of file pm_constant_pool.c.

◆ 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.

Parameters
listThe list to check.
idThe id to check for.
Returns
Whether the list includes the given id.

Definition at line 33 of file pm_constant_pool.c.

◆ pm_constant_id_list_init()

void pm_constant_id_list_init ( pm_constant_id_list_t * list)

Initialize a list of constant ids.

Parameters
listThe list to initialize.

Definition at line 7 of file pm_constant_pool.c.

◆ 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.

Parameters
listThe list to get the memory size of.
Returns
The memory size of the list.

Definition at line 44 of file pm_constant_pool.c.

◆ pm_constant_pool_free()

void pm_constant_pool_free ( pm_constant_pool_t * pool)

Free the memory associated with a constant pool.

Parameters
poolThe pool to free.

Definition at line 282 of file pm_constant_pool.c.

◆ 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.

Parameters
poolThe pool to get the constant from.
constant_idThe id of the constant to get.
Returns
A pointer to the constant.

Definition at line 179 of file pm_constant_pool.c.

◆ 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.

Parameters
poolThe pool to initialize.
capacityThe initial capacity of the pool.
Returns
Whether the initialization succeeded.

Definition at line 159 of file pm_constant_pool.c.

◆ 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.

Returns the id of the constant, or 0 if any potential calls to resize fail.

Parameters
poolThe pool to insert the constant into.
startA pointer to the start of the constant.
lengthThe length of the constant.
Returns
The id of the constant.

Returns the id of the constant, or 0 if any potential calls to resize fail.

Definition at line 274 of file pm_constant_pool.c.

◆ 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.

Returns the id of the constant, or 0 if any potential calls to resize fail.

Parameters
poolThe pool to insert the constant into.
startA pointer to the start of the constant.
lengthThe length of the constant.
Returns
The id of the constant.

Returns the id of the constant, or 0 if any potential calls to resize fail.

Definition at line 265 of file pm_constant_pool.c.

◆ 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.

Returns the id of the constant, or 0 if any potential calls to resize fail.

Parameters
poolThe pool to insert the constant into.
startA pointer to the start of the constant.
lengthThe length of the constant.
Returns
The id of the constant.

Insert a constant into a constant pool that is a slice of a source string.

Returns the id of the constant, or 0 if any potential calls to resize fail.

Definition at line 255 of file pm_constant_pool.c.

Variable Documentation

◆ PM_CONSTANT_POOL_BUCKET_CONSTANT

const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2
static

A constant constant is known at compile time.

Definition at line 94 of file pm_constant_pool.h.

Referenced by pm_constant_pool_insert_constant(), and pm_serialize_content().

◆ PM_CONSTANT_POOL_BUCKET_DEFAULT

const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0
static

By default, each constant is a slice of the source.

Definition at line 88 of file pm_constant_pool.h.

Referenced by pm_constant_pool_insert_shared().

◆ PM_CONSTANT_POOL_BUCKET_OWNED

const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1
static

An owned constant is one for which memory has been allocated.

Definition at line 91 of file pm_constant_pool.h.

Referenced by pm_constant_pool_free(), pm_constant_pool_insert_owned(), and pm_serialize_content().