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 <iostream>
16
#include <sys/mman.h>
17
#include <sys/types.h>
18
#include <unistd.h>
19
20
#include "
okssystem/Descriptor.hpp
"
21
#include "
okssystem/MapFile.hpp
"
22
#include "
okssystem/exceptions.hpp
"
23
24
#include "
ers/Assertion.hpp
"
25
34
35
OksSystem::MapFile::MapFile
(
const
std::string& name,
size_t
s,
size_t
o,
bool
read_mode,
bool
write_mode, mode_t perm)
36
:
File
(name)
37
{
38
ERS_PRECONDITION
((read_mode || write_mode));
39
const
int
page_size = getpagesize();
40
ERS_PRECONDITION
((s % page_size) == 0);
41
ERS_PRECONDITION
((o % page_size) == 0);
42
(void)page_size;
// to suppress warning when asserts are disabled
43
m_map_read
= read_mode;
44
m_map_write
= write_mode;
45
m_map_permission
= perm;
46
m_map_size
= s;
47
m_map_offset
= o;
48
m_map_address
= 0;
49
m_map_descriptor
= 0;
50
m_is_mapped
=
false
;
51
}
// MapFile
52
55
56
OksSystem::MapFile::~MapFile
()
57
{
58
if
(
m_map_descriptor
!= 0) {
59
close_fd
();
60
}
// file descriptor open
61
}
// ~MapFile
62
66
67
void
68
OksSystem::MapFile::open_fd
()
69
{
70
const
int
flags =
OksSystem::Descriptor::flags
(
m_map_read
,
m_map_write
);
71
m_map_descriptor
=
new
OksSystem::Descriptor
(
this
, flags,
m_map_permission
);
72
}
// open_fd
73
77
78
void
79
OksSystem::MapFile::close_fd
()
80
{
81
m_map_descriptor
->close();
82
delete
(
m_map_descriptor
);
83
m_map_descriptor
= 0;
84
}
// close_fd
85
90
91
void
92
OksSystem::MapFile::map_mem
()
93
{
94
ERS_PRECONDITION
(
m_map_descriptor
);
95
int
prot = 0;
96
int
flags = MAP_FILE | MAP_SHARED;
97
if
(
m_map_read
) {
98
prot |= PROT_READ;
99
}
100
if
(
m_map_write
) {
101
prot |= PROT_WRITE;
102
}
103
m_map_address
= ::mmap(0,
m_map_size
, prot, flags, *
m_map_descriptor
,
m_map_offset
);
104
if
(
m_map_address
== MAP_FAILED ||
m_map_address
== 0) {
105
m_is_mapped
=
false
;
106
std::string message =
"on file "
+ this->
full_name
();
107
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, errno,
"mmap"
, message.c_str());
108
}
/* map_failed */
else
{
109
m_is_mapped
=
true
;
110
}
111
}
// map_mem
112
116
117
void
118
OksSystem::MapFile::unmap_mem
()
119
{
120
ERS_PRECONDITION
(
m_map_address
!= 0);
121
const
int
status = munmap(
m_map_address
,
m_map_size
);
122
if
(status < 0) {
123
m_is_mapped
=
true
;
124
std::string message =
"on file "
+ this->
full_name
();
125
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, errno,
"munmap"
, message.c_str());
126
}
else
{
127
m_is_mapped
=
false
;
128
}
129
}
// unmap_mem
130
133
134
void
135
OksSystem::MapFile::zero
()
const
136
{
137
ERS_ASSERT
(
m_map_write
==
true
);
138
const
int
flags =
OksSystem::Descriptor::flags
(
false
,
true
);
139
OksSystem::Descriptor
fd
(
this
, flags,
m_map_permission
);
140
const
int
page_size = ::getpagesize();
141
const
int
page_num = (
m_map_size
+
m_map_offset
) / page_size;
142
void
* buffer = calloc(1, page_size);
143
for
(
int
i = 0; i < page_num; i++) {
144
fd
.
write
(buffer, page_size);
145
}
// for
146
free(buffer);
147
fd
.
close
();
148
}
// zero
149
155
156
void
157
OksSystem::MapFile::map
()
158
{
159
open_fd
();
160
map_mem
();
161
}
// map
162
168
169
void
170
OksSystem::MapFile::unmap
()
171
{
172
unmap_mem
();
173
close_fd
();
174
}
// unmap
175
176
bool
177
OksSystem::MapFile::is_loaded
()
const
throw()
178
{
179
return
(
m_map_address
!= 0);
180
}
// is_loaded
181
186
187
void
*
188
OksSystem::MapFile::address
()
const
throw()
189
{
190
return
m_map_address
;
191
}
// address
192
196
197
size_t
198
OksSystem::MapFile::memory_size
()
const
throw()
199
{
200
return
m_map_size
;
201
}
// memory_size
202
204
205
bool
206
OksSystem::MapFile::is_mapped
()
const
207
{
208
return
m_is_mapped
;
209
}
210
211
OksSystem::Descriptor
*
212
OksSystem::MapFile::fd
()
const
throw()
213
{
214
return
m_map_descriptor
;
215
}
Assertion.hpp
ERS_PRECONDITION
#define ERS_PRECONDITION(expression)
ERS_ASSERT
#define ERS_ASSERT(expression)
Descriptor.hpp
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:127
MapFile.hpp
OksSystem::Descriptor
File descriptor / Socket wrapper.
Definition
Descriptor.hpp:35
OksSystem::Descriptor::flags
static int flags(bool read_mode, bool write_mode)
Definition
Descriptor.cpp:27
OksSystem::Descriptor::close
void close()
close the descriptor
Definition
Descriptor.cpp:85
OksSystem::Descriptor::write
int write(const void *buffer, size_t number) const
Definition
Descriptor.cpp:117
OksSystem::File::File
File(const std::string &name)
Definition
File.cpp:327
OksSystem::File::full_name
const std::string & full_name() const
full name for file */
Definition
File.cpp:443
OksSystem::MapFile::unmap_mem
void unmap_mem()
unmaps the file into memory
Definition
MapFile.cpp:118
OksSystem::MapFile::m_map_address
void * m_map_address
the address of the map in memory
Definition
MapFile.hpp:59
OksSystem::MapFile::zero
void zero() const
builds a zeroed file with correct length
Definition
MapFile.cpp:135
OksSystem::MapFile::m_map_offset
size_t m_map_offset
offset in the file of the map
Definition
MapFile.hpp:61
OksSystem::MapFile::m_map_size
size_t m_map_size
the size of the map
Definition
MapFile.hpp:60
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:35
OksSystem::MapFile::is_mapped
bool is_mapped() const
is the file mopped im memory
Definition
MapFile.cpp:206
OksSystem::MapFile::~MapFile
~MapFile()
Definition
MapFile.cpp:56
OksSystem::MapFile::m_map_write
bool m_map_write
is the map writable
Definition
MapFile.hpp:65
OksSystem::MapFile::unmap
void unmap()
unmaps the file
Definition
MapFile.cpp:170
OksSystem::MapFile::m_map_descriptor
Descriptor * m_map_descriptor
internal file descriptor
Definition
MapFile.hpp:62
OksSystem::MapFile::m_is_mapped
bool m_is_mapped
is the file mapped in memory
Definition
MapFile.hpp:66
OksSystem::MapFile::is_loaded
bool is_loaded() const
is the map loaded in memory
Definition
MapFile.cpp:177
OksSystem::MapFile::fd
OksSystem::Descriptor * fd() const
the file descriptor. This method returns a valid pointer only if called after map().
Definition
MapFile.cpp:212
OksSystem::MapFile::open_fd
void open_fd()
opens the file descriptor for the map
Definition
MapFile.cpp:68
OksSystem::MapFile::close_fd
void close_fd()
closes the file descriptor for the map
Definition
MapFile.cpp:79
OksSystem::MapFile::memory_size
size_t memory_size() const
the size of the map
Definition
MapFile.cpp:198
OksSystem::MapFile::map_mem
void map_mem()
maps the file into memory
Definition
MapFile.cpp:92
OksSystem::MapFile::m_map_read
bool m_map_read
is the map readable
Definition
MapFile.hpp:64
OksSystem::MapFile::map
void map()
maps the file in memory
Definition
MapFile.cpp:157
OksSystem::MapFile::m_map_permission
mode_t m_map_permission
permissions associated with the file
Definition
MapFile.hpp:63
OksSystem::MapFile::address
void * address() const
the address of the memory mapped file
Definition
MapFile.cpp:188
exceptions.hpp
Generated on
for DUNE-DAQ by
1.18.0