snappy-stubs-internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following disclaimer
  11. // in the documentation and/or other materials provided with the
  12. // distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived from
  15. // this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Various stubs for the open-source version of Snappy.
  30. #ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
  31. #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
  32. #if HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include <stdint.h>
  36. #include <cassert>
  37. #include <cstdlib>
  38. #include <cstring>
  39. #include <limits>
  40. #include <string>
  41. #if HAVE_SYS_MMAN_H
  42. #include <sys/mman.h>
  43. #endif
  44. #if HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #if defined(_MSC_VER)
  48. #include <intrin.h>
  49. #endif // defined(_MSC_VER)
  50. #ifndef __has_feature
  51. #define __has_feature(x) 0
  52. #endif
  53. #if __has_feature(memory_sanitizer)
  54. #include <sanitizer/msan_interface.h>
  55. #define SNAPPY_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
  56. __msan_unpoison((address), (size))
  57. #else
  58. #define SNAPPY_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) /* empty */
  59. #endif // __has_feature(memory_sanitizer)
  60. #include "snappy-stubs-public.h"
  61. // Used to enable 64-bit optimized versions of some routines.
  62. #if defined(__PPC64__) || defined(__powerpc64__)
  63. #define ARCH_PPC 1
  64. #elif defined(__aarch64__) || defined(_M_ARM64)
  65. #define ARCH_ARM 1
  66. #endif
  67. // Needed by OS X, among others.
  68. #ifndef MAP_ANONYMOUS
  69. #define MAP_ANONYMOUS MAP_ANON
  70. #endif
  71. // The size of an array, if known at compile-time.
  72. // Will give unexpected results if used on a pointer.
  73. // We undefine it first, since some compilers already have a definition.
  74. #ifdef ARRAYSIZE
  75. #undef ARRAYSIZE
  76. #endif
  77. #define ARRAYSIZE(a) int{sizeof(a) / sizeof(*(a))}
  78. // Static prediction hints.
  79. #if HAVE_BUILTIN_EXPECT
  80. #define SNAPPY_PREDICT_FALSE(x) (__builtin_expect(x, 0))
  81. #define SNAPPY_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  82. #else
  83. #define SNAPPY_PREDICT_FALSE(x) x
  84. #define SNAPPY_PREDICT_TRUE(x) x
  85. #endif // HAVE_BUILTIN_EXPECT
  86. // Inlining hints.
  87. #if HAVE_ATTRIBUTE_ALWAYS_INLINE
  88. #define SNAPPY_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
  89. #else
  90. #define SNAPPY_ATTRIBUTE_ALWAYS_INLINE
  91. #endif // HAVE_ATTRIBUTE_ALWAYS_INLINE
  92. #if HAVE_BUILTIN_PREFETCH
  93. #define SNAPPY_PREFETCH(ptr) __builtin_prefetch(ptr, 0, 3)
  94. #else
  95. #define SNAPPY_PREFETCH(ptr) (void)(ptr)
  96. #endif
  97. // Stubbed version of ABSL_FLAG.
  98. //
  99. // In the open source version, flags can only be changed at compile time.
  100. #define SNAPPY_FLAG(flag_type, flag_name, default_value, help) \
  101. flag_type FLAGS_ ## flag_name = default_value
  102. namespace snappy {
  103. // Stubbed version of absl::GetFlag().
  104. template <typename T>
  105. inline T GetFlag(T flag) { return flag; }
  106. static const uint32_t kuint32max = std::numeric_limits<uint32_t>::max();
  107. static const int64_t kint64max = std::numeric_limits<int64_t>::max();
  108. // Potentially unaligned loads and stores.
  109. inline uint16_t UNALIGNED_LOAD16(const void *p) {
  110. // Compiles to a single movzx/ldrh on clang/gcc/msvc.
  111. uint16_t v;
  112. std::memcpy(&v, p, sizeof(v));
  113. return v;
  114. }
  115. inline uint32_t UNALIGNED_LOAD32(const void *p) {
  116. // Compiles to a single mov/ldr on clang/gcc/msvc.
  117. uint32_t v;
  118. std::memcpy(&v, p, sizeof(v));
  119. return v;
  120. }
  121. inline uint64_t UNALIGNED_LOAD64(const void *p) {
  122. // Compiles to a single mov/ldr on clang/gcc/msvc.
  123. uint64_t v;
  124. std::memcpy(&v, p, sizeof(v));
  125. return v;
  126. }
  127. inline void UNALIGNED_STORE16(void *p, uint16_t v) {
  128. // Compiles to a single mov/strh on clang/gcc/msvc.
  129. std::memcpy(p, &v, sizeof(v));
  130. }
  131. inline void UNALIGNED_STORE32(void *p, uint32_t v) {
  132. // Compiles to a single mov/str on clang/gcc/msvc.
  133. std::memcpy(p, &v, sizeof(v));
  134. }
  135. inline void UNALIGNED_STORE64(void *p, uint64_t v) {
  136. // Compiles to a single mov/str on clang/gcc/msvc.
  137. std::memcpy(p, &v, sizeof(v));
  138. }
  139. // Convert to little-endian storage, opposite of network format.
  140. // Convert x from host to little endian: x = LittleEndian.FromHost(x);
  141. // convert x from little endian to host: x = LittleEndian.ToHost(x);
  142. //
  143. // Store values into unaligned memory converting to little endian order:
  144. // LittleEndian.Store16(p, x);
  145. //
  146. // Load unaligned values stored in little endian converting to host order:
  147. // x = LittleEndian.Load16(p);
  148. class LittleEndian {
  149. public:
  150. // Functions to do unaligned loads and stores in little-endian order.
  151. static inline uint16_t Load16(const void *ptr) {
  152. // Compiles to a single mov/str on recent clang and gcc.
  153. #if SNAPPY_IS_BIG_ENDIAN
  154. const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
  155. return (static_cast<uint16_t>(buffer[0])) |
  156. (static_cast<uint16_t>(buffer[1]) << 8);
  157. #else
  158. // memcpy() turns into a single instruction early in the optimization
  159. // pipeline (relatively to a series of byte accesses). So, using memcpy
  160. // instead of byte accesses may lead to better decisions in more stages of
  161. // the optimization pipeline.
  162. uint16_t value;
  163. std::memcpy(&value, ptr, 2);
  164. return value;
  165. #endif
  166. }
  167. static inline uint32_t Load32(const void *ptr) {
  168. // Compiles to a single mov/str on recent clang and gcc.
  169. #if SNAPPY_IS_BIG_ENDIAN
  170. const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
  171. return (static_cast<uint32_t>(buffer[0])) |
  172. (static_cast<uint32_t>(buffer[1]) << 8) |
  173. (static_cast<uint32_t>(buffer[2]) << 16) |
  174. (static_cast<uint32_t>(buffer[3]) << 24);
  175. #else
  176. // See Load16() for the rationale of using memcpy().
  177. uint32_t value;
  178. std::memcpy(&value, ptr, 4);
  179. return value;
  180. #endif
  181. }
  182. static inline uint64_t Load64(const void *ptr) {
  183. // Compiles to a single mov/str on recent clang and gcc.
  184. #if SNAPPY_IS_BIG_ENDIAN
  185. const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
  186. return (static_cast<uint64_t>(buffer[0])) |
  187. (static_cast<uint64_t>(buffer[1]) << 8) |
  188. (static_cast<uint64_t>(buffer[2]) << 16) |
  189. (static_cast<uint64_t>(buffer[3]) << 24) |
  190. (static_cast<uint64_t>(buffer[4]) << 32) |
  191. (static_cast<uint64_t>(buffer[5]) << 40) |
  192. (static_cast<uint64_t>(buffer[6]) << 48) |
  193. (static_cast<uint64_t>(buffer[7]) << 56);
  194. #else
  195. // See Load16() for the rationale of using memcpy().
  196. uint64_t value;
  197. std::memcpy(&value, ptr, 8);
  198. return value;
  199. #endif
  200. }
  201. static inline void Store16(void *dst, uint16_t value) {
  202. // Compiles to a single mov/str on recent clang and gcc.
  203. #if SNAPPY_IS_BIG_ENDIAN
  204. uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
  205. buffer[0] = static_cast<uint8_t>(value);
  206. buffer[1] = static_cast<uint8_t>(value >> 8);
  207. #else
  208. // See Load16() for the rationale of using memcpy().
  209. std::memcpy(dst, &value, 2);
  210. #endif
  211. }
  212. static void Store32(void *dst, uint32_t value) {
  213. // Compiles to a single mov/str on recent clang and gcc.
  214. #if SNAPPY_IS_BIG_ENDIAN
  215. uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
  216. buffer[0] = static_cast<uint8_t>(value);
  217. buffer[1] = static_cast<uint8_t>(value >> 8);
  218. buffer[2] = static_cast<uint8_t>(value >> 16);
  219. buffer[3] = static_cast<uint8_t>(value >> 24);
  220. #else
  221. // See Load16() for the rationale of using memcpy().
  222. std::memcpy(dst, &value, 4);
  223. #endif
  224. }
  225. static void Store64(void* dst, uint64_t value) {
  226. // Compiles to a single mov/str on recent clang and gcc.
  227. #if SNAPPY_IS_BIG_ENDIAN
  228. uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
  229. buffer[0] = static_cast<uint8_t>(value);
  230. buffer[1] = static_cast<uint8_t>(value >> 8);
  231. buffer[2] = static_cast<uint8_t>(value >> 16);
  232. buffer[3] = static_cast<uint8_t>(value >> 24);
  233. buffer[4] = static_cast<uint8_t>(value >> 32);
  234. buffer[5] = static_cast<uint8_t>(value >> 40);
  235. buffer[6] = static_cast<uint8_t>(value >> 48);
  236. buffer[7] = static_cast<uint8_t>(value >> 56);
  237. #else
  238. // See Load16() for the rationale of using memcpy().
  239. std::memcpy(dst, &value, 8);
  240. #endif
  241. }
  242. static inline constexpr bool IsLittleEndian() {
  243. #if SNAPPY_IS_BIG_ENDIAN
  244. return false;
  245. #else
  246. return true;
  247. #endif // SNAPPY_IS_BIG_ENDIAN
  248. }
  249. };
  250. // Some bit-manipulation functions.
  251. class Bits {
  252. public:
  253. // Return floor(log2(n)) for positive integer n.
  254. static int Log2FloorNonZero(uint32_t n);
  255. // Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
  256. static int Log2Floor(uint32_t n);
  257. // Return the first set least / most significant bit, 0-indexed. Returns an
  258. // undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except
  259. // that it's 0-indexed.
  260. static int FindLSBSetNonZero(uint32_t n);
  261. static int FindLSBSetNonZero64(uint64_t n);
  262. private:
  263. // No copying
  264. Bits(const Bits&);
  265. void operator=(const Bits&);
  266. };
  267. #if HAVE_BUILTIN_CTZ
  268. inline int Bits::Log2FloorNonZero(uint32_t n) {
  269. assert(n != 0);
  270. // (31 ^ x) is equivalent to (31 - x) for x in [0, 31]. An easy proof
  271. // represents subtraction in base 2 and observes that there's no carry.
  272. //
  273. // GCC and Clang represent __builtin_clz on x86 as 31 ^ _bit_scan_reverse(x).
  274. // Using "31 ^" here instead of "31 -" allows the optimizer to strip the
  275. // function body down to _bit_scan_reverse(x).
  276. return 31 ^ __builtin_clz(n);
  277. }
  278. inline int Bits::Log2Floor(uint32_t n) {
  279. return (n == 0) ? -1 : Bits::Log2FloorNonZero(n);
  280. }
  281. inline int Bits::FindLSBSetNonZero(uint32_t n) {
  282. assert(n != 0);
  283. return __builtin_ctz(n);
  284. }
  285. #elif defined(_MSC_VER)
  286. inline int Bits::Log2FloorNonZero(uint32_t n) {
  287. assert(n != 0);
  288. // NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
  289. unsigned long where;
  290. _BitScanReverse(&where, n);
  291. return static_cast<int>(where);
  292. }
  293. inline int Bits::Log2Floor(uint32_t n) {
  294. // NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
  295. unsigned long where;
  296. if (_BitScanReverse(&where, n))
  297. return static_cast<int>(where);
  298. return -1;
  299. }
  300. inline int Bits::FindLSBSetNonZero(uint32_t n) {
  301. assert(n != 0);
  302. // NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
  303. unsigned long where;
  304. if (_BitScanForward(&where, n))
  305. return static_cast<int>(where);
  306. return 32;
  307. }
  308. #else // Portable versions.
  309. inline int Bits::Log2FloorNonZero(uint32_t n) {
  310. assert(n != 0);
  311. int log = 0;
  312. uint32_t value = n;
  313. for (int i = 4; i >= 0; --i) {
  314. int shift = (1 << i);
  315. uint32_t x = value >> shift;
  316. if (x != 0) {
  317. value = x;
  318. log += shift;
  319. }
  320. }
  321. assert(value == 1);
  322. return log;
  323. }
  324. inline int Bits::Log2Floor(uint32_t n) {
  325. return (n == 0) ? -1 : Bits::Log2FloorNonZero(n);
  326. }
  327. inline int Bits::FindLSBSetNonZero(uint32_t n) {
  328. assert(n != 0);
  329. int rc = 31;
  330. for (int i = 4, shift = 1 << 4; i >= 0; --i) {
  331. const uint32_t x = n << shift;
  332. if (x != 0) {
  333. n = x;
  334. rc -= shift;
  335. }
  336. shift >>= 1;
  337. }
  338. return rc;
  339. }
  340. #endif // End portable versions.
  341. #if HAVE_BUILTIN_CTZ
  342. inline int Bits::FindLSBSetNonZero64(uint64_t n) {
  343. assert(n != 0);
  344. return __builtin_ctzll(n);
  345. }
  346. #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
  347. // _BitScanForward64() is only available on x64 and ARM64.
  348. inline int Bits::FindLSBSetNonZero64(uint64_t n) {
  349. assert(n != 0);
  350. // NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
  351. unsigned long where;
  352. if (_BitScanForward64(&where, n))
  353. return static_cast<int>(where);
  354. return 64;
  355. }
  356. #else // Portable version.
  357. // FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero().
  358. inline int Bits::FindLSBSetNonZero64(uint64_t n) {
  359. assert(n != 0);
  360. const uint32_t bottombits = static_cast<uint32_t>(n);
  361. if (bottombits == 0) {
  362. // Bottom bits are zero, so scan the top bits.
  363. return 32 + FindLSBSetNonZero(static_cast<uint32_t>(n >> 32));
  364. } else {
  365. return FindLSBSetNonZero(bottombits);
  366. }
  367. }
  368. #endif // HAVE_BUILTIN_CTZ
  369. // Variable-length integer encoding.
  370. class Varint {
  371. public:
  372. // Maximum lengths of varint encoding of uint32_t.
  373. static const int kMax32 = 5;
  374. // Attempts to parse a varint32 from a prefix of the bytes in [ptr,limit-1].
  375. // Never reads a character at or beyond limit. If a valid/terminated varint32
  376. // was found in the range, stores it in *OUTPUT and returns a pointer just
  377. // past the last byte of the varint32. Else returns NULL. On success,
  378. // "result <= limit".
  379. static const char* Parse32WithLimit(const char* ptr, const char* limit,
  380. uint32_t* OUTPUT);
  381. // REQUIRES "ptr" points to a buffer of length sufficient to hold "v".
  382. // EFFECTS Encodes "v" into "ptr" and returns a pointer to the
  383. // byte just past the last encoded byte.
  384. static char* Encode32(char* ptr, uint32_t v);
  385. // EFFECTS Appends the varint representation of "value" to "*s".
  386. static void Append32(std::string* s, uint32_t value);
  387. };
  388. inline const char* Varint::Parse32WithLimit(const char* p,
  389. const char* l,
  390. uint32_t* OUTPUT) {
  391. const unsigned char* ptr = reinterpret_cast<const unsigned char*>(p);
  392. const unsigned char* limit = reinterpret_cast<const unsigned char*>(l);
  393. uint32_t b, result;
  394. if (ptr >= limit) return NULL;
  395. b = *(ptr++); result = b & 127; if (b < 128) goto done;
  396. if (ptr >= limit) return NULL;
  397. b = *(ptr++); result |= (b & 127) << 7; if (b < 128) goto done;
  398. if (ptr >= limit) return NULL;
  399. b = *(ptr++); result |= (b & 127) << 14; if (b < 128) goto done;
  400. if (ptr >= limit) return NULL;
  401. b = *(ptr++); result |= (b & 127) << 21; if (b < 128) goto done;
  402. if (ptr >= limit) return NULL;
  403. b = *(ptr++); result |= (b & 127) << 28; if (b < 16) goto done;
  404. return NULL; // Value is too long to be a varint32
  405. done:
  406. *OUTPUT = result;
  407. return reinterpret_cast<const char*>(ptr);
  408. }
  409. inline char* Varint::Encode32(char* sptr, uint32_t v) {
  410. // Operate on characters as unsigneds
  411. uint8_t* ptr = reinterpret_cast<uint8_t*>(sptr);
  412. static const uint8_t B = 128;
  413. if (v < (1 << 7)) {
  414. *(ptr++) = static_cast<uint8_t>(v);
  415. } else if (v < (1 << 14)) {
  416. *(ptr++) = static_cast<uint8_t>(v | B);
  417. *(ptr++) = static_cast<uint8_t>(v >> 7);
  418. } else if (v < (1 << 21)) {
  419. *(ptr++) = static_cast<uint8_t>(v | B);
  420. *(ptr++) = static_cast<uint8_t>((v >> 7) | B);
  421. *(ptr++) = static_cast<uint8_t>(v >> 14);
  422. } else if (v < (1 << 28)) {
  423. *(ptr++) = static_cast<uint8_t>(v | B);
  424. *(ptr++) = static_cast<uint8_t>((v >> 7) | B);
  425. *(ptr++) = static_cast<uint8_t>((v >> 14) | B);
  426. *(ptr++) = static_cast<uint8_t>(v >> 21);
  427. } else {
  428. *(ptr++) = static_cast<uint8_t>(v | B);
  429. *(ptr++) = static_cast<uint8_t>((v>>7) | B);
  430. *(ptr++) = static_cast<uint8_t>((v>>14) | B);
  431. *(ptr++) = static_cast<uint8_t>((v>>21) | B);
  432. *(ptr++) = static_cast<uint8_t>(v >> 28);
  433. }
  434. return reinterpret_cast<char*>(ptr);
  435. }
  436. // If you know the internal layout of the std::string in use, you can
  437. // replace this function with one that resizes the string without
  438. // filling the new space with zeros (if applicable) --
  439. // it will be non-portable but faster.
  440. inline void STLStringResizeUninitialized(std::string* s, size_t new_size) {
  441. s->resize(new_size);
  442. }
  443. // Return a mutable char* pointing to a string's internal buffer,
  444. // which may not be null-terminated. Writing through this pointer will
  445. // modify the string.
  446. //
  447. // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
  448. // next call to a string method that invalidates iterators.
  449. //
  450. // As of 2006-04, there is no standard-blessed way of getting a
  451. // mutable reference to a string's internal buffer. However, issue 530
  452. // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530)
  453. // proposes this as the method. It will officially be part of the standard
  454. // for C++0x. This should already work on all current implementations.
  455. inline char* string_as_array(std::string* str) {
  456. return str->empty() ? NULL : &*str->begin();
  457. }
  458. } // namespace snappy
  459. #endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_