summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Include/WAVM/Platform/Memory.h
blob: 84e49584c02a21a80b9000e11d195e9e2dd84792 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once

#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Platform/Defines.h"

namespace WAVM { namespace Platform {
	// Describes allowed memory accesses.
	enum class MemoryAccess
	{
		none,
		readOnly,
		readWrite,
		readExecute,
		readWriteExecute
	};

	// Returns the base 2 logarithm of the number of bytes in the smallest virtual page.
	WAVM_API Uptr getBytesPerPageLog2();

	// Returns the number of bytes in the smallest virtual page.
	inline Uptr getBytesPerPage() { return Uptr(1) << getBytesPerPageLog2(); }

	// Allocates virtual addresses without commiting physical pages to them.
	// Returns the base virtual address of the allocated addresses, or nullptr if the virtual
	// address space has been exhausted.
	WAVM_API U8* allocateVirtualPages(Uptr numPages);

	// Allocates virtual addresses without commiting physical pages to them.
	// Returns the base virtual address of the allocated addresses, or nullptr if the virtual
	// address space has been exhausted.
	WAVM_API U8* allocateAlignedVirtualPages(Uptr numPages,
											 Uptr alignmentLog2,
											 U8*& outUnalignedBaseAddress);

	// Commits physical memory to the specified virtual pages.
	// baseVirtualAddress must be a multiple of the preferred page size.
	// Return true if successful, or false if physical memory has been exhausted.
	WAVM_API bool commitVirtualPages(U8* baseVirtualAddress,
									 Uptr numPages,
									 MemoryAccess access = MemoryAccess::readWrite);

	// Changes the allowed access to the specified virtual pages.
	// baseVirtualAddress must be a multiple of the preferred page size.
	// Return true if successful, or false if the access-level could not be set.
	WAVM_API bool setVirtualPageAccess(U8* baseVirtualAddress, Uptr numPages, MemoryAccess access);

	// Decommits the physical memory that was committed to the specified virtual pages.
	// baseVirtualAddress must be a multiple of the preferred page size.
	WAVM_API void decommitVirtualPages(U8* baseVirtualAddress, Uptr numPages);

	// Frees virtual addresses. baseVirtualAddress must also be the address returned by
	// allocateVirtualPages.
	WAVM_API void freeVirtualPages(U8* baseVirtualAddress, Uptr numPages);

	// Frees an aligned virtual address block. unalignedBaseAddress must be the unaligned base
	// address returned in the outUnalignedBaseAddress parameter of a call to
	// allocateAlignedVirtualPages.
	WAVM_API void freeAlignedVirtualPages(U8* unalignedBaseAddress,
										  Uptr numPages,
										  Uptr alignmentLog2);

	// Gets memory usage information for this process.
	WAVM_API Uptr getPeakMemoryUsageBytes();
}}