prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / cachebase.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "asset.h"
23 #include "bcsignals.h"
24 #include "cachebase.h"
25 #include "format.inc"
26 #include "edl.h"
27 #include "mutex.h"
28
29 #include <string.h>
30
31
32
33
34 CacheItemBase::CacheItemBase()
35  : ListItem<CacheItemBase>()
36 {
37         age = 0;
38         source_id = -1;
39         path = 0;
40 }
41
42 CacheItemBase::~CacheItemBase()
43 {
44         delete [] path;
45 }
46
47
48
49
50 int CacheItemBase::get_size()
51 {
52         return 0;
53 }
54
55
56
57
58
59 CacheBase::CacheBase()
60  : List<CacheItemBase>()
61 {
62         lock = new Mutex("CacheBase::lock");
63         current_item = 0;
64         total_items = 0;
65 }
66
67 CacheBase::~CacheBase()
68 {
69         delete lock;
70 }
71
72
73
74 int CacheBase::get_age()
75 {
76         return EDL::next_id();
77 }
78
79
80 // Called when done with the item returned by get_.
81 // Ignore if item was 0.
82 void CacheBase::unlock()
83 {
84         lock->unlock();
85 }
86
87 void CacheBase::remove_all()
88 {
89 //printf("CacheBase::remove_all: removed %d entries\n", total_items);
90         lock->lock("CacheBase::remove_all");
91         while(last) delete last;
92         total_items = 0;
93         current_item = 0;
94         lock->unlock();
95 }
96
97
98 void CacheBase::remove_asset(Asset *asset)
99 {
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)
106                         del_item(current);
107         }
108         lock->unlock();
109 //printf("CacheBase::remove_asset: removed %d entries for %s\n", total, asset->path);
110 }
111
112
113 void CacheBase::del_item(CacheItemBase *item)
114 {
115         --total_items;
116         if( current_item == item )
117                 current_item = 0;
118         delete item;
119 }
120
121 int CacheBase::delete_oldest()
122 {
123         int result = 0;
124         lock->lock("CacheBase::delete_oldest");
125         CacheItemBase *oldest, *current;
126         for( oldest=current=first; current; current=NEXT ) {
127                 if( current->age < oldest->age )
128                         oldest = current;
129         }
130         if( oldest ) {
131                 del_item(oldest);
132                 result = 1;
133         }
134         lock->unlock();
135         return result;
136 }
137
138 int CacheBase::delete_item(CacheItemBase *item)
139 {
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());
144         del_item(item);
145         lock->unlock();
146         return 0;
147 }
148
149
150 int64_t CacheBase::get_memory_usage()
151 {
152         int64_t result = 0;
153         lock->lock("CacheBase::get_memory_usage");
154         for(CacheItemBase *current = first; current; current = NEXT)
155         {
156                 result += current->get_size();
157         }
158         lock->unlock();
159         return result;
160 }
161
162
163 void CacheBase::put_item(CacheItemBase *item)
164 {
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;
173         }
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);
180         ++total_items;
181 }
182
183
184 // Get first item from list with matching position or 0 if none found.
185 CacheItemBase* CacheBase::get_item(int64_t position)
186 {
187 // Get first position >= item
188         int64_t dist = 0x7fffffffffffffff;
189         if( current_item ) {
190                 dist = current_item->position - position;
191                 if( dist < 0 ) dist = -dist;
192         }
193         if( first ) {
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;
198                 }
199                 else {
200                         if( rt_dist < dist ) current_item = last;
201                 }
202         }
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;
207 // forward one item
208         current_item = current_item ? current_item->next : first; 
209         CacheItemBase *result = current_item && current_item->position==position ?
210                 current_item : 0;
211         return result;
212 }
213
214
215 void CacheBase::age_cache(int64_t max_items)
216 {
217         lock->lock("CacheBase::age_cache");
218         if( total_items > max_items ) {
219                 CacheItemBase *items[total_items];
220                 int n = 0;
221                 for( CacheItemBase *current=first; current; current=NEXT ) {
222                         // insert items into heap
223                         CacheItemBase *item = current;
224                         int i, k;
225                         int v = item->age;
226                         for( i=n++; i>0 && v<items[k=(i-1)/2]->age; i=k )
227                                 items[i] = items[k];
228                         items[i] = item;
229                 }
230                 //int starting_items = total_items;
231                 while( n > 0 && total_items > max_items ) {
232                         // delete lowest heap item
233                         CacheItemBase *item = items[0];
234                         del_item(item);
235                         int i, k;
236                         for( i=0; (k=2*(i+1)) < n; i=k ) {
237                                 if( items[k]->age > items[k-1]->age ) --k;
238                                 items[i] = items[k];
239                         }
240                         item = items[--n];
241                         int v = item->age;
242                         for( ; i>0 && v<items[k=(i-1)/2]->age; i=k )
243                                 items[i] = items[k];
244                         items[i] = item;
245                 }
246         }
247         lock->unlock();
248 //printf("age_cache total_items=%d+%d items\n", total_items,starting_items-total_items);
249 }
250