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"
34 CacheItemBase::CacheItemBase()
35 : ListItem<CacheItemBase>()
42 CacheItemBase::~CacheItemBase()
50 int CacheItemBase::get_size()
59 CacheBase::CacheBase()
60 : List<CacheItemBase>()
62 lock = new Mutex("CacheBase::lock");
67 CacheBase::~CacheBase()
74 int CacheBase::get_age()
76 return EDL::next_id();
80 // Called when done with the item returned by get_.
81 // Ignore if item was 0.
82 void CacheBase::unlock()
87 void CacheBase::remove_all()
89 //printf("CacheBase::remove_all: removed %d entries\n", total_items);
90 lock->lock("CacheBase::remove_all");
91 while(last) delete last;
98 void CacheBase::remove_asset(Asset *asset)
100 CacheItemBase *current, *next;
101 lock->lock("CacheBase::remove_id");
102 for( current=first; current; current=next ) {
103 next = current->next;
104 if( (current->path && !strcmp(current->path, asset->path)) ||
105 current->source_id == asset->id)
109 //printf("CacheBase::remove_asset: removed %d entries for %s\n", total, asset->path);
113 void CacheBase::del_item(CacheItemBase *item)
116 if( current_item == item )
121 int CacheBase::delete_oldest()
124 lock->lock("CacheBase::delete_oldest");
125 CacheItemBase *oldest, *current;
126 for( oldest=current=first; current; current=NEXT ) {
127 if( current->age < oldest->age )
138 int CacheBase::delete_item(CacheItemBase *item)
140 lock->lock("CacheBase::delete_item");
141 // Too much data to debug if audio.
142 // printf("CacheBase::delete_oldest: deleted position=" _LD " %d bytes\n",
143 // oldest_item->position, oldest_item->get_size());
150 int64_t CacheBase::get_memory_usage()
153 lock->lock("CacheBase::get_memory_usage");
154 for(CacheItemBase *current = first; current; current = NEXT)
156 result += current->get_size();
163 void CacheBase::put_item(CacheItemBase *item)
165 // Get first position >= item
166 int64_t position = item->position;
167 if( !current_item && first ) {
168 int64_t lt_dist = first ? position - first->position : 0;
169 if( lt_dist < 0 ) lt_dist = 0;
170 int64_t rt_dist = last ? last->position - position : 0;
171 if( rt_dist < 0 ) rt_dist = 0;
172 current_item = lt_dist < rt_dist ? first : last;
174 while(current_item && current_item->position > position)
175 current_item = current_item->previous;
176 if( !current_item ) current_item = first;
177 while(current_item && position > current_item->position)
178 current_item = current_item->next;
179 insert_before(current_item, item);
184 // Get first item from list with matching position or 0 if none found.
185 CacheItemBase* CacheBase::get_item(int64_t position)
187 // Get first position >= item
188 int64_t dist = 0x7fffffffffffffff;
190 dist = current_item->position - position;
191 if( dist < 0 ) dist = -dist;
194 int64_t lt_dist = position - first->position;
195 int64_t rt_dist = last->position - position;
196 if( lt_dist < rt_dist ) {
197 if( lt_dist < dist ) current_item = first;
200 if( rt_dist < dist ) current_item = last;
203 while(current_item && current_item->position < position)
204 current_item = current_item->next;
205 while(current_item && current_item->position >= position )
206 current_item = current_item->previous;
208 current_item = current_item ? current_item->next : first;
209 CacheItemBase *result = current_item && current_item->position==position ?
215 void CacheBase::age_cache(int64_t max_items)
217 lock->lock("CacheBase::age_cache");
218 if( total_items > max_items ) {
219 CacheItemBase *items[total_items];
221 for( CacheItemBase *current=first; current; current=NEXT ) {
222 // insert items into heap
223 CacheItemBase *item = current;
226 for( i=n++; i>0 && v<items[k=(i-1)/2]->age; i=k )
230 //int starting_items = total_items;
231 while( n > 0 && total_items > max_items ) {
232 // delete lowest heap item
233 CacheItemBase *item = items[0];
236 for( i=0; (k=2*(i+1)) < n; i=k ) {
237 if( items[k]->age > items[k-1]->age ) --k;
242 for( ; i>0 && v<items[k=(i-1)/2]->age; i=k )
248 //printf("age_cache total_items=%d+%d items\n", total_items,starting_items-total_items);