DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
MapFile.cpp
Go to the documentation of this file.
1/*
2 * MapFile.cxx
3 * ers
4 *
5 * Created by Matthias Wiesmann on 07.01.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10#include <sys/types.h>
11#include <sys/mman.h>
12#include <iostream>
13#include <unistd.h>
14
15
17#include "okssystem/MapFile.hpp"
19
20#include "ers/Assertion.hpp"
21
31OksSystem::MapFile::MapFile(const std::string &name, size_t s, size_t o, bool read_mode, bool write_mode, mode_t perm) : 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
46
51 if (m_map_descriptor!=0) {
52 close_fd();
53 } // file descriptor open
54} // ~MapFile
55
61 const int flags = OksSystem::Descriptor::flags(m_map_read,m_map_write);
62 m_map_descriptor = new OksSystem::Descriptor(this, flags,m_map_permission);
63} // open_fd
64
65
71 m_map_descriptor->close();
72 delete(m_map_descriptor);
73 m_map_descriptor = 0;
74} // close_fd
75
82 ERS_PRECONDITION(m_map_descriptor);
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 }
91 m_map_address = ::mmap(0,m_map_size,prot,flags,*m_map_descriptor,m_map_offset);
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
100
106 ERS_PRECONDITION(m_map_address!=0);
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
116
121 ERS_ASSERT(m_map_write==true);
122 const int flags = OksSystem::Descriptor::flags(false,true);
123 OksSystem::Descriptor fd(this, flags,m_map_permission);
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
133
134
142 open_fd();
143 map_mem();
144} // map
145
153 unmap_mem();
154 close_fd();
155} // unmap
156
157bool OksSystem::MapFile::is_loaded() const throw() {
158 return (m_map_address!=0);
159} // is_loaded
160
161
167void *OksSystem::MapFile::address() const throw() {
168 return m_map_address;
169} // address
170
175size_t OksSystem::MapFile::memory_size() const throw() {
176 return m_map_size;
177} // memory_size
178
182 return m_is_mapped;
183}
184
186 return m_map_descriptor;
187}
#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
Wrapper for file operations.
Definition File.hpp:32
void unmap_mem()
unmaps the file into memory
Definition MapFile.cpp:105
void * m_map_address
the address of the map in memory
Definition MapFile.hpp:49
void zero() const
builds a zeroed file with correct length
Definition MapFile.cpp:120
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
MapFile(const std::string &file, size_t size, size_t offset, bool read_mode, bool write_mode, mode_t permissions=0666)
Definition MapFile.cpp:31
bool is_mapped() const
is the file mopped im memory
Definition MapFile.cpp:181
bool m_map_write
is the map writable
Definition MapFile.hpp:55
void unmap()
unmaps the file
Definition MapFile.cpp:152
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 is_loaded() const
is the map loaded in memory
Definition MapFile.cpp:157
OksSystem::Descriptor * fd() const
the file descriptor. This method returns a valid pointer only if called after map().
Definition MapFile.cpp:185
void open_fd()
opens the file descriptor for the map
Definition MapFile.cpp:60
void close_fd()
closes the file descriptor for the map
Definition MapFile.cpp:70
size_t memory_size() const
the size of the map
Definition MapFile.cpp:175
void map_mem()
maps the file into memory
Definition MapFile.cpp:81
bool m_map_read
is the map readable
Definition MapFile.hpp:54
void map()
maps the file in memory
Definition MapFile.cpp:141
mode_t m_map_permission
permissions associated with the file
Definition MapFile.hpp:53
void * address() const
the address of the memory mapped file
Definition MapFile.cpp:167