1#include "prism/internal/constant_pool.h"
5#include "prism/internal/arena.h"
60 assert(index < list->capacity);
61 assert(list->
ids[index] == PM_CONSTANT_ID_UNSET);
63 list->
ids[index] = id;
72 for (
size_t index = 0; index < list->
size; index++) {
73 if (list->
ids[index] ==
id)
return true;
86pm_constant_pool_hash(
const uint8_t *start,
size_t length) {
90 static const uint64_t secret = 0x517cc1b727220a95ULL;
91 uint64_t hash = (uint64_t) length;
99 memcpy(&b, start + length - 4, 4);
100 hash ^= (uint64_t) a | ((uint64_t) b << 32);
101 }
else if (length > 0) {
102 hash ^= (uint64_t) start[0] | ((uint64_t) start[length >> 1] << 8) | ((uint64_t) start[length - 1] << 16);
105 }
else if (length <= 16) {
109 memcpy(&word, start, 8);
112 memcpy(&word, start + length - 8, 8);
116 const uint8_t *ptr = start;
117 size_t remaining = length;
119 while (remaining >= 8) {
121 memcpy(&word, ptr, 8);
131 memcpy(&word, start + length - 8, 8);
138 return (uint32_t) hash;
145next_power_of_two(uint32_t v) {
163is_power_of_two(uint32_t size) {
164 return (size & (size - 1)) == 0;
173 assert(is_power_of_two(pool->capacity));
175 uint32_t next_capacity = pool->capacity * 2;
176 const uint32_t mask = next_capacity - 1;
183 for (uint32_t index = 0; index < pool->capacity; index++) {
188 if (bucket->id != PM_CONSTANT_ID_UNSET) {
189 uint32_t next_index = bucket->hash & mask;
194 while (next_buckets[next_index].
id != PM_CONSTANT_ID_UNSET) {
195 next_index = (next_index + 1) & mask;
200 next_buckets[next_index] = *bucket;
205 memcpy(next_constants, pool->constants, pool->size *
sizeof(
pm_constant_t));
207 pool->constants = next_constants;
208 pool->buckets = next_buckets;
209 pool->capacity = next_capacity;
217 capacity = next_power_of_two(capacity);
222 pool->capacity = capacity;
230 assert(constant_id != PM_CONSTANT_ID_UNSET && constant_id <= pool->size);
231 return &pool->constants[constant_id - 1];
239pm_constant_pool_find(
const pm_constant_pool_t *pool,
const uint8_t *start,
size_t length) {
240 assert(is_power_of_two(pool->capacity));
241 const uint32_t mask = pool->capacity - 1;
243 uint32_t hash = pm_constant_pool_hash(start, length);
244 uint32_t index = hash & mask;
247 while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
248 if ((bucket->length == length) && memcmp(bucket->start, start, length) == 0) {
252 index = (index + 1) & mask;
255 return PM_CONSTANT_ID_UNSET;
262pm_constant_pool_insert(
pm_arena_t *arena,
pm_constant_pool_t *pool,
const uint8_t *start,
size_t length, pm_constant_pool_bucket_type_t type) {
263 if (pool->size >= (pool->capacity / 4 * 3)) {
264 pm_constant_pool_resize(arena, pool);
267 assert(is_power_of_two(pool->capacity));
268 const uint32_t mask = pool->capacity - 1;
270 uint32_t hash = pm_constant_pool_hash(start, length);
271 uint32_t index = hash & mask;
274 while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
278 if ((bucket->length == length) && memcmp(bucket->start, start, length) == 0) {
282 if (type != PM_CONSTANT_POOL_BUCKET_OWNED && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
286 bucket->start = start;
287 bucket->type = (
unsigned int) (type & 0x3);
288 pool->constants[bucket->id - 1].start = start;
294 index = (index + 1) & mask;
299 uint32_t
id = ++pool->size;
300 assert(pool->size < ((uint32_t) (1 << 30)));
303 .id = (
unsigned int) (
id & 0x3fffffff),
304 .type = (
unsigned int) (type & 0x3),
324 return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT);
334 return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
344 return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT);
352 return constant->start;
359 return constant->length;
#define PRISM_ALIGNOF
Get the alignment requirement of a type.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
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.