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"
31 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
34 * @class ACE_Shared_Memory_Pool_Options
36 * @brief Helper class for Shared Memory Pool constructor options.
38 * This should be a nested class, but that breaks too many
41 class ACE_Export ACE_Shared_Memory_Pool_Options
44 /// Initialization method.
45 ACE_Shared_Memory_Pool_Options (
46 const char *base_addr
= ACE_DEFAULT_BASE_ADDR
,
47 size_t max_segments
= ACE_DEFAULT_MAX_SEGMENTS
,
48 size_t file_perms
= ACE_DEFAULT_FILE_PERMS
,
49 ACE_OFF_T minimum_bytes
= 0,
50 size_t segment_size
= ACE_DEFAULT_SEGMENT_SIZE
);
52 /// Base address of the memory-mapped backing store.
53 const char *base_addr_
;
55 /// Number of shared memory segments to allocate.
58 /// What the minimum bytes of the initial segment should be.
59 ACE_OFF_T minimum_bytes_
;
61 /// File permissions to use when creating/opening a segment.
64 /// Shared memory segment size.
69 * @class ACE_Shared_Memory_Pool
71 * @brief Make a memory pool that is based on System V shared memory
72 * (shmget(2) etc.). This implementation allows memory to be
73 * shared between processes. If your platform doesn't support
74 * System V shared memory (e.g., Win32 and many RTOS platforms
75 * do not) then you should use ACE_MMAP_Memory_Pool instead of this
76 * class. In fact, you should probably use ACE_MMAP_Memory_Pool on
77 * platforms that *do* support System V shared memory since it
78 * provides more powerful features, such as persistent backing store
79 * and greatly scalability.
81 class ACE_Export ACE_Shared_Memory_Pool
: public ACE_Event_Handler
84 typedef ACE_Shared_Memory_Pool_Options OPTIONS
;
86 /// Initialize the pool.
87 ACE_Shared_Memory_Pool (const ACE_TCHAR
*backing_store_name
= nullptr,
88 const OPTIONS
*options
= nullptr);
90 virtual ~ACE_Shared_Memory_Pool () = default;
92 /// Ask system for initial chunk of local memory.
93 virtual void *init_acquire (size_t nbytes
,
94 size_t &rounded_bytes
,
98 * Acquire at least @a nbytes from the memory pool. @a rounded_byes is
99 * the actual number of bytes allocated. Also acquires an internal
100 * semaphore that ensures proper serialization of Memory_Pool
101 * initialization across processes.
103 virtual void *acquire (size_t nbytes
, 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 shm_addr_table_[0].
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 shm_addr_table_[0] 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, nullptr if shm_addr_table_[0]
128 virtual void *base_addr () const;
130 /// Dump the state of an object.
131 virtual void dump () 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. The shared memory
150 /// table is stored in the first shared memory segment
153 /// Shared memory segment key
156 /// Shared memory segment internal id
159 /// Is the segment currently used
163 /// File permissions to use when creating/opening a segment.
164 size_t const file_perms_
;
166 /// Number of shared memory segments in the SHM_TABLE table.
167 size_t const max_segments_
;
169 /// What the minimum bytes of the initial segment should be.
170 ACE_OFF_T
const minimum_bytes_
;
172 /// Shared memory segment size.
173 size_t const segment_size_
;
175 /// Base shared memory key for the segment.
178 /// Small table with the addresses of the shared memory segments mapped
179 /// into this address space. We need these addresses to call shmdt at
181 std::unique_ptr
<void *[]> const shm_addr_table_
;
183 /// Find the segment that contains the @a searchPtr
184 virtual int find_seg (const void *const searchPtr
,
188 /// Determine how much memory is currently in use.
189 virtual int in_use (ACE_OFF_T
&offset
, size_t &counter
);
192 ACE_Sig_Handler signal_handler_
;
194 /// Handle SIGSEGV and SIGBUS signals to remap shared memory
196 virtual int handle_signal (int, siginfo_t
*siginfo
, ucontext_t
*);
199 ACE_END_VERSIONED_NAMESPACE_DECL
201 #endif /* !ACE_LACKS_SYSV_SHMEM */
203 #include /**/ "ace/post.h"
205 #endif /* ACE_SHARED_MEMORY_POOL_H */