license header for review
[cinelerra_cv/ct.git] / cinelerra / cache.h
blob3286c3daa141ed902241303daf49a391c583cc3d
1 #ifndef CACHE_H
2 #define CACHE_H
4 // CICache for quickly reading data that is hard to decompress yet used
5 // over and over.
7 // Actual caching is done in the File object
8 // the CICache keeps files open while rendering.
10 // Since the CICache outlives EDLs it must copy every parameter given to it.
12 // Files given as arguments must outlive the cache.
14 // the cache is implemented with a lookup map which acts as storage backend
15 // and 2 lists
16 // the checked_out list keeps all checked out items
17 // when items are checked back in, they are moved to the head of the checked_in list
18 // age expiry is done by removing elements from the tail of the checked_in list
20 #include "arraylist.h"
21 #include "asset.inc"
22 #include "cache.inc"
23 #include "condition.inc"
24 #include "edl.inc"
25 #include "file.inc"
26 #include "mutex.inc"
27 #include "pluginserver.inc"
28 #include "preferences.inc"
30 #include <stdint.h>
32 class CICacheItem
34 public:
35 CICacheItem(CICache *cache, EDL *edl, Asset_GC asset);
36 CICacheItem(){};
37 ~CICacheItem();
39 File *file;
40 // Number of last get or put operation involving this object.
41 Asset_GC asset; // Copy of asset. CICache should outlive EDLs.
42 bool checked_out;
44 friend class CICache;
45 private:
46 Condition* item_lock;
47 CICacheItem_plist::iterator list;
48 CICache* cache;
52 class CICache
54 CICacheItem_map items;
55 CICacheItem_plist checked_in;
56 CICacheItem_plist checked_out;
58 public:
60 CICache(Preferences *preferences,
61 ArrayList<PluginServer*> *plugindb);
62 ~CICache();
64 friend class CICacheItem;
66 // Enter a new file into the cache which is already open.
67 // If the file doesn't exist return the arguments.
68 // If the file exists delete the arguments and return the file which exists.
69 // void update(File* &file);
70 // void set_edl(EDL *edl);
72 // open it, lock it and add it to the cache if it isn't here already
73 // If it's already checked out, the value of block causes it to wait
74 // until it's checked in.
75 File* check_out(Asset_GC asset, EDL *edl, int block = 1);
77 // unlock a file from the cache
78 int check_in(Asset_GC asset);
80 // delete an entry from the cache
81 // before deleting an asset, starting a new project or something
82 int delete_entry(Asset_GC asset);
83 int delete_entry(char *path);
84 // Remove all entries from the cache.
85 void remove_all();
87 // Get ID of oldest member.
88 // Called by MWindow::age_caches.
89 int64_t get_memory_usage();
91 // Called by age() and MWindow::age_caches
92 // returns 1 if nothing was available to delete
93 // 0 if successful
94 int delete_oldest();
96 // Called by check_in() and modules.
97 // deletes oldest assets until under the memory limit
98 // iterations is the upper limit how much much items will be removed from the cache,
99 // use -1 for a full aging cycle (remove items until preferences.cache_size is reached)
100 void age(int iterations);
103 int dump();
105 ArrayList<PluginServer*>* plugindb;
107 const Preferences& preferences;
109 private:
110 int64_t get_memory_usage_unlocked();
112 // for deleting items
113 int lock_all();
114 int unlock_all();
116 // to prevent one from checking the same asset out before it's checked in
117 // yet without blocking the asset trying to get checked in
118 // use a seperate mutex for checkouts and checkins
119 Mutex* total_lock;
120 Condition* check_out_lock;
121 // Copy of EDL
122 EDL *edl;
125 #endif
127 // Local Variables:
128 // mode: C++
129 // c-file-style: "linux"
130 // End: