]> git.sesse.net Git - casparcg/blob - core/consumer/bluefish/memory.h
2.0.0.2:
[casparcg] / core / consumer / bluefish / memory.h
1 #pragma once\r
2 \r
3 #include <Windows.h>\r
4 \r
5 #include <BlueVelvet4.h>\r
6 #include "../../frame/frame_format.h"\r
7 #include "exception.h"\r
8 \r
9 namespace caspar { namespace bluefish {\r
10         \r
11 static const size_t MAX_HANC_BUFFER_SIZE = 256*1024;\r
12 static const size_t MAX_VBI_BUFFER_SIZE = 36*1920*4;\r
13 \r
14 struct page_locked_buffer\r
15 {\r
16 public:\r
17         page_locked_buffer(size_t size) : size_(size), data_(static_cast<unsigned char*>(::VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)))\r
18         {\r
19                 if(!data_)      \r
20                         BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Failed to allocate memory for paged locked buffer."));  \r
21                 if(::VirtualLock(data_.get(), size_) == 0)      \r
22                         BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Failed to lock memory for paged locked buffer."));\r
23         }\r
24 \r
25         static void reserve_working_size(size_t size)\r
26         {\r
27                 SIZE_T workingSetMinSize = 0, workingSetMaxSize = 0;\r
28                 if(::GetProcessWorkingSetSize(::GetCurrentProcess(), &workingSetMinSize, &workingSetMaxSize))\r
29                 {\r
30                         CASPAR_LOG(debug) << TEXT("WorkingSet size: min = ") << workingSetMinSize << TEXT(", max = ") << workingSetMaxSize;\r
31                         \r
32                         workingSetMinSize += size;\r
33                         workingSetMaxSize += size;\r
34 \r
35                         if(!::SetProcessWorkingSetSize(::GetCurrentProcess(), workingSetMinSize, workingSetMaxSize))            \r
36                                 BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Failed set workingset."));              \r
37                 }\r
38         }\r
39 \r
40         PBYTE data() const { return data_.get(); }\r
41         size_t size() const { return size_; }\r
42 private:\r
43 \r
44         struct virtual_free\r
45         {\r
46                 void operator()(LPVOID lpAddress)\r
47                 {\r
48                         if(lpAddress != nullptr)                \r
49                                 try{::VirtualFree(lpAddress, 0, MEM_RELEASE);}catch(...){}              \r
50                 }\r
51         };\r
52 \r
53         size_t size_;\r
54         std::unique_ptr<BYTE, virtual_free> data_;\r
55 };\r
56 typedef std::shared_ptr<page_locked_buffer> page_locked_buffer_ptr;\r
57 \r
58 }}