snappy-stubs-internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include <string>
  36. #include <assert.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #ifdef HAVE_SYS_MMAN_H
  40. #include <sys/mman.h>
  41. #endif
  42. #include "snappy-stubs-public.h"
  43. #if defined(__x86_64__)
  44. // Enable 64-bit optimized versions of some routines.
  45. #define ARCH_K8 1
  46. #endif
  47. // Needed by OS X, among others.
  48. #ifndef MAP_ANONYMOUS
  49. #define MAP_ANONYMOUS MAP_ANON
  50. #endif
  51. // The size of an array, if known at compile-time.
  52. // Will give unexpected results if used on a pointer.
  53. // We undefine it first, since some compilers already have a definition.
  54. #ifdef ARRAYSIZE
  55. #undef ARRAYSIZE
  56. #endif
  57. #define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
  58. // Static prediction hints.
  59. #ifdef HAVE_BUILTIN_EXPECT
  60. #define PREDICT_FALSE(x) (__builtin_expect(x, 0))
  61. #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  62. #else
  63. #define PREDICT_FALSE(x) x
  64. #define PREDICT_TRUE(x) x
  65. #endif
  66. // This is only used for recomputing the tag byte table used during
  67. // decompression; for simplicity we just remove it from the open-source
  68. // version (anyone who wants to regenerate it can just do the call
  69. // themselves within main()).
  70. #define DEFINE_bool(flag_name, default_value, description) \
  71. bool FLAGS_ ## flag_name = default_value
  72. #define DECLARE_bool(flag_name) \
  73. extern bool FLAGS_ ## flag_name
  74. namespace snappy {
  75. static const uint32 kuint32max = static_cast<uint32>(0xFFFFFFFF);
  76. static const int64 kint64max = static_cast<int64>(0x7FFFFFFFFFFFFFFFLL);
  77. // Potentially unaligned loads and stores.
  78. // x86 and PowerPC can simply do these loads and stores native.
  79. #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
  80. #define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
  81. #define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
  82. #define UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
  83. #define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
  84. #define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
  85. #define UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
  86. // ARMv7 and newer support native unaligned accesses, but only of 16-bit
  87. // and 32-bit values (not 64-bit); older versions either raise a fatal signal,
  88. // do an unaligned read and rotate the words around a bit, or do the reads very
  89. // slowly (trip through kernel mode). There's no simple #define that says just
  90. // “ARMv7 or higher”, so we have to filter away all ARMv5 and ARMv6
  91. // sub-architectures.
  92. //
  93. // This is a mess, but there's not much we can do about it.
  94. //
  95. // To further complicate matters, only LDR instructions (single reads) are
  96. // allowed to be unaligned, not LDRD (two reads) or LDM (many reads). Unless we
  97. // explicitly tell the compiler that these accesses can be unaligned, it can and
  98. // will combine accesses. On armcc, the way to signal this is done by accessing
  99. // through the type (uint32 __packed *), but GCC has no such attribute
  100. // (it ignores __attribute__((packed)) on individual variables). However,
  101. // we can tell it that a _struct_ is unaligned, which has the same effect,
  102. // so we do that.
  103. #elif defined(__arm__) && \
  104. !defined(__ARM_ARCH_4__) && \
  105. !defined(__ARM_ARCH_4T__) && \
  106. !defined(__ARM_ARCH_5__) && \
  107. !defined(__ARM_ARCH_5T__) && \
  108. !defined(__ARM_ARCH_5TE__) && \
  109. !defined(__ARM_ARCH_5TEJ__) && \
  110. !defined(__ARM_ARCH_6__) && \
  111. !defined(__ARM_ARCH_6J__) && \
  112. !defined(__ARM_ARCH_6K__) && \
  113. !defined(__ARM_ARCH_6Z__) && \
  114. !defined(__ARM_ARCH_6ZK__) && \
  115. !defined(__ARM_ARCH_6T2__)
  116. #if __GNUC__
  117. #define ATTRIBUTE_PACKED __attribute__((__packed__))
  118. #else
  119. #define ATTRIBUTE_PACKED
  120. #endif
  121. namespace base {
  122. namespace internal {
  123. struct Unaligned16Struct {
  124. uint16 value;
  125. uint8 dummy; // To make the size non-power-of-two.
  126. } ATTRIBUTE_PACKED;
  127. struct Unaligned32Struct {
  128. uint32 value;
  129. uint8 dummy; // To make the size non-power-of-two.
  130. } ATTRIBUTE_PACKED;
  131. } // namespace internal
  132. } // namespace base
  133. #define UNALIGNED_LOAD16(_p) \
  134. ((reinterpret_cast<const ::snappy::base::internal::Unaligned16Struct *>(_p))->value)
  135. #define UNALIGNED_LOAD32(_p) \
  136. ((reinterpret_cast<const ::snappy::base::internal::Unaligned32Struct *>(_p))->value)
  137. #define UNALIGNED_STORE16(_p, _val) \
  138. ((reinterpret_cast< ::snappy::base::internal::Unaligned16Struct *>(_p))->value = \
  139. (_val))
  140. #define UNALIGNED_STORE32(_p, _val) \
  141. ((reinterpret_cast< ::snappy::base::internal::Unaligned32Struct *>(_p))->value = \
  142. (_val))
  143. // TODO(user): NEON supports unaligned 64-bit loads and stores.
  144. // See if that would be more efficient on platforms supporting it,
  145. // at least for copies.
  146. inline uint64 UNALIGNED_LOAD64(const void *p) {
  147. uint64 t;
  148. memcpy(&t, p, sizeof t);
  149. return t;
  150. }
  151. inline void UNALIGNED_STORE64(void *p, uint64 v) {
  152. memcpy(p, &v, sizeof v);
  153. }
  154. #else
  155. // These functions are provided for architectures that don't support
  156. // unaligned loads and stores.
  157. inline uint16 UNALIGNED_LOAD16(const void *p) {
  158. uint16 t;
  159. memcpy(&t, p, sizeof t);
  160. return t;
  161. }
  162. inline uint32 UNALIGNED_LOAD32(const void *p) {
  163. uint32 t;
  164. memcpy(&t, p, sizeof t);
  165. return t;
  166. }
  167. inline uint64 UNALIGNED_LOAD64(const void *p) {
  168. uint64 t;
  169. memcpy(&t, p, sizeof t);
  170. return t;
  171. }
  172. inline void UNALIGNED_STORE16(void *p, uint16 v) {
  173. memcpy(p, &v, sizeof v);
  174. }
  175. inline void UNALIGNED_STORE32(void *p, uint32 v) {
  176. memcpy(p, &v, sizeof v);
  177. }
  178. inline void UNALIGNED_STORE64(void *p, uint64 v) {
  179. memcpy(p, &v, sizeof v);
  180. }
  181. #endif
  182. // The following guarantees declaration of the byte swap functions.
  183. #ifdef WORDS_BIGENDIAN
  184. #ifdef HAVE_SYS_BYTEORDER_H
  185. #include <sys/byteorder.h>
  186. #endif
  187. #ifdef HAVE_SYS_ENDIAN_H
  188. #include <sys/endian.h>
  189. #endif
  190. #ifdef _MSC_VER
  191. #include <stdlib.h>
  192. #define bswap_16(x) _byteswap_ushort(x)
  193. #define bswap_32(x) _byteswap_ulong(x)
  194. #define bswap_64(x) _byteswap_uint64(x)
  195. #elif defined(__APPLE__)
  196. // Mac OS X / Darwin features
  197. #include <libkern/OSByteOrder.h>
  198. #define bswap_16(x) OSSwapInt16(x)
  199. #define bswap_32(x) OSSwapInt32(x)
  200. #define bswap_64(x) OSSwapInt64(x)
  201. #elif defined(HAVE_BYTESWAP_H)
  202. #include <byteswap.h>
  203. #elif defined(bswap32)
  204. // FreeBSD defines bswap{16,32,64} in <sys/endian.h> (already #included).
  205. #define bswap_16(x) bswap16(x)
  206. #define bswap_32(x) bswap32(x)
  207. #define bswap_64(x) bswap64(x)
  208. #elif defined(BSWAP_64)
  209. // Solaris 10 defines BSWAP_{16,32,64} in <sys/byteorder.h> (already #included).
  210. #define bswap_16(x) BSWAP_16(x)
  211. #define bswap_32(x) BSWAP_32(x)
  212. #define bswap_64(x) BSWAP_64(x)
  213. #else
  214. inline uint16 bswap_16(uint16 x) {
  215. return (x << 8) | (x >> 8);
  216. }
  217. inline uint32 bswap_32(uint32 x) {
  218. x = ((x & 0xff00ff00UL) >> 8) | ((x & 0x00ff00ffUL) << 8);
  219. return (x >> 16) | (x << 16);
  220. }
  221. inline uint64 bswap_64(uint64 x) {
  222. x = ((x & 0xff00ff00ff00ff00ULL) >> 8) | ((x & 0x00ff00ff00ff00ffULL) << 8);
  223. x = ((x & 0xffff0000ffff0000ULL) >> 16) | ((x & 0x0000ffff0000ffffULL) << 16);
  224. return (x >> 32) | (x << 32);
  225. }
  226. #endif
  227. #endif // WORDS_BIGENDIAN
  228. // Convert to little-endian storage, opposite of network format.
  229. // Convert x from host to little endian: x = LittleEndian.FromHost(x);
  230. // convert x from little endian to host: x = LittleEndian.ToHost(x);
  231. //
  232. // Store values into unaligned memory converting to little endian order:
  233. // LittleEndian.Store16(p, x);
  234. //
  235. // Load unaligned values stored in little endian converting to host order:
  236. // x = LittleEndian.Load16(p);
  237. class LittleEndian {
  238. public:
  239. // Conversion functions.
  240. #ifdef WORDS_BIGENDIAN
  241. static uint16 FromHost16(uint16 x) { return bswap_16(x); }
  242. static uint16 ToHost16(uint16 x) { return bswap_16(x); }
  243. static uint32 FromHost32(uint32 x) { return bswap_32(x); }
  244. static uint32 ToHost32(uint32 x) { return bswap_32(x); }
  245. static bool IsLittleEndian() { return false; }
  246. #else // !defined(WORDS_BIGENDIAN)
  247. static uint16 FromHost16(uint16 x) { return x; }
  248. static uint16 ToHost16(uint16 x) { return x; }
  249. static uint32 FromHost32(uint32 x) { return x; }
  250. static uint32 ToHost32(uint32 x) { return x; }
  251. static bool IsLittleEndian() { return true; }
  252. #endif // !defined(WORDS_BIGENDIAN)
  253. // Functions to do unaligned loads and stores in little-endian order.
  254. static uint16 Load16(const void *p) {
  255. return ToHost16(UNALIGNED_LOAD16(p));
  256. }
  257. static void Store16(void *p, uint16 v) {
  258. UNALIGNED_STORE16(p, FromHost16(v));
  259. }
  260. static uint32 Load32(const void *p) {
  261. return ToHost32(UNALIGNED_LOAD32(p));
  262. }
  263. static void Store32(void *p, uint32 v) {
  264. UNALIGNED_STORE32(p, FromHost32(v));
  265. }
  266. };
  267. // Some bit-manipulation functions.
  268. class Bits {
  269. public:
  270. // Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
  271. static int Log2Floor(uint32 n);
  272. // Return the first set least / most significant bit, 0-indexed. Returns an
  273. // undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except
  274. // that it's 0-indexed.
  275. static int FindLSBSetNonZero(uint32 n);
  276. static int FindLSBSetNonZero64(uint64 n);
  277. private:
  278. DISALLOW_COPY_AND_ASSIGN(Bits);
  279. };
  280. #ifdef HAVE_BUILTIN_CTZ
  281. inline int Bits::Log2Floor(uint32 n) {
  282. return n == 0 ? -1 : 31 ^ __builtin_clz(n);
  283. }
  284. inline int Bits::FindLSBSetNonZero(uint32 n) {
  285. return __builtin_ctz(n);
  286. }
  287. inline int Bits::FindLSBSetNonZero64(uint64 n) {
  288. return __builtin_ctzll(n);
  289. }
  290. #else // Portable versions.
  291. inline int Bits::Log2Floor(uint32 n) {
  292. if (n == 0)
  293. return -1;
  294. int log = 0;
  295. uint32 value = n;
  296. for (int i = 4; i >= 0; --i) {
  297. int shift = (1 << i);
  298. uint32 x = value >> shift;
  299. if (x != 0) {
  300. value = x;
  301. log += shift;
  302. }
  303. }
  304. assert(value == 1);
  305. return log;
  306. }
  307. inline int Bits::FindLSBSetNonZero(uint32 n) {
  308. int rc = 31;
  309. for (int i = 4, shift = 1 << 4; i >= 0; --i) {
  310. const uint32 x = n << shift;
  311. if (x != 0) {
  312. n = x;
  313. rc -= shift;
  314. }
  315. shift >>= 1;
  316. }
  317. return rc;
  318. }
  319. // FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero().
  320. inline int Bits::FindLSBSetNonZero64(uint64 n) {
  321. const uint32 bottombits = static_cast<uint32>(n);
  322. if (bottombits == 0) {
  323. // Bottom bits are zero, so scan in top bits
  324. return 32 + FindLSBSetNonZero(static_cast<uint32>(n >> 32));
  325. } else {
  326. return FindLSBSetNonZero(bottombits);
  327. }
  328. }
  329. #endif // End portable versions.
  330. // Variable-length integer encoding.
  331. class Varint {
  332. public:
  333. // Maximum lengths of varint encoding of uint32.
  334. static const int kMax32 = 5;
  335. // Attempts to parse a varint32 from a prefix of the bytes in [ptr,limit-1].
  336. // Never reads a character at or beyond limit. If a valid/terminated varint32
  337. // was found in the range, stores it in *OUTPUT and returns a pointer just
  338. // past the last byte of the varint32. Else returns NULL. On success,
  339. // "result <= limit".
  340. static const char* Parse32WithLimit(const char* ptr, const char* limit,
  341. uint32* OUTPUT);
  342. // REQUIRES "ptr" points to a buffer of length sufficient to hold "v".
  343. // EFFECTS Encodes "v" into "ptr" and returns a pointer to the
  344. // byte just past the last encoded byte.
  345. static char* Encode32(char* ptr, uint32 v);
  346. // EFFECTS Appends the varint representation of "value" to "*s".
  347. //static void Append32(string* s, uint32 value);
  348. };
  349. inline const char* Varint::Parse32WithLimit(const char* p,
  350. const char* l,
  351. uint32* OUTPUT) {
  352. const unsigned char* ptr = reinterpret_cast<const unsigned char*>(p);
  353. const unsigned char* limit = reinterpret_cast<const unsigned char*>(l);
  354. uint32 b, result;
  355. if (ptr >= limit) return NULL;
  356. b = *(ptr++); result = b & 127; if (b < 128) goto done;
  357. if (ptr >= limit) return NULL;
  358. b = *(ptr++); result |= (b & 127) << 7; if (b < 128) goto done;
  359. if (ptr >= limit) return NULL;
  360. b = *(ptr++); result |= (b & 127) << 14; if (b < 128) goto done;
  361. if (ptr >= limit) return NULL;
  362. b = *(ptr++); result |= (b & 127) << 21; if (b < 128) goto done;
  363. if (ptr >= limit) return NULL;
  364. b = *(ptr++); result |= (b & 127) << 28; if (b < 16) goto done;
  365. return NULL; // Value is too long to be a varint32
  366. done:
  367. *OUTPUT = result;
  368. return reinterpret_cast<const char*>(ptr);
  369. }
  370. inline char* Varint::Encode32(char* sptr, uint32 v) {
  371. // Operate on characters as unsigneds
  372. unsigned char* ptr = reinterpret_cast<unsigned char*>(sptr);
  373. static const int B = 128;
  374. if (v < (1<<7)) {
  375. *(ptr++) = v;
  376. } else if (v < (1<<14)) {
  377. *(ptr++) = v | B;
  378. *(ptr++) = v>>7;
  379. } else if (v < (1<<21)) {
  380. *(ptr++) = v | B;
  381. *(ptr++) = (v>>7) | B;
  382. *(ptr++) = v>>14;
  383. } else if (v < (1<<28)) {
  384. *(ptr++) = v | B;
  385. *(ptr++) = (v>>7) | B;
  386. *(ptr++) = (v>>14) | B;
  387. *(ptr++) = v>>21;
  388. } else {
  389. *(ptr++) = v | B;
  390. *(ptr++) = (v>>7) | B;
  391. *(ptr++) = (v>>14) | B;
  392. *(ptr++) = (v>>21) | B;
  393. *(ptr++) = v>>28;
  394. }
  395. return reinterpret_cast<char*>(ptr);
  396. }
  397. // If you know the internal layout of the std::string in use, you can
  398. // replace this function with one that resizes the string without
  399. // filling the new space with zeros (if applicable) --
  400. // it will be non-portable but faster.
  401. //inline void STLStringResizeUninitialized(string* s, size_t new_size) {s->resize(new_size);}
  402. // Return a mutable char* pointing to a string's internal buffer,
  403. // which may not be null-terminated. Writing through this pointer will
  404. // modify the string.
  405. //
  406. // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
  407. // next call to a string method that invalidates iterators.
  408. //
  409. // As of 2006-04, there is no standard-blessed way of getting a
  410. // mutable reference to a string's internal buffer. However, issue 530
  411. // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530)
  412. // proposes this as the method. It will officially be part of the standard
  413. // for C++0x. This should already work on all current implementations.
  414. //inline char* string_as_array(string* str) {return str->empty() ? NULL : &*str->begin();}
  415. } // namespace snappy
  416. #endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_