00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file Memory_Pool.h 00006 * 00007 * $Id: Memory_Pool.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $ 00008 * 00009 * @author Dougls C. Schmidt <schmidt@cs.wustl.edu> and Prashant Jain <pjain@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_MEMORY_POOL_H 00014 #define ACE_MEMORY_POOL_H 00015 #include "ace/pre.h" 00016 00017 #include "ace/ACE.h" 00018 00019 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00020 # pragma once 00021 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00022 00023 #include "ace/Event_Handler.h" 00024 #include "ace/Signal.h" 00025 #include "ace/Mem_Map.h" 00026 #if !defined (ACE_WIN32) 00027 #include "ace/SV_Semaphore_Complex.h" 00028 #endif /* !ACE_WIN32 */ 00029 00030 #include "ace/Unbounded_Set.h" 00031 00032 #if !defined (ACE_LACKS_SBRK) 00033 /** 00034 * @class ACE_Sbrk_Memory_Pool_Options 00035 * 00036 * @brief Helper class for Sbrk Memory Pool constructor options. 00037 * 00038 * This should be a nested class, but that breaks too many 00039 * compilers. 00040 */ 00041 class ACE_Export ACE_Sbrk_Memory_Pool_Options 00042 { 00043 }; 00044 00045 /** 00046 * @class ACE_Sbrk_Memory_Pool 00047 * 00048 * @brief Make a memory pool that is based on <sbrk(2)>. 00049 */ 00050 class ACE_Export ACE_Sbrk_Memory_Pool 00051 { 00052 public: 00053 typedef ACE_Sbrk_Memory_Pool_Options OPTIONS; 00054 00055 /// Initialize the pool. 00056 ACE_Sbrk_Memory_Pool (const ACE_TCHAR *backing_store_name = 0, 00057 const OPTIONS *options = 0); 00058 00059 virtual ~ACE_Sbrk_Memory_Pool (void); 00060 00061 // = Implementor operations. 00062 /// Ask system for initial chunk of local memory. 00063 virtual void *init_acquire (size_t nbytes, 00064 size_t &rounded_bytes, 00065 int &first_time); 00066 00067 /// Acquire at least NBYTES from the memory pool. ROUNDED_BYTES is 00068 /// the actual number of bytes allocated. 00069 virtual void *acquire (size_t nbytes, 00070 size_t &rounded_bytes); 00071 00072 /// Instruct the memory pool to release all of its resources. 00073 virtual int release (int destroy = 1); 00074 00075 /** 00076 * Sync <len> bytes of the memory region to the backing store 00077 * starting at <this->base_addr_>. If <len> == -1 then sync the 00078 * whole region. 00079 */ 00080 virtual int sync (ssize_t len = -1, int flags = MS_SYNC); 00081 00082 /// Sync <len> bytes of the memory region to the backing store 00083 /// starting at <addr_>. 00084 virtual int sync (void *addr, size_t len, int flags = MS_SYNC); 00085 00086 /** 00087 * Change the protection of the pages of the mapped region to <prot> 00088 * starting at <this->base_addr_> up to <len> bytes. If <len> == -1 00089 * then change protection of all pages in the mapped region. 00090 */ 00091 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); 00092 00093 /// Change the protection of the pages of the mapped region to <prot> 00094 /// starting at <addr> up to <len> bytes. 00095 virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); 00096 00097 /// Dump the state of an object. 00098 virtual void dump (void) const; 00099 00100 /// Return the base address of this memory pool, 0 if base_addr 00101 /// never changes. 00102 virtual void *base_addr (void) const; 00103 00104 /// Declare the dynamic allocation hooks. 00105 ACE_ALLOC_HOOK_DECLARE; 00106 00107 protected: 00108 /// Implement the algorithm for rounding up the request to an 00109 /// appropriate chunksize. 00110 virtual size_t round_up (size_t nbytes); 00111 }; 00112 #endif /* !ACE_LACKS_SBRK */ 00113 00114 #if !defined (ACE_LACKS_SYSV_SHMEM) 00115 00116 /** 00117 * @class ACE_Shared_Memory_Pool_Options 00118 * 00119 * @brief Helper class for Shared Memory Pool constructor options. 00120 * 00121 * This should be a nested class, but that breaks too many 00122 * compilers. 00123 */ 00124 class ACE_Export ACE_Shared_Memory_Pool_Options 00125 { 00126 public: 00127 /// Initialization method. 00128 ACE_Shared_Memory_Pool_Options (const char *base_addr = ACE_DEFAULT_BASE_ADDR, 00129 size_t max_segments = ACE_DEFAULT_MAX_SEGMENTS, 00130 size_t file_perms = ACE_DEFAULT_FILE_PERMS, 00131 off_t minimum_bytes = 0, 00132 size_t segment_size = ACE_DEFAULT_SEGMENT_SIZE); 00133 00134 /// Base address of the memory-mapped backing store. 00135 const char *base_addr_; 00136 00137 /// Number of shared memory segments to allocate. 00138 size_t max_segments_; 00139 00140 /// What the minimum bytes of the initial segment should be. 00141 off_t minimum_bytes_; 00142 00143 /// File permissions to use when creating/opening a segment. 00144 size_t file_perms_; 00145 00146 /// Shared memory segment size. 00147 size_t segment_size_; 00148 }; 00149 00150 /** 00151 * @class ACE_Shared_Memory_Pool 00152 * 00153 * @brief Make a memory pool that is based on System V shared memory 00154 * (shmget(2) etc.). This implementation allows memory to be 00155 * shared between processes. If your platform doesn't support 00156 * System V shared memory (e.g., Win32 and many RTOS platforms 00157 * do not) then you should use <ACE_MMAP_Memory_Pool> instead of this 00158 * class. In fact, you should probably use <ACE_MMAP_Memory_Pool> on 00159 * platforms that *do* support System V shared memory since it 00160 * provides more powerful features, such as persistent backing store 00161 * and greatly scalability. 00162 */ 00163 class ACE_Export ACE_Shared_Memory_Pool : public ACE_Event_Handler 00164 { 00165 public: 00166 typedef ACE_Shared_Memory_Pool_Options OPTIONS; 00167 00168 /// Initialize the pool. 00169 ACE_Shared_Memory_Pool (const ACE_TCHAR *backing_store_name = 0, 00170 const OPTIONS *options = 0); 00171 00172 virtual ~ACE_Shared_Memory_Pool (void); 00173 00174 /// Ask system for initial chunk of local memory. 00175 virtual void *init_acquire (size_t nbytes, 00176 size_t &rounded_bytes, 00177 int &first_time); 00178 00179 /** 00180 * Acquire at least NBYTES from the memory pool. ROUNDED_BYTES is 00181 * the actual number of bytes allocated. Also acquires an internal 00182 * semaphore that ensures proper serialization of Memory_Pool 00183 * initialization across processes. 00184 */ 00185 virtual void *acquire (size_t nbytes, 00186 size_t &rounded_bytes); 00187 00188 /// Instruct the memory pool to release all of its resources. 00189 virtual int release (int destroy = 1); 00190 00191 /// Sync the memory region to the backing store starting at 00192 /// <this->base_addr_>. 00193 virtual int sync (ssize_t len = -1, int flags = MS_SYNC); 00194 00195 /// Sync the memory region to the backing store starting at <addr_>. 00196 virtual int sync (void *addr, size_t len, int flags = MS_SYNC); 00197 00198 /** 00199 * Change the protection of the pages of the mapped region to <prot> 00200 * starting at <this->base_addr_> up to <len> bytes. If <len> == -1 00201 * then change protection of all pages in the mapped region. 00202 */ 00203 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); 00204 00205 /// Change the protection of the pages of the mapped region to <prot> 00206 /// starting at <addr> up to <len> bytes. 00207 virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); 00208 00209 /// Return the base address of this memory pool, 0 if base_addr 00210 /// never changes. 00211 virtual void *base_addr (void) const; 00212 00213 /// Dump the state of an object. 00214 virtual void dump (void) const; 00215 00216 /// Declare the dynamic allocation hooks. 00217 ACE_ALLOC_HOOK_DECLARE; 00218 00219 protected: 00220 /// Implement the algorithm for rounding up the request to an 00221 /// appropriate chunksize. 00222 virtual size_t round_up (size_t nbytes); 00223 00224 /** 00225 * Commits a new shared memory segment if necessary after an 00226 * <acquire> or a signal. <offset> is set to the new offset into 00227 * the backing store. 00228 */ 00229 virtual int commit_backing_store_name (size_t rounded_bytes, 00230 off_t &offset); 00231 00232 /// Keeps track of all the segments being used. 00233 struct SHM_TABLE 00234 { 00235 /// Shared memory segment key. 00236 key_t key_; 00237 00238 /// Shared memory segment internal id. 00239 int shmid_; 00240 00241 /// Is the segment currently used.; 00242 int used_; 00243 }; 00244 00245 /** 00246 * Base address of the shared memory segment. If this has the value 00247 * of 0 then the OS is free to select any address, otherwise this 00248 * value is what the OS must try to use to map the shared memory 00249 * segment. 00250 */ 00251 void *base_addr_; 00252 00253 /// File permissions to use when creating/opening a segment. 00254 size_t file_perms_; 00255 00256 /// Number of shared memory segments in the <SHM_TABLE> table. 00257 size_t max_segments_; 00258 00259 /// What the minimim bytes of the initial segment should be. 00260 off_t minimum_bytes_; 00261 00262 /// Shared memory segment size. 00263 size_t segment_size_; 00264 00265 /// Base shared memory key for the segment. 00266 key_t base_shm_key_; 00267 00268 /// Find the segment that contains the @a searchPtr 00269 virtual int find_seg (const void *const searchPtr, 00270 off_t &offset, 00271 size_t &counter); 00272 00273 /// Determine how much memory is currently in use. 00274 virtual int in_use (off_t &offset, 00275 size_t &counter); 00276 00277 /// Handles SIGSEGV. 00278 ACE_Sig_Handler signal_handler_; 00279 00280 /// Handle SIGSEGV and SIGBUS signals to remap shared memory 00281 /// properly. 00282 virtual int handle_signal (int signum, siginfo_t *, ucontext_t *); 00283 }; 00284 #endif /* !ACE_LACKS_SYSV_SHMEM */ 00285 00286 /** 00287 * @class ACE_Local_Memory_Pool_Options 00288 * 00289 * @brief Helper class for Local Memory Pool constructor options. 00290 * 00291 * This should be a nested class, but that breaks too many 00292 * compilers. 00293 */ 00294 class ACE_Export ACE_Local_Memory_Pool_Options 00295 { 00296 }; 00297 00298 /** 00299 * @class ACE_Local_Memory_Pool 00300 * 00301 * @brief Make a memory pool that is based on C++ new/delete. This is 00302 * useful for integrating existing components that use new/delete 00303 * into the ACE Malloc scheme... 00304 */ 00305 class ACE_Export ACE_Local_Memory_Pool 00306 { 00307 public: 00308 typedef ACE_Local_Memory_Pool_Options OPTIONS; 00309 00310 /// Initialize the pool. 00311 ACE_Local_Memory_Pool (const ACE_TCHAR *backing_store_name = 0, 00312 const OPTIONS *options = 0); 00313 00314 virtual ~ACE_Local_Memory_Pool (void); 00315 00316 /// Ask system for initial chunk of local memory. 00317 virtual void *init_acquire (size_t nbytes, 00318 size_t &rounded_bytes, 00319 int &first_time); 00320 00321 /// Acquire at least NBYTES from the memory pool. ROUNDED_BYTES is 00322 /// the actual number of bytes allocated. 00323 virtual void *acquire (size_t nbytes, 00324 size_t &rounded_bytes); 00325 00326 /// Instruct the memory pool to release all of its resources. 00327 virtual int release (int destroy = 1); 00328 00329 /** 00330 * Sync <len> bytes of the memory region to the backing store 00331 * starting at <this->base_addr_>. If <len> == -1 then sync the 00332 * whole region. 00333 */ 00334 virtual int sync (ssize_t len = -1, int flags = MS_SYNC); 00335 00336 /// Sync <len> bytes of the memory region to the backing store 00337 /// starting at <addr_>. 00338 virtual int sync (void *addr, size_t len, int flags = MS_SYNC); 00339 00340 /** 00341 * Change the protection of the pages of the mapped region to <prot> 00342 * starting at <this->base_addr_> up to <len> bytes. If <len> == -1 00343 * then change protection of all pages in the mapped region. 00344 */ 00345 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); 00346 00347 /// Change the protection of the pages of the mapped region to <prot> 00348 /// starting at <addr> up to <len> bytes. 00349 virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); 00350 00351 #if defined (ACE_WIN32) 00352 /** 00353 * Win32 Structural exception selector. The return value decides 00354 * how to handle memory pool related structural exceptions. Returns 00355 * 1, 0, or , -1. 00356 */ 00357 virtual int seh_selector (void *); 00358 #endif /* ACE_WIN32 */ 00359 00360 /** 00361 * Try to extend the virtual address space so that <addr> is now 00362 * covered by the address mapping. Always returns 0 since we can't 00363 * remap a local memory pool. 00364 */ 00365 virtual int remap (void *addr); 00366 00367 /// Return the base address of this memory pool, 0 if base_addr 00368 /// never changes. 00369 virtual void *base_addr (void) const; 00370 00371 /// Dump the state of an object. 00372 virtual void dump (void) const; 00373 00374 /// Declare the dynamic allocation hooks. 00375 ACE_ALLOC_HOOK_DECLARE; 00376 00377 protected: 00378 /// List of memory that we have allocated. 00379 ACE_Unbounded_Set<char *> allocated_chunks_; 00380 00381 /// Implement the algorithm for rounding up the request to an 00382 /// appropriate chunksize. 00383 virtual size_t round_up (size_t nbytes); 00384 00385 }; 00386 00387 /** 00388 * @class ACE_MMAP_Memory_Pool_Options 00389 * 00390 * @brief Helper class for MMAP Memory Pool constructor options. 00391 * 00392 * This should be a nested class, but that breaks too many 00393 * compilers. 00394 */ 00395 class ACE_Export ACE_MMAP_Memory_Pool_Options 00396 { 00397 public: 00398 enum 00399 { 00400 /** 00401 * The base address from the first call to mmap will be used for subsequent 00402 * calls to mmap. 00403 */ 00404 FIRSTCALL_FIXED = 0, 00405 00406 /** 00407 * The base address specified in base_addr will be used in all calls to 00408 * mmap. 00409 */ 00410 ALWAYS_FIXED = 1, 00411 00412 /** 00413 * The base address will be selected by the OS for each call to mmap. 00414 * Caution should be used with this mode since a call that requires the 00415 * backing store to grow may change pointers that are cached by the 00416 * application. 00417 */ 00418 NEVER_FIXED = 2 00419 }; 00420 00421 // = Initialization method. 00422 ACE_MMAP_Memory_Pool_Options (const void *base_addr = ACE_DEFAULT_BASE_ADDR, 00423 int use_fixed_addr = ALWAYS_FIXED, 00424 int write_each_page = 1, 00425 off_t minimum_bytes = 0, 00426 u_int flags = 0, 00427 int guess_on_fault = 1, 00428 LPSECURITY_ATTRIBUTES sa = 0, 00429 mode_t file_mode = ACE_DEFAULT_FILE_PERMS); 00430 00431 /// Base address of the memory-mapped backing store. 00432 const void *base_addr_; 00433 00434 /** 00435 * Determines whether we set <base_addr_> or if mmap(2) selects it 00436 * FIRSTCALL_FIXED The base address from the first call to mmap 00437 * will be used for subsequent calls to mmap 00438 * ALWAYS_FIXED The base address specified in base_addr will be 00439 * used in all calls to mmap. 00440 * NEVER_FIXED The base address will be selected by the OS for 00441 * each call to mmap. Caution should be used with 00442 * this mode since a call that requires the backing 00443 * store to grow may change pointers that are 00444 * cached by the application. 00445 */ 00446 int use_fixed_addr_; 00447 00448 /// Should each page be written eagerly to avoid surprises later 00449 /// on? 00450 int write_each_page_; 00451 00452 /// What the minimim bytes of the initial segment should be. 00453 off_t minimum_bytes_; 00454 00455 /// Any special flags that need to be used for <mmap>. 00456 u_int flags_; 00457 00458 /** 00459 * Try to remap without knowing the faulting address. This 00460 * parameter is ignored on platforms that know the faulting address 00461 * (UNIX with SI_ADDR and Win32). 00462 */ 00463 int guess_on_fault_; 00464 00465 /// Pointer to a security attributes object. Only used on NT. 00466 LPSECURITY_ATTRIBUTES sa_; 00467 00468 /// File mode for mmaped file, if it is created. 00469 mode_t file_mode_; 00470 }; 00471 00472 /** 00473 * @class ACE_MMAP_Memory_Pool 00474 * 00475 * @brief Make a memory pool that is based on <mmap(2)>. This 00476 * implementation allows memory to be shared between processes. 00477 */ 00478 class ACE_Export ACE_MMAP_Memory_Pool : public ACE_Event_Handler 00479 { 00480 public: 00481 typedef ACE_MMAP_Memory_Pool_Options OPTIONS; 00482 00483 // = Initialization and termination methods. 00484 00485 /// Initialize the pool. 00486 ACE_MMAP_Memory_Pool (const ACE_TCHAR *backing_store_name = 0, 00487 const OPTIONS *options = 0); 00488 00489 /// Destructor. 00490 virtual ~ACE_MMAP_Memory_Pool (void); 00491 00492 /// Ask system for initial chunk of shared memory. 00493 virtual void *init_acquire (size_t nbytes, 00494 size_t &rounded_bytes, 00495 int &first_time); 00496 00497 /** 00498 * Acquire at least <nbytes> from the memory pool. <rounded_bytes> 00499 * is the actual number of bytes allocated. Also acquires an 00500 * internal semaphore that ensures proper serialization of 00501 * <ACE_MMAP_Memory_Pool> initialization across processes. 00502 */ 00503 virtual void *acquire (size_t nbytes, 00504 size_t &rounded_bytes); 00505 00506 /// Instruct the memory pool to release all of its resources. 00507 virtual int release (int destroy = 1); 00508 00509 /// Sync the memory region to the backing store starting at 00510 /// <this->base_addr_>. 00511 virtual int sync (ssize_t len = -1, int flags = MS_SYNC); 00512 00513 /// Sync the memory region to the backing store starting at <addr_>. 00514 virtual int sync (void *addr, size_t len, int flags = MS_SYNC); 00515 00516 /** 00517 * Change the protection of the pages of the mapped region to <prot> 00518 * starting at <this->base_addr_> up to <len> bytes. If <len> == -1 00519 * then change protection of all pages in the mapped region. 00520 */ 00521 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); 00522 00523 /// Change the protection of the pages of the mapped region to <prot> 00524 /// starting at <addr> up to <len> bytes. 00525 virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); 00526 00527 #if defined (ACE_WIN32) 00528 /** 00529 * Win32 Structural exception selector. The return value decides 00530 * how to handle memory pool related structural exceptions. Returns 00531 * 1, 0, or , -1. 00532 */ 00533 virtual int seh_selector (void *); 00534 #endif /* ACE_WIN32 */ 00535 00536 /** 00537 * Try to extend the virtual address space so that <addr> is now 00538 * covered by the address mapping. The method succeeds and returns 00539 * 0 if the backing store has adequate memory to cover this address. 00540 * Otherwise, it returns -1. This method is typically called by a 00541 * UNIX signal handler for SIGSEGV or a Win32 structured exception 00542 * when another process has grown the backing store (and its 00543 * mapping) and our process now incurs a fault because our mapping 00544 * isn't in range (yet). 00545 */ 00546 virtual int remap (void *addr); 00547 00548 /// Return the base address of this memory pool. 00549 virtual void *base_addr (void) const; 00550 00551 /// Dump the state of an object. 00552 virtual void dump (void) const; 00553 00554 /// Declare the dynamic allocation hooks. 00555 ACE_ALLOC_HOOK_DECLARE; 00556 00557 protected: 00558 /// Implement the algorithm for rounding up the request to an 00559 /// appropriate chunksize. 00560 virtual size_t round_up (size_t nbytes); 00561 00562 /// Compute the new <map_size> of the backing store and commit the 00563 /// memory. 00564 virtual int commit_backing_store_name (size_t rounded_bytes, 00565 off_t &map_size); 00566 00567 /// Memory map the file up to <map_size> bytes. 00568 virtual int map_file (off_t map_size); 00569 00570 /// Handle SIGSEGV and SIGBUS signals to remap shared memory 00571 /// properly. 00572 virtual int handle_signal (int signum, siginfo_t *, ucontext_t *); 00573 00574 /// Handles SIGSEGV. 00575 ACE_Sig_Handler signal_handler_; 00576 00577 /// Memory-mapping object. 00578 ACE_Mem_Map mmap_; 00579 00580 /** 00581 * Base of mapped region. If this has the value of 0 then the OS is 00582 * free to select any address to map the file, otherwise this value 00583 * is what the OS must try to use to mmap the file. 00584 */ 00585 void *base_addr_; 00586 00587 /// Must we use the <base_addr_> or can we let mmap(2) select it? 00588 int use_fixed_addr_; 00589 00590 /// Flags passed into <ACE_OS::mmap>. 00591 int flags_; 00592 00593 /// Should we write a byte to each page to forceably allocate memory 00594 /// for this backing store? 00595 int write_each_page_; 00596 00597 /// What the minimum bytes of the initial segment should be. 00598 off_t minimum_bytes_; 00599 00600 /// Name of the backing store where the shared memory pool is kept. 00601 ACE_TCHAR backing_store_name_[MAXPATHLEN + 1]; 00602 00603 /** 00604 * Try to remap without knowing the faulting address. This 00605 * parameter is ignored on platforms that know the faulting address 00606 * (UNIX with SI_ADDR and Win32). 00607 */ 00608 int guess_on_fault_; 00609 00610 /// Security attributes object, only used on NT. 00611 LPSECURITY_ATTRIBUTES sa_; 00612 00613 /// Protection mode for mmaped file. 00614 mode_t file_mode_; 00615 }; 00616 00617 /** 00618 * @class ACE_Lite_MMAP_Memory_Pool 00619 * 00620 * @brief Make a ``lighter-weight'' memory pool based <ACE_Mem_Map>. 00621 * 00622 * This implementation allows memory to be shared between 00623 * processes. However, unlike the <ACE_MMAP_Memory_Pool> 00624 * the <sync> methods are no-ops, which means that we don't pay 00625 * for the price of flushing the memory to the backing store on 00626 * every update. Naturally, this trades off increased 00627 * performance for less reliability if the machine crashes. 00628 */ 00629 class ACE_Export ACE_Lite_MMAP_Memory_Pool : public ACE_MMAP_Memory_Pool 00630 { 00631 public: 00632 /// Initialize the pool. 00633 ACE_Lite_MMAP_Memory_Pool (const ACE_TCHAR *backing_store_name = 0, 00634 const OPTIONS *options = 0); 00635 00636 /// Destructor. 00637 virtual ~ACE_Lite_MMAP_Memory_Pool (void); 00638 00639 /// Overwrite the default sync behavior with no-op 00640 virtual int sync (ssize_t len = -1, int flags = MS_SYNC); 00641 00642 /// Overwrite the default sync behavior with no-op 00643 virtual int sync (void *addr, size_t len, int flags = MS_SYNC); 00644 }; 00645 00646 #if defined (ACE_WIN32) 00647 00648 /** 00649 * @class ACE_Pagefile_Memory_Pool_Options 00650 * 00651 * @brief Helper class for Pagefile Memory Pool constructor options. 00652 * 00653 * This should be a nested class, but that breaks too many 00654 * compilers. 00655 */ 00656 class ACE_Export ACE_Pagefile_Memory_Pool_Options 00657 { 00658 public: 00659 /// Initialization method. 00660 ACE_Pagefile_Memory_Pool_Options (void *base_addr = ACE_DEFAULT_PAGEFILE_POOL_BASE, 00661 size_t max_size = ACE_DEFAULT_PAGEFILE_POOL_SIZE); 00662 00663 /// Base address of the memory-mapped backing store. 00664 void *base_addr_; 00665 00666 /// Maximum size the pool may grow. 00667 size_t max_size_; 00668 }; 00669 00670 /** 00671 * @class ACE_Pagefile_Memory_Pool 00672 * 00673 * @brief Make a memory pool that is based on "anonymous" memory 00674 * regions allocated from the Win32 page file. 00675 */ 00676 class ACE_Export ACE_Pagefile_Memory_Pool 00677 { 00678 public: 00679 typedef ACE_Pagefile_Memory_Pool_Options OPTIONS; 00680 00681 /// Initialize the pool. 00682 ACE_Pagefile_Memory_Pool (const ACE_TCHAR *backing_store_name = 0, 00683 const OPTIONS *options = 0); 00684 00685 /// Ask system for initial chunk of shared memory. 00686 void *init_acquire (size_t nbytes, 00687 size_t &rounded_bytes, 00688 int &first_time); 00689 00690 /// Acquire at least <nbytes> from the memory pool. <rounded_bytes> 00691 /// is the actual number of bytes allocated. 00692 void *acquire (size_t nbytes, 00693 size_t &rounded_bytes); 00694 00695 /// Instruct the memory pool to release all of its resources. 00696 int release (int destroy = 1); 00697 00698 /** 00699 * Win32 Structural exception selector. The return value decides 00700 * how to handle memory pool related structural exceptions. Returns 00701 * 1, 0, or , -1. 00702 */ 00703 virtual int seh_selector (void *); 00704 00705 /** 00706 * Try to extend the virtual address space so that <addr> is now 00707 * covered by the address mapping. The method succeeds and returns 00708 * 0 if the backing store has adequate memory to cover this address. 00709 * Otherwise, it returns -1. This method is typically called by an 00710 * exception handler for a Win32 structured exception when another 00711 * process has grown the backing store (and its mapping) and our 00712 * process now incurs a fault because our mapping isn't in range 00713 * (yet). 00714 */ 00715 int remap (void *addr); 00716 00717 /// Round up to system page size. 00718 size_t round_to_page_size (size_t nbytes); 00719 00720 /// Round up to the chunk size required by the operation system 00721 size_t round_to_chunk_size (size_t nbytes); 00722 00723 // = Don't need this methods here ... 00724 int sync (ssize_t = -1, int = MS_SYNC); 00725 int sync (void *, size_t, int = MS_SYNC); 00726 int protect (ssize_t = -1, int = PROT_RDWR); 00727 int protect (void *, size_t, int = PROT_RDWR); 00728 00729 /// Return the base address of this memory pool, 0 if base_addr 00730 /// never changes. 00731 virtual void *base_addr (void) const; 00732 00733 void dump (void) const {} 00734 00735 protected: 00736 00737 /** 00738 * Map portions or the entire pool into the local virtual address 00739 * space. To do this, we compute the new <file_offset> of the 00740 * backing store and commit the memory. 00741 */ 00742 int map (int &firstTime, size_t appendBytes = 0); 00743 00744 /// Release the mapping. 00745 int unmap (void); 00746 00747 private: 00748 00749 /** 00750 * @class Control_Block 00751 * 00752 * @brief Attributes that are meaningful in local storage only. 00753 */ 00754 class Control_Block 00755 { 00756 public: 00757 /// Required base address 00758 void *req_base_; 00759 00760 /// Base address returned from system call 00761 void *mapped_base_; 00762 00763 /** 00764 * @class Shared_Control_Block 00765 * 00766 * @brief Pool statistics 00767 */ 00768 class Shared_Control_Block 00769 { 00770 public: 00771 /// Maximum size the pool may grow 00772 size_t max_size_; 00773 00774 /// Size of mapped shared memory segment 00775 size_t mapped_size_; 00776 00777 /// Offset to mapped but not yet acquired address space 00778 ptrdiff_t free_offset_; 00779 00780 /// Size of mapped but not yet acquired address space 00781 size_t free_size_; 00782 }; 00783 00784 Shared_Control_Block sh_; 00785 }; 00786 00787 // Base of mapped region. If this has the value of 0 then the OS is 00788 // free to select any address to map the file, otherwise this value 00789 // is what the OS must try to use to mmap the file. 00790 00791 /// Description of what our process mapped. 00792 Control_Block local_cb_; 00793 00794 /// Shared memory pool statistics. 00795 Control_Block *shared_cb_; 00796 00797 /// File mapping handle. 00798 ACE_HANDLE object_handle_; 00799 00800 /// System page size. 00801 size_t page_size_; 00802 00803 /// Name of the backing store where the shared memory pool is kept. 00804 ACE_TCHAR backing_store_name_[MAXPATHLEN]; 00805 }; 00806 00807 #endif /* ACE_WIN32 */ 00808 00809 #if defined (__ACE_INLINE__) 00810 #include "ace/Memory_Pool.i" 00811 #endif /* __ACE_INLINE__ */ 00812 00813 #include "ace/post.h" 00814 #endif /* ACE_MEMORY_POOL_H */
1.2.14 written by Dimitri van Heesch,
© 1997-2002