Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb::FileMemorySpace Class Reference

Memory space implementation that exports the contents of a file as a memory space. More...

#include <MemoryConfig.hxx>

Inheritance diagram for openlcb::FileMemorySpace:
openlcb::MemorySpace Destructable openlcb::ROFileMemorySpace

Public Member Functions

 FileMemorySpace (int fd, address_t len=AUTO_LEN)
 Creates a memory space based on an fd.
 
 FileMemorySpace (const char *name, address_t len=AUTO_LEN)
 Creates a memory space based on a file name.
 
bool read_only () OVERRIDE
 
address_t max_address () OVERRIDE
 
size_t write (address_t destination, const uint8_t *data, size_t len, errorcode_t *error, Notifiable *again) OVERRIDE
 
size_t read (address_t source, uint8_t *dst, size_t len, errorcode_t *error, Notifiable *again) OVERRIDE
 
- Public Member Functions inherited from openlcb::MemorySpace
virtual bool set_node (Node *node)
 Specifies which node the next operation pertains.
 
virtual address_t min_address ()
 
virtual errorcode_t freeze ()
 Handles space freeze command.
 
virtual errorcode_t unfreeze ()
 Handles space unfreeze command.
 

Static Public Attributes

static const address_t AUTO_LEN = (address_t) - 1
 
static const address_t UNLIMITED_LEN = (address_t) - 2
 
- Static Public Attributes inherited from openlcb::MemorySpace
static const errorcode_t ERROR_AGAIN = 0x3FFF
 This error code signals that the operation was only partially completed, the again notify was used and will be notified when the operation can be re-tried).
 

Private Member Functions

void ensure_file_open ()
 Makes fd a valid parameter, and ensures fileSize is filled in.
 

Private Attributes

address_t fileSize_
 
const char * name_
 
int fd_
 

Additional Inherited Members

- Public Types inherited from openlcb::MemorySpace
typedef uint32_t address_t
 
typedef uint16_t errorcode_t
 

Detailed Description

Memory space implementation that exports the contents of a file as a memory space.

The file can be specified either as a path or an fd. By default writes are also allowed.

Definition at line 244 of file MemoryConfig.hxx.

Constructor & Destructor Documentation

◆ FileMemorySpace() [1/2]

openlcb::FileMemorySpace::FileMemorySpace ( int  fd,
address_t  len = AUTO_LEN 
)

Creates a memory space based on an fd.

Parameters
fdis an open file descriptor with the data.
lentells how many bytes there are in the memory space. If specified as AUTO_LEN, then uses fstat to figure out the size of the file.

Definition at line 100 of file MemoryConfig.cxx.

◆ FileMemorySpace() [2/2]

openlcb::FileMemorySpace::FileMemorySpace ( const char *  name,
address_t  len = AUTO_LEN 
)

Creates a memory space based on a file name.

Opens the file at the first use, and never closes it.

Parameters
nameis the file name to open. The pointer must stay alive so long as *this is around.
lentells how many bytes there are in the memory space. If specified as AUTO_LEN, then uses fstat to figure out the size of the file.

Definition at line 108 of file MemoryConfig.cxx.

Member Function Documentation

◆ ensure_file_open()

void openlcb::FileMemorySpace::ensure_file_open ( )
private

Makes fd a valid parameter, and ensures fileSize is filled in.

Definition at line 116 of file MemoryConfig.cxx.

◆ max_address()

address_t openlcb::FileMemorySpace::max_address ( )
inlinevirtual
Returns
the largest valid address for this block. A read of 1 from this address should succeed in returning the last byte.

Implements openlcb::MemorySpace.

Definition at line 275 of file MemoryConfig.hxx.

◆ read()

size_t openlcb::FileMemorySpace::read ( address_t  source,
uint8_t *  dst,
size_t  len,
errorcode_t *  error,
Notifiable again 
)
virtual
Returns
the number of bytes successfully read (before hitting end of space). If *error is set to non-null, then the operation has failed. If the operation needs to be continued, then sets error to ERROR_AGAIN, and calls the Notifiable
Parameters
againwhen a re-try makes sense. The caller should call read once more, with the offset adjusted with the previously returned bytes.

Implements openlcb::MemorySpace.

Definition at line 181 of file MemoryConfig.cxx.

◆ read_only()

bool openlcb::FileMemorySpace::read_only ( )
inlinevirtual
Returns
whether the memory space does not accept writes.

Reimplemented from openlcb::MemorySpace.

Reimplemented in openlcb::ROFileMemorySpace.

Definition at line 270 of file MemoryConfig.hxx.

◆ write()

size_t openlcb::FileMemorySpace::write ( address_t  destination,
const uint8_t *  data,
size_t  len,
errorcode_t *  error,
Notifiable again 
)
virtual
Returns
the number of bytes successfully written (before hitting end of space). If *error is set to non-null, then the operation has failed. If the operation needs to be continued, then sets error to MemorySpace::ERROR_AGAIN, and calls the Notifiable
Parameters
againwhen a re-try makes sense. The caller should call write once more, with the offset adjusted with the previously returned bytes.

Reimplemented from openlcb::MemorySpace.

Definition at line 145 of file MemoryConfig.cxx.

Member Data Documentation

◆ AUTO_LEN

const address_t openlcb::FileMemorySpace::AUTO_LEN = (address_t) - 1
static

Definition at line 247 of file MemoryConfig.hxx.

◆ fd_

int openlcb::FileMemorySpace::fd_
private

Definition at line 293 of file MemoryConfig.hxx.

◆ fileSize_

address_t openlcb::FileMemorySpace::fileSize_
private

Definition at line 291 of file MemoryConfig.hxx.

◆ name_

const char* openlcb::FileMemorySpace::name_
private

Definition at line 292 of file MemoryConfig.hxx.

◆ UNLIMITED_LEN

const address_t openlcb::FileMemorySpace::UNLIMITED_LEN = (address_t) - 2
static

Definition at line 248 of file MemoryConfig.hxx.


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