BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_platformutil.h
Go to the documentation of this file.
1/// @file bsls_platformutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_platformutil.h -*-C++-*-
8#ifndef INCLUDED_BSLS_PLATFORMUTIL
9#define INCLUDED_BSLS_PLATFORMUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_platformutil bsls_platformutil
15/// @brief <span style="color: var(--deprecated-color-dark)">DEPRECATED:</span> Provide consistent interface for platform-dependent functionality.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_platformutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_platformutil-purpose"> Purpose</a>
25/// * <a href="#bsls_platformutil-classes"> Classes </a>
26/// * <a href="#bsls_platformutil-macros"> Macros </a>
27/// * <a href="#bsls_platformutil-description"> Description </a>
28/// * <a href="#bsls_platformutil-usage"> Usage </a>
29/// * <a href="#bsls_platformutil-types"> Types </a>
30/// * <a href="#bsls_platformutil-functions-and-macros"> Functions and Macros </a>
31///
32/// # Purpose {#bsls_platformutil-purpose}
33/// Provide consistent interface for platform-dependent functionality.
34///
35/// @deprecated Use @ref bsls_alignmentutil , @ref bsls_byteorder , @ref bsls_platform , and
36/// @ref bsls_types instead.
37///
38/// # Classes {#bsls_platformutil-classes}
39///
40/// - bsls::PlatformUtil: namespace for platform-neutral type names and API
41///
42/// # Macros {#bsls_platformutil-macros}
43///
44/// - BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN: flag set on little-endian platforms
45/// - BSLS_PLATFORMUTIL_IS_BIG_ENDIAN: flag set on big-endian platforms
46/// - BSLS_PLATFORMUTIL_HTONS(x): convert 16-bit from host to network byte order
47/// - BSLS_PLATFORMUTIL_HTONL(x): convert 32-bit from host to network byte order
48/// - BSLS_PLATFORMUTIL_NTOHS(x): convert 16-bit from network to host byte order
49/// - BSLS_PLATFORMUTIL_NTOHL(x): convert 32-bit from network to host byte order
50///
51/// @see bsls_platform, bsls_types
52///
53/// # Description {#bsls_platformutil-description}
54/// This component provides a namespace for a set of `typedef`s and
55/// functions that provide a stable, portable interface to platform-dependent
56/// functionality. In particular, this component supplies portable typenames
57/// for signed and unsigned 64-bit integers, and provides compile-time
58/// preprocessor macros that characterize the "endian-ness" of the underlying
59/// processor on the current platform. Runtime functionality to return the
60/// "endian-ness", and to round up to a multiple of the maximum required
61/// alignment for the processor, are also provided.
62///
63/// ## Usage {#bsls_platformutil-usage}
64///
65///
66/// The following illustrates how some of the types and functions supplied by
67/// this component might be used.
68///
69/// ### Types {#bsls_platformutil-types}
70///
71///
72/// `bsls::PlatformUtil::Int64` and `bsls::PlatformUtil::Uint64` identify the
73/// preferred fundamental types denoting signed and unsigned 64-bit integers,
74/// respectively:
75/// @code
76/// bsls::PlatformUtil::Uint64 stimulus = 787000000000ULL;
77/// @endcode
78/// Clients can use these types in the same way as an `int`. Clients can also
79/// mix usage with other fundamental integral types:
80/// @code
81/// bsls::PlatformUtil::Uint64 nationalDebt = 1000000000000ULL;
82/// nationalDebt += stimulus;
83///
84/// unsigned int deficitReduction = 1000000000;
85/// nationalDebt -= deficitReduction;
86///
87/// std::cout << "National Debt Level: " << nationalDebt << std::endl;
88/// @endcode
89/// `bsls::PlatformUtil::size_type` identifies the preferred integral type
90/// denoting the number of elements in a container, and the number of bytes in a
91/// single block of memory supplied by an allocator. For example, a typical use
92/// is as a `typedef` in an STL container:
93/// @code
94/// class vector {
95///
96/// // ...
97///
98/// public:
99/// typedef bsls::PlatformUtil::size_type size_type;
100///
101/// // ...
102/// };
103/// @endcode
104///
105/// ### Functions and Macros {#bsls_platformutil-functions-and-macros}
106///
107///
108/// The functions:
109/// @code
110/// bool bsls::PlatformUtil::isLittleEndian();
111/// bool bsls::PlatformUtil::isBigEndian();
112/// @endcode
113/// encapsulate the capability of determining whether a machine is big- or
114/// little-endian across all supported platforms. In addition, certain
115/// compile-time constants are also provided as preprocessor macros to
116/// facilitate conditional compilation:
117/// @code
118/// BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
119/// BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN
120/// @endcode
121/// These functions and macros are useful for writing platform-independent code,
122/// such as a function that converts the bytes in a `short` to network byte
123/// order (which is consistent with big-endian):
124/// @code
125/// short convertToNetworkByteOrder(short input)
126/// // Return the specified 'input' in network byte order.
127/// {
128/// #ifdef BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
129/// return input;
130/// #else
131/// return static_cast<short>(
132/// ((input >> 8) & 0xFF) | ((input & 0xFF) << 8));
133/// #endif
134/// }
135/// @endcode
136/// Note that in the above usage example, either the macros or the functions can
137/// be used to test whether a platform is big- or little-endian.
138/// @}
139/** @} */
140/** @} */
141
142/** @addtogroup bsl
143 * @{
144 */
145/** @addtogroup bsls
146 * @{
147 */
148/** @addtogroup bsls_platformutil
149 * @{
150 */
151
152#ifdef BDE_OPENSOURCE_PUBLICATION // DEPRECATED
153#error "bsls_platformutil is deprecated"
154#endif
155#include <bsls_alignmentutil.h>
156#include <bsls_platform.h>
157#include <bsls_types.h>
158
159#include <cstddef>
160
161
162
163namespace bsls {
164
165 // ===================
166 // struct PlatformUtil
167 // ===================
168
169/// Provide a namespace for a suite of `typedef`s and pure procedures that
170/// encapsulate, platform-dependent types and APIs.
171///
172/// See @ref bsls_platformutil
174
175 // TYPES
176
177 /// The alias `size_type` refers to the preferred type for denoting a
178 /// number of elements in either `bslma` allocators or container types.
179 ///
180 /// \note Note that this type is signed, as negative values may make sense in
181 /// certain contexts. Also note that the standard-compliant allocators
182 /// (e.g., `bsl::allocator` and `std::allocator`) use an *unsigned*
183 /// size type, but that is fine because they also have a mechanism
184 /// (@ref max_size ) to determine overflows resulting from converting from
185 /// one size type to the other.
186 ///
187 /// @deprecated Use @ref Types::size_type instead.
189
191
192 /// The aliases `UintPtr` and `IntPtr` are guaranteed to have the same
193 /// size as pointers.
194 ///
195 /// @deprecated Use @ref Types::UintPtr` and `Types::IntPtr instead.
197
199
200 /// The aliases `Int64` and `Uint64` stand for whatever type `Types`
201 /// implements for the appropriate supported platforms.
202 ///
203 /// @deprecated Use @ref Types::Int64` and `Types::Uint64 instead.
205
206#ifndef BDE_OMIT_INTERNAL_DEPRECATED
207 /// The alias `MaxAlign` refers to a type that is maximally-aligned on
208 /// the current platform.
209 ///
210 /// @deprecated Use @ref AlignmentUtil::MaxAlignedType instead.
212#endif // BDE_OMIT_INTERNAL_DEPRECATED
213
214 // CLASS METHODS
215
216 /// Return `true` if this platform is "big-endian", and `false` otherwise.
217 ///
218 /// \note Note that "big-endian" (i.e., the most significant byte
219 /// of data at the lowest byte address) is consistent with network byte
220 /// order.
221 ///
222 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_BIG_ENDIAN`
223 /// defined in @ref bsls_platform instead.
224 static bool isBigEndian();
225
226 /// Return `true` if this platform is "little-endian", and `false` otherwise.
227 ///
228 /// \note Note that "little-endian" (i.e., the least significant
229 /// byte of data at the lowest byte address) is inconsistent with
230 /// network byte order.
231 ///
232 /// @deprecated Use preprocessor macro
233 /// `BSLS_PLATFORMUTIL_IS_BIG_ENDIAN` defined in @ref bsls_platform
234 /// instead.
235 static bool isLittleEndian();
236
237 /// Return the specified `size` (in bytes) rounded up to the smallest
238 /// integral multiple of the maximum alignment.
239 ///
240 /// \pre The behavior is undefined unless `0 <= size`.
241 ///
242 /// lmf_ForwardingTypeDEPRECATED: Use `AlignmentUtil::roundUpToMaximalAlignment` instead.
243 static int roundUpToMaximalAlignment(int size);
244};
245
246} // close package namespace
247
248// ============================================================================
249// COMPILE-TIME CONSTANT PRE-PROCESSOR MACROS
250// ============================================================================
251
252// The following preprocessor macros are **DEPRECATED**. Please use their
253// replacements in @ref bsls_byteorder and @ref bsls_platform instead.
254
255#if defined(BSLS_PLATFORM_CPU_X86_64)
256 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_LITTLE_ENDIAN`
257 /// defined in @ref bsls_platform instead.
258 #define BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN BSLS_PLATFORM_IS_LITTLE_ENDIAN
259#endif
260
261#if defined(BSLS_PLATFORM_CPU_X86)
262 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_LITTLE_ENDIAN`
263 /// defined in @ref bsls_platform instead.
264 #define BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN \
265 BSLS_PLATFORM_IS_LITTLE_ENDIAN
266#endif
267
268#if defined(BSLS_PLATFORM_CPU_ARM)
269 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_LITTLE_ENDIAN`
270 /// defined in @ref bsls_platform instead.
271 #define BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN \
272 BSLS_PLATFORM_IS_LITTLE_ENDIAN
273#endif
274
275#if !defined(BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN)
276 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_BIG_ENDIAN`
277 /// defined in @ref bsls_platform instead.
278 #define BSLS_PLATFORMUTIL_IS_BIG_ENDIAN BSLS_PLATFORM_IS_BIG_ENDIAN
279#endif
280
281#ifndef BDE_OMIT_INTERNAL_DEPRECATED
282
283#if defined(BSLS_PLATFORMUTIL_IS_BIG_ENDIAN)
284#define BSLS_PLATFORMUTIL_HTONL(x) (x)
285#define BSLS_PLATFORMUTIL_HTONS(x) (x)
286#define BSLS_PLATFORMUTIL_NTOHL(x) (x)
287#define BSLS_PLATFORMUTIL_NTOHS(x) (x)
288 /// @deprecated Use preprocessor macros `BSLS_BYTEORDER_*TO*` defined in
289 /// @ref bsls_byteorder instead.
290#else
291 /// Use built-in if using gcc 4.3.
292
293#if (defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VERSION >= 40300) \
294 || defined(BSLS_PLATFORM_CMP_CLANG)
295#define BSLS_PLATFORMUTIL_NTOHL(x) (unsigned) __builtin_bswap32(x)
296#else
297#define BSLS_PLATFORMUTIL_NTOHL(x) \
298 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
299 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
300#endif
301#define BSLS_PLATFORMUTIL_HTONL(x) BSLS_PLATFORMUTIL_NTOHL(x)
302#define BSLS_PLATFORMUTIL_NTOHS(x) \
303 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
304#define BSLS_PLATFORMUTIL_HTONS(x) BSLS_PLATFORMUTIL_NTOHS(x)
305#endif
306
307#endif // BDE_OMIT_INTERNAL_DEPRECATED
308
309namespace bsls {
310
311// ============================================================================
312// INLINE FUNCTION DEFINITIONS
313// ============================================================================
314
315 // -------------------
316 // struct PlatformUtil
317 // -------------------
318
319// CLASS METHODS
320inline
322{
323#if defined(BSLS_PLATFORM_IS_LITTLE_ENDIAN)
324 return BSLS_PLATFORM_IS_LITTLE_ENDIAN;
325#else
326 return false;
327#endif
328}
329
330inline
332{
333#if defined(BSLS_PLATFORM_IS_BIG_ENDIAN)
334 return BSLS_PLATFORM_IS_BIG_ENDIAN;
335#else
336 return false;
337#endif
338}
339
340inline
342{
343 enum { BSLS_MAX_ALIGN = AlignmentUtil::BSLS_MAX_ALIGNMENT };
344 return ((size + BSLS_MAX_ALIGN - 1) / BSLS_MAX_ALIGN) * BSLS_MAX_ALIGN;
345}
346
347} // close package namespace
348
349#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
350// ============================================================================
351// BACKWARD COMPATIBILITY
352// ============================================================================
353
354/// This alias is defined for backward compatibility.
356#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
357
358
359
360#if !defined(BSL_DOUBLE_UNDERSCORE_XLAT) || 1 == BSL_DOUBLE_UNDERSCORE_XLAT
361
362#ifdef BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
363# define BSLS_PLATFORMUTIL__IS_BIG_ENDIAN BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
364#endif
365#ifdef BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN
366# define BSLS_PLATFORMUTIL__IS_LITTLE_ENDIAN BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN
367#endif
368#define BSLS_PLATFORMUTIL__HTONL(x) BSLS_PLATFORMUTIL_HTONL(x)
369#define BSLS_PLATFORMUTIL__HTONS(x) BSLS_PLATFORMUTIL_HTONS(x)
370#define BSLS_PLATFORMUTIL__NTOHL(x) BSLS_PLATFORMUTIL_NTOHL(x)
371#define BSLS_PLATFORMUTIL__NTOHS(x) BSLS_PLATFORMUTIL_NTOHS(x)
372
373#endif
374
375#endif
376
377// ----------------------------------------------------------------------------
378// Copyright 2013 Bloomberg Finance L.P.
379//
380// Licensed under the Apache License, Version 2.0 (the "License");
381// you may not use this file except in compliance with the License.
382// You may obtain a copy of the License at
383//
384// http://www.apache.org/licenses/LICENSE-2.0
385//
386// Unless required by applicable law or agreed to in writing, software
387// distributed under the License is distributed on an "AS IS" BASIS,
388// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
389// See the License for the specific language governing permissions and
390// limitations under the License.
391// ----------------------------- END-OF-FILE ----------------------------------
392
393/** @} */
394/** @} */
395/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
bsls::PlatformUtil bsls_PlatformUtil
This alias is defined for backward compatibility.
Definition bsls_platformutil.h:355
Definition bdlt_iso8601util.h:707
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:307
@ BSLS_MAX_ALIGNMENT
Definition bsls_alignmentutil.h:300
Definition bsls_platformutil.h:173
Types::Uint64 Uint64
Definition bsls_platformutil.h:204
static bool isLittleEndian()
Definition bsls_platformutil.h:321
static bool isBigEndian()
Definition bsls_platformutil.h:331
AlignmentUtil::MaxAlignedType MaxAlign
Definition bsls_platformutil.h:211
Types::Int64 Int64
Definition bsls_platformutil.h:198
Types::UintPtr UintPtr
Definition bsls_platformutil.h:190
static int roundUpToMaximalAlignment(int size)
Definition bsls_platformutil.h:341
Types::size_type size_type
Definition bsls_platformutil.h:188
Types::IntPtr IntPtr
Definition bsls_platformutil.h:196
std::size_t UintPtr
Definition bsls_types.h:128
std::size_t size_type
Definition bsls_types.h:126
unsigned long long Uint64
Definition bsls_types.h:139
std::ptrdiff_t IntPtr
Definition bsls_types.h:132
long long Int64
Definition bsls_types.h:134