123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- struct scm_cache_entry
- {
- scm_t_bits key
- scm_t_bits value
- }
- struct scm_cache
- {
- scm_t_bits eviction_cookie
- struct scm_cache_entry entries[SCM_CACHE_SIZE];
- }
- static inline struct scm_cache*
- scm_make_cache (void)
- {
- struct scm_cache *ret = scm_gc_typed_calloc (struct scm_cache);
- ret->eviction_cookie = (scm_t_bits) ret
- return ret
- }
- static inline int
- scm_cache_full_p (struct scm_cache *cache)
- {
- return cache->entries[0].key != 0
- }
- static inline void
- scm_cache_evict_1 (struct scm_cache *cache, struct scm_cache_entry *evicted)
- {
- size_t idx
- cache->eviction_cookie = scm_ihashq (SCM_PACK (cache->eviction_cookie), -1)
- idx = cache->eviction_cookie & (SCM_CACHE_SIZE - 1)
- memcpy (evicted, cache->entries + idx, sizeof (*evicted))
- memmove (cache->entries + 1,
- cache->entries,
- sizeof (cache->entries[0]) * idx)
- cache->entries[0].key = 0
- cache->entries[0].value = 0
- }
- static inline struct scm_cache_entry*
- scm_cache_lookup (struct scm_cache *cache, SCM k)
- {
- scm_t_bits k_bits = SCM_UNPACK (k)
- struct scm_cache_entry *entry = cache->entries;
-
- if (entry[8].key <= k_bits) entry += 8
- if (entry[4].key <= k_bits) entry += 4
- if (entry[2].key <= k_bits) entry += 2
- if (entry[1].key <= k_bits) entry += 1
- return entry
- }
- static inline void
- scm_cache_insert (struct scm_cache *cache, SCM k, SCM v,
- struct scm_cache_entry *evicted)
- {
- struct scm_cache_entry *entry
- if (scm_cache_full_p (cache))
- scm_cache_evict_1 (cache, evicted)
- entry = scm_cache_lookup (cache, k)
- if (entry->key == SCM_UNPACK (k))
- {
- entry->value = SCM_UNPACK (v)
- return
- }
- memmove (cache->entries,
- cache->entries + 1,
- (entry - cache->entries) * sizeof (*entry))
- entry->key = SCM_UNPACK (k)
- entry->value = SCM_UNPACK (v)
- }
|