]> git.sesse.net Git - casparcg/blob - common/memory/page_locked_allocator.h
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / memory / page_locked_allocator.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #pragma once\r
21 \r
22 #include <unordered_map>\r
23 #include <tbb/mutex.h>\r
24 \r
25 namespace caspar\r
26 {\r
27         \r
28 template <class T>\r
29 class page_locked_allocator\r
30 {\r
31 public:\r
32         typedef size_t    size_type;\r
33         typedef ptrdiff_t difference_type;\r
34         typedef T*        pointer;\r
35         typedef const T*  const_pointer;\r
36         typedef T&        reference;\r
37         typedef const T&  const_reference;\r
38         typedef T         value_type;\r
39 \r
40         page_locked_allocator() {}\r
41         page_locked_allocator(const page_locked_allocator&) {}\r
42   \r
43         pointer allocate(size_type n, const void * = 0) \r
44         {\r
45                 tbb::mutex::scoped_lock lock(get().mutex);\r
46 \r
47                 size_type size = n * sizeof(T);         \r
48                 if(get().free < size)\r
49                         allocate_store(size);\r
50 \r
51                 auto p = ::VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);\r
52                 if(!p)\r
53                         throw std::bad_alloc();\r
54 \r
55                 if(::VirtualLock(p, size) == 0) \r
56                 {\r
57                         ::VirtualFree(p, 0, MEM_RELEASE);\r
58                         throw std::bad_alloc();\r
59                 }\r
60                 \r
61                 get().free -= size;\r
62                 get().map[p] = size;\r
63                 return reinterpret_cast<T*>(p);\r
64         }\r
65   \r
66         void deallocate(void* p, size_type) \r
67         {\r
68                 tbb::mutex::scoped_lock lock(get().mutex);\r
69 \r
70                 if(!p || get().map.find(p) == get().map.end())\r
71                         return;\r
72 \r
73                 try\r
74                 {\r
75                         ::VirtualFree(p, 0, MEM_RELEASE);\r
76                         get().free += get().map[p];\r
77                         get().map.erase(p);\r
78                 }\r
79                 catch(...){}            \r
80         }\r
81 \r
82         pointer           address(reference x) const { return &x; }\r
83         const_pointer     address(const_reference x) const { return &x; }\r
84         page_locked_allocator<T>&  operator=(const page_locked_allocator&) { return *this; }\r
85         void              construct(pointer p, const T& val) { new ((T*) p) T(val); }\r
86         void              destroy(pointer p) { p->~T(); }\r
87 \r
88         size_type         max_size() const { return size_t(-1); }\r
89 \r
90         template <class U>\r
91         struct rebind { typedef page_locked_allocator<U> other; };\r
92 \r
93         template <class U>\r
94         page_locked_allocator(const page_locked_allocator<U>&) {}\r
95 \r
96         template <class U>\r
97         page_locked_allocator& operator=(const page_locked_allocator<U>&) { return *this; }\r
98 \r
99 private:\r
100 \r
101         void allocate_store(size_type size)\r
102         {               \r
103                 SIZE_T workingSetMinSize = 0, workingSetMaxSize = 0;\r
104                 if(::GetProcessWorkingSetSize(::GetCurrentProcess(), &workingSetMinSize, &workingSetMaxSize))\r
105                 {                       \r
106                         workingSetMinSize += size;\r
107                         workingSetMaxSize += size;\r
108 \r
109                         if(!::SetProcessWorkingSetSize(::GetCurrentProcess(), workingSetMinSize, workingSetMaxSize))            \r
110                                 throw std::bad_alloc();         \r
111 \r
112                         get().free += size;\r
113                 }\r
114         }\r
115 \r
116         struct impl\r
117         {\r
118                 impl() : free(0){}\r
119                 std::unordered_map<void*, size_type> map;\r
120                 size_type free;\r
121                 tbb::mutex mutex;\r
122         };\r
123 \r
124         static impl& get()\r
125         {\r
126                 static impl i;\r
127                 return i;\r
128         }\r
129 };\r
130 }