Mem_Map.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // $Id: Mem_Map.inl 80826 2008-03-04 14:51:23Z wotte $
00004 
00005 #include "ace/OS_NS_unistd.h"
00006 #include "ace/OS_NS_sys_mman.h"
00007 #include "ace/OS_NS_sys_stat.h"
00008 
00009 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00010 
00011 ACE_INLINE ACE_HANDLE
00012 ACE_Mem_Map::handle (void) const
00013 {
00014   ACE_TRACE ("ACE_Mem_Map::handle");
00015   return this->handle_;
00016 }
00017 
00018 // Return the name of file that is mapped (if any).
00019 
00020 ACE_INLINE const ACE_TCHAR *
00021 ACE_Mem_Map::filename (void) const
00022 {
00023   return this->filename_;
00024 }
00025 
00026 ACE_INLINE int
00027 ACE_Mem_Map::map (ACE_HANDLE handle,
00028                   size_t length,
00029                   int prot,
00030                   int share,
00031                   void *addr,
00032                   ACE_OFF_T offset,
00033                   LPSECURITY_ATTRIBUTES sa)
00034 {
00035   ACE_TRACE ("ACE_Mem_Map::map");
00036   return this->map_it (handle, length, prot, share, addr, offset, sa);
00037 }
00038 
00039 // Remap the file associated with <this->handle_>.
00040 
00041 ACE_INLINE int
00042 ACE_Mem_Map::map (size_t length,
00043                   int prot,
00044                   int share,
00045                   void *addr,
00046                   ACE_OFF_T offset,
00047                   LPSECURITY_ATTRIBUTES sa)
00048 {
00049   ACE_TRACE ("ACE_Mem_Map::map");
00050   // If we're already mapped at a particular location then try to
00051   // remap the file using the same base address.
00052   if (addr == 0 && this->base_addr_ != 0 && this->base_addr_ != MAP_FAILED)
00053     {
00054       share |= MAP_FIXED;
00055       addr = this->base_addr_;
00056     }
00057 
00058   return this->map_it (this->handle (), length, prot,
00059                        share, addr, offset, sa);
00060 }
00061 
00062 // This operator passes back the starting address of the mapped file.
00063 
00064 ACE_INLINE int
00065 ACE_Mem_Map::operator () (void *&addr)
00066 {
00067   ACE_TRACE ("ACE_Mem_Map::operator");
00068 
00069   if (this->base_addr_ == MAP_FAILED)
00070     return -1;
00071   else
00072     {
00073       addr = this->base_addr_;
00074       return 0;
00075     }
00076 }
00077 
00078 // Return the base address.
00079 
00080 ACE_INLINE void *
00081 ACE_Mem_Map::addr (void) const
00082 {
00083   ACE_TRACE ("ACE_Mem_Map::addr");
00084 
00085   return this->base_addr_;
00086 }
00087 
00088 // This function returns the number of bytes currently mapped in the
00089 // file.
00090 
00091 ACE_INLINE size_t
00092 ACE_Mem_Map::size (void) const
00093 {
00094   ACE_TRACE ("ACE_Mem_Map::size");
00095   return this->length_;
00096 }
00097 
00098 ACE_INLINE int
00099 ACE_Mem_Map::close_filemapping_handle (void)
00100 {
00101   int result = 0;
00102 
00103   if (this->file_mapping_ != this->handle_
00104       && this->file_mapping_ != ACE_INVALID_HANDLE)
00105     {
00106       result = ACE_OS::close (this->file_mapping_);
00107       this->file_mapping_ = ACE_INVALID_HANDLE;
00108     }
00109 
00110   return result;
00111 }
00112 
00113 // Unmap the region starting at <this->base_addr_>.
00114 
00115 ACE_INLINE int
00116 ACE_Mem_Map::unmap (ssize_t len)
00117 {
00118   ACE_TRACE ("ACE_Mem_Map::unmap");
00119 
00120   this->close_filemapping_handle ();
00121 
00122   if (this->base_addr_ != MAP_FAILED)
00123     {
00124       int const result = ACE_OS::munmap (this->base_addr_,
00125                                          len < 0 ? this->length_ : len);
00126       this->base_addr_ = MAP_FAILED;
00127       return result;
00128     }
00129   else
00130     return 0;
00131 }
00132 
00133 // Unmap the region starting at <addr_>.
00134 
00135 ACE_INLINE int
00136 ACE_Mem_Map::unmap (void *addr, ssize_t len)
00137 {
00138   ACE_TRACE ("ACE_Mem_Map::unmap");
00139 
00140   this->close_filemapping_handle ();
00141 
00142   return ACE_OS::munmap (addr,
00143                          len < 0 ? this->length_ : len);
00144 }
00145 
00146 // Sync <len> bytes of the memory region to the backing store starting
00147 // at <this->base_addr_>.
00148 
00149 ACE_INLINE int
00150 ACE_Mem_Map::sync (size_t len, int flags)
00151 {
00152   ACE_TRACE ("ACE_Mem_Map::sync");
00153   return ACE_OS::msync (this->base_addr_,
00154                         len,
00155                         flags);
00156 }
00157 
00158 // Sync the whole mapped region.
00159 ACE_INLINE int
00160 ACE_Mem_Map::sync (int flags)
00161 {
00162   ACE_TRACE ("ACE_Mem_Map::sync");
00163   return ACE_OS::msync (this->base_addr_,
00164                         this->length_,
00165                         flags);
00166 }
00167 
00168 // Sync <len> bytes of the memory region to the backing store starting
00169 // at <addr_>.
00170 
00171 ACE_INLINE int
00172 ACE_Mem_Map::sync (void *addr, size_t len, int flags)
00173 {
00174   ACE_TRACE ("ACE_Mem_Map::sync");
00175   return ACE_OS::msync (addr, len, flags);
00176 }
00177 
00178 // Change the protection of the pages of the mapped region to <prot>
00179 // starting at <this->base_addr_> up to <len> bytes.
00180 
00181 ACE_INLINE int
00182 ACE_Mem_Map::protect (size_t len, int prot)
00183 {
00184   ACE_TRACE ("ACE_Mem_Map::protect");
00185   return ACE_OS::mprotect (this->base_addr_, len, prot);
00186 }
00187 
00188 
00189 // Change the protection of all the pages of the mapped region to <prot>
00190 // starting at <this->base_addr_>.
00191 
00192 ACE_INLINE int
00193 ACE_Mem_Map::protect (int prot)
00194 {
00195   ACE_TRACE ("ACE_Mem_Map::protect");
00196   return ACE_OS::mprotect (this->base_addr_, this->length_, prot);
00197 }
00198 
00199 // Change the protection of the pages of the mapped region to <prot>
00200 // starting at <addr> up to <len> bytes.
00201 
00202 ACE_INLINE int
00203 ACE_Mem_Map::protect (void *addr, size_t len, int prot)
00204 {
00205   ACE_TRACE ("ACE_Mem_Map::protect");
00206   return ACE_OS::mprotect (addr, len, prot);
00207 }
00208 
00209 // Hook into the underlying VM system.
00210 
00211 ACE_INLINE int
00212 ACE_Mem_Map::advise (int behavior, int len)
00213 {
00214   ACE_TRACE ("ACE_Mem_Map::advise");
00215   const size_t advise_len =
00216     len < 0 ? this->length_ : static_cast<size_t> (len);
00217 
00218   return ACE_OS::madvise ((caddr_t) this->base_addr_,
00219                           advise_len,
00220                           behavior);
00221 }
00222 
00223 ACE_INLINE int
00224 ACE_Mem_Map::close_handle (void)
00225 {
00226   int result = 0;
00227 
00228   if (this->close_handle_)
00229     {
00230       this->close_handle_ = false;
00231       result = ACE_OS::close (this->handle_);
00232       this->handle_ = ACE_INVALID_HANDLE;
00233     }
00234 
00235   return result;
00236 }
00237 
00238 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:40 2010 for ACE by  doxygen 1.4.7