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_asset(Asset *asset)
93 CacheItemBase *current, *next;
94 lock->lock("CacheBase::remove_id");
95 for( current=first; current; current=next ) {
97 if( (current->path && !strcmp(current->path, asset->path)) ||
98 current->source_id == asset->id)
102 //printf("CacheBase::remove_asset: removed %d entries for %s\n", total, asset->path);
106 void CacheBase::del_item(CacheItemBase *item)
109 if( current_item == item )
114 int CacheBase::delete_oldest()
117 lock->lock("CacheBase::delete_oldest");
118 CacheItemBase *oldest, *current;
119 for( oldest=current=first; current; current=NEXT ) {
120 if( current->age < oldest->age )
123 if( oldest && oldest->position >= 0 ) {
124 result = oldest->get_size();
131 int CacheBase::delete_item(CacheItemBase *item)
133 lock->lock("CacheBase::delete_item");
134 // Too much data to debug if audio.
135 // printf("CacheBase::delete_oldest: deleted position=%jd %d bytes\n",
136 // oldest_item->position, oldest_item->get_size());
143 int64_t CacheBase::get_memory_usage()
146 lock->lock("CacheBase::get_memory_usage");
147 for(CacheItemBase *current = first; current; current = NEXT)
149 result += current->get_size();
156 void CacheBase::put_item(CacheItemBase *item)
158 // Get first position >= item
159 int64_t position = item->position;
160 if( !current_item && first ) {
161 int64_t lt_dist = first ? position - first->position : 0;
162 if( lt_dist < 0 ) lt_dist = 0;
163 int64_t rt_dist = last ? last->position - position : 0;
164 if( rt_dist < 0 ) rt_dist = 0;
165 current_item = lt_dist < rt_dist ? first : last;
167 while(current_item && current_item->position > position)
168 current_item = current_item->previous;
169 if( !current_item ) current_item = first;
170 while(current_item && position > current_item->position)
171 current_item = current_item->next;
172 insert_before(current_item, item);
177 // Get first item from list with matching position or 0 if none found.
178 CacheItemBase* CacheBase::get_item(int64_t position)
180 // Get first position >= item
181 int64_t dist = 0x7fffffffffffffff;
183 dist = current_item->position - position;
184 if( dist < 0 ) dist = -dist;
187 int64_t lt_dist = position - first->position;
188 int64_t rt_dist = last->position - position;
189 if( lt_dist < rt_dist ) {
190 if( lt_dist < dist ) current_item = first;
193 if( rt_dist < dist ) current_item = last;
196 while(current_item && current_item->position < position)
197 current_item = current_item->next;
198 while(current_item && current_item->position >= position )
199 current_item = current_item->previous;
201 current_item = current_item ? current_item->next : first;
202 CacheItemBase *result = current_item && current_item->position==position ?
208 void CacheBase::age_cache(int64_t max_items)
210 lock->lock("CacheBase::age_cache");
211 if( total_items > max_items ) {
212 CacheItemBase *items[total_items];
214 for( CacheItemBase *current=first; current; current=NEXT ) {
215 // insert items into heap
216 CacheItemBase *item = current;
219 for( i=n++; i>0 && v<items[k=(i-1)/2]->age; i=k )
223 //int starting_items = total_items;
224 while( n > 0 && total_items > max_items ) {
225 // delete lowest heap item
226 CacheItemBase *item = items[0];
229 for( i=0; (k=2*(i+1)) < n; i=k ) {
230 if( items[k]->age > items[k-1]->age ) --k;
235 for( ; i>0 && v<items[k=(i-1)/2]->age; i=k )
241 //printf("age_cache total_items=%d+%d items\n", total_items,starting_items-total_items);