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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2010 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPICE_CHANNEL_CACHE_H_
# define SPICE_CHANNEL_CACHE_H_
#include <inttypes.h> /* For PRIx64 */
#include "common/mem.h"
#include "common/ring.h"
G_BEGIN_DECLS
typedef struct display_cache_item {
guint64 id;
gboolean lossy;
guint32 ref_count;
} display_cache_item;
typedef struct display_cache {
GHashTable *table;
gboolean ref_counted;
}display_cache;
static inline display_cache_item* cache_item_new(guint64 id, gboolean lossy)
{
display_cache_item *self = g_slice_new(display_cache_item);
self->id = id;
self->lossy = lossy;
self->ref_count = 1;
return self;
}
static inline void cache_item_free(display_cache_item *self)
{
g_slice_free(display_cache_item, self);
}
static inline display_cache* cache_new(GDestroyNotify value_destroy)
{
display_cache * self = g_slice_new(display_cache);
self->table = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify) cache_item_free,
value_destroy);
self->ref_counted = FALSE;
return self;
}
static inline display_cache * cache_image_new(GDestroyNotify value_destroy)
{
display_cache * self = cache_new(value_destroy);
self->ref_counted = TRUE;
return self;
};
static inline gpointer cache_find(display_cache *cache, uint64_t id)
{
return g_hash_table_lookup(cache->table, &id);
}
static inline gpointer cache_find_lossy(display_cache *cache, uint64_t id, gboolean *lossy)
{
gpointer value;
display_cache_item *item;
if (!g_hash_table_lookup_extended(cache->table, &id, (gpointer*)&item, &value))
return NULL;
*lossy = item->lossy;
return value;
}
static inline void cache_add_lossy(display_cache *cache, uint64_t id,
gpointer value, gboolean lossy)
{
display_cache_item *item = cache_item_new(id, lossy);
display_cache_item *current_item;
gpointer current_image;
//If image is currently in the table add its reference count before replacing it
if(cache->ref_counted) {
if(g_hash_table_lookup_extended(cache->table, &id, (gpointer*) ¤t_item,
(gpointer*) ¤t_image)) {
item->ref_count = current_item->ref_count + 1;
}
}
g_hash_table_replace(cache->table, item, value);
}
static inline void cache_add(display_cache *cache, uint64_t id, gpointer value)
{
cache_add_lossy(cache, id, value, FALSE);
}
static inline gboolean cache_remove(display_cache *cache, uint64_t id)
{
display_cache_item * item;
gpointer value;
if( g_hash_table_lookup_extended(cache->table, &id, (gpointer*) &item, &value)) {
--item->ref_count;
if(!cache->ref_counted || item->ref_count == 0 ) {
return g_hash_table_remove(cache->table, &id);
}
}
else {
return FALSE;
}
return TRUE;
}
static inline void cache_clear(display_cache *cache)
{
g_hash_table_remove_all(cache->table);
}
static inline void cache_free(display_cache *cache)
{
g_hash_table_unref(cache->table);
g_slice_free(display_cache, cache);
}
G_END_DECLS
#endif // SPICE_CHANNEL_CACHE_H_
|