EventCountTest cleanups
[folly.git] / folly / GroupVarint.h
index dc26919547da4f9fe5217408ef2c1e3ed9849a04..1115f550d064f8fd56b268621805f666fbad1842 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #error GroupVarint.h requires GCC
 #endif
 
-#if defined(__x86_64__) || defined(__i386__)
+#include <folly/Portability.h>
+
+#if FOLLY_X64 || defined(__i386__)
 #define HAVE_GROUP_VARINT 1
 
 #include <cstdint>
 #include <limits>
-#include "folly/detail/GroupVarintDetail.h"
-#include "folly/Bits.h"
-#include "folly/Range.h"
+#include <folly/detail/GroupVarintDetail.h>
+#include <folly/Bits.h>
+#include <folly/Range.h>
 #include <glog/logging.h>
 
 #ifdef __SSSE3__
@@ -509,15 +511,17 @@ class GroupVarintDecoder {
     : 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.end();
+    limit_ = end_;
     pos_ = 0;
     count_ = 0;
     remaining_ = maxCount;
@@ -540,10 +544,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_);
@@ -591,7 +596,8 @@ class GroupVarintDecoder {
   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_;
@@ -603,6 +609,6 @@ typedef GroupVarintDecoder<uint64_t> GroupVarint64Decoder;
 
 }  // namespace folly
 
-#endif /* defined(__x86_64__) || defined(__i386__) */
+#endif /* FOLLY_X64 || defined(__i386__) */
 #endif /* FOLLY_GROUPVARINT_H_ */