Line data Source code
1 :
2 : /* Copyright (c) 2009-2016, 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 <lunchbox/api.h>
22 : #include <boost/noncopyable.hpp>
23 : #include <iostream>
24 : #include <string>
25 :
26 : namespace lunchbox
27 : {
28 : namespace detail { class MemoryMap; }
29 :
30 : /**
31 : * Helper to map a file to a memory address (mmap).
32 : * @deprecated Use boost::iostreams::mapped_file_source
33 : *
34 : * Example: @include tests/memoryMap.cpp
35 : */
36 : class MemoryMap : public boost::noncopyable
37 : {
38 : public:
39 : /** Construct a new memory map. @version 1.0 */
40 : LUNCHBOX_API MemoryMap();
41 :
42 : /**
43 : * Construct and initialize a new, readonly memory map.
44 : *
45 : * @throw std::runtime_error if file can't be mapped.
46 : * @version 1.7.1 */
47 : LUNCHBOX_API explicit MemoryMap( const std::string& filename );
48 :
49 : /**
50 : * Construct and initialize a new, read-write memory map.
51 : *
52 : * @throw std::runtime_error if file can't be created.
53 : * @version 1.9.1
54 : */
55 : LUNCHBOX_API MemoryMap( const std::string& filename, const size_t size );
56 :
57 : /**
58 : * Destruct the memory map.
59 : *
60 : * Unmaps the file, if it is still mapped.
61 : * @sa unmap()
62 : * @version 1.0
63 : */
64 : LUNCHBOX_API ~MemoryMap();
65 :
66 : /**
67 : * Map a file to a memory address.
68 : *
69 : * The file is only mapped read-only. The file is automatically unmapped
70 : * when the memory map is deleted.
71 : *
72 : * @param filename The filename of the file to map.
73 : * @return the pointer to the mapped file, or 0 upon error.
74 : * @version 1.0
75 : */
76 : LUNCHBOX_API const void* map( const std::string& filename );
77 :
78 : /**
79 : * Remap a different file for this memory map.
80 : *
81 : * The file is only mapped read-only. An existing map is unmapped.
82 : *
83 : * @param filename The filename of the file to map.
84 : * @return the pointer to the mapped file, or 0 upon error.
85 : * @version 1.9.1
86 : */
87 : LUNCHBOX_API const void* remap( const std::string& filename );
88 :
89 : /**
90 : * Create a writable file to a memory address.
91 : *
92 : * The file is mapped read-write. An existing file will be overwritten. The
93 : * file is automatically unmapped when the memory map is deleted.
94 : *
95 : * @param filename The filename of the file to map.
96 : * @param size this size of the file.
97 : * @return the pointer to the mapped file, or 0 upon error.
98 : * @version 1.9.1
99 : */
100 : LUNCHBOX_API void* create( const std::string& filename, const size_t size );
101 :
102 : /**
103 : * Recreate a different writable file for this memory map.
104 : *
105 : * The file is only mapped read-write. An existing map is unmapped.
106 : *
107 : * @param filename The filename of the file to map.
108 : * @param size this size of the file.
109 : * @return the pointer to the mapped file, or 0 upon error.
110 : * @version 1.0
111 : */
112 : LUNCHBOX_API void* recreate( const std::string& filename,
113 : const size_t size );
114 :
115 : /** Unmap the file. @version 1.0 */
116 : LUNCHBOX_API void unmap();
117 :
118 : /** @return the pointer to the memory map. @version 1.0 */
119 : LUNCHBOX_API const void* getAddress() const;
120 :
121 : /** @return the pointer to the memory map. @version 1.9.1 */
122 : LUNCHBOX_API void* getAddress();
123 :
124 : /** @return the pointer to the memory map. @version 1.9.1 */
125 0 : template< class T > const T* getAddress() const
126 0 : { return static_cast< const T* >( getAddress( )); }
127 :
128 : /** @return the pointer to the memory map. @version 1.9.1 */
129 2 : template< class T > T* getAddress()
130 2 : { return static_cast< T* >( getAddress( )); }
131 :
132 : /** @return the size of the memory map. @version 1.0 */
133 : LUNCHBOX_API size_t getSize() const;
134 :
135 : private:
136 : detail::MemoryMap* const impl_;
137 : };
138 :
139 : inline std::ostream& operator << ( std::ostream& os, const MemoryMap& m )
140 : {
141 : return os << "MemoryMap at " << m.getAddress() << " size " << m.getSize();
142 : }
143 :
144 : }
145 :
146 : #endif //LUNCHBOX_MEMORYMAP_H
|