3 //=============================================================================
5 * @file Shared_Memory_Pool.h
7 * @author Dougls C. Schmidt <d.schmidt@vanderbilt.edu>
8 * @author Prashant Jain <pjain@cs.wustl.edu>
10 //=============================================================================
12 #ifndef ACE_SHARED_MEMORY_POOL_H
13 #define ACE_SHARED_MEMORY_POOL_H
15 #include /**/ "ace/pre.h"
17 #include /**/ "ace/ACE_export.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 #if !defined (ACE_LACKS_SYSV_SHMEM)
26 #include "ace/Event_Handler.h"
27 #include "ace/Sig_Handler.h"
28 #include "ace/os_include/sys/os_mman.h"
30 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
33 * @class ACE_Shared_Memory_Pool_Options
35 * @brief Helper class for Shared Memory Pool constructor options.
37 * This should be a nested class, but that breaks too many
40 class ACE_Export ACE_Shared_Memory_Pool_Options
43 /// Initialization method.
44 ACE_Shared_Memory_Pool_Options (
45 const char *base_addr
= ACE_DEFAULT_BASE_ADDR
,
46 size_t max_segments
= ACE_DEFAULT_MAX_SEGMENTS
,
47 size_t file_perms
= ACE_DEFAULT_FILE_PERMS
,
48 ACE_OFF_T minimum_bytes
= 0,
49 size_t segment_size
= ACE_DEFAULT_SEGMENT_SIZE
);
51 /// Base address of the memory-mapped backing store.
52 const char *base_addr_
;
54 /// Number of shared memory segments to allocate.
57 /// What the minimum bytes of the initial segment should be.
58 ACE_OFF_T minimum_bytes_
;
60 /// File permissions to use when creating/opening a segment.
63 /// Shared memory segment size.
68 * @class ACE_Shared_Memory_Pool
70 * @brief Make a memory pool that is based on System V shared memory
71 * (shmget(2) etc.). This implementation allows memory to be
72 * shared between processes. If your platform doesn't support
73 * System V shared memory (e.g., Win32 and many RTOS platforms
74 * do not) then you should use ACE_MMAP_Memory_Pool instead of this
75 * class. In fact, you should probably use ACE_MMAP_Memory_Pool on
76 * platforms that *do* support System V shared memory since it
77 * provides more powerful features, such as persistent backing store
78 * and greatly scalability.
80 class ACE_Export ACE_Shared_Memory_Pool
: public ACE_Event_Handler
83 typedef ACE_Shared_Memory_Pool_Options OPTIONS
;
85 /// Initialize the pool.
86 ACE_Shared_Memory_Pool (const ACE_TCHAR
*backing_store_name
= 0,
87 const OPTIONS
*options
= 0);
89 virtual ~ACE_Shared_Memory_Pool (void);
91 /// Ask system for initial chunk of local memory.
92 virtual void *init_acquire (size_t nbytes
,
93 size_t &rounded_bytes
,
97 * Acquire at least @a nbytes from the memory pool. @a rounded_byes is
98 * the actual number of bytes allocated. Also acquires an internal
99 * semaphore that ensures proper serialization of Memory_Pool
100 * initialization across processes.
102 virtual void *acquire (size_t nbytes
,
103 size_t &rounded_bytes
);
105 /// Instruct the memory pool to release all of its resources.
106 virtual int release (int destroy
= 1);
108 /// Sync the memory region to the backing store starting at
109 /// @c this->base_addr_.
110 virtual int sync (ssize_t len
= -1, int flags
= MS_SYNC
);
112 /// Sync the memory region to the backing store starting at @a addr.
113 virtual int sync (void *addr
, size_t len
, int flags
= MS_SYNC
);
116 * Change the protection of the pages of the mapped region to @a prot
117 * starting at @c this->base_addr_ up to @a len bytes. If @a len == -1
118 * then change protection of all pages in the mapped region.
120 virtual int protect (ssize_t len
= -1, int prot
= PROT_RDWR
);
122 /// Change the protection of the pages of the mapped region to @a prot
123 /// starting at @a addr up to @a len bytes.
124 virtual int protect (void *addr
, size_t len
, int prot
= PROT_RDWR
);
126 /// Return the base address of this memory pool, 0 if base_addr
128 virtual void *base_addr (void) const;
130 /// Dump the state of an object.
131 virtual void dump (void) const;
133 /// Declare the dynamic allocation hooks.
134 ACE_ALLOC_HOOK_DECLARE
;
137 /// Implement the algorithm for rounding up the request to an
138 /// appropriate chunksize.
139 virtual size_t round_up (size_t nbytes
);
142 * Commits a new shared memory segment if necessary after an
143 * acquire() or a signal. @a offset is set to the new offset into
146 virtual int commit_backing_store_name (size_t rounded_bytes
,
149 /// Keeps track of all the segments being used.
152 /// Shared memory segment key.
155 /// Shared memory segment internal id.
158 /// Is the segment currently used.;
163 * Base address of the shared memory segment. If this has the value
164 * of 0 then the OS is free to select any address, otherwise this
165 * value is what the OS must try to use to map the shared memory
170 /// File permissions to use when creating/opening a segment.
173 /// Number of shared memory segments in the <SHM_TABLE> table.
174 size_t max_segments_
;
176 /// What the minimim bytes of the initial segment should be.
177 ACE_OFF_T minimum_bytes_
;
179 /// Shared memory segment size.
180 size_t segment_size_
;
182 /// Base shared memory key for the segment.
185 /// Find the segment that contains the @a searchPtr
186 virtual int find_seg (const void *const searchPtr
,
190 /// Determine how much memory is currently in use.
191 virtual int in_use (ACE_OFF_T
&offset
,
195 ACE_Sig_Handler signal_handler_
;
197 /// Handle SIGSEGV and SIGBUS signals to remap shared memory
199 virtual int handle_signal (int, siginfo_t
*siginfo
, ucontext_t
*);
202 ACE_END_VERSIONED_NAMESPACE_DECL
204 #endif /* !ACE_LACKS_SYSV_SHMEM */
206 #include /**/ "ace/post.h"
208 #endif /* ACE_SHARED_MEMORY_POOL_H */