DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
okssystem
src
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
21
#include "
okssystem/Descriptor.hpp
"
22
#include "
okssystem/MapFile.hpp
"
23
#include "
okssystem/exceptions.hpp
"
24
25
#include "
ers/Assertion.hpp
"
26
35
36
OksSystem::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;
48
m_map_descriptor
= 0;
49
m_is_mapped
=
false
;
50
}
// MapFile
51
54
55
OksSystem::MapFile::~MapFile
() {
56
if
(
m_map_descriptor
!=0) {
57
close_fd
();
58
}
// file descriptor open
59
}
// ~MapFile
60
64
65
void
OksSystem::MapFile::open_fd
() {
66
const
int
flags =
OksSystem::Descriptor::flags
(
m_map_read
,
m_map_write
);
67
m_map_descriptor
=
new
OksSystem::Descriptor
(
this
, flags,
m_map_permission
);
68
}
// open_fd
69
70
74
75
void
OksSystem::MapFile::close_fd
() {
76
m_map_descriptor
->close();
77
delete
(
m_map_descriptor
);
78
m_map_descriptor
= 0;
79
}
// close_fd
80
85
86
void
OksSystem::MapFile::map_mem
() {
87
ERS_PRECONDITION
(
m_map_descriptor
);
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
}
96
m_map_address
= ::mmap(0,
m_map_size
,prot,flags,*
m_map_descriptor
,
m_map_offset
);
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
110
void
OksSystem::MapFile::unmap_mem
(){
111
ERS_PRECONDITION
(
m_map_address
!=0);
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
125
void
OksSystem::MapFile::zero
()
const
{
126
ERS_ASSERT
(
m_map_write
==
true
);
127
const
int
flags =
OksSystem::Descriptor::flags
(
false
,
true
);
128
OksSystem::Descriptor
fd
(
this
, flags,
m_map_permission
);
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
146
void
OksSystem::MapFile::map
() {
147
open_fd
();
148
map_mem
();
149
}
// map
150
156
157
void
OksSystem::MapFile::unmap
() {
158
unmap_mem
();
159
close_fd
();
160
}
// unmap
161
162
bool
OksSystem::MapFile::is_loaded
()
const
throw() {
163
return
(
m_map_address
!=0);
164
}
// is_loaded
165
166
171
172
void
*
OksSystem::MapFile::address
()
const
throw() {
173
return
m_map_address
;
174
}
// address
175
179
180
size_t
OksSystem::MapFile::memory_size
()
const
throw() {
181
return
m_map_size
;
182
}
// memory_size
183
185
186
bool
OksSystem::MapFile::is_mapped
()
const
{
187
return
m_is_mapped
;
188
}
189
190
OksSystem::Descriptor
*
OksSystem::MapFile::fd
()
const
throw() {
191
return
m_map_descriptor
;
192
}
Assertion.hpp
ERS_PRECONDITION
#define ERS_PRECONDITION(expression)
ERS_ASSERT
#define ERS_ASSERT(expression)
Descriptor.hpp
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:141
MapFile.hpp
OksSystem::Descriptor
File descriptor / Socket wrapper.
Definition
Descriptor.hpp:34
OksSystem::Descriptor::flags
static int flags(bool read_mode, bool write_mode)
Definition
Descriptor.cpp:26
OksSystem::File::File
File(const std::string &name)
Definition
File.cpp:301
OksSystem::File::full_name
const std::string & full_name() const
full name for file */
Definition
File.cpp:411
OksSystem::MapFile::unmap_mem
void unmap_mem()
unmaps the file into memory
Definition
MapFile.cpp:110
OksSystem::MapFile::m_map_address
void * m_map_address
the address of the map in memory
Definition
MapFile.hpp:54
OksSystem::MapFile::zero
void zero() const
builds a zeroed file with correct length
Definition
MapFile.cpp:125
OksSystem::MapFile::m_map_offset
size_t m_map_offset
offset in the file of the map
Definition
MapFile.hpp:56
OksSystem::MapFile::m_map_size
size_t m_map_size
the size of the map
Definition
MapFile.hpp:55
OksSystem::MapFile::MapFile
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
OksSystem::MapFile::is_mapped
bool is_mapped() const
is the file mopped im memory
Definition
MapFile.cpp:186
OksSystem::MapFile::~MapFile
~MapFile()
Definition
MapFile.cpp:55
OksSystem::MapFile::m_map_write
bool m_map_write
is the map writable
Definition
MapFile.hpp:60
OksSystem::MapFile::unmap
void unmap()
unmaps the file
Definition
MapFile.cpp:157
OksSystem::MapFile::m_map_descriptor
Descriptor * m_map_descriptor
internal file descriptor
Definition
MapFile.hpp:57
OksSystem::MapFile::m_is_mapped
bool m_is_mapped
is the file mapped in memory
Definition
MapFile.hpp:61
OksSystem::MapFile::is_loaded
bool is_loaded() const
is the map loaded in memory
Definition
MapFile.cpp:162
OksSystem::MapFile::fd
OksSystem::Descriptor * fd() const
the file descriptor. This method returns a valid pointer only if called after map().
Definition
MapFile.cpp:190
OksSystem::MapFile::open_fd
void open_fd()
opens the file descriptor for the map
Definition
MapFile.cpp:65
OksSystem::MapFile::close_fd
void close_fd()
closes the file descriptor for the map
Definition
MapFile.cpp:75
OksSystem::MapFile::memory_size
size_t memory_size() const
the size of the map
Definition
MapFile.cpp:180
OksSystem::MapFile::map_mem
void map_mem()
maps the file into memory
Definition
MapFile.cpp:86
OksSystem::MapFile::m_map_read
bool m_map_read
is the map readable
Definition
MapFile.hpp:59
OksSystem::MapFile::map
void map()
maps the file in memory
Definition
MapFile.cpp:146
OksSystem::MapFile::m_map_permission
mode_t m_map_permission
permissions associated with the file
Definition
MapFile.hpp:58
OksSystem::MapFile::address
void * address() const
the address of the memory mapped file
Definition
MapFile.cpp:172
exceptions.hpp
Generated on
for DUNE-DAQ by
1.17.0