2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
2009-01-16 02:58:34 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2018-05-15 21:28:41 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2008-07-12 17:40:22 +00:00
|
|
|
// This class lets you create a block of anonymous RAM, and then arbitrarily map views into it.
|
2013-04-19 13:21:45 +00:00
|
|
|
// Multiple views can mirror the same section of the block, which makes it very convenient for
|
2021-06-21 00:13:00 +00:00
|
|
|
// emulating memory mirrors.
|
|
|
|
class MemArena final
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-10-13 19:21:25 +00:00
|
|
|
public:
|
2021-06-21 00:13:00 +00:00
|
|
|
MemArena();
|
|
|
|
~MemArena();
|
|
|
|
MemArena(const MemArena&) = delete;
|
|
|
|
MemArena(MemArena&&) = delete;
|
|
|
|
MemArena& operator=(const MemArena&) = delete;
|
|
|
|
MemArena& operator=(MemArena&&) = delete;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Allocate the singular memory segment handled by this MemArena. This will be the actual
|
|
|
|
/// 'physical' available memory for this arena. After allocation, it can be interacted with using
|
|
|
|
/// CreateView() and ReleaseView(). Used to make a mappable region for emulated memory.
|
|
|
|
///
|
|
|
|
/// @param size The amount of bytes that should be allocated in this region.
|
|
|
|
///
|
2014-11-01 20:30:14 +00:00
|
|
|
void GrabSHMSegment(size_t size);
|
2021-06-21 00:13:00 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Release the memory segment previously allocated with GrabSHMSegment().
|
|
|
|
/// Should not be called before all views have been released.
|
|
|
|
///
|
2014-11-01 20:30:14 +00:00
|
|
|
void ReleaseSHMSegment();
|
2021-06-21 00:13:00 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Map a memory region in the memory segment previously allocated with GrabSHMSegment().
|
|
|
|
///
|
|
|
|
/// @param offset Offset within the memory segment to map at.
|
|
|
|
/// @param size Size of the region to map.
|
|
|
|
///
|
|
|
|
/// @return Pointer to the memory region, or nullptr on failure.
|
|
|
|
///
|
|
|
|
void* CreateView(s64 offset, size_t size);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Unmap a memory region previously mapped with CreateView().
|
|
|
|
/// Should not be called on a view that is still mapped into the virtual memory region.
|
|
|
|
///
|
|
|
|
/// @param view Pointer returned by CreateView().
|
|
|
|
/// @param size Size passed to the corresponding CreateView() call.
|
|
|
|
///
|
2016-01-21 20:16:51 +00:00
|
|
|
void ReleaseView(void* view, size_t size);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2021-06-21 00:13:00 +00:00
|
|
|
///
|
|
|
|
/// Reserve the singular 'virtual' memory region handled by this MemArena. This is used to create
|
|
|
|
/// our 'fastmem' memory area for the emulated game code to access directly.
|
|
|
|
///
|
|
|
|
/// @param memory_size Size in bytes of the memory region to reserve.
|
|
|
|
///
|
|
|
|
/// @return Pointer to the memory region, or nullptr on failure.
|
|
|
|
///
|
|
|
|
u8* ReserveMemoryRegion(size_t memory_size);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Release the memory region previously reserved with ReserveMemoryRegion().
|
|
|
|
/// Should not be called while any memory region is still mapped.
|
|
|
|
///
|
|
|
|
void ReleaseMemoryRegion();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Map a section from the memory segment previously allocated with GrabSHMSegment()
|
|
|
|
/// into the region previously reserved with ReserveMemoryRegion().
|
|
|
|
///
|
|
|
|
/// @param offset Offset within the memory segment previous allocated by GrabSHMSegment() to map
|
|
|
|
/// from.
|
|
|
|
/// @param size Size of the region to map.
|
|
|
|
/// @param base Address within the memory region from ReserveMemoryRegion() where to map it.
|
|
|
|
///
|
|
|
|
/// @return The address we actually ended up mapping, which should be the given 'base'.
|
|
|
|
///
|
|
|
|
void* MapInMemoryRegion(s64 offset, size_t size, void* base);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Unmap a memory region previously mapped with MapInMemoryRegion().
|
|
|
|
///
|
|
|
|
/// @param view Pointer returned by MapInMemoryRegion().
|
|
|
|
/// @param size Size passed to the corresponding MapInMemoryRegion() call.
|
|
|
|
///
|
|
|
|
void UnmapFromMemoryRegion(void* view, size_t size);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
private:
|
2008-07-12 17:40:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-10-13 19:21:25 +00:00
|
|
|
HANDLE hMemoryMapping;
|
2008-07-12 17:40:22 +00:00
|
|
|
#else
|
2008-10-13 19:21:25 +00:00
|
|
|
int fd;
|
2008-07-12 17:40:22 +00:00
|
|
|
#endif
|
|
|
|
};
|
2018-05-15 21:28:41 +00:00
|
|
|
|
|
|
|
} // namespace Common
|