2 #include "ace/OS_NS_unistd.h"
3 #include "ace/OS_NS_sys_mman.h"
4 #include "ace/OS_NS_sys_stat.h"
6 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
9 ACE_Mem_Map::handle () const
11 ACE_TRACE ("ACE_Mem_Map::handle");
15 // Return the name of file that is mapped (if any).
17 ACE_INLINE const ACE_TCHAR *
18 ACE_Mem_Map::filename () const
20 return this->filename_;
24 ACE_Mem_Map::map (ACE_HANDLE handle,
30 LPSECURITY_ATTRIBUTES sa)
32 ACE_TRACE ("ACE_Mem_Map::map");
33 return this->map_it (handle, length, prot, share, addr, offset, sa);
36 // Remap the file associated with <this->handle_>.
39 ACE_Mem_Map::map (size_t length,
44 LPSECURITY_ATTRIBUTES sa)
46 ACE_TRACE ("ACE_Mem_Map::map");
47 // If we're already mapped at a particular location then try to
48 // remap the file using the same base address.
49 if (addr == 0 && this->base_addr_ != 0 && this->base_addr_ != MAP_FAILED)
52 addr = this->base_addr_;
55 return this->map_it (this->handle (), length, prot,
56 share, addr, offset, sa);
59 // This operator passes back the starting address of the mapped file.
62 ACE_Mem_Map::operator () (void *&addr)
64 ACE_TRACE ("ACE_Mem_Map::operator");
66 if (this->base_addr_ == MAP_FAILED)
70 addr = this->base_addr_;
75 // Return the base address.
78 ACE_Mem_Map::addr () const
80 ACE_TRACE ("ACE_Mem_Map::addr");
82 return this->base_addr_;
85 // This function returns the number of bytes currently mapped in the
89 ACE_Mem_Map::size () const
91 ACE_TRACE ("ACE_Mem_Map::size");
96 ACE_Mem_Map::close_filemapping_handle ()
100 if (this->file_mapping_ != this->handle_
101 && this->file_mapping_ != ACE_INVALID_HANDLE)
103 result = ACE_OS::close (this->file_mapping_);
104 this->file_mapping_ = ACE_INVALID_HANDLE;
110 // Unmap the region starting at <this->base_addr_>.
113 ACE_Mem_Map::unmap (ssize_t len)
115 ACE_TRACE ("ACE_Mem_Map::unmap");
117 this->close_filemapping_handle ();
119 if (this->base_addr_ != MAP_FAILED)
121 int const result = ACE_OS::munmap (this->base_addr_,
122 len < 0 ? this->length_ : (size_t)len);
123 this->base_addr_ = MAP_FAILED;
130 // Unmap the region starting at <addr_>.
133 ACE_Mem_Map::unmap (void *addr, ssize_t len)
135 ACE_TRACE ("ACE_Mem_Map::unmap");
137 this->close_filemapping_handle ();
139 return ACE_OS::munmap (addr,
140 len < 0 ? this->length_ : (size_t)len);
143 // Sync <len> bytes of the memory region to the backing store starting
144 // at <this->base_addr_>.
147 ACE_Mem_Map::sync (size_t len, int flags)
149 ACE_TRACE ("ACE_Mem_Map::sync");
150 return ACE_OS::msync (this->base_addr_,
155 // Sync the whole mapped region.
157 ACE_Mem_Map::sync (int flags)
159 ACE_TRACE ("ACE_Mem_Map::sync");
160 return ACE_OS::msync (this->base_addr_,
165 // Sync <len> bytes of the memory region to the backing store starting
169 ACE_Mem_Map::sync (void *addr, size_t len, int flags)
171 ACE_TRACE ("ACE_Mem_Map::sync");
172 return ACE_OS::msync (addr, len, flags);
175 // Change the protection of the pages of the mapped region to <prot>
176 // starting at <this->base_addr_> up to <len> bytes.
179 ACE_Mem_Map::protect (size_t len, int prot)
181 ACE_TRACE ("ACE_Mem_Map::protect");
182 return ACE_OS::mprotect (this->base_addr_, len, prot);
186 // Change the protection of all the pages of the mapped region to <prot>
187 // starting at <this->base_addr_>.
190 ACE_Mem_Map::protect (int prot)
192 ACE_TRACE ("ACE_Mem_Map::protect");
193 return ACE_OS::mprotect (this->base_addr_, this->length_, prot);
196 // Change the protection of the pages of the mapped region to <prot>
197 // starting at <addr> up to <len> bytes.
200 ACE_Mem_Map::protect (void *addr, size_t len, int prot)
202 ACE_TRACE ("ACE_Mem_Map::protect");
203 return ACE_OS::mprotect (addr, len, prot);
206 // Hook into the underlying VM system.
209 ACE_Mem_Map::advise (int behavior, int len)
211 ACE_TRACE ("ACE_Mem_Map::advise");
212 const size_t advise_len =
213 len < 0 ? this->length_ : static_cast<size_t> (len);
215 return ACE_OS::madvise ((caddr_t) this->base_addr_,
221 ACE_Mem_Map::close_handle ()
225 if (this->close_handle_)
227 this->close_handle_ = false;
228 result = ACE_OS::close (this->handle_);
229 this->handle_ = ACE_INVALID_HANDLE;
235 ACE_END_VERSIONED_NAMESPACE_DECL