summaryrefslogtreecommitdiffstats
path: root/Whisper/CPU/LargeBuffer.h
blob: d9a9a22be1dc1df44ba4c6739553ba3a9f77b64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once

namespace CpuCompute
{
	// A large memory buffer allocated with VirtualAlloc kernel API, bypassing the heap.
	class LargeBuffer
	{
		void* pv = nullptr;
	public:
		LargeBuffer() = default;
		LargeBuffer( const LargeBuffer& ) = delete;
		LargeBuffer( LargeBuffer&& that ) noexcept
		{
			pv = that.pv;
			that.pv = nullptr;
		}
		~LargeBuffer()
		{
			deallocate();
		}
		void operator=( LargeBuffer&& that ) noexcept
		{
			std::swap( pv, that.pv );
		}
		void operator=( const LargeBuffer& that ) = delete;

		// Allocate buffer with specified count of bytes, and read+write memory protection
		// The OS kernel guarantees zero-initialization of that memory.
		HRESULT allocate( size_t cb );

		// Change memory protection of the buffer to read only
		HRESULT setReadOnly( size_t cb );

		// Unless the pointer is nullptr, deallocate the buffer
		void deallocate();

		// Pointer to the start of the buffer, aligned by memory page = 4 kilobytes
		uint8_t* pointer() const
		{
			assert( nullptr != pv );
			return (uint8_t*)pv;
		}
	};
}