infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
cache.c
Go to the documentation of this file.
1
20#include <stdlib.h>
21#include <string.h>
22
24#define CACHE_BUCKETS 1021
25
31
36
41static uint64_t _cache_hash(const char * sig, void * target_fn, bool is_safe) {
42 uint64_t h = 5381;
43 int c;
44 while ((c = *sig++))
45 h = ((h << 5) + h) + c;
46 h ^= (uint64_t)(uintptr_t)target_fn;
47 if (is_safe)
48 h ^= 0x123456789ABCDEF0ULL;
49 return h;
50}
51
57infix_forward_t * _infix_cache_lookup(const char * signature, void * target_fn, bool is_safe) {
58 uint64_t h = _cache_hash(signature, target_fn, is_safe);
59 size_t index = h % CACHE_BUCKETS;
60
62 for (_cache_entry_t * entry = g_trampoline_cache[index]; entry; entry = entry->next) {
63 if (entry->trampoline->target_fn == target_fn && entry->trampoline->is_safe == is_safe &&
64 strcmp(entry->trampoline->signature, signature) == 0) {
65 entry->trampoline->ref_count++;
67 return entry->trampoline;
68 }
69 }
71 return NULL;
72}
73
80 size_t index = h % CACHE_BUCKETS;
81
83 // Double check it's not already there
84 for (_cache_entry_t * entry = g_trampoline_cache[index]; entry; entry = entry->next) {
85 if (entry->trampoline->target_fn == trampoline->target_fn &&
86 entry->trampoline->is_safe == trampoline->is_safe &&
87 strcmp(entry->trampoline->signature, trampoline->signature) == 0) {
89 return;
90 }
91 }
92
94 if (!entry) {
96 return;
97 }
98
99 entry->trampoline = trampoline;
100 trampoline->ref_count++; // Cache reference
101 entry->next = g_trampoline_cache[index];
102 g_trampoline_cache[index] = entry;
104}
105
112 for (size_t i = 0; i < CACHE_BUCKETS; ++i) {
114 while (entry) {
115 _cache_entry_t * next = entry->next;
116 if (--entry->trampoline->ref_count == 0)
118 infix_free(entry);
119 entry = next;
120 }
121 g_trampoline_cache[i] = nullptr;
122 }
124}
125
131 if (!trampoline->signature)
132 return false;
134 size_t index = h % CACHE_BUCKETS;
135
137 while (*p) {
138 if ((*p)->trampoline == trampoline) {
139 _cache_entry_t * to_free = *p;
140 *p = to_free->next;
141 infix_free(to_free);
142 trampoline->ref_count--; // Decrement since cache no longer holds it
143 return true;
144 }
145 p = &((*p)->next);
146 }
147 return false;
148}
149
161
167 if (!trampoline)
168 return;
169
171 if (--trampoline->ref_count > 0) {
173 return;
174 }
175
176 // Reference count is 0. Remove from cache and destroy.
179
181}
char * p
Definition 904_registry_benchmark.c:25
void _infix_cache_release(infix_forward_t *trampoline)
Definition cache.c:166
static bool _cache_remove_no_lock(infix_forward_t *trampoline)
Definition cache.c:130
static infix_mutex_t g_cache_mutex
Definition cache.c:35
#define CACHE_BUCKETS
Definition cache.c:24
void _infix_cache_clear(void)
Definition cache.c:110
bool _infix_cache_remove(infix_forward_t *trampoline)
Definition cache.c:155
static _cache_entry_t * g_trampoline_cache[CACHE_BUCKETS]
Definition cache.c:33
static uint64_t _cache_hash(const char *sig, void *target_fn, bool is_safe)
Definition cache.c:41
infix_forward_t * _infix_cache_lookup(const char *signature, void *target_fn, bool is_safe)
Definition cache.c:57
void _infix_cache_insert(infix_forward_t *trampoline)
Definition cache.c:78
#define infix_free
A macro that can be defined to override the default free function.
Definition infix.h:383
#define infix_malloc
A macro that can be defined to override the default malloc function.
Definition infix.h:371
Internal data structures, function prototypes, and constants.
pthread_mutex_t infix_mutex_t
Definition infix_internals.h:161
#define INFIX_MUTEX_INITIALIZER
Definition infix_internals.h:162
#define INFIX_MUTEX_UNLOCK(m)
Definition infix_internals.h:164
INFIX_INTERNAL void _infix_forward_destroy_internal(infix_forward_t *trampoline)
Definition trampoline.c:701
#define INFIX_MUTEX_LOCK(m)
Definition infix_internals.h:163
Definition cache.c:27
infix_forward_t * trampoline
Definition cache.c:28
struct _cache_entry_t * next
Definition cache.c:29
Internal definition of a forward trampoline handle.
Definition infix_internals.h:90
void * target_fn
Definition infix_internals.h:99
size_t ref_count
Definition infix_internals.h:102
bool is_safe
Definition infix_internals.h:101
char * signature
Definition infix_internals.h:103