Repository navigation
Expand file tree
/
Copy pathcache.c
More file actions
584 lines (445 loc) · 11 KB
/
Copy pathcache.c
File metadata and controls
584 lines (445 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Ambient - the ultimate desktop
* ------------------------------
* Copyright 2001-2005 by David Gerber <zapek@morphos.net>
* Copyright 2005-2007 Ambient Open Source Team
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: cache.c,v 1.13 2022/01/06 18:11:52 piru Exp $
*/
#include "ambient.h"
/* public */
#include <exec/interrupts.h>
#include <exec/memory.h>
#include <exec/semaphores.h>
#include <emul/emulinterface.h>
#include <proto/exec.h>
#include <proto/btree.h>
/* private */
#include "cache.h"
#include "gfx_bitmap.h"
#include "gfx_blit.h"
#include "gfx_alpha.h"
#include "time_func.h"
/* unused cacheobject maximum lifetime (seconds) */
#if MORPHOS_BETABUILD
#define CLEANUP_THRESHOLD ( 1 * 60 )
#else
#define CLEANUP_THRESHOLD ( 30 * 60 )
#endif
/* lowmem handler */
static LONG memhandlerfunc(void);
static struct IntData {
struct EmulLibEntry InterruptFunc;
struct Interrupt Interrupt;
struct ExecBase *SysBase;
} mosintdata;
static struct SignalSemaphore cachesem;
static ULONG memhandler_cache_active;
/* */
static APTR tree;
static APTR pool;
static ULONG global_stamp = 1;
struct cachenode
{
ULONG stamp; /* last usage counter for LRU flushing. */
ULONG flag; /* which value entries are used. we can't just check for 0 there. */
APTR value[ CACHETAG_MAX ];
};
/* resource duplication and releasing callbacks */
typedef LONG ( *allocResource_t ) ( APTR val, APTR *copy );
typedef void ( *freeResource_t ) ( APTR val );
static LONG allocCopyPtr( APTR val, APTR *copy )
{
*copy = val;
return TRUE;
}
static LONG allocCopyBitMap( APTR val, APTR *copy )
{
APTR sb = val;
UBYTE *spixels = gfx_bitmap_array( sb );
ULONG w = gfx_bitmap_width( sb );
ULONG h = gfx_bitmap_height( sb );
ULONG bpp = gfx_alpha_ispresent( sb ) ? 4 : 3;
APTR darray = AllocPooled(pool, w * h * bpp + 12 );
if ( darray )
{
ULONG *header = darray;
UBYTE *dpixels = (UBYTE*)darray + 12;
ULONG y, rm = gfx_bitmap_modulo( sb );
header[ 0 ] = w;
header[ 1 ] = h;
header[ 2 ] = bpp;
for( y=0; y<h; y++)
{
if ( bpp == 4 )
{
memcpy( dpixels + y * w * 4, spixels + y * rm * 4, w * 4 );
}
else
{
ULONG x;
for( x=0; x<w; x++)
{
dpixels[ ( y * w + x ) * 3 + 0 ] = spixels[ ( y * rm + x ) * 4 + 1 ];
dpixels[ ( y * w + x ) * 3 + 1 ] = spixels[ ( y * rm + x ) * 4 + 2 ];
dpixels[ ( y * w + x ) * 3 + 2 ] = spixels[ ( y * rm + x ) * 4 + 3 ];
}
}
}
*copy = darray;
return TRUE;
}
return FALSE;
}
static void freeDummy( APTR val UNUSED )
{
}
static void freeBitMap( APTR val )
{
if ( val )
{
const ULONG *header = val;
const ULONG size = header[0] * header[1] * header[2] + 12;
FreePooled( pool, val, size );
}
}
static allocResource_t allocator[ CACHETAG_MAX ] = {
allocCopyPtr, /* CACHETAG_MIMETYPE - just copy pointer */
allocCopyBitMap, /* CACHETAG_THUMBNAIL - copy passed bitmap to buffer */
};
static freeResource_t deallocator[ CACHETAG_MAX ] = {
freeDummy, /* CACHETAG_MIMETYPE - nothing to free */
freeBitMap, /* CACHETAG_THUMBNAIL - delete bitmap */
};
/* internal functions */
static void cnTouch( struct cachenode *cn )
{
if ( cn != NULL )
{
cn->stamp = timev();
}
}
static struct cachenode *cnCreate( ULONG hash UNUSED )
{
struct cachenode *cn = AllocPooled( pool, sizeof( *cn ) );
if ( cn != NULL )
{
cn->flag = 0;
cnTouch( cn );
}
return cn;
}
static void cnFlush( struct cachenode *cn )
{
if ( cn != NULL )
{
LONG i;
for(i=0; i<CACHETAG_MAX; i++)
{
if ( cn->flag & ( 1<<i ) )
{
/* delete contents of each tag using specific routine */
if ( deallocator[ i ] != NULL )
deallocator[ i ]( cn->value[ i ] );
}
}
cn->flag = 0;
}
}
static void cnDelete( struct cachenode *cn )
{
if ( cn != NULL )
{
cnFlush( cn );
FreePooled( pool, cn, sizeof( *cn ) );
}
}
static struct cachenode *cnLookup( ULONG hash )
{
APTR n = FindTreeNodeByKey( tree, (APTR)hash);
if ( n != NULL )
{
struct cachenode *cn = ( struct cachenode* )GetTreeNodeData( tree, n );
cnTouch( cn );
return cn;
}
return NULL;
}
/* API */
LONG cache_get( ULONG hash, LONG tag, APTR *val )
{
LONG rc = FALSE;
if (AttemptSemaphore(&cachesem)) /* we really don't want to be blocked*/
{
struct cachenode *cn = cnLookup( hash );
if ( cn != NULL )
{
if ( cn->flag & ( 1<<tag ) )
{
/* we only update stamp on read access */
#warning "This makes little sense. The value set by cnLookup is trashed by this walking counter. WTF? - Piru"
cn->stamp = global_stamp++;
/* */
*val = cn->value[ tag ];
rc = TRUE;
/*
* keep the cache locked for CACHETAG_THUMBNAIL. caller
* must unlock after done by calling cache_unlock(). this
* avoids a race when called from multiple threads. - Piru
*/
if (tag == CACHETAG_THUMBNAIL)
return rc;
}
}
ReleaseSemaphore(&cachesem);
}
return rc;
}
void cache_unlock( void )
{
ReleaseSemaphore(&cachesem);
}
void cache_set( ULONG hash, LONG tag, APTR val )
{
if (AttemptSemaphore(&cachesem)) /* we really don't want to be blocked*/
{
struct cachenode *cn = cnLookup( hash );
if ( !cn )
{
/* node not in cache. allocate new one and insert */
cn = cnCreate( hash );
if ( cn )
{
InsertTreeNode( tree, (APTR)hash, (APTR)cn );
}
}
if ( cn )
{
if ( cn->flag & ( 1<<tag ) )
{
/* delete previous one first */
if ( deallocator[ tag ] )
deallocator[ tag ]( cn->value[ tag ] );
cn->flag &= ~(1<<tag);
}
if ( allocator[ tag ] )
{
APTR copy;
if ( allocator[ tag ]( val, © ) )
{
cn->value[ tag ] = copy;
cn->flag |= 1<<tag;
}
}
}
ReleaseSemaphore(&cachesem);
}
}
static void invalidateFunc(APTR tagp, const APTR n)
{
struct cachenode *cn = NULL;
LONG tag = (LONG)tagp;
if ( !n )
return;
cn = ( struct cachenode * )GetTreeNodeData( tree, n );
if ( cn )
{
if ( tag == CACHETAG_ALL )
{
/* invalidate all tags = delete key from tree */
cnFlush( cn );
}
else
{
if ( cn->flag & ( 1<<tag ) )
{
/* delete previous one */
if ( deallocator[ tag ] )
deallocator[ tag ]( cn->value[ tag ] );
cn->flag &= ~(1<<tag);
}
}
}
}
struct disposal_state
{
ULONG stamp_threshold;
ULONG counter;
APTR *node;
};
static void deleteunusedCountFunc(APTR disposal_state_p, const APTR n)
{
struct cachenode *cn;
struct disposal_state *ds = ( struct disposal_state* )disposal_state_p;
if ( !n )
return;
cn = ( struct cachenode * )GetTreeNodeData( tree, n );
if ( cn != NULL && ( cn->stamp <= ds->stamp_threshold || cn->flag == 0 ) )
{
ds->counter++;
}
}
static void deleteunusedMarkFunc(APTR disposal_state_p, const APTR n)
{
struct cachenode *cn;
struct disposal_state *ds = ( struct disposal_state* )disposal_state_p;
if ( !n )
return;
cn = ( struct cachenode * )GetTreeNodeData( tree, n );
if ( cn != NULL && ( cn->stamp <= ds->stamp_threshold || cn->flag == 0 ) )
{
*ds->node++ = n;
}
}
void cache_invalidate( ULONG hash, LONG tag )
{
/*
* Here we want to wait even if someone else has the semaphore. This
* ensures that invalidate will work properly every time. - Piru
*/
ObtainSemaphore(&cachesem);
if ( hash )
{
/* single tree node */
APTR n = FindTreeNodeByKey( tree, (APTR)hash);
if ( n )
invalidateFunc( (APTR)tag, n );
}
else
{
/* all tree nodes */
ForTreeNodes( tree, invalidateFunc, (APTR)tag );
}
ReleaseSemaphore(&cachesem);
}
void cache_deleteunused( void )
{
if (AttemptSemaphore(&cachesem))
{
struct disposal_state ds;
ds.stamp_threshold = timev() - CLEANUP_THRESHOLD;
ds.counter = 0;
DB(("Cache cleanup initialized...\n"));
ForTreeNodes( tree, deleteunusedCountFunc, (APTR)&ds );
if ( ds.counter > 0 )
{
APTR *nodes = AllocPooled( pool, ds.counter * sizeof( APTR ) );
if ( nodes != NULL )
{
LONG i;
DB(("%d nodes marked for deleting...\n", ds.counter));
ds.node = nodes;
ForTreeNodes( tree, deleteunusedMarkFunc, (APTR)&ds );
for(i=0; i<ds.counter; i++)
{
DeleteTreeNode( tree, nodes[ i ] );
}
FreePooled( pool, nodes, ds.counter * sizeof( APTR ) );
}
}
DB(("Finished.\n"));
ReleaseSemaphore(&cachesem);
}
}
/* */
static APTR btAlloc( APTR userdata UNUSED, ULONG size )
{
return AllocPooled( pool, size );
}
static void btFree( APTR userdata UNUSED, APTR mem, ULONG size )
{
FreePooled( pool, mem, size );
}
static LONG btCompare( APTR userdata UNUSED, APTR keya, APTR keyb )
{
if ( (ULONG)keya > (ULONG)keyb )
return 1;
else if ( (ULONG)keya < (ULONG)keyb )
return -1;
else
return 0;
}
static void btDestroyKey( APTR userdata UNUSED, APTR key UNUSED )
{
}
static void btDestroyData( APTR userdata UNUSED, APTR data )
{
cnDelete( data );
}
struct BTArgArray argarray = {
btAlloc,
btFree,
btCompare,
btDestroyKey,
btDestroyData,
NULL,
};
ULONG cache_init( void )
{
/* memory management */
pool = CreatePool( MEMF_ANY, 65536, 1024 );
if ( pool )
{
/* initialize tree */
tree = CreateTree( BT_DEFAULT, &argarray );
if ( tree )
{
/* install lowmem handler */
InitSemaphore(&cachesem);
mosintdata.SysBase = SysBase;
mosintdata.InterruptFunc.Trap = TRAP_LIB;
mosintdata.InterruptFunc.Extension = 0;
mosintdata.InterruptFunc.Func = (void (*)(void))memhandlerfunc;
mosintdata.Interrupt.is_Node.ln_Type = NT_INTERRUPT;
mosintdata.Interrupt.is_Node.ln_Pri = 1;
mosintdata.Interrupt.is_Node.ln_Name = "Ambient's Cache MemHandler";
mosintdata.Interrupt.is_Data = &mosintdata;
mosintdata.Interrupt.is_Code = (void(*)(void))&mosintdata.InterruptFunc;
AddMemHandler(&mosintdata.Interrupt);
memhandler_cache_active = TRUE;
/* initialize disposal list */
/* and we are done */
return TRUE;
}
DeletePool( pool );
}
return FALSE;
}
void cache_cleanup( void )
{
if (memhandler_cache_active)
{
RemMemHandler(&mosintdata.Interrupt);
}
DeleteTree( tree );
DeletePool( pool );
}
static LONG memhandlerfunc(void)
{
struct ExecBase *SysBase = mosintdata.SysBase;
if (AttemptSemaphore(&cachesem))
{
/*
* If this is lowmem situation, then we better free whatever we can.
*/
FlushTree( tree );
ReleaseSemaphore(&cachesem);
return (MEM_ALL_DONE);
}
return (MEM_DID_NOTHING);
}