2025-12-02 06:54:32 +01:00
|
|
|
#include "methodcache.h"
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
MethodCache_t* rava_methodcache_create(void) {
|
|
|
|
|
MethodCache_t* cache = malloc(sizeof(MethodCache_t));
|
|
|
|
|
if (cache) {
|
|
|
|
|
memset(cache, 0, sizeof(MethodCache_t));
|
|
|
|
|
}
|
|
|
|
|
return cache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rava_methodcache_destroy(MethodCache_t* cache) {
|
|
|
|
|
if (cache) free(cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RavaMethod_t* rava_methodcache_lookup(MethodCache_t* cache,
|
|
|
|
|
const char* classname,
|
|
|
|
|
const char* methodname) {
|
|
|
|
|
if (!cache || !classname || !methodname) return NULL;
|
|
|
|
|
|
|
|
|
|
uint32_t class_hash = rava_methodcache_hash(classname);
|
|
|
|
|
uint32_t method_hash = rava_methodcache_hash(methodname);
|
|
|
|
|
uint32_t idx = (class_hash ^ method_hash) & RAVA_METHOD_CACHE_MASK;
|
|
|
|
|
|
|
|
|
|
MethodCacheEntry_t* entry = &cache->entries[idx];
|
|
|
|
|
|
2025-12-03 15:01:02 +01:00
|
|
|
for (int i = 0; i < RAVA_METHOD_CACHE_WAYS; i++) {
|
|
|
|
|
MethodCacheSlot_t* slot = &entry->slots[i];
|
|
|
|
|
if (slot->class_hash == class_hash &&
|
|
|
|
|
slot->method_hash == method_hash &&
|
|
|
|
|
slot->method != NULL) {
|
|
|
|
|
entry->lru = (uint8_t)i;
|
|
|
|
|
return slot->method;
|
|
|
|
|
}
|
2025-12-02 06:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rava_methodcache_insert(MethodCache_t* cache,
|
|
|
|
|
const char* classname,
|
|
|
|
|
const char* methodname,
|
|
|
|
|
RavaMethod_t* method) {
|
|
|
|
|
if (!cache || !classname || !methodname) return;
|
|
|
|
|
|
|
|
|
|
uint32_t class_hash = rava_methodcache_hash(classname);
|
|
|
|
|
uint32_t method_hash = rava_methodcache_hash(methodname);
|
|
|
|
|
uint32_t idx = (class_hash ^ method_hash) & RAVA_METHOD_CACHE_MASK;
|
|
|
|
|
|
|
|
|
|
MethodCacheEntry_t* entry = &cache->entries[idx];
|
2025-12-03 15:01:02 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i < RAVA_METHOD_CACHE_WAYS; i++) {
|
|
|
|
|
if (entry->slots[i].method == NULL) {
|
|
|
|
|
entry->slots[i].class_hash = class_hash;
|
|
|
|
|
entry->slots[i].method_hash = method_hash;
|
|
|
|
|
entry->slots[i].method = method;
|
|
|
|
|
entry->lru = (uint8_t)i;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int evict = (entry->lru + 1) % RAVA_METHOD_CACHE_WAYS;
|
|
|
|
|
entry->slots[evict].class_hash = class_hash;
|
|
|
|
|
entry->slots[evict].method_hash = method_hash;
|
|
|
|
|
entry->slots[evict].method = method;
|
|
|
|
|
entry->lru = (uint8_t)evict;
|
2025-12-02 06:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rava_methodcache_clear(MethodCache_t* cache) {
|
|
|
|
|
if (cache) {
|
|
|
|
|
memset(cache->entries, 0, sizeof(cache->entries));
|
|
|
|
|
}
|
|
|
|
|
}
|