summaryrefslogtreecommitdiff
path: root/gtk/spice-channel-cache.h
blob: c7f289cc46b03576db21250f59339952430d36a5 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
static inline void cache_init(display_cache *cache, const char *name)
{
    int i;

    cache->name = name;
    ring_init(&cache->lru);
    for (i = 0; i < SPICE_N_ELEMENTS(cache->hash); i++) {
        ring_init(&cache->hash[i]);
    }
}

static inline Ring *cache_head(display_cache *cache, uint64_t id)
{
    return &cache->hash[id % SPICE_N_ELEMENTS(cache->hash)];
}

static inline void cache_used(display_cache *cache, display_cache_item *item)
{
    ring_remove(&item->lru_link);
    ring_add(&cache->lru, &item->lru_link);
}

static inline display_cache_item *cache_get_lru(display_cache *cache)
{
    display_cache_item *item;
    RingItem *ring;

    ring = ring_get_tail(&cache->lru);
    item = SPICE_CONTAINEROF(ring, display_cache_item, lru_link);
    return item;
}

static inline display_cache_item *cache_find(display_cache *cache, uint64_t id)
{
    display_cache_item *item;
    RingItem *ring;
    Ring *head;

    head = cache_head(cache, id);
    for (ring = head; ring != NULL; ring = ring_next(head, ring)) {
        item = SPICE_CONTAINEROF(ring, display_cache_item, hash_link);
        if (item->id == id) {
            return item;
        }
    }
    fprintf(stderr, "%s: %s %" PRIx64 " [not found]\n", __FUNCTION__,
            cache->name, id);
    return NULL;
}

static inline display_cache_item *cache_add(display_cache *cache, uint64_t id)
{
    display_cache_item *item;

    item = spice_new0(display_cache_item, 1);
    item->id = id;
    ring_add(cache_head(cache, item->id), &item->hash_link);
    ring_add(&cache->lru, &item->lru_link);
    cache->nitems++;
#if 0
    fprintf(stderr, "%s: %s %" PRIx64 " (%d)\n", __FUNCTION__,
            cache->name, id, cache->nitems);
#endif
    return item;
}

static inline void cache_del(display_cache *cache, display_cache_item *item)
{
#if 0
    fprintf(stderr, "%s: %s %" PRIx64 "\n", __FUNCTION__,
            cache->name, item->id);
#endif
    ring_remove(&item->hash_link);
    ring_remove(&item->lru_link);
    free(item);
    cache->nitems--;
}