core/ObjectPool.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef _AVS_OBJECTPOOL_H | ||
| 2 | #define _AVS_OBJECTPOOL_H | ||
| 3 | |||
| 4 | #include <list> | ||
| 5 | #include <unordered_map> | ||
| 6 | #include <new> | ||
| 7 | #include <cassert> | ||
| 8 | |||
| 9 | template <typename T> | ||
| 10 | class ObjectPool | ||
| 11 | { | ||
| 12 | private: | ||
| 13 | |||
| 14 | typedef std::list<char*> ListType; | ||
| 15 | ListType UseList; | ||
| 16 | ListType FreeList; | ||
| 17 | |||
| 18 | typedef std::unordered_map<char*, typename ListType::iterator> MapType; | ||
| 19 | MapType Map; | ||
| 20 | |||
| 21 | 10 | static void DestructList(ListType* list, bool call_dtor) | |
| 22 | { | ||
| 23 | 10 | const ListType::iterator end_it = list->end(); | |
| 24 |
2/4ObjectPool<PrefetcherJobParams>::DestructList(std::__cxx11::list<char*, std::allocator<char*> >*, bool):
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 14 not taken.
ObjectPool<LruCache<unsigned long, PVideoFrame>::LruEntry>::DestructList(std::__cxx11::list<char*, std::allocator<char*> >*, bool):
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 14 taken 5 times.
|
10 | if (call_dtor) |
| 25 | { | ||
| 26 |
1/4ObjectPool<PrefetcherJobParams>::DestructList(std::__cxx11::list<char*, std::allocator<char*> >*, bool):
✗ Branch 12 → 5 not taken.
✗ Branch 12 → 13 not taken.
ObjectPool<LruCache<unsigned long, PVideoFrame>::LruEntry>::DestructList(std::__cxx11::list<char*, std::allocator<char*> >*, bool):
✗ Branch 12 → 5 not taken.
✓ Branch 12 → 13 taken 5 times.
|
5 | for (ListType::iterator it = list->begin(); it != end_it; ++it) |
| 27 | { | ||
| 28 | ✗ | T* obj = (T*)(*it); | |
| 29 | ✗ | obj->~T(); | |
| 30 | ✗ | delete [] (*it); | |
| 31 | } | ||
| 32 | } | ||
| 33 | else | ||
| 34 | { | ||
| 35 |
1/4ObjectPool<PrefetcherJobParams>::DestructList(std::__cxx11::list<char*, std::allocator<char*> >*, bool):
✗ Branch 20 → 15 not taken.
✗ Branch 20 → 21 not taken.
ObjectPool<LruCache<unsigned long, PVideoFrame>::LruEntry>::DestructList(std::__cxx11::list<char*, std::allocator<char*> >*, bool):
✗ Branch 20 → 15 not taken.
✓ Branch 20 → 21 taken 5 times.
|
5 | for (ListType::iterator it = list->begin(); it != end_it; ++it) |
| 36 | { | ||
| 37 | ✗ | delete [] (*it); | |
| 38 | } | ||
| 39 | } | ||
| 40 | 10 | } | |
| 41 | |||
| 42 | ✗ | char * Alloc() | |
| 43 | { | ||
| 44 | ✗ | char* buff = NULL; | |
| 45 | ✗ | if (!FreeList.empty()) | |
| 46 | { | ||
| 47 | ✗ | buff = FreeList.front(); | |
| 48 | ✗ | UseList.splice(UseList.begin(), FreeList, FreeList.begin()); | |
| 49 | } | ||
| 50 | else | ||
| 51 | { | ||
| 52 | ✗ | buff = new(std::nothrow) char[sizeof(T)]; | |
| 53 | ✗ | if (buff == NULL) | |
| 54 | ✗ | return NULL; | |
| 55 | |||
| 56 | ✗ | UseList.emplace_front(buff); | |
| 57 | } | ||
| 58 | |||
| 59 | ✗ | Map[buff] = UseList.begin(); | |
| 60 | ✗ | return buff; | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | void Free(char* obj) | |
| 64 | { | ||
| 65 | ✗ | MapType::iterator mit = Map.find(obj); | |
| 66 | ✗ | assert(mit != Map.end()); | |
| 67 | |||
| 68 | ✗ | ListType::iterator lit = mit->second; | |
| 69 | ✗ | assert(*lit == obj); | |
| 70 | |||
| 71 | ✗ | FreeList.splice(FreeList.begin(), UseList, lit); | |
| 72 | |||
| 73 | // This line is actually not needed, but very cheap and results in a fully consistent map | ||
| 74 | ✗ | mit->second = FreeList.begin(); // Needed because MSVC violates spec and invalidates old list iterator | |
| 75 | ✗ | } | |
| 76 | |||
| 77 | public: | ||
| 78 | |||
| 79 | |||
| 80 | // For the unfortunate lack of variadic templates in MSVC++2012 | ||
| 81 | ✗ | T* Construct() | |
| 82 | { | ||
| 83 | ✗ | char* buff = Alloc(); | |
| 84 | ✗ | if (buff == NULL) | |
| 85 | ✗ | throw std::bad_alloc(); | |
| 86 | |||
| 87 | try | ||
| 88 | { | ||
| 89 | ✗ | return new(buff) T(); | |
| 90 | } | ||
| 91 | catch(...) | ||
| 92 | { | ||
| 93 | Free(buff); | ||
| 94 | throw; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | // For the unfortunate lack of variadic templates in MSVC++2012 | ||
| 99 | template<typename K> | ||
| 100 | ✗ | T* Construct(const K& param0) | |
| 101 | { | ||
| 102 | ✗ | char* buff = Alloc(); | |
| 103 | ✗ | if (buff == NULL) | |
| 104 | ✗ | throw std::bad_alloc(); | |
| 105 | |||
| 106 | try | ||
| 107 | { | ||
| 108 | ✗ | return new(buff) T(param0); | |
| 109 | } | ||
| 110 | ✗ | catch(...) | |
| 111 | { | ||
| 112 | ✗ | Free(buff); | |
| 113 | ✗ | throw; | |
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | ✗ | void Destruct(T* obj) | |
| 118 | { | ||
| 119 | ✗ | obj->~T(); | |
| 120 | ✗ | Free((char*)obj); | |
| 121 | ✗ | } | |
| 122 | |||
| 123 | 5 | ~ObjectPool() | |
| 124 | { | ||
| 125 | 5 | DestructList(&FreeList, false); | |
| 126 | 5 | DestructList(&UseList, true); | |
| 127 | 5 | } | |
| 128 | }; | ||
| 129 | |||
| 130 | #endif // _AVS_OBJECTPOOL_H | ||
| 131 |