Lunchbox  1.17.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
memoryMap.h
1 
2 /* Copyright (c) 2009-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_MEMORYMAP_H
19 #define LUNCHBOX_MEMORYMAP_H
20 
21 #include <boost/noncopyable.hpp>
22 #include <iostream>
23 #include <lunchbox/api.h>
24 #include <string>
25 
26 namespace lunchbox
27 {
28 namespace detail
29 {
30 class MemoryMap;
31 }
32 
39 class MemoryMap : public boost::noncopyable
40 {
41 public:
43  LUNCHBOX_API MemoryMap();
44 
50  LUNCHBOX_API explicit MemoryMap(const std::string& filename);
51 
58  LUNCHBOX_API MemoryMap(const std::string& filename, const size_t size);
59 
67  LUNCHBOX_API ~MemoryMap();
68 
79  LUNCHBOX_API const void* map(const std::string& filename);
80 
90  LUNCHBOX_API const void* remap(const std::string& filename);
91 
103  LUNCHBOX_API void* create(const std::string& filename, const size_t size);
104 
115  LUNCHBOX_API void* recreate(const std::string& filename, size_t size);
116 
127  LUNCHBOX_API void* resize(size_t size);
128 
130  LUNCHBOX_API void unmap();
131 
133  LUNCHBOX_API const void* getAddress() const;
134 
136  LUNCHBOX_API void* getAddress();
137 
139  template <class T>
140  const T* getAddress() const
141  {
142  return static_cast<const T*>(getAddress());
143  }
144 
146  template <class T>
148  {
149  return static_cast<T*>(getAddress());
150  }
151 
153  template <class T>
154  T& get(const size_t i)
155  {
156  return getAddress<T>()[i];
157  }
158 
160  template <class T>
161  const T& get(const size_t i) const
162  {
163  return getAddress<T>()[i];
164  }
165 
167  LUNCHBOX_API size_t getSize() const;
168 
169 private:
170  detail::MemoryMap* const impl_;
171 };
172 
173 inline std::ostream& operator<<(std::ostream& os, const MemoryMap& m)
174 {
175  return os << "MemoryMap at " << m.getAddress() << " size " << m.getSize();
176 }
177 }
178 
179 #endif // LUNCHBOX_MEMORYMAP_H
Defines export visibility macros for library Lunchbox.
const void * getAddress() const
size_t getSize() const
Helper to map a file to a memory address (mmap).
Definition: memoryMap.h:39
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
Definition: array.h:59
const T * getAddress() const
Definition: memoryMap.h:140