Prism Ruby parser
Data Structures | Macros | Typedefs | Functions
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>
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.
 
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...
 

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.

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.

Typedef Documentation

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

Function Documentation

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

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

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

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

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

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

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

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

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

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

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

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

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