Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Mem_Map.i

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

Generated on Mon Jun 16 11:20:18 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002