2023-06-20 18:53:02 +03:00
|
|
|
// The constant pool is 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.
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
#ifndef PRISM_CONSTANT_POOL_H
|
|
|
|
#define PRISM_CONSTANT_POOL_H
|
2023-06-20 18:53:02 +03:00
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
#include "prism/defines.h"
|
2023-06-30 21:30:24 +03:00
|
|
|
|
2023-09-06 19:43:36 +03:00
|
|
|
#include <assert.h>
|
2023-06-20 18:53:02 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
typedef uint32_t pm_constant_id_t;
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_constant_id_t *ids;
|
2023-06-20 18:53:02 +03:00
|
|
|
size_t size;
|
|
|
|
size_t capacity;
|
2023-09-27 19:24:48 +03:00
|
|
|
} pm_constant_id_list_t;
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
// Initialize a list of constant ids.
|
2023-09-27 19:24:48 +03:00
|
|
|
void pm_constant_id_list_init(pm_constant_id_list_t *list);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
// Append a constant id to a list of constant ids. Returns false if any
|
|
|
|
// potential reallocations fail.
|
2023-09-27 19:24:48 +03:00
|
|
|
bool pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
// Checks if the current constant id list includes the given constant id.
|
|
|
|
bool
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
// Get the memory size of a list of constant ids.
|
2023-09-27 19:24:48 +03:00
|
|
|
size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
// Free the memory associated with a list of constant ids.
|
2023-09-27 19:24:48 +03:00
|
|
|
void pm_constant_id_list_free(pm_constant_id_list_t *list);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2023-09-06 19:43:36 +03:00
|
|
|
unsigned int id: 31;
|
|
|
|
bool owned: 1;
|
2023-09-19 20:19:35 +03:00
|
|
|
uint32_t hash;
|
2023-09-19 20:56:49 +03:00
|
|
|
} pm_constant_pool_bucket_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2023-08-29 17:48:20 +03:00
|
|
|
const uint8_t *start;
|
2023-06-20 18:53:02 +03:00
|
|
|
size_t length;
|
2023-09-27 19:24:48 +03:00
|
|
|
} pm_constant_t;
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2023-09-19 20:56:49 +03:00
|
|
|
pm_constant_pool_bucket_t *buckets;
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_constant_t *constants;
|
2023-09-16 02:20:56 +03:00
|
|
|
uint32_t size;
|
|
|
|
uint32_t capacity;
|
2023-09-27 19:24:48 +03:00
|
|
|
} pm_constant_pool_t;
|
2023-06-20 18:53:02 +03:00
|
|
|
|
2023-08-17 03:57:34 +03:00
|
|
|
// Define an empty constant pool.
|
2023-09-19 20:56:49 +03:00
|
|
|
#define PM_CONSTANT_POOL_EMPTY ((pm_constant_pool_t) { .buckets = NULL, .constants = NULL, .size = 0, .capacity = 0 })
|
2023-08-17 03:57:34 +03:00
|
|
|
|
2023-06-20 18:53:02 +03:00
|
|
|
// Initialize a new constant pool with a given capacity.
|
2023-09-27 19:24:48 +03:00
|
|
|
bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
2023-10-01 13:34:55 +03:00
|
|
|
static inline pm_constant_t* pm_constant_pool_id_to_constant(pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
|
|
|
|
assert(constant_id > 0 && constant_id <= pool->size);
|
|
|
|
return &pool->constants[constant_id - 1];
|
|
|
|
}
|
|
|
|
|
2023-09-06 19:43:36 +03:00
|
|
|
// 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.
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
|
2023-09-06 19:43:36 +03:00
|
|
|
|
|
|
|
// 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.
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
// Free the memory associated with a constant pool.
|
2023-09-27 19:24:48 +03:00
|
|
|
void pm_constant_pool_free(pm_constant_pool_t *pool);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
#endif
|