22 if (list->
ids == NULL)
return false;
34 for (
size_t index = 0; index < list->
size; index++) {
35 if (list->
ids[index] ==
id)
return true;
53 if (list->
ids != NULL) {
63pm_constant_pool_hash(
const uint8_t *start,
size_t length) {
65 uint32_t value = 5381;
67 for (
size_t index = 0; index < length; index++) {
68 value = ((value << 5) + value) + start[index];
78next_power_of_two(uint32_t v) {
96is_power_of_two(uint32_t size) {
97 return (size & (size - 1)) == 0;
106 assert(is_power_of_two(pool->
capacity));
108 uint32_t next_capacity = pool->
capacity * 2;
109 if (next_capacity < pool->capacity)
return false;
111 const uint32_t mask = next_capacity - 1;
114 void *next = calloc(next_capacity, element_size);
115 if (next == NULL)
return false;
122 for (uint32_t index = 0; index < pool->
capacity; index++) {
127 if (bucket->
id != 0) {
128 uint32_t next_index = bucket->
hash & mask;
133 while (next_buckets[next_index].
id != 0) {
134 next_index = (next_index + 1) & mask;
139 next_buckets[next_index] = *bucket;
160 const uint32_t maximum = (~((uint32_t) 0));
161 if (capacity >= ((maximum / 2) + 1))
return false;
163 capacity = next_power_of_two(capacity);
165 void *memory = calloc(capacity, element_size);
166 if (memory == NULL)
return false;
180 assert(constant_id > 0 && constant_id <= pool->size);
181 return &pool->
constants[constant_id - 1];
190 if (!pm_constant_pool_resize(pool))
return 0;
193 assert(is_power_of_two(pool->
capacity));
194 const uint32_t mask = pool->
capacity - 1;
196 uint32_t hash = pm_constant_pool_hash(start, length);
197 uint32_t index = hash & mask;
200 while (bucket = &pool->
buckets[index], bucket->
id != 0) {
206 if ((constant->
length == length) && memcmp(constant->
start, start, length) == 0) {
215 free((
void *) start);
220 free((
void *) constant->
start);
221 constant->
start = start;
228 index = (index + 1) & mask;
233 uint32_t
id = ++pool->
size;
234 assert(pool->
size < ((uint32_t) (1 << 30)));
237 .id = (
unsigned int) (
id & 0x3fffffff),
238 .type = (
unsigned int) (type & 0x3),
285 for (uint32_t index = 0; index < pool->
capacity; index++) {
291 free((
void *) constant->
start);
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.
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.
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.
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.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string.