3 //=============================================================================
5 * @file Shared_Memory_Pool.h
7 * $Id: Shared_Memory_Pool.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Dougls C. Schmidt <schmidt@cs.wustl.edu>
10 * @author Prashant Jain <pjain@cs.wustl.edu>
12 //=============================================================================
14 #ifndef ACE_SHARED_MEMORY_POOL_H
15 #define ACE_SHARED_MEMORY_POOL_H
17 #include /**/ "ace/pre.h"
19 #include /**/ "ace/ACE_export.h"
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 #if !defined (ACE_LACKS_SYSV_SHMEM)
28 #include "ace/Event_Handler.h"
29 #include "ace/Sig_Handler.h"
30 #include "ace/os_include/sys/os_mman.h"
32 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
35 * @class ACE_Shared_Memory_Pool_Options
37 * @brief Helper class for Shared Memory Pool constructor options.
39 * This should be a nested class, but that breaks too many
42 class ACE_Export ACE_Shared_Memory_Pool_Options
45 /// Initialization method.
46 ACE_Shared_Memory_Pool_Options (
47 const char *base_addr
= ACE_DEFAULT_BASE_ADDR
,
48 size_t max_segments
= ACE_DEFAULT_MAX_SEGMENTS
,
49 size_t file_perms
= ACE_DEFAULT_FILE_PERMS
,
50 ACE_OFF_T minimum_bytes
= 0,
51 size_t segment_size
= ACE_DEFAULT_SEGMENT_SIZE
);
53 /// Base address of the memory-mapped backing store.
54 const char *base_addr_
;
56 /// Number of shared memory segments to allocate.
59 /// What the minimum bytes of the initial segment should be.
60 ACE_OFF_T minimum_bytes_
;
62 /// File permissions to use when creating/opening a segment.
65 /// Shared memory segment size.
70 * @class ACE_Shared_Memory_Pool
72 * @brief Make a memory pool that is based on System V shared memory
73 * (shmget(2) etc.). This implementation allows memory to be
74 * shared between processes. If your platform doesn't support
75 * System V shared memory (e.g., Win32 and many RTOS platforms
76 * do not) then you should use ACE_MMAP_Memory_Pool instead of this
77 * class. In fact, you should probably use ACE_MMAP_Memory_Pool on
78 * platforms that *do* support System V shared memory since it
79 * provides more powerful features, such as persistent backing store
80 * and greatly scalability.
82 class ACE_Export ACE_Shared_Memory_Pool
: public ACE_Event_Handler
85 typedef ACE_Shared_Memory_Pool_Options OPTIONS
;
87 /// Initialize the pool.
88 ACE_Shared_Memory_Pool (const ACE_TCHAR
*backing_store_name
= 0,
89 const OPTIONS
*options
= 0);
91 virtual ~ACE_Shared_Memory_Pool (void);
93 /// Ask system for initial chunk of local memory.
94 virtual void *init_acquire (size_t nbytes
,
95 size_t &rounded_bytes
,
99 * Acquire at least @a nbytes from the memory pool. @a rounded_byes is
100 * the actual number of bytes allocated. Also acquires an internal
101 * semaphore that ensures proper serialization of Memory_Pool
102 * initialization across processes.
104 virtual void *acquire (size_t nbytes
,
105 size_t &rounded_bytes
);
107 /// Instruct the memory pool to release all of its resources.
108 virtual int release (int destroy
= 1);
110 /// Sync the memory region to the backing store starting at
111 /// @c this->base_addr_.
112 virtual int sync (ssize_t len
= -1, int flags
= MS_SYNC
);
114 /// Sync the memory region to the backing store starting at @a addr.
115 virtual int sync (void *addr
, size_t len
, int flags
= MS_SYNC
);
118 * Change the protection of the pages of the mapped region to @a prot
119 * starting at @c this->base_addr_ up to @a len bytes. If @a len == -1
120 * then change protection of all pages in the mapped region.
122 virtual int protect (ssize_t len
= -1, int prot
= PROT_RDWR
);
124 /// Change the protection of the pages of the mapped region to @a prot
125 /// starting at @a addr up to @a len bytes.
126 virtual int protect (void *addr
, size_t len
, int prot
= PROT_RDWR
);
128 /// Return the base address of this memory pool, 0 if base_addr
130 virtual void *base_addr (void) const;
132 /// Dump the state of an object.
133 virtual void dump (void) const;
135 /// Declare the dynamic allocation hooks.
136 ACE_ALLOC_HOOK_DECLARE
;
139 /// Implement the algorithm for rounding up the request to an
140 /// appropriate chunksize.
141 virtual size_t round_up (size_t nbytes
);
144 * Commits a new shared memory segment if necessary after an
145 * <acquire> or a signal. @a offset is set to the new offset into
148 virtual int commit_backing_store_name (size_t rounded_bytes
,
151 /// Keeps track of all the segments being used.
154 /// Shared memory segment key.
157 /// Shared memory segment internal id.
160 /// Is the segment currently used.;
165 * Base address of the shared memory segment. If this has the value
166 * of 0 then the OS is free to select any address, otherwise this
167 * value is what the OS must try to use to map the shared memory
172 /// File permissions to use when creating/opening a segment.
175 /// Number of shared memory segments in the <SHM_TABLE> table.
176 size_t max_segments_
;
178 /// What the minimim bytes of the initial segment should be.
179 ACE_OFF_T minimum_bytes_
;
181 /// Shared memory segment size.
182 size_t segment_size_
;
184 /// Base shared memory key for the segment.
187 /// Find the segment that contains the @a searchPtr
188 virtual int find_seg (const void *const searchPtr
,
192 /// Determine how much memory is currently in use.
193 virtual int in_use (ACE_OFF_T
&offset
,
197 ACE_Sig_Handler signal_handler_
;
199 /// Handle SIGSEGV and SIGBUS signals to remap shared memory
201 virtual int handle_signal (int signum
, siginfo_t
*, ucontext_t
*);
204 ACE_END_VERSIONED_NAMESPACE_DECL
206 #endif /* !ACE_LACKS_SYSV_SHMEM */
208 #include /**/ "ace/post.h"
210 #endif /* ACE_SHARED_MEMORY_POOL_H */