00001 // MMAP_Allocator.cpp,v 1.2 2006/06/19 19:47:33 jwillemsen Exp 00002 00003 #include "tao/MMAP_Allocator.h" 00004 00005 00006 #ifdef ACE_HAS_SENDFILE 00007 00008 #include "ace/Mem_Map.h" 00009 #include "ace/Default_Constants.h" 00010 00011 00012 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00013 00014 namespace 00015 { 00016 // Default size mmap()ed memory pool will be the sum of the control 00017 // block size and the default CDR buffer chosen by the user. The 00018 // final size may be rounded up by the allocator to be aligned on 00019 // the appropriate boundary/page. 00020 // 00021 // @@ This type really should be ACE_LOFF_T but ACE's mmap()-based 00022 // classes currently do not handle large file offsets. 00023 // -Ossama 00024 off_t const the_default_buf_size = 00025 sizeof (ACE_Control_Block) + ACE_DEFAULT_CDR_BUFSIZE; 00026 00027 ACE_MMAP_Memory_Pool_Options const the_pool_options ( 00028 ACE_DEFAULT_BASE_ADDR, 00029 ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED, 00030 0, // No need to sync 00031 the_default_buf_size, 00032 MAP_SHARED, // Written data must be reflected in the backing store 00033 // file in order for sendfile() to be able to read it. 00034 1, 00035 0, 00036 /* 0 */ ACE_DEFAULT_FILE_PERMS, 00037 true); // Generate for each mmap an unqiue pool 00038 } 00039 00040 00041 // Ideally we should open the backing store with shm_open() so that we 00042 // can avoid creating an on-disk backing store. An on-disk backing 00043 // store could potentially cause sendfile() to block on disk I/O, 00044 // which is undesirable. Unfortunately, ACE_Mem_Map currently 00045 // provides no way to configure which "open" function to use, 00046 // i.e. open(2) or shm_open(3). Alternatively we could shm_open() the 00047 // backing store file beforehand. Unfortunately, 00048 // ACE_{Lite_}MMAP_Memory_Pool currently doesn't provide a means to 00049 // pass the file descriptor for the desired backing store to the 00050 // underlying ACE_Mem_Map object. 00051 // 00052 // It may be tempting to mmap() /dev/zero. That would certainly 00053 // provide the desired transient "scratch space" memory that we can 00054 // write and read to and from, respectively. Unfortunately, the data 00055 // in /dev/zero-based memory mapped buffer can only be read through 00056 // the buffer itself, not through the file descriptor (e.g. using 00057 // read(2)) for the open()ed /dev/zero device. Reading through the 00058 // /dev/zero file descriptor simply returns zero, as /dev/zero was 00059 // designed to do. So unfortunate. :) 00060 TAO_MMAP_Allocator::TAO_MMAP_Allocator (void) 00061 : TAO_MMAP_Allocator_Base ((char const *) 0 /* pool name */, 00062 0, // No need to explicitly name the lock. 00063 &the_pool_options) 00064 { 00065 } 00066 00067 TAO_MMAP_Allocator::~TAO_MMAP_Allocator (void) 00068 { 00069 } 00070 00071 // @@ Should be const but underlying allocator methods are not! 00072 ACE_HANDLE 00073 TAO_MMAP_Allocator::handle (void) 00074 { 00075 return this->alloc ().memory_pool ().mmap ().handle (); 00076 } 00077 00078 // @@ Should be const but underlying allocator methods are not! 00079 off_t 00080 TAO_MMAP_Allocator::offset (void * p) 00081 { 00082 ACE_Mem_Map const & m = this->alloc ().memory_pool ().mmap (); 00083 00084 ptrdiff_t const off = reinterpret_cast<ptrdiff_t> (p); 00085 ptrdiff_t const base = reinterpret_cast<ptrdiff_t> (m.addr ()); 00086 ptrdiff_t const end = base + m.size (); 00087 00088 // Check if p is in the range of the mmap pool, if not we return -1 00089 if (off < base || off > end) 00090 return -1; 00091 00092 off_t const the_offset = static_cast<off_t> (off - base); 00093 00094 return (the_offset < 0 ? static_cast<off_t> (-1) : the_offset); 00095 } 00096 00097 TAO_END_VERSIONED_NAMESPACE_DECL 00098 00099 #endif /* ACE_HAS_SENDFILE */