]> git.sesse.net Git - casparcg/blob - common/memory/page_locked_allocator.h
* Created custom decklink allocator for reducing memory footprint.
[casparcg] / common / memory / page_locked_allocator.h
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #pragma once\r
23 \r
24 #include <unordered_map>\r
25 #include <tbb/mutex.h>\r
26 \r
27 namespace caspar\r
28 {\r
29         \r
30 template <class T>\r
31 class page_locked_allocator\r
32 {\r
33 public:\r
34         typedef size_t    size_type;\r
35         typedef ptrdiff_t difference_type;\r
36         typedef T*        pointer;\r
37         typedef const T*  const_pointer;\r
38         typedef T&        reference;\r
39         typedef const T&  const_reference;\r
40         typedef T         value_type;\r
41 \r
42         page_locked_allocator() {}\r
43         page_locked_allocator(const page_locked_allocator&) {}\r
44   \r
45         pointer allocate(size_type n, const void * = 0) \r
46         {\r
47                 tbb::mutex::scoped_lock lock(get().mutex);\r
48 \r
49                 size_type size = n * sizeof(T);         \r
50                 if(get().free < size)\r
51                         allocate_store(size);\r
52 \r
53                 auto p = ::VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);\r
54                 if(!p)\r
55                         throw std::bad_alloc();\r
56 \r
57                 if(::VirtualLock(p, size) == 0) \r
58                 {\r
59                         ::VirtualFree(p, 0, MEM_RELEASE);\r
60                         throw std::bad_alloc();\r
61                 }\r
62                 \r
63                 get().free -= size;\r
64                 get().map[p] = size;\r
65                 return reinterpret_cast<T*>(p);\r
66         }\r
67   \r
68         void deallocate(void* p, size_type) \r
69         {\r
70                 tbb::mutex::scoped_lock lock(get().mutex);\r
71 \r
72                 if(!p || get().map.find(p) == get().map.end())\r
73                         return;\r
74 \r
75                 try\r
76                 {\r
77                         ::VirtualFree(p, 0, MEM_RELEASE);\r
78                         get().free += get().map[p];\r
79                         get().map.erase(p);\r
80                 }\r
81                 catch(...){}            \r
82         }\r
83 \r
84         pointer           address(reference x) const { return &x; }\r
85         const_pointer     address(const_reference x) const { return &x; }\r
86         page_locked_allocator<T>&  operator=(const page_locked_allocator&) { return *this; }\r
87         bool                       operator!=(const page_locked_allocator&) const { return false; }\r
88         bool                       operator==(const page_locked_allocator&) const { return true; }\r
89         void              construct(pointer p, const T& val) { new ((T*) p) T(val); }\r
90         void              destroy(pointer p) { p->~T(); }\r
91 \r
92         size_type         max_size() const { return size_t(-1); }\r
93 \r
94         template <class U>\r
95         struct rebind { typedef page_locked_allocator<U> other; };\r
96 \r
97         template <class U>\r
98         page_locked_allocator(const page_locked_allocator<U>&) {}\r
99 \r
100         template <class U>\r
101         page_locked_allocator& operator=(const page_locked_allocator<U>&) { return *this; }\r
102 \r
103 private:\r
104 \r
105         void allocate_store(size_type size)\r
106         {               \r
107                 SIZE_T workingSetMinSize = 0, workingSetMaxSize = 0;\r
108                 if(::GetProcessWorkingSetSize(::GetCurrentProcess(), &workingSetMinSize, &workingSetMaxSize))\r
109                 {                       \r
110                         workingSetMinSize += size;\r
111                         workingSetMaxSize += size;\r
112 \r
113                         if(!::SetProcessWorkingSetSize(::GetCurrentProcess(), workingSetMinSize, workingSetMaxSize))            \r
114                                 throw std::bad_alloc();         \r
115 \r
116                         get().free += size;\r
117                 }\r
118         }\r
119 \r
120         struct impl\r
121         {\r
122                 impl() : free(0){}\r
123                 std::unordered_map<void*, size_type> map;\r
124                 size_type free;\r
125                 tbb::mutex mutex;\r
126         };\r
127 \r
128         static impl& get()\r
129         {\r
130                 static impl i;\r
131                 return i;\r
132         }\r
133 };\r
134 }