bump libtool version
[swfdec.git] / libswfdec / swfdec_cache.c
blob09d58a0b18e9f5470fca2a6dd673330765dacef4
1 /* Swfdec
2 * Copyright (C) 2005 David Schleef <ds@schleef.org>
3 * 2007 Benjamin Otte <otte@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include "swfdec_cache.h"
26 #include "swfdec_debug.h"
28 SwfdecCache *
29 swfdec_cache_new (guint max_size)
31 SwfdecCache *cache;
33 g_return_val_if_fail (max_size > 0, NULL);
35 cache = g_new0 (SwfdecCache, 1);
36 cache->refcount = 1;
37 cache->queue = g_queue_new ();
38 cache->max_size = max_size;
40 return cache;
43 void
44 swfdec_cache_ref (SwfdecCache *cache)
46 g_return_if_fail (cache != NULL);
48 cache->refcount++;
51 void
52 swfdec_cache_unref (SwfdecCache *cache)
54 g_return_if_fail (cache != NULL);
55 g_return_if_fail (cache->refcount > 0);
57 cache->refcount--;
58 if (cache->refcount > 0)
59 return;
61 g_queue_free (cache->queue);
62 g_free (cache);
65 guint
66 swfdec_cache_get_usage (SwfdecCache *cache)
68 g_return_val_if_fail (cache != NULL, 0);
70 return cache->usage;
73 void
74 swfdec_cache_shrink (SwfdecCache *cache, guint max_usage)
76 g_return_if_fail (cache != NULL);
78 while (cache->usage > max_usage) {
79 SwfdecCacheHandle *handle = g_queue_pop_tail (cache->queue);
80 g_assert (handle);
81 cache->usage -= handle->size;
82 SWFDEC_LOG ("%p removing %p (%u => %u)", cache, handle,
83 cache->usage + handle->size, cache->usage);
84 handle->unload (handle);
88 /**
89 * swfdec_cache_add_handle:
90 * @cache: a #SwfdecCache
91 * @handle: a handle to add
93 * Adds @handle to @cache. If not enough space is available in the cache,
94 * the cache will unload existing handles first. If handle is already part
95 * of cache, its usage information will be updated. This will make it less
96 * likely that it gets unloaded.
97 **/
98 void
99 swfdec_cache_add_handle (SwfdecCache *cache, const SwfdecCacheHandle *handle)
101 GList *list;
103 g_return_if_fail (cache != NULL);
104 g_return_if_fail (handle != NULL);
105 g_return_if_fail (handle->size > 0);
106 g_return_if_fail (handle->unload != NULL);
108 list = g_queue_find (cache->queue, handle);
109 if (list) {
110 /* bring to front of queue */
111 g_queue_unlink (cache->queue, list);
112 g_queue_push_head_link (cache->queue, list);
113 } else {
114 swfdec_cache_shrink (cache, cache->max_size - handle->size);
115 g_queue_push_head (cache->queue, (gpointer) handle);
116 cache->usage += handle->size;
117 SWFDEC_LOG ("%p adding %p (%u => %u)", cache, handle,
118 cache->usage - handle->size, cache->usage);
123 * swfdec_cache_remove_handle:
124 * @cache: a #SwfdecCache
125 * @handle: the handle to remove
127 * Removes the given @handle from the @cache, if it was part of it. If the
128 * handle wasn't part of the cache, nothing happens.
130 void
131 swfdec_cache_remove_handle (SwfdecCache *cache, const SwfdecCacheHandle *handle)
133 GList *list;
135 g_return_if_fail (cache != NULL);
136 g_return_if_fail (handle != NULL);
137 g_return_if_fail (handle->size > 0);
138 g_return_if_fail (handle->unload != NULL);
140 list = g_queue_find (cache->queue, handle);
141 if (list) {
142 g_queue_delete_link (cache->queue, list);
143 cache->usage -= handle->size;
144 SWFDEC_LOG ("%p removing %p (%u => %u)", cache, handle,
145 cache->usage + handle->size, cache->usage);