Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / MMAP_Allocator.cpp
blob04453f7cd8d5aa46ed6c13dacc71f62bdd45553c
1 #include "tao/MMAP_Allocator.h"
3 #if TAO_HAS_SENDFILE == 1
5 #include "ace/Mem_Map.h"
6 #include "ace/Default_Constants.h"
9 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
11 namespace
13 // Default size mmap()ed memory pool will be the sum of the control
14 // block size and the default CDR buffer chosen by the user. The
15 // final size may be rounded up by the allocator to be aligned on
16 // the appropriate boundary/page.
18 // @@ This type really should be ACE_LOFF_T but ACE's mmap()-based
19 // classes currently do not handle large file offsets.
20 // -Ossama
21 off_t const the_default_buf_size =
22 sizeof (ACE_Control_Block) + ACE_DEFAULT_CDR_BUFSIZE;
24 ACE_MMAP_Memory_Pool_Options const the_pool_options (
25 ACE_DEFAULT_BASE_ADDR,
26 ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED,
27 0, // No need to sync
28 the_default_buf_size,
29 MAP_SHARED, // Written data must be reflected in the backing store
30 // file in order for sendfile() to be able to read it.
33 /* 0 */ ACE_DEFAULT_FILE_PERMS,
34 true); // Generate for each mmap an unqiue pool
38 // Ideally we should open the backing store with shm_open() so that we
39 // can avoid creating an on-disk backing store. An on-disk backing
40 // store could potentially cause sendfile() to block on disk I/O,
41 // which is undesirable. Unfortunately, ACE_Mem_Map currently
42 // provides no way to configure which "open" function to use,
43 // i.e. open(2) or shm_open(3). Alternatively we could shm_open() the
44 // backing store file beforehand. Unfortunately,
45 // ACE_{Lite_}MMAP_Memory_Pool currently doesn't provide a means to
46 // pass the file descriptor for the desired backing store to the
47 // underlying ACE_Mem_Map object.
49 // It may be tempting to mmap() /dev/zero. That would certainly
50 // provide the desired transient "scratch space" memory that we can
51 // write and read to and from, respectively. Unfortunately, the data
52 // in /dev/zero-based memory mapped buffer can only be read through
53 // the buffer itself, not through the file descriptor (e.g. using
54 // read(2)) for the open()ed /dev/zero device. Reading through the
55 // /dev/zero file descriptor simply returns zero, as /dev/zero was
56 // designed to do. So unfortunate. :)
57 TAO_MMAP_Allocator::TAO_MMAP_Allocator ()
58 : TAO_MMAP_Allocator_Base ((char const *) nullptr /* pool name */,
59 nullptr, // No need to explicitly name the lock.
60 &the_pool_options)
64 TAO_MMAP_Allocator::~TAO_MMAP_Allocator ()
68 // @@ Should be const but underlying allocator methods are not!
69 ACE_HANDLE
70 TAO_MMAP_Allocator::handle ()
72 return this->alloc ().memory_pool ().mmap ().handle ();
75 // @@ Should be const but underlying allocator methods are not!
76 off_t
77 TAO_MMAP_Allocator::offset (void * p)
79 ACE_Mem_Map const & m = this->alloc ().memory_pool ().mmap ();
81 ptrdiff_t const off = reinterpret_cast<ptrdiff_t> (p);
82 ptrdiff_t const base = reinterpret_cast<ptrdiff_t> (m.addr ());
83 ptrdiff_t const end = base + m.size ();
85 // Check if p is in the range of the mmap pool, if not we return -1
86 if (off < base || off > end)
87 return -1;
89 off_t const the_offset = static_cast<off_t> (off - base);
91 return (the_offset < 0 ? static_cast<off_t> (-1) : the_offset);
94 TAO_END_VERSIONED_NAMESPACE_DECL
96 #endif /* TAO_HAS_SENDFILE==1 */