3 #include "os/windows/windows.h"
\r
5 #include <unordered_map>
\r
6 #include <tbb/mutex.h>
\r
7 #include <tbb/atomic.h>
\r
9 namespace caspar { namespace detail {
\r
12 std::unordered_map<void*, size_t> g_map;
\r
15 void allocate_store(size_t size)
\r
17 SIZE_T workingSetMinSize = 0, workingSetMaxSize = 0;
\r
18 if(::GetProcessWorkingSetSize(::GetCurrentProcess(), &workingSetMinSize, &workingSetMaxSize))
\r
20 workingSetMinSize += size;
\r
21 workingSetMaxSize += size;
\r
23 if(!::SetProcessWorkingSetSize(::GetCurrentProcess(), workingSetMinSize, workingSetMaxSize))
\r
24 throw std::bad_alloc();
\r
30 void* alloc_page_locked(size_t size)
\r
32 tbb::mutex::scoped_lock lock(g_mutex);
\r
35 allocate_store(size);
\r
37 auto p = ::VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
\r
39 throw std::bad_alloc();
\r
41 if(::VirtualLock(p, size) == 0)
\r
43 ::VirtualFree(p, 0, MEM_RELEASE);
\r
44 throw std::bad_alloc();
\r
53 void free_page_locked(void* p)
\r
55 tbb::mutex::scoped_lock lock(g_mutex);
\r
57 if(g_map.find(p) == g_map.end())
\r
60 ::VirtualFree(p, 0, MEM_RELEASE);
\r