Make a memory pool that is based on mmap(2)
. This implementation allows memory to be shared between processes.
More...
#include <MMAP_Memory_Pool.h>
Public Types | |
typedef ACE_MMAP_Memory_Pool_Options | OPTIONS |
Public Member Functions | |
ACE_MMAP_Memory_Pool (const ACE_TCHAR *backing_store_name=0, const OPTIONS *options=0) | |
Initialize the pool. | |
virtual | ~ACE_MMAP_Memory_Pool (void) |
Destructor. | |
virtual void * | init_acquire (size_t nbytes, size_t &rounded_bytes, int &first_time) |
Ask system for initial chunk of shared memory. | |
virtual void * | acquire (size_t nbytes, size_t &rounded_bytes) |
virtual int | release (int destroy=1) |
Instruct the memory pool to release all of its resources. | |
virtual int | sync (size_t len, int flags=MS_SYNC) |
virtual int | sync (int flags=MS_SYNC) |
virtual int | sync (void *addr, size_t len, int flags=MS_SYNC) |
Sync the memory region to the backing store starting at addr. | |
virtual int | protect (size_t len, int prot=PROT_RDWR) |
virtual int | protect (int prot=PROT_RDWR) |
virtual int | protect (void *addr, size_t len, int prot=PROT_RDWR) |
virtual int | remap (void *addr) |
virtual void * | base_addr (void) const |
Return the base address of this memory pool. | |
virtual void | dump (void) const |
Dump the state of an object. | |
ACE_Mem_Map const & | mmap (void) const |
Get reference to underlying ACE_Mem_Map object. | |
ACE_Mem_Map & | mmap (void) |
Get reference to underlying ACE_Mem_Map object. | |
Public Attributes | |
ACE_ALLOC_HOOK_DECLARE | |
Declare the dynamic allocation hooks. | |
Protected Member Functions | |
virtual size_t | round_up (size_t nbytes) |
virtual int | commit_backing_store_name (size_t rounded_bytes, size_t &map_size) |
virtual int | map_file (size_t map_size) |
Memory map the file up to map_size bytes. | |
virtual int | handle_signal (int signum, siginfo_t *, ucontext_t *) |
Protected Attributes | |
ACE_Sig_Handler | signal_handler_ |
Handles SIGSEGV. | |
ACE_Mem_Map | mmap_ |
Memory-mapping object. | |
void * | base_addr_ |
int | use_fixed_addr_ |
Must we use the base_addr_ or can we let mmap(2) select it? | |
int | flags_ |
Flags passed into ACE_OS::mmap(). | |
bool | write_each_page_ |
size_t | minimum_bytes_ |
What the minimum bytes of the initial segment should be. | |
ACE_TCHAR | backing_store_name_ [MAXPATHLEN+1] |
Name of the backing store where the shared memory pool is kept. | |
bool | guess_on_fault_ |
LPSECURITY_ATTRIBUTES | sa_ |
Security attributes object, only used on NT. | |
mode_t | file_mode_ |
Protection mode for mmaped file. | |
bool | install_signal_handler_ |
Should we install a signal handler. |
Make a memory pool that is based on mmap(2)
. This implementation allows memory to be shared between processes.
Definition at line 136 of file MMAP_Memory_Pool.h.
Definition at line 139 of file MMAP_Memory_Pool.h.
ACE_MMAP_Memory_Pool::ACE_MMAP_Memory_Pool | ( | const ACE_TCHAR * | backing_store_name = 0 , |
|
const OPTIONS * | options = 0 | |||
) |
Initialize the pool.
Definition at line 115 of file MMAP_Memory_Pool.cpp.
: base_addr_ (0), use_fixed_addr_(0), flags_ (MAP_SHARED), write_each_page_ (false), minimum_bytes_ (0), sa_ (0), file_mode_ (ACE_DEFAULT_FILE_PERMS), install_signal_handler_ (true) { ACE_TRACE ("ACE_MMAP_Memory_Pool::ACE_MMAP_Memory_Pool"); #if (defined (ACE_HAS_SIGINFO_T) && !defined (ACE_LACKS_SI_ADDR)) || defined (ACE_WIN32) // For plaforms that give the faulting address. guess_on_fault_ = false; #else // For plaforms that do NOT give the faulting address, let the // options decide whether to guess or not. if (options) guess_on_fault_ = options->guess_on_fault_; else // If no options are specified, default to true. guess_on_fault_ = true; #endif /* (defined (ACE_HAS_SIGINFO_T) && !defined (ACE_LACKS_SI_ADDR)) || defined (ACE_WIN32) */ // Only change the defaults if <options> != 0. if (options) { if (options->flags_ != 0) this->flags_ = options->flags_; use_fixed_addr_ = options->use_fixed_addr_; if (use_fixed_addr_ == ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED) { this->base_addr_ = const_cast<void *> (options->base_addr_); ACE_SET_BITS (flags_, MAP_FIXED); } this->write_each_page_ = options->write_each_page_; this->minimum_bytes_ = options->minimum_bytes_; if (options->sa_ != 0) this->sa_ = options->sa_; this->file_mode_ = options->file_mode_; this->install_signal_handler_ = options->install_signal_handler_; } if (backing_store_name == 0) { // Only create a new unique filename for the backing store file // if the user didn't supply one... #if defined (ACE_DEFAULT_BACKING_STORE) // Create a temporary file. ACE_OS::strcpy (this->backing_store_name_, ACE_DEFAULT_BACKING_STORE); #else /* ACE_DEFAULT_BACKING_STORE */ if (ACE::get_temp_dir (this->backing_store_name_, MAXPATHLEN - 17) == -1) // -17 for ace-malloc-XXXXXX { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Temporary path too long, ") ACE_TEXT ("defaulting to current directory\n"))); this->backing_store_name_[0] = 0; } // Add the filename to the end ACE_OS::strcat (this->backing_store_name_, ACE_TEXT ("ace-malloc-XXXXXX")); // If requested an unique filename, use mktemp to get a random file. if (options && options->unique_) ACE_OS::mktemp(this->backing_store_name_); #endif /* ACE_DEFAULT_BACKING_STORE */ } else ACE_OS::strsncpy (this->backing_store_name_, backing_store_name, (sizeof this->backing_store_name_ / sizeof (ACE_TCHAR))); #if !defined (ACE_WIN32) if (this->install_signal_handler_) { if (this->signal_handler_.register_handler (SIGSEGV, this) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT("%p\n"), this->backing_store_name_)); } #endif /* ACE_WIN32 */ }
ACE_MMAP_Memory_Pool::~ACE_MMAP_Memory_Pool | ( | void | ) | [virtual] |
void * ACE_MMAP_Memory_Pool::acquire | ( | size_t | nbytes, | |
size_t & | rounded_bytes | |||
) | [virtual] |
Acquire at least nbytes from the memory pool. rounded_bytes is the actual number of bytes allocated. Also acquires an internal semaphore that ensures proper serialization of ACE_MMAP_Memory_Pool initialization across processes.
Definition at line 331 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::acquire"); rounded_bytes = this->round_up (nbytes); // ACE_DEBUG ((LM_DEBUG, "(%P|%t) acquiring more chunks, nbytes = // %B, rounded_bytes = %B\n", nbytes, rounded_bytes)); size_t map_size; if (this->commit_backing_store_name (rounded_bytes, map_size) == -1) return 0; else if (this->map_file (map_size) == -1) return 0; // ACE_DEBUG ((LM_DEBUG, "(%P|%t) acquired more chunks, nbytes = %B, // rounded_bytes = %B, map_size = %B\n", nbytes, rounded_bytes, // map_size)); return (void *) ((char *) this->mmap_.addr () + (this->mmap_.size () - rounded_bytes)); }
void * ACE_MMAP_Memory_Pool::base_addr | ( | void | ) | const [virtual] |
Return the base address of this memory pool.
Definition at line 546 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::base_addr"); return this->base_addr_; }
int ACE_MMAP_Memory_Pool::commit_backing_store_name | ( | size_t | rounded_bytes, | |
size_t & | map_size | |||
) | [protected, virtual] |
Compute the new map_size of the backing store and commit the memory.
Definition at line 212 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::commit_backing_store_name"); #if defined (__Lynx__) map_size = rounded_bytes; #else size_t seek_len; if (this->write_each_page_) // Write to the end of every block to ensure that we have enough // space in the backing store. seek_len = this->round_up (1); // round_up(1) is one page. else // We're willing to risk it all in the name of efficiency... seek_len = rounded_bytes; // The following loop will execute multiple times (if // this->write_each_page == 1) or just once (if // this->write_each_page == 0). for (size_t cur_block = 0; cur_block < rounded_bytes; cur_block += seek_len) { map_size = ACE_Utils::truncate_cast<size_t> ( ACE_OS::lseek (this->mmap_.handle (), static_cast<ACE_OFF_T> (seek_len - 1), SEEK_END)); if (map_size == static_cast<size_t> (-1) || ACE_OS::write (this->mmap_.handle (), "", 1) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), this->backing_store_name_), -1); } #if defined (ACE_OPENVMS) ::fsync(this->mmap_.handle()); #endif // Increment by one to put us at the beginning of the next chunk... ++map_size; #endif /* __Lynx__ */ return 0; }
virtual void ACE_MMAP_Memory_Pool::dump | ( | void | ) | const [virtual] |
Dump the state of an object.
int ACE_MMAP_Memory_Pool::handle_signal | ( | int | signum, | |
siginfo_t * | siginfo, | |||
ucontext_t * | ||||
) | [protected, virtual] |
Handle SIGSEGV and SIGBUS signals to remap memory properly. When a process reads or writes to non-mapped memory a signal (SIGBUS or SIGSEGV) will be triggered. At that point, the ACE_Sig_Handler (which is part of the ACE_Reactor) will catch the signal and dispatch the handle_signal() method defined here. If the SIGSEGV signal occurred due to the fact that the mapping wasn't uptodate with respect to the backing store, the handler method below will update the mapping accordingly. When the signal handler returns, the instruction should be restarted and the operation should work.
Reimplemented from ACE_Event_Handler.
Definition at line 489 of file MMAP_Memory_Pool.cpp.
{ if (signum != SIGSEGV) return -1; #if 0 else ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) received %S\n"), signum)); #endif // ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) new mapping address = %@\n"), (char *) this->base_addr_ + current_map_size)); #if defined (ACE_HAS_SIGINFO_T) && !defined (ACE_LACKS_SI_ADDR) // Make sure that the pointer causing the problem is within the // range of the backing store. if (siginfo != 0) { // ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) si_signo = %d, si_code = %d, addr = %@\n"), siginfo->si_signo, siginfo->si_code, siginfo->si_addr)); if (this->remap ((void *) siginfo->si_addr) == -1) return -1; // ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) address %@ out of range\n", // siginfo->si_addr), -1); return 0; } #else ACE_UNUSED_ARG(siginfo); #endif /* ACE_HAS_SIGINFO_T && !defined ACE_LACKS_SI_ADDR */ // If guess_on_fault_ is true, then we want to try to remap without // knowing the faulting address. guess_on_fault_ can only be true // on platforms that do not provide the faulting address through // signals or exceptions. We check to see if the mapping is up to // date. If it is, then this fault isn't due to this mapping and we // pass it on. if (guess_on_fault_) { // Check if the current mapping is up to date. size_t const current_map_size = ACE_Utils::truncate_cast<size_t> (ACE_OS::filesize (this->mmap_.handle ())); if (static_cast<size_t> (current_map_size) == this->mmap_.size ()) { // The mapping is up to date so this really is a bad // address. Thus, remove current signal handler so process // will fail with default action and core file will be // written. this->signal_handler_.remove_handler (SIGSEGV); return 0; } // Extend the mapping to cover the size of the backing store. return this->map_file (current_map_size); } else return -1; }
void * ACE_MMAP_Memory_Pool::init_acquire | ( | size_t | nbytes, | |
size_t & | rounded_bytes, | |||
int & | first_time | |||
) | [virtual] |
Ask system for initial chunk of shared memory.
Definition at line 358 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::init_acquire"); first_time = 0; if (nbytes < static_cast <size_t> (this->minimum_bytes_)) nbytes = static_cast <size_t> (this->minimum_bytes_); if (this->mmap_.open (this->backing_store_name_, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, this->file_mode_, this->sa_) != -1) { // First time in, so need to acquire memory. first_time = 1; void *result = this->acquire (nbytes, rounded_bytes); // After the first time, reset the flag so that subsequent calls // will use MAP_FIXED if (use_fixed_addr_ == ACE_MMAP_Memory_Pool_Options::FIRSTCALL_FIXED) { ACE_SET_BITS (flags_, MAP_FIXED); } return result; } else if (errno == EEXIST) { errno = 0; // Reopen file *without* using O_EXCL... if (this->mmap_.map (this->backing_store_name_, static_cast<size_t> (-1), O_RDWR, this->file_mode_, PROT_RDWR, this->flags_, this->base_addr_, 0, this->sa_) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("MMAP_Memory_Pool::init_acquire, EEXIST")), 0); // After the first time, reset the flag so that subsequent calls // will use MAP_FIXED if (use_fixed_addr_ == ACE_MMAP_Memory_Pool_Options::FIRSTCALL_FIXED) { ACE_SET_BITS (flags_, MAP_FIXED); } #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) // Update the mapped segment information ACE_BASED_POINTER_REPOSITORY::instance ()->bind (this->mmap_.addr(), this->mmap_.size()); #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ return this->mmap_.addr (); } else ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("MMAP_Memory_Pool::init_acquire")), 0); }
int ACE_MMAP_Memory_Pool::map_file | ( | size_t | map_size | ) | [protected, virtual] |
Memory map the file up to map_size bytes.
Definition at line 267 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::map_file"); #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) void* obase_addr = this->base_addr_; #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ // Unmap the existing mapping. this->mmap_.unmap (); #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) if (use_fixed_addr_ == ACE_MMAP_Memory_Pool_Options::NEVER_FIXED) this->base_addr_ = 0; #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ // Remap the file; try to stay at the same location as a previous mapping // but do not force it with MAP_FIXED. Doing so will give the OS permission // to map locations currently holding other things (such as the heap, or // the C library) into the map file, producing very unexpected results. if (this->mmap_.map (map_size, PROT_RDWR, this->flags_, this->base_addr_, 0, this->sa_) == -1 || (this->base_addr_ != 0 #ifdef ACE_HAS_WINCE && this->mmap_.addr () == 0)) // WinCE does not allow users to specify alloc addr. #else && this->mmap_.addr () != this->base_addr_)) #endif // ACE_HAS_WINCE { #if 0 ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) addr = %@, base_addr = %@, map_size = %B, %p\n"), this->mmap_.addr (), this->base_addr_, map_size, this->backing_store_name_)); #endif /* 0 */ return -1; } else { #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) this->base_addr_ = this->mmap_.addr (); if (obase_addr && this->base_addr_ != obase_addr) { ACE_BASED_POINTER_REPOSITORY::instance ()->unbind (obase_addr); } ACE_BASED_POINTER_REPOSITORY::instance ()->bind (this->base_addr_, map_size); #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ return 0; } }
ACE_Mem_Map const & ACE_MMAP_Memory_Pool::mmap | ( | void | ) | const [inline] |
Get reference to underlying ACE_Mem_Map object.
Definition at line 9 of file MMAP_Memory_Pool.inl.
{ return mmap_; }
ACE_Mem_Map & ACE_MMAP_Memory_Pool::mmap | ( | void | ) | [inline] |
Get reference to underlying ACE_Mem_Map object.
Definition at line 16 of file MMAP_Memory_Pool.inl.
{ return mmap_; }
int ACE_MMAP_Memory_Pool::protect | ( | int | prot = PROT_RDWR |
) | [virtual] |
Change the protection of all the pages of the mapped region to prot starting at this->base_addr_
.
Definition at line 94 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::protect"); size_t const len = ACE_Utils::truncate_cast<size_t> ( ACE_OS::lseek (this->mmap_.handle (), 0, SEEK_END)); return this->mmap_.protect (len, prot); }
int ACE_MMAP_Memory_Pool::protect | ( | void * | addr, | |
size_t | len, | |||
int | prot = PROT_RDWR | |||
) | [virtual] |
Change the protection of the pages of the mapped region to prot starting at addr up to len bytes.
Definition at line 109 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::protect"); return ACE_OS::mprotect (addr, len, prot); }
int ACE_MMAP_Memory_Pool::protect | ( | size_t | len, | |
int | prot = PROT_RDWR | |||
) | [virtual] |
Change the protection of the pages of the mapped region to prot starting at this->base_addr_
up to len bytes. If len == -1 then change protection of all pages in the mapped region.
Definition at line 86 of file MMAP_Memory_Pool.cpp.
int ACE_MMAP_Memory_Pool::release | ( | int | destroy = 1 |
) | [virtual] |
Instruct the memory pool to release all of its resources.
Definition at line 38 of file MMAP_Memory_Pool.cpp.
int ACE_MMAP_Memory_Pool::remap | ( | void * | addr | ) | [virtual] |
Try to extend the virtual address space so that addr is now covered by the address mapping. The method succeeds and returns 0 if the backing store has adequate memory to cover this address. Otherwise, it returns -1. This method is typically called by a UNIX signal handler for SIGSEGV or a Win32 structured exception when another process has grown the backing store (and its mapping) and our process now incurs a fault because our mapping isn't in range (yet).
Definition at line 443 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::remap"); // ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Remapping with fault address at: %@\n"), addr)); size_t const current_map_size = ACE_Utils::truncate_cast<size_t> (ACE_OS::filesize (this->mmap_.handle ())); // ACE_OS::lseek (this->mmap_.handle (), 0, SEEK_END); if (!(addr < (void *) ((char *) this->mmap_.addr () + current_map_size) && addr >= this->mmap_.addr ())) return -1; // Extend the mapping to cover the size of the backing store. return this->map_file (current_map_size); }
size_t ACE_MMAP_Memory_Pool::round_up | ( | size_t | nbytes | ) | [protected, virtual] |
Implement the algorithm for rounding up the request to an appropriate chunksize.
Definition at line 553 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::round_up"); return ACE::round_to_pagesize (nbytes); }
int ACE_MMAP_Memory_Pool::sync | ( | size_t | len, | |
int | flags = MS_SYNC | |||
) | [virtual] |
Sync the memory region to the backing store starting at this->base_addr_
.
Reimplemented in ACE_Lite_MMAP_Memory_Pool.
Definition at line 54 of file MMAP_Memory_Pool.cpp.
int ACE_MMAP_Memory_Pool::sync | ( | void * | addr, | |
size_t | len, | |||
int | flags = MS_SYNC | |||
) | [virtual] |
Sync the memory region to the backing store starting at addr.
Sync len bytes of the memory region to the backing store starting at <addr_>.
Reimplemented in ACE_Lite_MMAP_Memory_Pool.
Definition at line 75 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::sync"); return ACE_OS::msync (addr, len, flags); }
int ACE_MMAP_Memory_Pool::sync | ( | int | flags = MS_SYNC |
) | [virtual] |
Sync the memory region to the backing store starting at this->base_addr_
. Will sync as much as the backing file allows.
Reimplemented in ACE_Lite_MMAP_Memory_Pool.
Definition at line 62 of file MMAP_Memory_Pool.cpp.
{ ACE_TRACE ("ACE_MMAP_Memory_Pool::sync"); size_t const len = ACE_Utils::truncate_cast<size_t> ( ACE_OS::lseek (this->mmap_.handle (), 0, SEEK_END)); return this->mmap_.sync (len, flags); }
Declare the dynamic allocation hooks.
Definition at line 230 of file MMAP_Memory_Pool.h.
ACE_TCHAR ACE_MMAP_Memory_Pool::backing_store_name_[MAXPATHLEN+1] [protected] |
Name of the backing store where the shared memory pool is kept.
Definition at line 289 of file MMAP_Memory_Pool.h.
void* ACE_MMAP_Memory_Pool::base_addr_ [protected] |
Base of mapped region. If this has the value of 0 then the OS is free to select any address to map the file, otherwise this value is what the OS must try to use to mmap the file.
Definition at line 273 of file MMAP_Memory_Pool.h.
mode_t ACE_MMAP_Memory_Pool::file_mode_ [protected] |
Protection mode for mmaped file.
Definition at line 302 of file MMAP_Memory_Pool.h.
int ACE_MMAP_Memory_Pool::flags_ [protected] |
Flags passed into ACE_OS::mmap().
Definition at line 279 of file MMAP_Memory_Pool.h.
bool ACE_MMAP_Memory_Pool::guess_on_fault_ [protected] |
Try to remap without knowing the faulting address. This parameter is ignored on platforms that know the faulting address (UNIX with SI_ADDR and Win32).
Definition at line 296 of file MMAP_Memory_Pool.h.
bool ACE_MMAP_Memory_Pool::install_signal_handler_ [protected] |
Should we install a signal handler.
Definition at line 305 of file MMAP_Memory_Pool.h.
size_t ACE_MMAP_Memory_Pool::minimum_bytes_ [protected] |
What the minimum bytes of the initial segment should be.
Definition at line 286 of file MMAP_Memory_Pool.h.
ACE_Mem_Map ACE_MMAP_Memory_Pool::mmap_ [protected] |
Memory-mapping object.
Definition at line 266 of file MMAP_Memory_Pool.h.
LPSECURITY_ATTRIBUTES ACE_MMAP_Memory_Pool::sa_ [protected] |
Security attributes object, only used on NT.
Definition at line 299 of file MMAP_Memory_Pool.h.
ACE_Sig_Handler ACE_MMAP_Memory_Pool::signal_handler_ [protected] |
Handles SIGSEGV.
Definition at line 262 of file MMAP_Memory_Pool.h.
int ACE_MMAP_Memory_Pool::use_fixed_addr_ [protected] |
Must we use the base_addr_
or can we let mmap(2) select it?
Definition at line 276 of file MMAP_Memory_Pool.h.
bool ACE_MMAP_Memory_Pool::write_each_page_ [protected] |
Should we write a byte to each page to forceably allocate memory for this backing store?
Definition at line 283 of file MMAP_Memory_Pool.h.