DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
MapFile.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3// Fork baseline commit: system-00-00-20 (2020-09-25).
4// Renamed since fork: yes (from src/MapFile.cxx to src/MapFile.cpp).
5
6/*
7 * MapFile.cxx
8 * ers
9 *
10 * Created by Matthias Wiesmann on 07.01.05.
11 * Copyright 2005 CERN. All rights reserved.
12 *
13 */
14
15#include <sys/types.h>
16#include <sys/mman.h>
17#include <iostream>
18#include <unistd.h>
19
20
22#include "okssystem/MapFile.hpp"
24
25#include "ers/Assertion.hpp"
26
35
36OksSystem::MapFile::MapFile(const std::string &name, size_t s, size_t o, bool read_mode, bool write_mode, mode_t perm) : File(name) {
37 ERS_PRECONDITION((read_mode || write_mode));
38 const int page_size = getpagesize();
39 ERS_PRECONDITION((s % page_size)==0);
40 ERS_PRECONDITION((o % page_size)==0);
41 (void) page_size;// to suppress warning when asserts are disabled
42 m_map_read = read_mode;
43 m_map_write = write_mode;
44 m_map_permission = perm;
45 m_map_size = s;
46 m_map_offset = o;
47 m_map_address = 0;
49 m_is_mapped = false;
50} // MapFile
51
54
56 if (m_map_descriptor!=0) {
57 close_fd();
58 } // file descriptor open
59} // ~MapFile
60
64
69
70
74
76 m_map_descriptor->close();
77 delete(m_map_descriptor);
79} // close_fd
80
85
88 int prot = 0;
89 int flags = MAP_FILE | MAP_SHARED;
90 if (m_map_read) {
91 prot|=PROT_READ;
92 }
93 if (m_map_write) {
94 prot|=PROT_WRITE;
95 }
97 if (m_map_address==MAP_FAILED || m_map_address==0) {
98 m_is_mapped = false;
99 std::string message = "on file " + this->full_name();
100 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "mmap", message.c_str() );
101 } /* map_failed */ else {
102 m_is_mapped = true;
103 }
104} // map_mem
105
109
112 const int status = munmap(m_map_address,m_map_size);
113 if (status<0) {
114 m_is_mapped = true;
115 std::string message = "on file " + this->full_name();
116 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "munmap", message.c_str() );
117 } else {
118 m_is_mapped = false;
119 }
120} // unmap_mem
121
124
126 ERS_ASSERT(m_map_write==true);
127 const int flags = OksSystem::Descriptor::flags(false,true);
129 const int page_size = ::getpagesize();
130 const int page_num = (m_map_size+m_map_offset) / page_size;
131 void *buffer= calloc(1,page_size);
132 for(int i=0;i<page_num;i++) {
133 fd.write(buffer,page_size);
134 } // for
135 free(buffer);
136 fd.close();
137} // zero
138
139
145
147 open_fd();
148 map_mem();
149} // map
150
156
158 unmap_mem();
159 close_fd();
160} // unmap
161
162bool OksSystem::MapFile::is_loaded() const throw() {
163 return (m_map_address!=0);
164} // is_loaded
165
166
171
172void *OksSystem::MapFile::address() const throw() {
173 return m_map_address;
174} // address
175
179
180size_t OksSystem::MapFile::memory_size() const throw() {
181 return m_map_size;
182} // memory_size
183
185
187 return m_is_mapped;
188}
189
#define ERS_PRECONDITION(expression)
#define ERS_ASSERT(expression)
#define ERS_HERE
File descriptor / Socket wrapper.
static int flags(bool read_mode, bool write_mode)
File(const std::string &name)
Definition File.cpp:301
const std::string & full_name() const
full name for file *‍/
Definition File.cpp:411
void unmap_mem()
unmaps the file into memory
Definition MapFile.cpp:110
void * m_map_address
the address of the map in memory
Definition MapFile.hpp:54
void zero() const
builds a zeroed file with correct length
Definition MapFile.cpp:125
size_t m_map_offset
offset in the file of the map
Definition MapFile.hpp:56
size_t m_map_size
the size of the map
Definition MapFile.hpp:55
MapFile(const std::string &file, size_t size, size_t offset, bool read_mode, bool write_mode, mode_t permissions=0666)
Definition MapFile.cpp:36
bool is_mapped() const
is the file mopped im memory
Definition MapFile.cpp:186
bool m_map_write
is the map writable
Definition MapFile.hpp:60
void unmap()
unmaps the file
Definition MapFile.cpp:157
Descriptor * m_map_descriptor
internal file descriptor
Definition MapFile.hpp:57
bool m_is_mapped
is the file mapped in memory
Definition MapFile.hpp:61
bool is_loaded() const
is the map loaded in memory
Definition MapFile.cpp:162
OksSystem::Descriptor * fd() const
the file descriptor. This method returns a valid pointer only if called after map().
Definition MapFile.cpp:190
void open_fd()
opens the file descriptor for the map
Definition MapFile.cpp:65
void close_fd()
closes the file descriptor for the map
Definition MapFile.cpp:75
size_t memory_size() const
the size of the map
Definition MapFile.cpp:180
void map_mem()
maps the file into memory
Definition MapFile.cpp:86
bool m_map_read
is the map readable
Definition MapFile.hpp:59
void map()
maps the file in memory
Definition MapFile.cpp:146
mode_t m_map_permission
permissions associated with the file
Definition MapFile.hpp:58
void * address() const
the address of the memory mapped file
Definition MapFile.cpp:172