4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcsignals.h"
24 #include "cachebase.h"
33 CacheItemBase::CacheItemBase()
34 : ListItem<CacheItemBase>()
41 CacheItemBase::~CacheItemBase()
47 int CacheItemBase::get_size()
52 CacheBase::CacheBase()
53 : List<CacheItemBase>()
55 lock = new Mutex("CacheBase::lock");
60 CacheBase::~CacheBase()
67 int CacheBase::get_age()
69 return EDL::next_id();
73 // Called when done with the item returned by get_.
74 // Ignore if item was 0.
75 void CacheBase::unlock()
80 void CacheBase::remove_all()
82 //printf("CacheBase::remove_all: removed %d entries\n", total_items);
83 lock->lock("CacheBase::remove_all");
84 while(last) delete last;
91 void CacheBase::remove_item(int source_id, char *path)
93 CacheItemBase *current, *next;
94 lock->lock("CacheBase::remove_id");
95 for( current=first; current; current=next ) {
97 if( current->source_id == source_id ||
98 (path && current->path && !strcmp(current->path, path)) )
103 void CacheBase::remove_item(Indexable *idxbl)
105 remove_item(idxbl->id, idxbl->path);
107 void CacheBase::remove_asset(Asset *asset)
112 void CacheBase::del_item(CacheItemBase *item)
115 if( current_item == item )
120 int CacheBase::delete_oldest()
123 lock->lock("CacheBase::delete_oldest");
124 CacheItemBase *oldest, *current;
125 for( oldest=current=first; current; current=NEXT ) {
126 if( current->age < oldest->age )
129 if( oldest && oldest->position >= 0 ) {
130 result = oldest->get_size();
137 int CacheBase::delete_item(CacheItemBase *item)
139 lock->lock("CacheBase::delete_item");
140 // Too much data to debug if audio.
141 // printf("CacheBase::delete_oldest: deleted position=%jd %d bytes\n",
142 // oldest_item->position, oldest_item->get_size());
149 int64_t CacheBase::get_memory_usage()
152 lock->lock("CacheBase::get_memory_usage");
153 for(CacheItemBase *current = first; current; current = NEXT)
155 result += current->get_size();
162 void CacheBase::put_item(CacheItemBase *item)
164 // Get first position >= item
165 int64_t position = item->position;
166 if( !current_item && first ) {
167 int64_t lt_dist = first ? position - first->position : 0;
168 if( lt_dist < 0 ) lt_dist = 0;
169 int64_t rt_dist = last ? last->position - position : 0;
170 if( rt_dist < 0 ) rt_dist = 0;
171 current_item = lt_dist < rt_dist ? first : last;
173 while(current_item && current_item->position > position)
174 current_item = current_item->previous;
175 if( !current_item ) current_item = first;
176 while(current_item && position > current_item->position)
177 current_item = current_item->next;
178 insert_before(current_item, item);
183 // Get first item from list with matching position or 0 if none found.
184 CacheItemBase* CacheBase::get_item(int64_t position)
186 // Get first position >= item
187 int64_t dist = 0x7fffffffffffffff;
189 dist = current_item->position - position;
190 if( dist < 0 ) dist = -dist;
193 int64_t lt_dist = position - first->position;
194 int64_t rt_dist = last->position - position;
195 if( lt_dist < rt_dist ) {
196 if( lt_dist < dist ) current_item = first;
199 if( rt_dist < dist ) current_item = last;
202 while(current_item && current_item->position < position)
203 current_item = current_item->next;
204 while(current_item && current_item->position >= position )
205 current_item = current_item->previous;
207 current_item = current_item ? current_item->next : first;
208 CacheItemBase *result = current_item && current_item->position==position ?
214 void CacheBase::age_cache(int64_t max_items)
216 lock->lock("CacheBase::age_cache");
217 if( total_items > max_items ) {
218 CacheItemBase *items[total_items];
220 for( CacheItemBase *current=first; current; current=NEXT ) {
221 // insert items into heap
222 CacheItemBase *item = current;
225 for( i=n++; i>0 && v<items[k=(i-1)/2]->age; i=k )
229 //int starting_items = total_items;
230 while( n > 0 && total_items > max_items ) {
231 // delete lowest heap item
232 CacheItemBase *item = items[0];
235 for( i=0; (k=2*(i+1)) < n; i=k ) {
236 if( items[k]->age > items[k-1]->age ) --k;
241 for( ; i>0 && v<items[k=(i-1)/2]->age; i=k )
247 //printf("age_cache total_items=%d+%d items\n", total_items,starting_items-total_items);