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 <iostream>
16#include <sys/mman.h>
17#include <sys/types.h>
18#include <unistd.h>
19
21#include "okssystem/MapFile.hpp"
23
24#include "ers/Assertion.hpp"
25
34
35OksSystem::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;
50 m_is_mapped = false;
51} // MapFile
52
55
57{
58 if (m_map_descriptor != 0) {
59 close_fd();
60 } // file descriptor open
61} // ~MapFile
62
66
67void
73
77
78void
80{
81 m_map_descriptor->close();
82 delete (m_map_descriptor);
84} // close_fd
85
90
91void
93{
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
117void
119{
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
134void
136{
137 ERS_ASSERT(m_map_write == true);
138 const int flags = OksSystem::Descriptor::flags(false, true);
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
156void
158{
159 open_fd();
160 map_mem();
161} // map
162
168
169void
171{
172 unmap_mem();
173 close_fd();
174} // unmap
175
176bool
178{
179 return (m_map_address != 0);
180} // is_loaded
181
186
187void*
189{
190 return m_map_address;
191} // address
192
196
197size_t
199{
200 return m_map_size;
201} // memory_size
202
204
205bool
207{
208 return m_is_mapped;
209}
210
213{
214 return m_map_descriptor;
215}
#define ERS_PRECONDITION(expression)
#define ERS_ASSERT(expression)
#define ERS_HERE
File descriptor / Socket wrapper.
static int flags(bool read_mode, bool write_mode)
void close()
close the descriptor
int write(const void *buffer, size_t number) const
File(const std::string &name)
Definition File.cpp:327
const std::string & full_name() const
full name for file *‍/
Definition File.cpp:443
void unmap_mem()
unmaps the file into memory
Definition MapFile.cpp:118
void * m_map_address
the address of the map in memory
Definition MapFile.hpp:59
void zero() const
builds a zeroed file with correct length
Definition MapFile.cpp:135
size_t m_map_offset
offset in the file of the map
Definition MapFile.hpp:61
size_t m_map_size
the size of the map
Definition MapFile.hpp:60
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
bool is_mapped() const
is the file mopped im memory
Definition MapFile.cpp:206
bool m_map_write
is the map writable
Definition MapFile.hpp:65
void unmap()
unmaps the file
Definition MapFile.cpp:170
Descriptor * m_map_descriptor
internal file descriptor
Definition MapFile.hpp:62
bool m_is_mapped
is the file mapped in memory
Definition MapFile.hpp:66
bool is_loaded() const
is the map loaded in memory
Definition MapFile.cpp:177
OksSystem::Descriptor * fd() const
the file descriptor. This method returns a valid pointer only if called after map().
Definition MapFile.cpp:212
void open_fd()
opens the file descriptor for the map
Definition MapFile.cpp:68
void close_fd()
closes the file descriptor for the map
Definition MapFile.cpp:79
size_t memory_size() const
the size of the map
Definition MapFile.cpp:198
void map_mem()
maps the file into memory
Definition MapFile.cpp:92
bool m_map_read
is the map readable
Definition MapFile.hpp:64
void map()
maps the file in memory
Definition MapFile.cpp:157
mode_t m_map_permission
permissions associated with the file
Definition MapFile.hpp:63
void * address() const
the address of the memory mapped file
Definition MapFile.cpp:188