Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This is a convenience header to pull in the platform-specific | |
| 6 // headers that define functions for byte-order conversion, | |
| 7 // particularly: ntohs(), htons(), ntohl(), htonl(). Prefer including | |
| 8 // this file instead of directly writing the #if / #else, since it | |
| 9 // avoids duplicating the platform-specific selections. | |
| 10 // This header also provides ntohll() and htonll() for byte-order conversion | |
| 11 // for 64-bit integers. | |
| 12 | |
| 13 #ifndef BASE_SYS_BYTEORDER_H_ | |
| 14 #define BASE_SYS_BYTEORDER_H_ | |
| 15 | |
| 16 #include "build/build_config.h" | |
| 17 | |
| 18 #if defined(OS_WIN) | |
| 19 #include <winsock2.h> | |
| 20 #else | |
| 21 #include <arpa/inet.h> | |
| 22 #endif | |
| 23 | |
| 24 // Include headers to provide bswap for all platforms. | |
| 25 #if defined(COMPILER_MSVC) | |
| 26 #include <stdlib.h> | |
| 27 #elif defined(OS_MACOSX) | |
| 28 #include <libkern/OSByteOrder.h> | |
| 29 #elif defined(OS_OPENBSD) | |
| 30 #include <sys/endian.h> | |
| 31 #else | |
| 32 #include <byteswap.h> | |
| 33 #endif | |
| 34 | |
| 35 | |
| 36 namespace base { | |
| 37 | |
| 38 inline uint64_t ByteSwap(uint64_t x) { | |
| 39 #if defined(COMPILER_MSVC) | |
| 40 return _byteswap_uint64(x); | |
| 41 #elif defined(OS_MACOSX) | |
| 42 return OSSwapInt64(x); | |
| 43 #elif defined(OS_OPENBSD) | |
| 44 return swap64(x); | |
| 45 #else | |
| 46 return bswap_64(x); | |
| 47 #endif | |
| 48 } | |
| 49 | |
| 50 inline uint64_t ntohll(uint64_t x) { | |
|
brettw
2011/12/25 00:16:01
Can these have documentation? I've never heard of
Ilya Sherman
2011/12/26 08:05:26
Added some documentation. I'd prefer to keep this
| |
| 51 #if defined(ARCH_CPU_LITTLE_ENDIAN) | |
| 52 return ByteSwap(x); | |
| 53 #else | |
| 54 return x; | |
| 55 #endif | |
| 56 } | |
| 57 | |
| 58 inline uint64_t htonll(uint64_t x) { | |
| 59 #if defined(ARCH_CPU_LITTLE_ENDIAN) | |
| 60 return ByteSwap(x); | |
| 61 #else | |
| 62 return x; | |
| 63 #endif | |
| 64 } | |
| 65 | |
| 66 } // namespace base | |
| 67 | |
| 68 | |
| 69 #endif // BASE_SYS_BYTEORDER_H_ | |
| OLD | NEW |