add bluray dv, misc fixes
[goodguy/history.git] / cinelerra-5.1 / 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 "edl.h"
26 #include "mutex.h"
27
28 #include <string.h>
29
30
31
32
33 CacheItemBase::CacheItemBase()
34  : ListItem<CacheItemBase>()
35 {
36         age = 0;
37         source_id = -1;
38         path = 0;
39 }
40
41 CacheItemBase::~CacheItemBase()
42 {
43         delete [] path;
44 }
45
46
47 int CacheItemBase::get_size()
48 {
49         return 0;
50 }
51
52 CacheBase::CacheBase()
53  : List<CacheItemBase>()
54 {
55         lock = new Mutex("CacheBase::lock");
56         current_item = 0;
57         total_items = 0;
58 }
59
60 CacheBase::~CacheBase()
61 {
62         delete lock;
63 }
64
65
66
67 int CacheBase::get_age()
68 {
69         return EDL::next_id();
70 }
71
72
73 // Called when done with the item returned by get_.
74 // Ignore if item was 0.
75 void CacheBase::unlock()
76 {
77         lock->unlock();
78 }
79
80 void CacheBase::remove_all()
81 {
82 //printf("CacheBase::remove_all: removed %d entries\n", total_items);
83         lock->lock("CacheBase::remove_all");
84         while(last) delete last;
85         total_items = 0;
86         current_item = 0;
87         lock->unlock();
88 }
89
90
91 void CacheBase::remove_asset(Asset *asset)
92 {
93         CacheItemBase *current, *next;
94         lock->lock("CacheBase::remove_id");
95         for( current=first; current; current=next ) {
96                 next = current->next;
97                 if( (current->path && !strcmp(current->path, asset->path)) ||
98                         current->source_id == asset->id)
99                         del_item(current);
100         }
101         lock->unlock();
102 //printf("CacheBase::remove_asset: removed %d entries for %s\n", total, asset->path);
103 }
104
105
106 void CacheBase::del_item(CacheItemBase *item)
107 {
108         --total_items;
109         if( current_item == item )
110                 current_item = 0;
111         delete item;
112 }
113
114 int CacheBase::delete_oldest()
115 {
116         int result = 0;
117         lock->lock("CacheBase::delete_oldest");
118         CacheItemBase *oldest, *current;
119         for( oldest=current=first; current; current=NEXT ) {
120                 if( current->age < oldest->age )
121                         oldest = current;
122         }
123         if( oldest && oldest->position >= 0 ) {
124                 del_item(oldest);
125                 result = 1;
126         }
127         lock->unlock();
128         return result;
129 }
130
131 int CacheBase::delete_item(CacheItemBase *item)
132 {
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());
137         del_item(item);
138         lock->unlock();
139         return 0;
140 }
141
142
143 int64_t CacheBase::get_memory_usage()
144 {
145         int64_t result = 0;
146         lock->lock("CacheBase::get_memory_usage");
147         for(CacheItemBase *current = first; current; current = NEXT)
148         {
149                 result += current->get_size();
150         }
151         lock->unlock();
152         return result;
153 }
154
155
156 void CacheBase::put_item(CacheItemBase *item)
157 {
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;
166         }
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);
173         ++total_items;
174 }
175
176
177 // Get first item from list with matching position or 0 if none found.
178 CacheItemBase* CacheBase::get_item(int64_t position)
179 {
180 // Get first position >= item
181         int64_t dist = 0x7fffffffffffffff;
182         if( current_item ) {
183                 dist = current_item->position - position;
184                 if( dist < 0 ) dist = -dist;
185         }
186         if( first ) {
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;
191                 }
192                 else {
193                         if( rt_dist < dist ) current_item = last;
194                 }
195         }
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;
200 // forward one item
201         current_item = current_item ? current_item->next : first;
202         CacheItemBase *result = current_item && current_item->position==position ?
203                 current_item : 0;
204         return result;
205 }
206
207
208 void CacheBase::age_cache(int64_t max_items)
209 {
210         lock->lock("CacheBase::age_cache");
211         if( total_items > max_items ) {
212                 CacheItemBase *items[total_items];
213                 int n = 0;
214                 for( CacheItemBase *current=first; current; current=NEXT ) {
215                         // insert items into heap
216                         CacheItemBase *item = current;
217                         int i, k;
218                         int v = item->age;
219                         for( i=n++; i>0 && v<items[k=(i-1)/2]->age; i=k )
220                                 items[i] = items[k];
221                         items[i] = item;
222                 }
223                 //int starting_items = total_items;
224                 while( n > 0 && total_items > max_items ) {
225                         // delete lowest heap item
226                         CacheItemBase *item = items[0];
227                         del_item(item);
228                         int i, k;
229                         for( i=0; (k=2*(i+1)) < n; i=k ) {
230                                 if( items[k]->age > items[k-1]->age ) --k;
231                                 items[i] = items[k];
232                         }
233                         item = items[--n];
234                         int v = item->age;
235                         for( ; i>0 && v<items[k=(i-1)/2]->age; i=k )
236                                 items[i] = items[k];
237                         items[i] = item;
238                 }
239         }
240         lock->unlock();
241 //printf("age_cache total_items=%d+%d items\n", total_items,starting_items-total_items);
242 }
243