54 assert(index < list->capacity);
57 list->
ids[index] = id;
66 for (
size_t index = 0; index < list->
size; index++) {
67 if (list->
ids[index] ==
id)
return true;
80pm_constant_pool_hash(
const uint8_t *start,
size_t length) {
84 static const uint64_t secret = 0x517cc1b727220a95ULL;
85 uint64_t hash = (uint64_t) length;
93 memcpy(&b, start + length - 4, 4);
94 hash ^= (uint64_t) a | ((uint64_t) b << 32);
95 }
else if (length > 0) {
96 hash ^= (uint64_t) start[0] | ((uint64_t) start[length >> 1] << 8) | ((uint64_t) start[length - 1] << 16);
99 }
else if (length <= 16) {
103 memcpy(&word, start, 8);
106 memcpy(&word, start + length - 8, 8);
110 const uint8_t *ptr = start;
111 size_t remaining = length;
113 while (remaining >= 8) {
115 memcpy(&word, ptr, 8);
125 memcpy(&word, start + length - 8, 8);
132 return (uint32_t) hash;
139next_power_of_two(uint32_t v) {
157is_power_of_two(uint32_t size) {
158 return (size & (size - 1)) == 0;
167 assert(is_power_of_two(pool->
capacity));
169 uint32_t next_capacity = pool->
capacity * 2;
170 const uint32_t mask = next_capacity - 1;
177 for (uint32_t index = 0; index < pool->
capacity; index++) {
183 uint32_t next_index = bucket->
hash & mask;
189 next_index = (next_index + 1) & mask;
194 next_buckets[next_index] = *bucket;
211 capacity = next_power_of_two(capacity);
225 return &pool->
constants[constant_id - 1];
234 assert(is_power_of_two(pool->
capacity));
235 const uint32_t mask = pool->
capacity - 1;
237 uint32_t hash = pm_constant_pool_hash(start, length);
238 uint32_t index = hash & mask;
242 if ((bucket->
length == length) && memcmp(bucket->
start, start, length) == 0) {
246 index = (index + 1) & mask;
258 pm_constant_pool_resize(arena, pool);
261 assert(is_power_of_two(pool->
capacity));
262 const uint32_t mask = pool->
capacity - 1;
264 uint32_t hash = pm_constant_pool_hash(start, length);
265 uint32_t index = hash & mask;
272 if ((bucket->
length == length) && memcmp(bucket->
start, start, length) == 0) {
280 bucket->
start = start;
281 bucket->
type = (
unsigned int) (
type & 0x3);
288 index = (index + 1) & mask;
293 uint32_t
id = ++pool->
size;
294 assert(pool->
size < ((uint32_t) (1 << 30)));
297 .
id = (
unsigned int) (
id & 0x3fffffff),
298 .type = (
unsigned int) (
type & 0x3),
VALUE type(ANYARGS)
ANYARGS-ed function type.
A bump allocator for the prism parser.
static PRISM_FORCE_INLINE void * pm_arena_alloc(pm_arena_t *arena, size_t size, size_t alignment)
Allocate memory from the arena.
static void * pm_arena_zalloc(pm_arena_t *arena, size_t size, size_t alignment)
Allocate zero-initialized memory from the arena.
A data structure that stores a set of strings.
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT
By default, each constant is a slice of the source.
#define PM_CONSTANT_ID_UNSET
When we allocate constants into the pool, we reserve 0 to mean that the slot is not yet filled.
unsigned int pm_constant_pool_bucket_type_t
The type of bucket in the constant pool hash map.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED
An owned constant is one for which memory has been allocated.
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT
A constant constant is known at compile time.
#define PRISM_ALIGNOF
Get the alignment requirement of a type.
size_t size
The number of constant ids in the list.
size_t capacity
The number of constant ids that have been allocated in the list.
pm_constant_id_t * ids
The constant ids in the list.
A bucket in the hash map.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string, stored directly in the bucket to avoid a pointer chase to the c...
uint32_t hash
The hash of the bucket.
unsigned int id
The incremental ID used for indexing back into the pool.
pm_constant_pool_bucket_type_t type
The type of the bucket, which determines how to free it.
The overall constant pool, which stores constants found while parsing.
uint32_t capacity
The number of buckets that have been allocated in the hash map.
pm_constant_pool_bucket_t * buckets
The buckets in the hash map.
uint32_t size
The number of buckets in the hash map.
pm_constant_t * constants
The constants that are stored in the buckets.
A constant in the pool which effectively stores a string.
const uint8_t * start
A pointer to the start of the string.