Reported by Fedora team for gcc-13 and Andrew created patch here
[goodguy/cinelerra.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_item(int source_id, char *path)
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->source_id == source_id ||
98                     (path && current->path && !strcmp(current->path, path)) )
99                         del_item(current);
100         }
101         lock->unlock();
102 }
103 void CacheBase::remove_item(Indexable *idxbl)
104 {
105         remove_item(idxbl->id, idxbl->path);
106 }
107 void CacheBase::remove_asset(Asset *asset)
108 {
109         remove_item(asset);
110 }
111
112 void CacheBase::del_item(CacheItemBase *item)
113 {
114         --total_items;
115         if( current_item == item )
116                 current_item = 0;
117         delete item;
118 }
119
120 int CacheBase::delete_oldest()
121 {
122         int result = 0;
123         lock->lock("CacheBase::delete_oldest");
124         CacheItemBase *oldest, *current;
125         for( oldest=current=first; current; current=NEXT ) {
126                 if( current->age < oldest->age )
127                         oldest = current;
128         }
129         if( oldest && oldest->position >= 0 ) {
130                 result = oldest->get_size();
131                 del_item(oldest);
132         }
133         lock->unlock();
134         return result;
135 }
136
137 int CacheBase::delete_item(CacheItemBase *item)
138 {
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());
143         del_item(item);
144         lock->unlock();
145         return 0;
146 }
147
148
149 int64_t CacheBase::get_memory_usage()
150 {
151         int64_t result = 0;
152         lock->lock("CacheBase::get_memory_usage");
153         for(CacheItemBase *current = first; current; current = NEXT)
154         {
155                 result += current->get_size();
156         }
157         lock->unlock();
158         return result;
159 }
160
161
162 void CacheBase::put_item(CacheItemBase *item)
163 {
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;
172         }
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);
179         ++total_items;
180 }
181
182
183 // Get first item from list with matching position or 0 if none found.
184 CacheItemBase* CacheBase::get_item(int64_t position)
185 {
186 // Get first position >= item
187         int64_t dist = 0x7fffffffffffffff;
188         if( current_item ) {
189                 dist = current_item->position - position;
190                 if( dist < 0 ) dist = -dist;
191         }
192         if( first ) {
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;
197                 }
198                 else {
199                         if( rt_dist < dist ) current_item = last;
200                 }
201         }
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;
206 // forward one item
207         current_item = current_item ? current_item->next : first;
208         CacheItemBase *result = current_item && current_item->position==position ?
209                 current_item : 0;
210         return result;
211 }
212
213
214 void CacheBase::age_cache(int64_t max_items)
215 {
216         lock->lock("CacheBase::age_cache");
217         if( total_items > max_items ) {
218                 CacheItemBase *items[total_items];
219                 int n = 0;
220                 for( CacheItemBase *current=first; current; current=NEXT ) {
221                         // insert items into heap
222                         CacheItemBase *item = current;
223                         int i, k;
224                         int v = item->age;
225                         for( i=n++; i>0 && v<items[k=(i-1)/2]->age; i=k )
226                                 items[i] = items[k];
227                         items[i] = item;
228                 }
229                 //int starting_items = total_items;
230                 while( n > 0 && total_items > max_items ) {
231                         // delete lowest heap item
232                         CacheItemBase *item = items[0];
233                         del_item(item);
234                         int i, k;
235                         for( i=0; (k=2*(i+1)) < n; i=k ) {
236                                 if( items[k]->age > items[k-1]->age ) --k;
237                                 items[i] = items[k];
238                         }
239                         item = items[--n];
240                         int v = item->age;
241                         for( ; i>0 && v<items[k=(i-1)/2]->age; i=k )
242                                 items[i] = items[k];
243                         items[i] = item;
244                 }
245         }
246         lock->unlock();
247 //printf("age_cache total_items=%d+%d items\n", total_items,starting_items-total_items);
248 }
249