Mem_Map.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Mem_Map.inl,v 4.8 2006/06/09 07:51:09 jwillemsen Exp
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                   ssize_t length,
00029                   int prot,
00030                   int share,
00031                   void *addr,
00032                   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 (ssize_t length,
00043                   int prot,
00044                   int share,
00045                   void *addr,
00046                   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       // On LynxOS, this will result in unlinking of the (hidden)
00107       // shared memory file if there are no more references to it.
00108       result = ACE_OS::close (this->file_mapping_);
00109       this->file_mapping_ = ACE_INVALID_HANDLE;
00110     }
00111 
00112   return result;
00113 }
00114 
00115 // Unmap the region starting at <this->base_addr_>.
00116 
00117 ACE_INLINE int
00118 ACE_Mem_Map::unmap (ssize_t len)
00119 {
00120   ACE_TRACE ("ACE_Mem_Map::unmap");
00121 
00122   this->close_filemapping_handle ();
00123 
00124 #if defined (ACE_HAS_LYNXOS_BROKEN_MMAP)
00125   int writeback_result = 0;
00126   if (write_enabled_)
00127     {
00128       // Write back the contents of the shared memory object to the
00129       // file.
00130       off_t const filesize = ACE_OS::filesize (handle_);
00131       writeback_result =
00132         ACE_OS::lseek (handle_, 0, 0) != -1
00133         && ACE_OS::write (handle_,
00134                           base_addr_,
00135                           (int) filesize) == filesize ? 0 : -1;
00136     }
00137 #endif /* ACE_HAS_LYNXOS_BROKEN_MMAP */
00138   if (this->base_addr_ != MAP_FAILED)
00139     {
00140       int const result = ACE_OS::munmap (this->base_addr_,
00141                                          len < 0 ? this->length_ : len);
00142       this->base_addr_ = MAP_FAILED;
00143       return result;
00144     }
00145   else
00146 #if defined (ACE_HAS_LYNXOS_BROKEN_MMAP)
00147     return writeback_result;
00148 #else  /* ! ACE_HAS_LYNXOS_BROKEN_MMAP */
00149     return 0;
00150 #endif /* ! ACE_HAS_LYNXOS_BROKEN_MMAP */
00151 }
00152 
00153 // Unmap the region starting at <addr_>.
00154 
00155 ACE_INLINE int
00156 ACE_Mem_Map::unmap (void *addr, ssize_t len)
00157 {
00158   ACE_TRACE ("ACE_Mem_Map::unmap");
00159 
00160   this->close_filemapping_handle ();
00161 
00162 #if defined (ACE_HAS_LYNXOS_BROKEN_MMAP)
00163   int writeback_result = 0;
00164   if (write_enabled_)
00165     {
00166       // Write back the contents of the shared memory object to the file.
00167       off_t const filesize = ACE_OS::filesize (handle_);
00168       writeback_result =
00169         ACE_OS::lseek (handle_, 0, 0) != -1
00170         && ACE_OS::write (handle_,
00171                           base_addr_,
00172                           filesize) == filesize ? 0 : -1;
00173     }
00174 #endif /* ACE_HAS_LYNXOS_BROKEN_MMAP */
00175 
00176 #if defined (ACE_HAS_LYNXOS_BROKEN_MMAP)
00177   return ACE_OS::munmap (addr,
00178                          len < 0 ? this->length_ : len)
00179     | writeback_result;
00180 #else  /* ! ACE_HAS_LYNXOS_BROKEN_MMAP */
00181   return ACE_OS::munmap (addr,
00182                          len < 0 ? this->length_ : len);
00183 #endif /* ! ACE_HAS_LYNXOS_BROKEN_MMAP */
00184 }
00185 
00186 // Sync <len> bytes of the memory region to the backing store starting
00187 // at <this->base_addr_>.  If <len> == -1 then sync the whole mapped
00188 // region.
00189 
00190 ACE_INLINE int
00191 ACE_Mem_Map::sync (ssize_t len, int flags)
00192 {
00193   ACE_TRACE ("ACE_Mem_Map::sync");
00194   return ACE_OS::msync (this->base_addr_,
00195                         len < 0 ? this->length_ : len,
00196                         flags);
00197 }
00198 
00199 // Sync <len> bytes of the memory region to the backing store starting
00200 // at <addr_>.
00201 
00202 ACE_INLINE int
00203 ACE_Mem_Map::sync (void *addr, size_t len, int flags)
00204 {
00205   ACE_TRACE ("ACE_Mem_Map::sync");
00206   return ACE_OS::msync (addr, len, flags);
00207 }
00208 
00209 // Change the protection of the pages of the mapped region to <prot>
00210 // starting at <this->base_addr_> up to <len> bytes.  If <len> == -1
00211 // then change protection of all pages in the mapped region.
00212 
00213 ACE_INLINE int
00214 ACE_Mem_Map::protect (ssize_t len, int prot)
00215 {
00216   ACE_TRACE ("ACE_Mem_Map::protect");
00217   if (len < 0)
00218     len = this->length_;
00219   return ACE_OS::mprotect (this->base_addr_, len, prot);
00220 }
00221 
00222 // Change the protection of the pages of the mapped region to <prot>
00223 // starting at <addr> up to <len> bytes.
00224 
00225 ACE_INLINE int
00226 ACE_Mem_Map::protect (void *addr, size_t len, int prot)
00227 {
00228   ACE_TRACE ("ACE_Mem_Map::protect");
00229   return ACE_OS::mprotect (addr, len, prot);
00230 }
00231 
00232 // Hook into the underlying VM system.
00233 
00234 ACE_INLINE int
00235 ACE_Mem_Map::advise (int behavior, int len)
00236 {
00237   ACE_TRACE ("ACE_Mem_Map::advise");
00238   const size_t advise_len =
00239     len < 0 ? this->length_ : static_cast<size_t> (len);
00240 
00241   return ACE_OS::madvise ((caddr_t) this->base_addr_,
00242                           advise_len,
00243                           behavior);
00244 }
00245 
00246 ACE_INLINE int
00247 ACE_Mem_Map::close_handle (void)
00248 {
00249   int result = 0;
00250 
00251   if (this->close_handle_)
00252     {
00253       this->close_handle_ = false;
00254       result = ACE_OS::close (this->handle_);
00255       this->handle_ = ACE_INVALID_HANDLE;
00256     }
00257 
00258   return result;
00259 }
00260 
00261 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:55 2006 for ACE by doxygen 1.3.6