DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::MapFile Class Reference

Wrapper for memory map operations. More...

#include <MapFile.hpp>

Inheritance diagram for OksSystem::MapFile:
[legend]
Collaboration diagram for OksSystem::MapFile:
[legend]

Public Member Functions

 MapFile (const std::string &file, size_t size, size_t offset, bool read_mode, bool write_mode, mode_t permissions=0666)
 
 ~MapFile ()
 
void zero () const
 builds a zeroed file with correct length
 
void map ()
 maps the file in memory
 
void unmap ()
 unmaps the file
 
bool is_mapped () const
 is the file mopped im memory
 
bool is_loaded () const throw ()
 is the map loaded in memory
 
void * address () const throw ()
 the address of the memory mapped file
 
size_t memory_size () const throw ()
 the size of the map
 
OksSystem::Descriptorfd () const throw ()
 the file descriptor. This method returns a valid pointer only if called after map().
 
- Public Member Functions inherited from OksSystem::File
 File (const std::string &name)
 
 File (const char *name)
 
 File (const File &other)
 
virtual ~File ()
 
 operator std::string () const throw ()
 
 operator const char * () const throw ()
 
 operator bool () const throw ()
 
bool equals (const File &other) const throw ()
 compare two files *‍/
 
 operator size_t () const
 
const std::string & full_name () const throw ()
 full name for file *‍/
 
const char * c_full_name () const throw ()
 
std::string short_name () const throw ()
 short name for file *‍/
 
std::string parent_name () const throw ()
 path of directory containing file *‍/
 
std::string extension () const throw ()
 extension for file *‍/
 
int depth () const throw ()
 depth of the file *‍/
 
OksSystem::File parent () const
 parent of the current file *‍/
 
OksSystem::File child (const std::string &name) const
 named child of the current directory *‍/
 
OksSystem::File temporary (const std::string &prefix) const
 
bool exists () const throw ()
 does the file exist *‍/
 
mode_t permissions () const
 permissions for the file *‍/
 
std::string pretty_permissions () const
 pretty permissions for the file *‍/
 
size_t size () const
 size of file *‍/
 
uid_t owner_id () const
 owner id of file *‍/
 
User owner () const
 owner of the file *‍/
 
gid_t group () const
 group of file *‍/
 
bool is_regular () const
 is the file a directory *‍/
 
bool is_directory () const
 is the file a regular file *‍/
 
bool is_fifo () const
 is the file a named pipe *‍/
 
std::string file_type () const
 type of the file *‍/
 
file_list_t directory () const
 list of file in directory *‍/
 
void unlink () const
 deletes (unlinks) file *‍/
 
void rmdir () const
 deletes directory *‍/
 
void remove () const
 recursively delete files and directories *‍/
 
void rename (const File &other) const
 rename or moves the file *‍/
 
void permissions (mode_t permissions) const
 sets the type of the file *‍/
 
void make_dir (mode_t permissions) const
 creates a directory *‍/
 
void make_path (mode_t permissions) const
 creates a full path *‍/
 
void make_fifo (mode_t permissions) const
 creates a FIFO (named pipe) *‍/
 
void ensure_path (mode_t permissions) const
 creates the parent path *‍/
 
std::istream * input () const
 returns an input stream from the file*‍/
 
std::ostream * output (bool append=false) const
 returns an output stream to the file*‍/
 

Protected Member Functions

void open_fd ()
 opens the file descriptor for the map
 
void close_fd ()
 closes the file descriptor for the map
 
void map_mem ()
 maps the file into memory
 
void unmap_mem ()
 unmaps the file into memory
 
- Protected Member Functions inherited from OksSystem::File
void set_name (const std::string &name)
 sets the name of the file *‍/
 
mode_t get_mode () const
 get mode associated with file (permission + type) *‍/
 

Protected Attributes

void * m_map_address
 the address of the map in memory
 
size_t m_map_size
 the size of the map
 
size_t m_map_offset
 offset in the file of the map
 
Descriptorm_map_descriptor
 internal file descriptor
 
mode_t m_map_permission
 permissions associated with the file
 
bool m_map_read
 is the map readable

 
bool m_map_write
 is the map writable

 
bool m_is_mapped
 is the file mapped in memory
 
- Protected Attributes inherited from OksSystem::File
std::string m_full_name
 full name (path) of the file *‍/
 

Static Private Attributes

static const char *const MAP_LOAD_MSG
 message pattern - map is loaded
 
static const char *const MAP_NOT_LOAD_MSG
 message pattern - map is not loaded
 
static const char *const PAGE_SIZE_MSG
 message pattern - illegal page size
 
static const char *const READ_OR_WRITE_MSG
 message pattern - map mode is neither read nor write
 
static const char *const MAP_NOT_WRITE_MSG
 message pattern - map is not writable
 

Additional Inherited Members

- Public Types inherited from OksSystem::File
typedef std::vector< OksSystem::Filefile_list_t
 
- Static Public Member Functions inherited from OksSystem::File
static std::string protocol (const std::string &url) throw ()
 extracts the protocol part of an url *‍/
 
static std::string extension (const std::string &url) throw ()
 extracts the extension of a path or an url *‍/
 
static std::string uri (const std::string &url) throw ()
 extracts the local path of an url *‍/
 
static std::string short_name (const std::string &url) throw ()
 extracts the short file name of an url *‍/
 
static int depth (const std::string &path) throw ()
 calculates depth of a path *‍/
 
static std::string first_line (const std::string &text)
 extracts the first line of a text *‍/
 
static File from_url (const std::string &url)
 build a file out of an URL *‍/
 
static std::string working_directory ()
 current directory of process *‍/
 
static void working_directory (const File &dir)
 set working directory of process *‍/
 
static std::string expand_home (const std::string path)
 resolve home directory *‍/
 
static std::string pretty_permissions (mode_t permissions)
 pretty prints permissions *‍/
 
static std::string to_string (mode_t permissions) throw ()
 converts permission to string *‍/
 
static std::string pretty_open_flag (int flags)
 pretty prints open flags *‍/
 
static std::string pretty_size (size_t size, bool cut_small)
 pretty prints a file size *‍/
 
- Static Public Attributes inherited from OksSystem::File
static const char *const FILE_FLAG_STR = "-rwxS"
 column headers for display of permissions *‍/
 
static const char *const FILE_PROTOCOL = "file"
 string for the file protocol *‍/
 
- Static Protected Member Functions inherited from OksSystem::File
static int unit (int u)
 calculates the value of a computer unit of order n (i.e KB,GB, etc *‍/
 
- Static Protected Attributes inherited from OksSystem::File
static const char *const HUMAN_SIZE_STR [] = { "B", "KB", "MB", "GB" }
 strings for pretty printing file sizes *‍/
 
static const char *const HUMAN_OPEN_STR [] = { "READ", "WRITE", "NOBLOCK", "APPEND", "CREATE", "TRUNCATE","EXCLUSIVE" }
 strings for pretty printing open flags *‍/
 
static const int KILOBYTE = 1024
 number of bytes in a kilobyte *‍/
 
static const char *const FILE_COMMAND_PATH = "/usr/bin/file"
 

Detailed Description

Wrapper for memory map operations.

This class offers facilities to handle memory mapped files. They can be used to map a file into memory

Author
Matthias Wiesmann
Version
1.0

Definition at line 26 of file MapFile.hpp.

Constructor & Destructor Documentation

◆ MapFile()

OksSystem::MapFile::MapFile ( const std::string & name,
size_t s,
size_t o,
bool read_mode,
bool write_mode,
mode_t perm = 0666 )

Builds a memory mapped file.

Parameters
namethe path of the file to map.
sizethe size of the region to memory map - should be a multiple of the pagesize.
offsetthe offset in the file - should be a multiple of pagesize.
read_modeshould the map be read from the file.
write_modeshould the map be writable.
permissionsthe permissions associated with the file

Definition at line 31 of file MapFile.cpp.

31 : File(name) {
32 ERS_PRECONDITION((read_mode || write_mode));
33 const int page_size = getpagesize();
34 ERS_PRECONDITION((s % page_size)==0);
35 ERS_PRECONDITION((o % page_size)==0);
36 (void) page_size;// to suppress warning when asserts are disabled
37 m_map_read = read_mode;
38 m_map_write = write_mode;
39 m_map_permission = perm;
40 m_map_size = s;
41 m_map_offset = o;
42 m_map_address = 0;
44 m_is_mapped = false;
45} // MapFile
#define ERS_PRECONDITION(expression)
File(const std::string &name)
Definition File.cpp:296
void * m_map_address
the address of the map in memory
Definition MapFile.hpp:49
size_t m_map_offset
offset in the file of the map
Definition MapFile.hpp:51
size_t m_map_size
the size of the map
Definition MapFile.hpp:50
bool m_map_write
is the map writable
Definition MapFile.hpp:55
Descriptor * m_map_descriptor
internal file descriptor
Definition MapFile.hpp:52
bool m_is_mapped
is the file mapped in memory
Definition MapFile.hpp:56
bool m_map_read
is the map readable
Definition MapFile.hpp:54
mode_t m_map_permission
permissions associated with the file
Definition MapFile.hpp:53

◆ ~MapFile()

OksSystem::MapFile::~MapFile ( )

Destructor - the file should be unmapped before destruction.

Definition at line 50 of file MapFile.cpp.

50 {
51 if (m_map_descriptor!=0) {
52 close_fd();
53 } // file descriptor open
54} // ~MapFile
void close_fd()
closes the file descriptor for the map
Definition MapFile.cpp:70

Member Function Documentation

◆ address()

void * OksSystem::MapFile::address ( ) const
throw ( )

the address of the memory mapped file

Gives the actual address of the map in memory. If the file is not yet mapped, this will be 0.

Returns
point to the start address of the mapped file in memory.

Definition at line 167 of file MapFile.cpp.

167 {
168 return m_map_address;
169} // address

◆ close_fd()

void OksSystem::MapFile::close_fd ( )
protected

closes the file descriptor for the map

Closes the file descriptor associated with the memory map.

Exceptions
CloseFailif the close okssystem call fails

Definition at line 70 of file MapFile.cpp.

70 {
72 delete(m_map_descriptor);
74} // close_fd
void close()
close the descriptor

◆ fd()

OksSystem::Descriptor * OksSystem::MapFile::fd ( ) const
throw ( )

the file descriptor. This method returns a valid pointer only if called after map().

Definition at line 185 of file MapFile.cpp.

185 {
186 return m_map_descriptor;
187}

◆ is_loaded()

bool OksSystem::MapFile::is_loaded ( ) const
throw ( )

is the map loaded in memory

Definition at line 157 of file MapFile.cpp.

157 {
158 return (m_map_address!=0);
159} // is_loaded

◆ is_mapped()

bool OksSystem::MapFile::is_mapped ( ) const

is the file mopped im memory

Returns if the file is mapped or not in memory

Definition at line 181 of file MapFile.cpp.

181 {
182 return m_is_mapped;
183}

◆ map()

void OksSystem::MapFile::map ( )

maps the file in memory

Maps the file into memory. This is done first by opening the file descriptor for the file, then doing the actual map.

Exceptions
OpenFailerror while opening the file
MMapFailerror while mapping the file

Definition at line 141 of file MapFile.cpp.

141 {
142 open_fd();
143 map_mem();
144} // map
void open_fd()
opens the file descriptor for the map
Definition MapFile.cpp:60
void map_mem()
maps the file into memory
Definition MapFile.cpp:81

◆ map_mem()

void OksSystem::MapFile::map_mem ( )
protected

maps the file into memory

Internal method - does the actual memory mapping.

Exceptions
MMapFailif the mmap operation succeeded
Preconditionif the file descriptor is not valid.

Definition at line 81 of file MapFile.cpp.

81 {
83 int prot = 0;
84 int flags = MAP_FILE | MAP_SHARED;
85 if (m_map_read) {
86 prot|=PROT_READ;
87 }
88 if (m_map_write) {
89 prot|=PROT_WRITE;
90 }
92 if (m_map_address==MAP_FAILED || m_map_address==0) {
93 m_is_mapped = false;
94 std::string message = "on file " + this->full_name();
95 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "mmap", message.c_str() );
96 } /* map_failed */ else {
97 m_is_mapped = true;
98 }
99} // map_mem
#define ERS_HERE
const std::string & full_name() const
full name for file *‍/
Definition File.cpp:406

◆ memory_size()

size_t OksSystem::MapFile::memory_size ( ) const
throw ( )

the size of the map

Gives the actual size of the map in memory.

Returns
the size of the memory region

Definition at line 175 of file MapFile.cpp.

175 {
176 return m_map_size;
177} // memory_size

◆ open_fd()

void OksSystem::MapFile::open_fd ( )
protected

opens the file descriptor for the map

Opens the file descriptor associated with the memory map.

Exceptions
OpenFailif the open okssystem call fails

Definition at line 60 of file MapFile.cpp.

60 {
63} // open_fd
File descriptor / Socket wrapper.
static int flags(bool read_mode, bool write_mode)

◆ unmap()

void OksSystem::MapFile::unmap ( )

unmaps the file

Unmaps the file from memory This is done first by unmapping the file, then closing the file-descriptor.

Exceptions
CloseFailerror while closing the file.
MUnmapFailerror while unmapping the file.

Definition at line 152 of file MapFile.cpp.

152 {
153 unmap_mem();
154 close_fd();
155} // unmap
void unmap_mem()
unmaps the file into memory
Definition MapFile.cpp:105

◆ unmap_mem()

void OksSystem::MapFile::unmap_mem ( )
protected

unmaps the file into memory

Internal method - does the actual memory unmapping.

Exceptions
MUnmapFailif the munmap operation fails.

Definition at line 105 of file MapFile.cpp.

105 {
107 const int status = munmap(m_map_address,m_map_size);
108 if (status<0) {
109 m_is_mapped = true;
110 std::string message = "on file " + this->full_name();
111 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "munmap", message.c_str() );
112 } else {
113 m_is_mapped = false;
114 }
115} // unmap_mem

◆ zero()

void OksSystem::MapFile::zero ( ) const

builds a zeroed file with correct length

Creates a zero filled file with correct length

Definition at line 120 of file MapFile.cpp.

120 {
121 ERS_ASSERT(m_map_write==true);
122 const int flags = OksSystem::Descriptor::flags(false,true);
124 const int page_size = ::getpagesize();
125 const int page_num = (m_map_size+m_map_offset) / page_size;
126 void *buffer= calloc(1,page_size);
127 for(int i=0;i<page_num;i++) {
128 fd.write(buffer,page_size);
129 } // for
130 free(buffer);
131 fd.close();
132} // zero
#define ERS_ASSERT(expression)
int write(const void *buffer, size_t number) const
OksSystem::Descriptor * fd() const
the file descriptor. This method returns a valid pointer only if called after map().
Definition MapFile.cpp:185

Member Data Documentation

◆ m_is_mapped

bool OksSystem::MapFile::m_is_mapped
protected

is the file mapped in memory

Definition at line 56 of file MapFile.hpp.

◆ m_map_address

void* OksSystem::MapFile::m_map_address
protected

the address of the map in memory

Definition at line 49 of file MapFile.hpp.

◆ m_map_descriptor

Descriptor* OksSystem::MapFile::m_map_descriptor
protected

internal file descriptor

Definition at line 52 of file MapFile.hpp.

◆ m_map_offset

size_t OksSystem::MapFile::m_map_offset
protected

offset in the file of the map

Definition at line 51 of file MapFile.hpp.

◆ m_map_permission

mode_t OksSystem::MapFile::m_map_permission
protected

permissions associated with the file

Definition at line 53 of file MapFile.hpp.

◆ m_map_read

bool OksSystem::MapFile::m_map_read
protected

is the map readable

Definition at line 54 of file MapFile.hpp.

◆ m_map_size

size_t OksSystem::MapFile::m_map_size
protected

the size of the map

Definition at line 50 of file MapFile.hpp.

◆ m_map_write

bool OksSystem::MapFile::m_map_write
protected

is the map writable

Definition at line 55 of file MapFile.hpp.

◆ MAP_LOAD_MSG

const char* const OksSystem::MapFile::MAP_LOAD_MSG
staticprivate

message pattern - map is loaded

Definition at line 60 of file MapFile.hpp.

◆ MAP_NOT_LOAD_MSG

const char* const OksSystem::MapFile::MAP_NOT_LOAD_MSG
staticprivate

message pattern - map is not loaded

Definition at line 61 of file MapFile.hpp.

◆ MAP_NOT_WRITE_MSG

const char* const OksSystem::MapFile::MAP_NOT_WRITE_MSG
staticprivate

message pattern - map is not writable

Definition at line 64 of file MapFile.hpp.

◆ PAGE_SIZE_MSG

const char* const OksSystem::MapFile::PAGE_SIZE_MSG
staticprivate

message pattern - illegal page size

Definition at line 62 of file MapFile.hpp.

◆ READ_OR_WRITE_MSG

const char* const OksSystem::MapFile::READ_OR_WRITE_MSG
staticprivate

message pattern - map mode is neither read nor write

Definition at line 63 of file MapFile.hpp.


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