Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::SharedMemory Class Referencefinal

A block memory that is mapped into multiple processes. More...

#include <ipc.h>

Inheritance diagram for MaxOS::processes::SharedMemory:
MaxOS::processes::Resource

Public Member Functions

 SharedMemory (const string &name, size_t size, resource_type_t type)
 Creates a new shared memory block.
 
void open (size_t flags) final
 Map the shared memory into the address space of the owning process.
 
void close (size_t flags) final
 Frees the page containing the shared memory.
 
int read (void *buffer, size_t size, size_t flags) final
 Get the address that the current process has this shared memory mapped to.
 
uintptr_t physical_address () const
 Gets the physical address of the shared memory block.
 
size_t size () const
 Gets the size of the shared memory block.
 
- Public Member Functions inherited from MaxOS::processes::Resource
 Resource (const string &name, size_t flags, resource_type_t type)
 Constructs a new Resource object.
 
string name ()
 Gets the name of this resource.
 
resource_type_t type ()
 Gets the type of this resource.
 
virtual int write (const void *buffer, size_t size, size_t flags)
 write a certain amount of bytes to a resource
 

Detailed Description

A block memory that is mapped into multiple processes.

Definition at line 29 of file ipc.h.

Constructor & Destructor Documentation

◆ SharedMemory()

SharedMemory::SharedMemory ( const string name,
size_t  size,
resource_type_t  type 
)

Creates a new shared memory block.

Parameters
nameThe name of the block
sizeThe size of the shared memory region
typeThe type of resource

Definition at line 29 of file ipc.cpp.

30: Resource(name, size, syscore::ResourceType::SHARED_MEMORY),
31 m_size(size)
32{
33
34 m_physical_address = (uintptr_t) PhysicalMemoryManager::s_current_manager->allocate_area(0, size);
35}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
Definition physical.h:185
Represents a generic resource that can be opened, closed, read from and written to.
Definition resource.h:29
string name()
Gets the name of this resource.
Definition resource.cpp:79
size_t size() const
Gets the size of the shared memory block.
Definition ipc.cpp:72

References MaxOS::memory::PhysicalMemoryManager::s_current_manager, and size().

Member Function Documentation

◆ close()

void SharedMemory::close ( size_t  flags)
finalvirtual

Frees the page containing the shared memory.

Parameters
flagsNot used for this resource

Reimplemented from MaxOS::processes::Resource.

Definition at line 99 of file ipc.cpp.

99 {
100
101 PhysicalMemoryManager::s_current_manager->free_area(m_physical_address, m_size);
102
103}

References MaxOS::memory::PhysicalMemoryManager::s_current_manager.

◆ open()

void SharedMemory::open ( size_t  flags)
finalvirtual

Map the shared memory into the address space of the owning process.

Parameters
flagsNot used for this resource

Reimplemented from MaxOS::processes::Resource.

Definition at line 82 of file ipc.cpp.

82 {
83
84 // Process has already opened this memory
85 auto it = m_mappings.find(GlobalScheduler::current_process()->pid());
86 if(it != m_mappings.end())
87 return;
88
89 auto virtual_address = (uintptr_t)GlobalScheduler::current_process()->memory_manager->vmm()->load_shared_memory(m_physical_address, m_size);
90 m_mappings.insert(GlobalScheduler::current_process()->pid(), virtual_address);
91
92}
iterator find(Key)
Finds an element in the map based on the key.
Definition map.h:153
iterator end()
Returns the end of the map.
Definition map.h:141
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Definition map.h:253
static Process * current_process()
Gets the process on the currently executing core.

References MaxOS::processes::GlobalScheduler::current_process(), MaxOS::common::Map< Key, Value >::end(), MaxOS::common::Map< Key, Value >::find(), and MaxOS::common::Map< Key, Value >::insert().

◆ physical_address()

uintptr_t SharedMemory::physical_address ( ) const

Gets the physical address of the shared memory block.

Returns
The physical address

Definition at line 62 of file ipc.cpp.

62 {
63
64 return m_physical_address;
65}

◆ read()

int SharedMemory::read ( void buffer,
size_t  size,
size_t  flags 
)
finalvirtual

Get the address that the current process has this shared memory mapped to.

Parameters
bufferNot used
sizeNot used
flagsNot used
Returns
The virtual address in the current process's address space or 0 if not found

Reimplemented from MaxOS::processes::Resource.

Definition at line 47 of file ipc.cpp.

47 {
48
49 // Process hasn't opened the resource
50 auto it = m_mappings.find(GlobalScheduler::current_process()->pid());
51 if(it == m_mappings.end())
52 return 0;
53
54 return it->second;
55}

References MaxOS::processes::GlobalScheduler::current_process(), MaxOS::common::Map< Key, Value >::end(), and MaxOS::common::Map< Key, Value >::find().

◆ size()

size_t SharedMemory::size ( ) const

Gets the size of the shared memory block.

Returns
The size

Definition at line 72 of file ipc.cpp.

72 {
73
74 return m_size;
75}

Referenced by SharedMemory().


The documentation for this class was generated from the following files: