RPA Toolkit
947771f2664a1a05212499ec71d194cb7984bf6a
[rpatk.git] / rpa2 / rpacache.c
1 #include "rpacache.h"
2 #include "rmem.h"
3 #include "rparecord.h"
4
5 #define RPA_MCACHE_BUCKET(_top_, _ruleid_) ( ( (((rulong)(_top_))>>0) ^ ((((rulong)(_ruleid_))>>2)<<4) ) & RPA_MCACHE_MASK)
6
7
8 rpacache_t *rpa_cache_create()
9 {
10         rpacache_t *cache = (rpacache_t*) r_zmalloc(sizeof(*cache));
11
12         if (!cache)
13                 return NULL;
14         return cache;
15 }
16
17
18 void rpa_cache_destroy(rpacache_t *cache)
19 {
20         if (!cache)
21                 return;
22         r_free(cache);
23 }
24
25 void rpa_cache_invalidate(rpacache_t *cache)
26 {
27         ++cache->serial;
28 }
29
30
31 void rpa_cache_disable(rpacache_t *cache, rlong disable)
32 {
33         cache->disalbled = disable;
34 }
35
36
37 void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rlong startrec, rlong endrec)
38 {
39         rulong bucket = RPA_MCACHE_BUCKET(top, ruleid);
40
41         if (ret <= 0 || cache->disalbled)
42                 return;
43         cache->entry[bucket].ruleid = ruleid;
44         cache->entry[bucket].top = top;
45         cache->entry[bucket].ret = ret;
46         cache->entry[bucket].startrec = startrec;
47         cache->entry[bucket].endrec = endrec;
48         cache->entry[bucket].serial = cache->serial;
49
50 }
51
52
53 rpacachedentry_t *rpa_cache_lookup(rpacache_t *cache, rlong top, rlong ruleid)
54 {
55         rulong bucket = RPA_MCACHE_BUCKET(top, ruleid);
56         rpacachedentry_t *entry = &cache->entry[bucket];
57
58         if (entry->serial == cache->serial && entry->ruleid == ruleid && entry->top == top) {
59 //              rparecord_t *prec = (rparecord_t *)r_array_slot(entry->records, 0);
60 //              r_printf("HIT the cache @ %ld,  top = %ld, ret = %ld, rulename = %s\n", bucket, entry->top, entry->ret, prec->rule);
61                 ++cache->hit;
62                 return entry;
63         }
64         return NULL;
65 }