Add mechanizm for caching local and peer addresses in AsyncSSLSocket.
[folly.git] / folly / GroupVarint.h
index 0af5cc48bb1708995aa87e8fd79b0fea464e3215..fb99ac8738fe998dbcf1077c87eaf33d93f04a55 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef FOLLY_GROUPVARINT_H_
 #define FOLLY_GROUPVARINT_H_
 
-#ifndef __GNUC__
-#error GroupVarint.h requires GCC
+#if !defined(__GNUC__) && !defined(_MSC_VER)
+#error GroupVarint.h requires GCC or MSVC
 #endif
 
-#if !defined(__x86_64__) && !defined(__i386__)
-#error GroupVarint.h requires x86_64 or i386
-#endif
+#include <folly/Portability.h>
+
+#if FOLLY_X64 || defined(__i386__) || FOLLY_PPC64
+#define HAVE_GROUP_VARINT 1
 
 #include <cstdint>
 #include <limits>
-#include "folly/detail/GroupVarintDetail.h"
-#include "folly/Range.h"
+#include <folly/detail/GroupVarintDetail.h>
+#include <folly/Bits.h>
+#include <folly/Range.h>
 #include <glog/logging.h>
 
-#ifdef __SSSE3__
+#if FOLLY_SSE >= 3
 #include <x86intrin.h>
 namespace folly {
 namespace detail {
@@ -133,13 +135,13 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
     uint8_t b2key = key(c);
     uint8_t b3key = key(d);
     *p++ = (b3key << 6) | (b2key << 4) | (b1key << 2) | b0key;
-    *reinterpret_cast<uint32_t*>(p) = a;
+    storeUnaligned(p, a);
     p += b0key+1;
-    *reinterpret_cast<uint32_t*>(p) = b;
+    storeUnaligned(p, b);
     p += b1key+1;
-    *reinterpret_cast<uint32_t*>(p) = c;
+    storeUnaligned(p, c);
     p += b2key+1;
-    *reinterpret_cast<uint32_t*>(p) = d;
+    storeUnaligned(p, d);
     p += b3key+1;
     return p;
   }
@@ -160,21 +162,21 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    */
   static const char* decode_simple(const char* p, uint32_t* a, uint32_t* b,
                                    uint32_t* c, uint32_t* d) {
-    size_t k = *reinterpret_cast<const uint8_t*>(p);
+    size_t k = loadUnaligned<uint8_t>(p);
     const char* end = p + detail::groupVarintLengths[k];
     ++p;
     size_t k0 = b0key(k);
-    *a = *reinterpret_cast<const uint32_t*>(p) & kMask[k0];
+    *a = loadUnaligned<uint32_t>(p) & kMask[k0];
     p += k0+1;
     size_t k1 = b1key(k);
-    *b = *reinterpret_cast<const uint32_t*>(p) & kMask[k1];
+    *b = loadUnaligned<uint32_t>(p) & kMask[k1];
     p += k1+1;
     size_t k2 = b2key(k);
-    *c = *reinterpret_cast<const uint32_t*>(p) & kMask[k2];
+    *c = loadUnaligned<uint32_t>(p) & kMask[k2];
     p += k2+1;
     size_t k3 = b3key(k);
-    *d = *reinterpret_cast<const uint32_t*>(p) & kMask[k3];
-    p += k3+1;
+    *d = loadUnaligned<uint32_t>(p) & kMask[k3];
+    // p += k3+1;
     return end;
   }
 
@@ -186,7 +188,11 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
     return decode_simple(p, dest, dest+1, dest+2, dest+3);
   }
 
-#ifdef __SSSE3__
+#if FOLLY_SSE >= 3
+  /**
+   * Just like the non-SSSE3 decode below, but with the additional constraint
+   * that we must be able to read at least 17 bytes from the input pointer, p.
+   */
   static const char* decode(const char* p, uint32_t* dest) {
     uint8_t key = p[0];
     __m128i val = _mm_loadu_si128((const __m128i*)(p+1));
@@ -196,6 +202,10 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
     return p + detail::groupVarintLengths[key];
   }
 
+  /**
+   * Just like decode_simple, but with the additional constraint that
+   * we must be able to read at least 17 bytes from the input pointer, p.
+   */
   static const char* decode(const char* p, uint32_t* a, uint32_t* b,
                             uint32_t* c, uint32_t* d) {
     uint8_t key = p[0];
@@ -204,7 +214,7 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
     __m128i r = _mm_shuffle_epi8(val, mask);
 
     // Extracting 32 bits at a time out of an XMM register is a SSE4 feature
-#ifdef __SSE4__
+#if FOLLY_SSE >= 4
     *a = _mm_extract_epi32(r, 0);
     *b = _mm_extract_epi32(r, 1);
     *c = _mm_extract_epi32(r, 2);
@@ -294,7 +304,7 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    * buffer of size bytes.
    */
   static size_t partialCount(const char* p, size_t size) {
-    uint16_t v = *reinterpret_cast<const uint16_t*>(p);
+    uint16_t v = loadUnaligned<uint16_t>(p);
     size_t s = kHeaderSize;
     s += 1 + b0key(v);
     if (s > size) return 0;
@@ -314,7 +324,7 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    * return the number of bytes used by the encoding.
    */
   static size_t encodedSize(const char* p) {
-    uint16_t n = *reinterpret_cast<const uint16_t*>(p);
+    uint16_t n = loadUnaligned<uint16_t>(p);
     return (kHeaderSize + kGroupSize +
             b0key(n) + b1key(n) + b2key(n) + b3key(n) + b4key(n));
   }
@@ -331,18 +341,19 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
     uint8_t b2key = key(c);
     uint8_t b3key = key(d);
     uint8_t b4key = key(e);
-    *reinterpret_cast<uint16_t*>(p) =
-      (b4key << 12) | (b3key << 9) | (b2key << 6) | (b1key << 3) | b0key;
+    storeUnaligned<uint16_t>(
+        p,
+        (b4key << 12) | (b3key << 9) | (b2key << 6) | (b1key << 3) | b0key);
     p += 2;
-    *reinterpret_cast<uint64_t*>(p) = a;
+    storeUnaligned(p, a);
     p += b0key+1;
-    *reinterpret_cast<uint64_t*>(p) = b;
+    storeUnaligned(p, b);
     p += b1key+1;
-    *reinterpret_cast<uint64_t*>(p) = c;
+    storeUnaligned(p, c);
     p += b2key+1;
-    *reinterpret_cast<uint64_t*>(p) = d;
+    storeUnaligned(p, d);
     p += b3key+1;
-    *reinterpret_cast<uint64_t*>(p) = e;
+    storeUnaligned(p, e);
     p += b4key+1;
     return p;
   }
@@ -363,22 +374,22 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static const char* decode(const char* p, uint64_t* a, uint64_t* b,
                             uint64_t* c, uint64_t* d, uint64_t* e) {
-    uint16_t k = *reinterpret_cast<const uint16_t*>(p);
+    uint16_t k = loadUnaligned<uint16_t>(p);
     p += 2;
     uint8_t k0 = b0key(k);
-    *a = *reinterpret_cast<const uint64_t*>(p) & kMask[k0];
+    *a = loadUnaligned<uint64_t>(p) & kMask[k0];
     p += k0+1;
     uint8_t k1 = b1key(k);
-    *b = *reinterpret_cast<const uint64_t*>(p) & kMask[k1];
+    *b = loadUnaligned<uint64_t>(p) & kMask[k1];
     p += k1+1;
     uint8_t k2 = b2key(k);
-    *c = *reinterpret_cast<const uint64_t*>(p) & kMask[k2];
+    *c = loadUnaligned<uint64_t>(p) & kMask[k2];
     p += k2+1;
     uint8_t k3 = b3key(k);
-    *d = *reinterpret_cast<const uint64_t*>(p) & kMask[k3];
+    *d = loadUnaligned<uint64_t>(p) & kMask[k3];
     p += k3+1;
     uint8_t k4 = b4key(k);
-    *e = *reinterpret_cast<const uint64_t*>(p) & kMask[k4];
+    *e = loadUnaligned<uint64_t>(p) & kMask[k4];
     p += k4+1;
     return p;
   }
@@ -501,20 +512,24 @@ class GroupVarintDecoder {
   typedef GroupVarint<T> Base;
   typedef T type;
 
-  GroupVarintDecoder() { }
+  GroupVarintDecoder() = default;
 
   explicit GroupVarintDecoder(StringPiece data,
                               size_t maxCount = (size_t)-1)
-    : p_(data.data()),
-      end_(data.data() + data.size()),
+    : rrest_(data.end()),
+      p_(data.data()),
+      end_(data.end()),
+      limit_(end_),
       pos_(0),
       count_(0),
       remaining_(maxCount) {
   }
 
-  void reset(StringPiece data, size_t maxCount=(size_t)-1) {
+  void reset(StringPiece data, size_t maxCount = (size_t)-1) {
+    rrest_ = data.end();
     p_ = data.data();
-    end_ = data.data() + data.size();
+    end_ = data.end();
+    limit_ = end_;
     pos_ = 0;
     count_ = 0;
     remaining_ = maxCount;
@@ -537,10 +552,11 @@ class GroupVarintDecoder {
       // The best way to ensure this is to ensure that data has at least
       // Base::kMaxSize - 1 bytes readable *after* the end, otherwise we'll copy
       // into a temporary buffer.
-      if (rem < Base::kMaxSize) {
+      if (limit_ - p_ < Base::kMaxSize) {
         memcpy(tmp_, p_, rem);
         p_ = tmp_;
         end_ = p_ + rem;
+        limit_ = tmp_ + sizeof(tmp_);
       }
       pos_ = 0;
       const char* n = Base::decode(p_, buf_);
@@ -578,13 +594,18 @@ class GroupVarintDecoder {
   StringPiece rest() const {
     // This is only valid after next() returned false
     CHECK(pos_ == count_ && (p_ == end_ || remaining_ == 0));
-    return StringPiece(p_, end_ - p_);
+    // p_ may point to the internal buffer (tmp_), but we want
+    // to return subpiece of the original data
+    size_t size = end_ - p_;
+    return StringPiece(rrest_ - size, rrest_);
   }
 
  private:
+  const char* rrest_;
   const char* p_;
   const char* end_;
-  char tmp_[Base::kMaxSize];
+  const char* limit_;
+  char tmp_[2 * Base::kMaxSize];
   type buf_[Base::kGroupSize];
   size_t pos_;
   size_t count_;
@@ -596,5 +617,5 @@ typedef GroupVarintDecoder<uint64_t> GroupVarint64Decoder;
 
 }  // namespace folly
 
+#endif /* FOLLY_X64 || defined(__i386__) || FOLLY_PPC64 */
 #endif /* FOLLY_GROUPVARINT_H_ */
-