Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

493 lines
15 KiB

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