home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / cache.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  7.5 KB  |  326 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2005 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #ifndef f_VD2_SYSTEM_CACHE_H
  27. #define f_VD2_SYSTEM_CACHE_H
  28.  
  29. #include <vd2/system/thread.h>
  30. #include <vd2/system/vdstl.h>
  31.  
  32. ///////////////////////////////////////////////////////////////////////////
  33.  
  34. struct vdhashmap_node {
  35.     vdhashmap_node *mpHashPrev;
  36.     vdhashmap_node *mpHashNext;
  37. };
  38.  
  39. template<class K>
  40. struct vdhash {
  41.     size_t operator()(const K key) const {
  42.         return (size_t)key;
  43.     }
  44. };
  45.  
  46. template<class K, class V, class Hash = vdhash<K>, int N = 256>
  47. class vdhashmap_iterator {
  48. public:
  49.     typedef    vdhashmap_node    node;
  50.  
  51.     bool operator==(vdhashmap_iterator& x) const { return mpNode == x.mpNode; }
  52.     bool operator!=(vdhashmap_iterator& x) const { return mpNode != x.mpNode; }
  53.  
  54.     V& operator*() const { return *static_cast<V *>((node *)mpNode); }
  55.     V *operator->() const { return static_cast<V *>((node *)mpNode); }
  56.  
  57.     vdhashmap_iterator& operator++() {
  58.         do {
  59.             mpNode = ((node *)mpNode)->mpHashNext;
  60.             if (mpNode != mpTableNode)
  61.                 break;
  62.  
  63.             ++mpTableNode;
  64.             mpNode = mpTableNode->mpHashNext;
  65.         } while(mpNode);
  66.  
  67.         return *this;
  68.     }
  69.  
  70.     vdhashmap_iterator operator++(int) {
  71.         vdhashmap_iterator it(*this);
  72.         ++*this;
  73.         return it;
  74.     }
  75.  
  76. public:
  77.     vdhashmap_node *mpNode;
  78.     vdhashmap_node *mpTableNode;
  79. };
  80.  
  81. template<class K, class V, class Hash = vdhash<K>, int N = 256>
  82. class vdhashmap {
  83. public:
  84.     typedef    K                    key_type;
  85.     typedef    V                    value_type;
  86.     typedef    Hash                hash_type;
  87.     typedef vdhashmap_node        node;
  88.     typedef    vdhashmap_iterator<K, V>    iterator;
  89.  
  90.     vdhashmap() {
  91.         for(int i=0; i<N; ++i)
  92.             m.mpTable[i].mpHashPrev = m.mpTable[i].mpHashNext = &m.mpTable[i];
  93.     }
  94.  
  95.     iterator begin() {
  96.         int i;
  97.         for(i=0; i<N && !m.mpTable[i]; ++i)
  98.             ;
  99.         iterator it = { m.mpTable[i].mpFirst, &m.mpTable[i] };
  100.         return it;
  101.     }
  102.  
  103.     iterator end() {
  104.         iterator it = { NULL, NULL };
  105.         return it;
  106.     }
  107.  
  108.     V *operator[](const K& key) {
  109.         const size_t htidx = m(key) % N;
  110.  
  111.         node *r = &m.mpTable[htidx];
  112.         for(node *p = r->mpHashNext; p != r; p = p->mpHashNext) {
  113.             if (static_cast<V *>(p)->mHashKey == key)
  114.                 return static_cast<V *>(p);
  115.         }
  116.  
  117.         return NULL;
  118.     }
  119.  
  120.     iterator find(const K& key) {
  121.         const size_t htidx = m(key) % N;
  122.  
  123.         node *r = &m.mpTable[htidx];
  124.         for(node *p = r->mpHashNext; p != r; p = p->mpHashNext) {
  125.             if (static_cast<V *>(p)->mHashKey == key) {
  126.                 iterator it = { p, &m.mpTable[htidx] };
  127.                 return it;
  128.             }
  129.         }
  130.  
  131.         return end();
  132.     }
  133.  
  134.     iterator insert(V *p) {
  135.         const size_t htidx = m(p->mHashKey) % N;
  136.  
  137.         node *r = &m.mpTable[htidx];
  138.         node *n = r->mpHashNext;
  139.         r->mpHashNext = p;
  140.         p->mpHashPrev = &m.mpTable[htidx];
  141.         p->mpHashNext = n;
  142.         n->mpHashPrev = p;
  143.  
  144.         iterator it = { p, &m.mpTable[htidx] };
  145.         return it;
  146.     }
  147.  
  148.     void erase(V *x) {
  149.         node *p = x->mpHashPrev;
  150.         node *n = x->mpHashNext;
  151.  
  152.         p->mpHashNext = n;
  153.         n->mpHashPrev = p;
  154.     }
  155.  
  156.     void erase(iterator it) {
  157.         erase(it.mpNode);
  158.     }
  159.  
  160. protected:
  161.     struct Data : public Hash {
  162.         vdhashmap_node    mpTable[N];
  163.     } m;
  164. };
  165.  
  166. ///////////////////////////////////////////////////////////////////////////
  167.  
  168. class VDCachedObject;
  169.  
  170. class IVDCacheAllocator {
  171. public:
  172.     virtual VDCachedObject *OnCacheAllocate() = 0;
  173. };
  174.  
  175. ///////////////////////////////////////////////////////////////////////////
  176.  
  177. enum VDCacheState {
  178.     kVDCacheStateFree,
  179.     kVDCacheStatePending,
  180.     kVDCacheStateReady,
  181.     kVDCacheStateActive,
  182.     kVDCacheStateComplete,
  183.     kVDCacheStateIdle,
  184.     kVDCacheStateAborting,
  185.     kVDCacheStateCount
  186. };
  187.  
  188. struct VDCachedObjectNodes : public vdlist_node, public vdhashmap_node {
  189.     sint64    mHashKey;
  190. };
  191.  
  192. class VDCache {
  193. public:
  194.     VDCache(IVDCacheAllocator *pAllocator);
  195.     ~VDCache();
  196.  
  197.     void Shutdown();
  198.  
  199.     int GetStateCount(int state);
  200.  
  201.     void DumpListStatus(int state);
  202.  
  203.     VDCachedObject *Create(sint64 key, bool& is_new);
  204.  
  205.     VDCachedObject *Allocate(sint64 key);
  206.     void Schedule(VDCachedObject *);            // Moves a Pending or Active object to Ready.
  207.     VDCachedObject *GetNextReady();                // Selects a Ready object and moves it to Active.
  208.     void MarkCompleted(VDCachedObject *);        // Marks an object as completed.
  209.  
  210. public:
  211.     void NotifyFree(VDCachedObject *pObject);
  212.  
  213. protected:
  214.     void Evict(uint32 level);
  215.  
  216. protected:
  217.     VDCriticalSection    mLock;
  218.  
  219.     IVDCacheAllocator    *mpAllocator;
  220.     uint32        mObjectCount;
  221.     uint32        mObjectLimit;
  222.  
  223.     typedef vdlist<VDCachedObjectNodes> ObjectList;
  224.     ObjectList    mLists[kVDCacheStateCount];
  225.  
  226.     vdhashmap<sint64, VDCachedObjectNodes>    mHash;
  227. };
  228.  
  229. ///////////////////////////////////////////////////////////////////////////
  230.  
  231. class VDCachedObject : private VDCachedObjectNodes {
  232.     friend class VDCache;
  233. public:
  234.     VDCachedObject();
  235.     virtual ~VDCachedObject() {}
  236.  
  237.     int AddRef();
  238.     int Release();
  239.  
  240.     void WeakAddRef();
  241.     void WeakRelease();
  242.  
  243. protected:
  244.     virtual void OnCacheEvict() {}
  245.     virtual void OnCacheAbortPending() {}
  246.     virtual void DumpStatus() {}
  247.  
  248. protected:
  249.     int GetRefCount() const { return mRefCount; }
  250.     void SetCache(VDCache *pCache);
  251.  
  252.     VDCacheState GetState() const { return mState; }
  253.     void SetState(VDCacheState state) { mState = state; }
  254.  
  255.     sint64 GetCacheKey() const { return mHashKey; }
  256.  
  257.     virtual bool IsValid() const { return true; }
  258.  
  259. protected:
  260.     VDCache            *mpCache;
  261.     VDAtomicInt        mRefCount;
  262.     VDCacheState    mState;
  263. };
  264.  
  265. ///////////////////////////////////////////////////////////////////////////
  266.  
  267. class VDPooledObject;
  268.  
  269. class IVDPoolAllocator {
  270. public:
  271.     virtual VDPooledObject *OnPoolAllocate() = 0;
  272. };
  273.  
  274. ///////////////////////////////////////////////////////////////////////////
  275.  
  276. enum VDPoolState {
  277.     kVDPoolStateFree,
  278.     kVDPoolStateActive,
  279.     kVDPoolStateCount
  280. };
  281.  
  282. struct VDPooledObjectNodes : public vdlist_node {};
  283.  
  284. class VDPool {
  285. public:
  286.     VDPool(IVDPoolAllocator *pAllocator);
  287.     ~VDPool();
  288.  
  289.     void Shutdown();
  290.  
  291.     VDPooledObject *Allocate();
  292.  
  293. public:
  294.     void NotifyFree(VDPooledObject *pObject);
  295.  
  296. protected:
  297.     VDCriticalSection    mLock;
  298.  
  299.     IVDPoolAllocator    *mpAllocator;
  300.     uint32        mObjectCount;
  301.     uint32        mObjectLimit;
  302.  
  303.     typedef vdlist<VDPooledObjectNodes> ObjectList;
  304.     ObjectList    mLists[kVDPoolStateCount];
  305. };
  306.  
  307. class VDPooledObject : private VDPooledObjectNodes {
  308.     friend class VDPool;
  309. public:
  310.     VDPooledObject();
  311.     virtual ~VDPooledObject() {}
  312.  
  313.     int AddRef();
  314.     int Release();
  315.  
  316. protected:
  317.     int GetRefCount() const { return mRefCount; }
  318.     void SetPool(VDPool *pPool);
  319.  
  320. protected:
  321.     VDPool            *mpPool;
  322.     VDAtomicInt        mRefCount;
  323. };
  324.  
  325. #endif
  326.