Line data Source code
1 :
2 : /* Copyright (c) 2009-2014, 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 : /** Construct and initialize a new, readonly memory map. @version 1.7.1 */
43 : LUNCHBOX_API explicit MemoryMap( const std::string& filename );
44 :
45 : /** Construct and initialize a new, read-write memory map. @version 1.9.1 */
46 : LUNCHBOX_API MemoryMap( const std::string& filename, const size_t size );
47 :
48 : /**
49 : * Destruct the memory map.
50 : *
51 : * Unmaps the file, if it is still mapped.
52 : * @sa unmap()
53 : * @version 1.0
54 : */
55 : LUNCHBOX_API ~MemoryMap();
56 :
57 : /**
58 : * Map a file to a memory address.
59 : *
60 : * The file is only mapped read-only. The file is automatically unmapped
61 : * when the memory map is deleted.
62 : *
63 : * @param filename The filename of the file to map.
64 : * @return the pointer to the mapped file, or 0 upon error.
65 : * @version 1.0
66 : */
67 : LUNCHBOX_API const void* map( const std::string& filename );
68 :
69 : /**
70 : * Remap a different file for this memory map.
71 : *
72 : * The file is only mapped read-only. An existing map is unmapped.
73 : *
74 : * @param filename The filename of the file to map.
75 : * @return the pointer to the mapped file, or 0 upon error.
76 : * @version 1.9.1
77 : */
78 : LUNCHBOX_API const void* remap( const std::string& filename );
79 :
80 : /**
81 : * Create a writable file to a memory address.
82 : *
83 : * The file is mapped read-write. An existing file will be overwritten. The
84 : * file is automatically unmapped when the memory map is deleted.
85 : *
86 : * @param filename The filename of the file to map.
87 : * @param size this size of the file.
88 : * @return the pointer to the mapped file, or 0 upon error.
89 : * @version 1.9.1
90 : */
91 : LUNCHBOX_API void* create( const std::string& filename, const size_t size );
92 :
93 : /**
94 : * Recreate a different writable file for this memory map.
95 : *
96 : * The file is only mapped read-write. An existing map is unmapped.
97 : *
98 : * @param filename The filename of the file to map.
99 : * @param size this size of the file.
100 : * @return the pointer to the mapped file, or 0 upon error.
101 : * @version 1.0
102 : */
103 : LUNCHBOX_API void* recreate( const std::string& filename,
104 : const size_t size );
105 :
106 : /** Unmap the file. @version 1.0 */
107 : LUNCHBOX_API void unmap();
108 :
109 : /** @return the pointer to the memory map. @version 1.0 */
110 : LUNCHBOX_API const void* getAddress() const;
111 :
112 : /** @return the pointer to the memory map. @version 1.9.1 */
113 : LUNCHBOX_API void* getAddress();
114 :
115 : /** @return the pointer to the memory map. @version 1.9.1 */
116 : template< class T > const T* getAddress() const
117 : { return static_cast< const T* >( getAddress( )); }
118 :
119 : /** @return the pointer to the memory map. @version 1.9.1 */
120 2 : template< class T > T* getAddress()
121 2 : { return static_cast< T* >( getAddress( )); }
122 :
123 : /** @return the size of the memory map. @version 1.0 */
124 : LUNCHBOX_API size_t getSize() const;
125 :
126 : private:
127 : detail::MemoryMap* const impl_;
128 : };// LB_DEPRECATED;
129 :
130 : inline std::ostream& operator << ( std::ostream& os, const MemoryMap& m )
131 : {
132 : return os << "MemoryMap at " << m.getAddress() << " size " << m.getSize();
133 : }
134 :
135 : }
136 :
137 : #endif //LUNCHBOX_MEMORYMAP_H
|