BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_memoryutil.h
Go to the documentation of this file.
1/// @file bdls_memoryutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_memoryutil.h -*-C++-*-
8#ifndef INCLUDED_BDLS_MEMORYUTIL
9#define INCLUDED_BDLS_MEMORYUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_memoryutil bdls_memoryutil
15/// @brief Provide a set of portable utilities for memory manipulation.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_memoryutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_memoryutil-purpose"> Purpose</a>
25/// * <a href="#bdls_memoryutil-classes"> Classes </a>
26/// * <a href="#bdls_memoryutil-description"> Description </a>
27/// * <a href="#bdls_memoryutil-usage"> Usage </a>
28/// * <a href="#bdls_memoryutil-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdls_memoryutil-purpose}
31/// Provide a set of portable utilities for memory manipulation.
32///
33/// # Classes {#bdls_memoryutil-classes}
34///
35/// - bdls::MemoryUtil: struct which scopes memory system utilities.
36///
37/// # Description {#bdls_memoryutil-description}
38/// This component, `bdls::MemoryUtil`, defines a
39/// platform-independent interface for memory manipulation, providing utilities
40/// for querying page size, allocating/deallocating page-aligned memory, and
41/// utility to change memory protection.
42///
43/// ## Usage {#bdls_memoryutil-usage}
44///
45///
46/// This section illustrates intended use of this component.
47///
48/// ### Example 1: Basic Usage {#bdls_memoryutil-example-1-basic-usage}
49///
50///
51/// First, allocate one page of memory.
52/// @code
53/// int pageSize = bdls::MemoryUtil::pageSize();
54/// char *data = (char*)bdls::MemoryUtil::allocate(pageSize);
55/// @endcode
56/// Write into the allocated buffer.
57/// @code
58/// data[0] = '1';
59/// @endcode
60///
61/// Make the memory write protected
62/// @code
63/// bdls::MemoryUtil::protect(data, pageSize,
64/// bdls::MemoryUtil::k_ACCESS_READ);
65/// @endcode
66///
67/// Verify that data still could be read.
68/// @code
69/// assert('1' == data[0]);
70/// @endcode
71///
72/// Once again, try writing into the buffer. This should crash our process.
73/// @code
74/// data[0] = '2';
75/// @endcode
76///
77/// Restore read/write access and free the allocated memory. Actually, this
78/// will never be executed, as the process has already crashed.
79/// @code
80/// bdls::MemoryUtil::protect(data, pageSize,
81/// bdls::MemoryUtil::k_ACCESS_READ_WRITE);
82/// bdls::MemoryUtil::free(data);
83/// @endcode
84/// @}
85/** @} */
86/** @} */
87
88/** @addtogroup bdl
89 * @{
90 */
91/** @addtogroup bdls
92 * @{
93 */
94/** @addtogroup bdls_memoryutil
95 * @{
96 */
97
98#include <bdlscm_version.h>
99
100
101
102namespace bdls {
103 // =================
104 // struct MemoryUtil
105 // =================
106
108
109 // TYPES
141
142 // CLASS METHODS
143
144 /// Return the memory page size of the platform.
145 static int pageSize();
146
147 /// Change the access protection on a region of memory starting at the
148 /// specified `address` and `numBytes` long, according to specified `mode`,
149 /// making memory readable if `(mode & ACCESS_READ)` is nonzero and
150 /// writable if `(mode & ACCESS_WRITE)` is nonzero. Return 0 on success,
151 /// and a nonzero value otherwise. The behavior is undefined if `addr` is
152 /// not aligned on a page boundary, if `numBytes` is not a multiple of
153 /// `pageSize()`, or if `numBytes` is 0. Note that some platforms do not
154 /// support certain protection modes, e.g., on some platforms the memory
155 /// cannot be made writable but unreadable, or readable but non-executable.
156 /// On these platforms the actual access protection set on the region might
157 /// be more permissive than the specified one. Also note that most memory
158 /// allocators do not expect memory protection on allocated memory to be
159 /// changed, so you must reset protection back to ACCESS_READ_WRITE before
160 /// releasing the memory.
161 static int protect(void *address, int numBytes, int mode);
162
163 /// Allocate an area of memory of the specified size `numBytes`, aligned on
164 /// a page boundary. Return a pointer to allocated memory on success, and
165 /// a null pointer otherwise. Note that the allocated memory is readable
166 /// and writable, and read/write access to this memory, if revoked, must be
167 /// restored before deallocation.
168 static void *allocate(int numBytes);
169
170 /// Deallocate a memory area at the specified `address` previously
171 /// allocated with the `allocate` method. Return 0 on success, and a
172 /// nonzero value otherwise. The behavior is undefined if read or write
173 /// access to any memory in this area has been revoked and not restored.
174 /// Note that deallocating memory does not change memory protection.
175 static int deallocate(void *address);
176};
177
178} // close package namespace
179
180#endif
181
182// ----------------------------------------------------------------------------
183// Copyright 2015 Bloomberg Finance L.P.
184//
185// Licensed under the Apache License, Version 2.0 (the "License");
186// you may not use this file except in compliance with the License.
187// You may obtain a copy of the License at
188//
189// http://www.apache.org/licenses/LICENSE-2.0
190//
191// Unless required by applicable law or agreed to in writing, software
192// distributed under the License is distributed on an "AS IS" BASIS,
193// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
194// See the License for the specific language governing permissions and
195// limitations under the License.
196// ----------------------------- END-OF-FILE ----------------------------------
197
198/** @} */
199/** @} */
200/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdls_fdstreambuf.h:412
Definition bdls_memoryutil.h:107
static int deallocate(void *address)
static void * allocate(int numBytes)
static int pageSize()
Return the memory page size of the platform.
Access
Definition bdls_memoryutil.h:110
@ k_ACCESS_READ_EXECUTE
Definition bdls_memoryutil.h:116
@ BDESU_ACCESS_READ_EXECUTE
Definition bdls_memoryutil.h:128
@ BDESU_ACCESS_READ_WRITE_EXECUTE
Definition bdls_memoryutil.h:130
@ k_ACCESS_READ_WRITE_EXECUTE
Definition bdls_memoryutil.h:118
@ k_ACCESS_WRITE_EXECUTE
Definition bdls_memoryutil.h:117
@ BDESU_ACCESS_READ
Definition bdls_memoryutil.h:124
@ BDESU_ACCESS_READ_WRITE
Definition bdls_memoryutil.h:127
@ BDESU_ACCESS_WRITE
Definition bdls_memoryutil.h:125
@ k_ACCESS_READ
Definition bdls_memoryutil.h:112
@ k_ACCESS_EXECUTE
Definition bdls_memoryutil.h:114
@ BDESU_ACCESS_NONE
Definition bdls_memoryutil.h:123
@ ACCESS_READ_WRITE
Definition bdls_memoryutil.h:135
@ ACCESS_READ_WRITE_EXECUTE
Definition bdls_memoryutil.h:138
@ BDESU_ACCESS_WRITE_EXECUTE
Definition bdls_memoryutil.h:129
@ ACCESS_WRITE
Definition bdls_memoryutil.h:132
@ k_ACCESS_WRITE
Definition bdls_memoryutil.h:113
@ ACCESS_EXECUTE
Definition bdls_memoryutil.h:133
@ BDESU_ACCESS_EXECUTE
Definition bdls_memoryutil.h:126
@ ACCESS_NONE
Definition bdls_memoryutil.h:134
@ k_ACCESS_READ_WRITE
Definition bdls_memoryutil.h:115
@ ACCESS_WRITE_EXECUTE
Definition bdls_memoryutil.h:137
@ ACCESS_READ_EXECUTE
Definition bdls_memoryutil.h:136
@ k_ACCESS_NONE
Definition bdls_memoryutil.h:111
@ ACCESS_READ
Definition bdls_memoryutil.h:131
static int protect(void *address, int numBytes, int mode)