summaryrefslogtreecommitdiff
path: root/src/libdcache/dcache_init.c
blob: 8995f81e959ad9958fd158115fd8ba061bc21a6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* ISC license. */

#include <stdint.h>
#include <string.h>

#include <skalibs/uint16.h>
#include <skalibs/tai.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/avltree.h>

#include <shibari/dcache.h>

static int key_cmp (void const *a, void const *b, void *x)
{
  int r = memcmp(a, b, 4) ;
  if (r) return r ;
  {
    char const *aa = a ;
    char const *bb = b ;
    uint16_t len ;
    uint16_unpack_big(aa+2, &len) ;
    return memcmp(aa+4, bb+4, len) ;
  }
}

static int tai_cmp (void const *a, void const *b, void *x)
{
  tai const *ta = a ;
  tai const *tb = b ;
  (void)x ;
  return tai_less(ta, tb) ? -1 : tai_less(tb, ta) ;
}

static void *key_dtok (uint32_t d, void *x)
{
  return &GENSETDYN_P(dcache_node, (gensetdyn *)x, d)->sa.s ;
}

static void *entry_dtok (uint32_t d, void *x)
{
  return &GENSETDYN_P(dcache_node, (gensetdyn *)x, d)->entry ;
}

static void *expire_dtok (uint32_t d, void *x)
{
  return &GENSETDYN_P(dcache_node, (gensetdyn *)x, d)->expire ;
}


void dcache_init (dcache *z, uint64_t max)
{
  gensetdyn_init(&z->storage, sizeof(dcache_node), max >> 9, 3, 8) ;
  avltree_init(&z->by_key, max >> 9, 3, 8, &key_dtok, &key_cmp, &z->storage) ;
  avltree_init(&z->by_entry, max >> 9, 3, 8, &entry_dtok, &tai_cmp, &z->storage) ;
  avltree_init(&z->by_expire, max >> 9, 3, 8, &expire_dtok, &tai_cmp, &z->storage) ;
  z->max = max ;
  z->size = 0 ;
  z->motion = 0 ;
}