Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
Data Structures | Macros | Typedefs | Functions | Variables
pm_constant_pool.h File Reference

(348a53415339076afc4a02fcd09f3ae36e9c4c61)

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>
Include dependency graph for pm_constant_pool.h:
This graph shows which files directly or indirectly include this file:

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

Macros

#define PM_CONSTANT_ID_UNSET   0
 When we allocate constants into the pool, we reserve 0 to mean that the slot is not yet filled. More...
 

Typedefs

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

Functions

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

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. More...
 
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. More...
 
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2
 A constant constant is known at compile time. More...
 

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.

Macro Definition Documentation

◆ PM_CONSTANT_ID_UNSET

#define PM_CONSTANT_ID_UNSET   0

When we allocate constants into the pool, we reserve 0 to mean that the slot is not yet filled.

This constant is reused in other places to indicate the lack of a constant id.

Definition at line 26 of 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 31 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 101 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 30 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 68 of file pm_constant_pool.c.

Referenced by pm_node_destroy().

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

Parameters
listThe list to initialize.
capacityThe initial capacity of the list.

Definition at line 17 of file pm_constant_pool.c.

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

Parameters
listThe list to insert into.
indexThe index at which to insert.
idThe id to insert.

Definition at line 45 of file pm_constant_pool.c.

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

Returns the id of the constant, or 0 if the constant is not found.

Parameters
poolThe pool to find the constant in.
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 the constant is not found.

Definition at line 205 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 324 of file pm_constant_pool.c.

Referenced by pm_parser_free().

◆ 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 195 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 175 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 PM_CONSTANT_ID_UNSET if any potential calls to resize fail.

Definition at line 316 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,
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 PM_CONSTANT_ID_UNSET if any potential calls to resize fail.

Definition at line 306 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 PM_CONSTANT_ID_UNSET if any potential calls to resize fail.

Definition at line 296 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 110 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 104 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 107 of file pm_constant_pool.h.

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