Swap a few APIs to reduce sign and implicit truncations required to work with it
authorChristopher Dykes <cdykes@fb.com>
Wed, 1 Feb 2017 01:35:51 +0000 (17:35 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 1 Feb 2017 01:47:54 +0000 (17:47 -0800)
Summary: This results in a more uniform API within Folly, allowing for a more uniform API to use outside of Folly.

Reviewed By: yfeldblum

Differential Revision: D4471471

fbshipit-source-id: f798a6498bd1a05ed12adea362ff4aedd25789ee

35 files changed:
folly/AtomicHashArray-inl.h
folly/AtomicHashArray.h
folly/AtomicHashMap.h
folly/AtomicUnorderedMap.h
folly/Benchmark.cpp
folly/ConcurrentSkipList-inl.h
folly/Conv.h
folly/DiscriminatedPtr.h
folly/Format.cpp
folly/IndexedMemPool.h
folly/SocketAddress.cpp
folly/build/GenerateFingerprintTables.cpp
folly/detail/CacheLocality.cpp
folly/detail/IPAddressSource.h
folly/detail/RangeSse42.cpp
folly/experimental/EliasFanoCoding.h
folly/fibers/test/FibersTest.cpp
folly/futures/Future-inl.h
folly/gen/Parallel-inl.h
folly/gen/String-inl.h
folly/io/Cursor.cpp
folly/io/async/AsyncSSLSocket.cpp
folly/io/async/ssl/OpenSSLUtils.cpp
folly/io/test/IOBufCursorTest.cpp
folly/io/test/IOBufQueueTest.cpp
folly/json.cpp
folly/portability/Stdio.cpp
folly/stats/BucketedTimeSeries-defs.h
folly/stats/BucketedTimeSeries.h
folly/stats/MultiLevelTimeSeries.h
folly/stats/TimeseriesHistogram.h
folly/test/DeterministicSchedule.cpp
folly/test/DeterministicSchedule.h
folly/test/IndexedMemPoolTest.cpp
folly/test/MergeTest.cpp

index 390dbb16b2b93bfa10b987cf615b55ed832379b5..d2cb08f460718ed651079f4c3bc47c52253d2b53 100644 (file)
@@ -31,7 +31,7 @@ template <class KeyT, class ValueT, class HashFcn, class EqualFcn,
 AtomicHashArray<KeyT, ValueT, HashFcn, EqualFcn,
                 Allocator, ProbeFcn, KeyConvertFcn>::
 AtomicHashArray(size_t capacity, KeyT emptyKey, KeyT lockedKey,
-                KeyT erasedKey, double _maxLoadFactor, size_t cacheSize)
+                KeyT erasedKey, double _maxLoadFactor, uint32_t cacheSize)
     : capacity_(capacity),
       maxEntries_(size_t(_maxLoadFactor * capacity_ + 0.5)),
       kEmptyKey_(emptyKey), kLockedKey_(lockedKey), kErasedKey_(erasedKey),
index 42f565606f1fd77eaa0b73e2969199e58093d36f..96a647d1a3da6bcf0e0f907273a20da1c09f7a24 100644 (file)
@@ -176,12 +176,12 @@ class AtomicHashArray : boost::noncopyable {
    *   deleter to make sure everything is cleaned up properly.
    */
   struct Config {
-    KeyT   emptyKey;
-    KeyT   lockedKey;
-    KeyT   erasedKey;
+    KeyT emptyKey;
+    KeyT lockedKey;
+    KeyT erasedKey;
     double maxLoadFactor;
     double growthFactor;
-    int    entryCountThreadCacheSize;
+    uint32_t entryCountThreadCacheSize;
     size_t capacity; // if positive, overrides maxLoadFactor
 
   public:
@@ -329,7 +329,7 @@ class AtomicHashArray : boost::noncopyable {
     numPendingEntries_.setCacheSize(newSize);
   }
 
-  int getEntryCountThreadCacheSize() const {
+  uint32_t getEntryCountThreadCacheSize() const {
     return numEntries_.getCacheSize();
   }
 
@@ -401,8 +401,13 @@ friend class AtomicHashMap<KeyT,
 
   // Force constructor/destructor private since create/destroy should be
   // used externally instead
-  AtomicHashArray(size_t capacity, KeyT emptyKey, KeyT lockedKey,
-                  KeyT erasedKey, double maxLoadFactor, size_t cacheSize);
+  AtomicHashArray(
+      size_t capacity,
+      KeyT emptyKey,
+      KeyT lockedKey,
+      KeyT erasedKey,
+      double maxLoadFactor,
+      uint32_t cacheSize);
 
   ~AtomicHashArray() = default;
 
index ef27f860df41229eb586e63ea7961783ceedcd93..70d71491a4499231f3de70377da3bf31088645a1 100644 (file)
@@ -448,7 +448,7 @@ typedef AtomicHashArray<KeyT, ValueT, HashFcn, EqualFcn,
   std::atomic<SubMap*> subMaps_[kNumSubMaps_];
   std::atomic<uint32_t> numMapsAllocated_;
 
-  inline bool tryLockMap(int idx) {
+  inline bool tryLockMap(unsigned int idx) {
     SubMap* val = nullptr;
     return subMaps_[idx].compare_exchange_strong(val, (SubMap*)kLockedPtr_,
       std::memory_order_acquire);
index 12c02579d6b2f81fd25bbdaaa6150620b5fbaca0..45877e1ce475a59b5392cc92b4e40aeaf6194f26 100644 (file)
@@ -338,8 +338,7 @@ struct AtomicUnorderedInsertMap {
   }
 
  private:
-
-  enum {
+  enum : IndexType {
     kMaxAllocationTries = 1000, // after this we throw
   };
 
@@ -437,7 +436,7 @@ struct AtomicUnorderedInsertMap {
   /// Allocates a slot and returns its index.  Tries to put it near
   /// slots_[start].
   IndexType allocateNear(IndexType start) {
-    for (auto tries = 0; tries < kMaxAllocationTries; ++tries) {
+    for (IndexType tries = 0; tries < kMaxAllocationTries; ++tries) {
       auto slot = allocationAttempt(start, tries);
       auto prev = slots_[slot].headAndState_.load(std::memory_order_acquire);
       if ((prev & 3) == EMPTY &&
index 4a96bf3697356584a1095960c5d98d039d3c1526..ecb7a8af3e0ae44a12fa867d0a7b8f1d34929a92 100644 (file)
@@ -52,7 +52,7 @@ DEFINE_int32(
 
 DEFINE_int64(
     bm_max_iters,
-    1L << 30L,
+    1 << 30,
     "Maximum # of iterations we'll try for each benchmark.");
 
 DEFINE_int32(
index cf04ee725d25579a20047c213b3a8c73a5fab209..122f21c64789e381b288be87378b44e0b6988363 100644 (file)
@@ -41,11 +41,12 @@ template<typename ValT, typename NodeT> class csl_iterator;
 
 template<typename T>
 class SkipListNode : private boost::noncopyable {
-  enum {
+  enum : uint16_t {
     IS_HEAD_NODE = 1,
     MARKED_FOR_REMOVAL = (1 << 1),
     FULLY_LINKED = (1 << 2),
   };
+
  public:
   typedef T value_type;
 
@@ -78,7 +79,7 @@ class SkipListNode : private boost::noncopyable {
   SkipListNode* copyHead(SkipListNode* node) {
     DCHECK(node != nullptr && height_ > node->height_);
     setFlags(node->getFlags());
-    for (int i = 0; i < node->height_; ++i) {
+    for (uint8_t i = 0; i < node->height_; ++i) {
       setSkip(i, node->skip(i));
     }
     return this;
index 6f98b4fabc69e71e746c2c29b7179bc7feda60ed..20cf3d51b2fe84c208014bf30732e129a211c95a 100644 (file)
@@ -1204,7 +1204,8 @@ typename std::enable_if<
     Expected<Tgt, ConversionCode>>::type
 convertTo(const Src& value) noexcept {
   /* static */ if (
-      std::numeric_limits<Tgt>::max() < std::numeric_limits<Src>::max()) {
+      folly::_t<std::make_unsigned<Tgt>>(std::numeric_limits<Tgt>::max()) <
+      folly::_t<std::make_unsigned<Src>>(std::numeric_limits<Src>::max())) {
     if (greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)) {
       return makeUnexpected(ConversionCode::ARITH_POSITIVE_OVERFLOW);
     }
@@ -1239,7 +1240,7 @@ convertTo(const Src& value) noexcept {
       return makeUnexpected(ConversionCode::ARITH_NEGATIVE_OVERFLOW);
     }
   }
-  return boost::implicit_cast<Tgt>(value);
+  return static_cast<Tgt>(value);
 }
 
 /**
index 542064bddf81c9cc85b2cada74ddfb39fc454317..7fda96b914c7815c08b323525864a2517c2a13ee 100644 (file)
@@ -190,8 +190,8 @@ class DiscriminatedPtr {
    * Get the 1-based type index of T in Types.
    */
   template <typename T>
-  size_t typeIndex() const {
-    return dptr_detail::GetTypeIndex<T, Types...>::value;
+  uint16_t typeIndex() const {
+    return uint16_t(dptr_detail::GetTypeIndex<T, Types...>::value);
   }
 
   uint16_t index() const { return data_ >> 48; }
index 11f87e9da61e64617d04fdd440c5794fa24d063e..db95ea24088f34aa4c57234cf37b90a76854253e 100644 (file)
@@ -50,14 +50,14 @@ void FormatValue<double>::formatHelper(
   }
 
   // 2+: for null terminator and optional sign shenanigans.
-  constexpr size_t bufLen =
+  constexpr int bufLen =
       2 + constexpr_max(
               2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
                   DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
               constexpr_max(8 + DoubleToStringConverter::kMaxExponentialDigits,
                             7 + DoubleToStringConverter::kMaxPrecisionDigits));
   char buf[bufLen];
-  StringBuilder builder(buf + 1, static_cast<int> (sizeof(buf) - 1));
+  StringBuilder builder(buf + 1, bufLen - 1);
 
   char plusSign;
   switch (arg.sign) {
@@ -159,7 +159,7 @@ void FormatValue<double>::formatHelper(
     prefixLen = 1;
   }
 
-  piece = fbstring(p, len);
+  piece = fbstring(p, size_t(len));
 }
 
 
index 9b373da57e03cc2f91005be8cb4f9787f485290b..1fa430823b4bc9bded795054e7bcb067dd4e2557 100644 (file)
@@ -84,12 +84,13 @@ struct IndexedMemPoolRecycler;
 /// constructed, but delays element construction.  This means that only
 /// elements that are actually returned to the caller get paged into the
 /// process's resident set (RSS).
-template <typename T,
-          int NumLocalLists_ = 32,
-          int LocalListLimit_ = 200,
-          template<typename> class Atom = std::atomic,
-          bool EagerRecycleWhenTrivial = false,
-          bool EagerRecycleWhenNotTrivial = true>
+template <
+    typename T,
+    uint32_t NumLocalLists_ = 32,
+    uint32_t LocalListLimit_ = 200,
+    template <typename> class Atom = std::atomic,
+    bool EagerRecycleWhenTrivial = false,
+    bool EagerRecycleWhenNotTrivial = true>
 struct IndexedMemPool : boost::noncopyable {
   typedef T value_type;
 
@@ -149,7 +150,7 @@ struct IndexedMemPool : boost::noncopyable {
   /// Destroys all of the contained elements
   ~IndexedMemPool() {
     if (!eagerRecycle()) {
-      for (size_t i = size_; i > 0; --i) {
+      for (uint32_t i = size_; i > 0; --i) {
         slots_[i].~Slot();
       }
     }
@@ -160,7 +161,7 @@ struct IndexedMemPool : boost::noncopyable {
   /// simultaneously allocated and not yet recycled.  Because of the
   /// local lists it is possible that more elements than this are returned
   /// successfully
-  size_t capacity() {
+  uint32_t capacity() {
     return capacityForMaxIndex(actualCapacity_);
   }
 
@@ -294,15 +295,15 @@ struct IndexedMemPool : boost::noncopyable {
 
   ////////// fields
 
+  /// the number of bytes allocated from mmap, which is a multiple of
+  /// the page size of the machine
+  size_t mmapLength_;
+
   /// the actual number of slots that we will allocate, to guarantee
   /// that we will satisfy the capacity requested at construction time.
   /// They will be numbered 1..actualCapacity_ (note the 1-based counting),
   /// and occupy slots_[1..actualCapacity_].
-  size_t actualCapacity_;
-
-  /// the number of bytes allocated from mmap, which is a multiple of
-  /// the page size of the machine
-  size_t mmapLength_;
+  uint32_t actualCapacity_;
 
   /// this records the number of slots that have actually been constructed.
   /// To allow use of atomic ++ instead of CAS, we let this overflow.
@@ -325,7 +326,7 @@ struct IndexedMemPool : boost::noncopyable {
 
   ///////////// private methods
 
-  size_t slotIndex(uint32_t idx) const {
+  uint32_t slotIndex(uint32_t idx) const {
     assert(0 < idx &&
            idx <= actualCapacity_ &&
            idx <= size_.load(std::memory_order_acquire));
index 88781dad0d1594397bdc4f0b7b4da529d95a32d4..ec30b5dee379e186ed631b497a6e57f87a54ccdd 100644 (file)
@@ -574,10 +574,10 @@ size_t SocketAddress::hash() const {
   if (external_) {
     enum { kUnixPathMax = sizeof(storage_.un.addr->sun_path) };
     const char *path = storage_.un.addr->sun_path;
-    size_t pathLength = storage_.un.pathLength();
+    auto pathLength = storage_.un.pathLength();
     // TODO: this probably could be made more efficient
-    for (unsigned int n = 0; n < pathLength; ++n) {
-      boost::hash_combine(seed, folly::hash::twang_mix64(path[n]));
+    for (off_t n = 0; n < pathLength; ++n) {
+      boost::hash_combine(seed, folly::hash::twang_mix64(uint64_t(path[n])));
     }
   }
 
@@ -707,7 +707,7 @@ void SocketAddress::updateUnixAddressLength(socklen_t addrlen) {
     // abstract namespace.  honor the specified length
   } else {
     // Call strnlen(), just in case the length was overspecified.
-    socklen_t maxLength = addrlen - offsetof(struct sockaddr_un, sun_path);
+    size_t maxLength = addrlen - offsetof(struct sockaddr_un, sun_path);
     size_t pathLength = strnlen(storage_.un.addr->sun_path, maxLength);
     storage_.un.len =
         socklen_t(offsetof(struct sockaddr_un, sun_path) + pathLength);
@@ -725,11 +725,11 @@ bool SocketAddress::operator<(const SocketAddress& other) const {
     //
     // Note that this still meets the requirements for a strict weak
     // ordering, so we can use this operator<() with standard C++ containers.
-    size_t thisPathLength = storage_.un.pathLength();
+    auto thisPathLength = storage_.un.pathLength();
     if (thisPathLength == 0) {
       return false;
     }
-    size_t otherPathLength = other.storage_.un.pathLength();
+    auto otherPathLength = other.storage_.un.pathLength();
     if (otherPathLength == 0) {
       return true;
     }
index 4911fb589ec6c52efd83ab180f783dc428924d2a..3ae3cd649ebbbc3d43905642c6bbcc886341a942 100644 (file)
@@ -71,7 +71,7 @@ void computeTables(FILE* file, const FingerprintPolynomial<DEG>& poly) {
   // where k is the number of bits in the fingerprint (and deg(P)) and
   // Q(X) = q7*X^7 + q6*X^6 + ... + q1*X + q0 is a degree-7 polyonomial
   // whose coefficients are the bits of q.
-  for (int x = 0; x < 256; x++) {
+  for (uint16_t x = 0; x < 256; x++) {
     FingerprintPolynomial<DEG> t;
     t.setHigh8Bits(uint8_t(x));
     for (int i = 0; i < 8; i++) {
index 7b9f7e9d81a5d02d400cab808e70672f5036ecc4..09da2871592999375cfa38f3b29227347678da4e 100644 (file)
@@ -156,11 +156,12 @@ CacheLocality CacheLocality::readFromSysfsTree(
               // a sub-optimal ordering, but it won't crash
               auto& lhsEquiv = equivClassesByCpu[lhs];
               auto& rhsEquiv = equivClassesByCpu[rhs];
-              for (int i = int(std::min(lhsEquiv.size(), rhsEquiv.size())) - 1;
+              for (ssize_t i = ssize_t(std::min(lhsEquiv.size(), rhsEquiv.size())) - 1;
                    i >= 0;
                    --i) {
-                if (lhsEquiv[i] != rhsEquiv[i]) {
-                  return lhsEquiv[i] < rhsEquiv[i];
+                auto idx = size_t(i);
+                if (lhsEquiv[idx] != rhsEquiv[idx]) {
+                  return lhsEquiv[idx] < rhsEquiv[idx];
                 }
               }
 
index a785c98582b7a48357160c0d71329ae34bd2d93c..53ae7b6d04a30105ffeeee2b3f082842d4e14bba 100644 (file)
@@ -92,17 +92,21 @@ struct Bytes {
       ba[byteIndex] = one[byteIndex];
       ++byteIndex;
     }
-    auto bitIndex = std::min(mask, (uint8_t)(byteIndex * 8));
+    auto bitIndex = std::min(mask, uint8_t(byteIndex * 8));
+    uint8_t bI = uint8_t(bitIndex / 8);
+    uint8_t bM = uint8_t(bitIndex % 8);
     // Compute the bit up to which the two byte arrays match in the
     // unmatched byte.
     // Here the check is bitIndex < mask since the 0th mask entry in
     // kMasks array holds the mask for masking the MSb in this byte.
     // We could instead make it hold so that no 0th entry masks no
     // bits but thats a useless iteration.
-    while (bitIndex < mask && ((one[bitIndex / 8] & kMasks[bitIndex % 8]) ==
-                               (two[bitIndex / 8] & kMasks[bitIndex % 8]))) {
-      ba[bitIndex / 8] = one[bitIndex / 8] & kMasks[bitIndex % 8];
+    while (bitIndex < mask &&
+           ((one[bI] & kMasks[bM]) == (two[bI] & kMasks[bM]))) {
+      ba[bI] = uint8_t(one[bI] & kMasks[bM]);
       ++bitIndex;
+      bI = uint8_t(bitIndex / 8);
+      bM = uint8_t(bitIndex % 8);
     }
     return {ba, bitIndex};
   }
@@ -190,7 +194,7 @@ inline void writeIntegerString(IntegralType val, char** buffer) {
   }
 
   IntegralType powerToPrint = 1;
-  for (int i = 1; i < DigitCount; ++i) {
+  for (IntegralType i = 1; i < DigitCount; ++i) {
     powerToPrint *= Base;
   }
 
index d54a4a36a8c2dd064b2eb76ff59e02b95e7c6a32..fc9a759f3cf3be984cc4c35c9a0b38747e2126a6 100644 (file)
@@ -160,7 +160,7 @@ size_t scanHaystackBlock(const StringPieceLite haystack,
   // This load is safe because needles.size() >= 16
   auto arr2 = _mm_loadu_si128(
       reinterpret_cast<const __m128i*>(needles.data()));
-  size_t b =
+  auto b =
       _mm_cmpestri(arr2, 16, arr1, int(haystack.size() - blockStartIdx), 0);
 
   size_t j = nextAlignedIndex(needles.data());
@@ -174,7 +174,7 @@ size_t scanHaystackBlock(const StringPieceLite haystack,
         arr1,
         int(haystack.size() - blockStartIdx),
         0);
-    b = std::min<size_t>(index, b);
+    b = std::min(index, b);
   }
 
   if (b < 16) {
index acc949a56359daf8616cf2db88d1079b50175234..b10e63a222f16b64fc895f913250423a9c597796 100644 (file)
@@ -580,8 +580,8 @@ class EliasFanoReader {
       return true;
     }
 
-    size_t upperValue = (value >> numLowerBits_);
-    size_t upperSkip = upperValue - upper_.value();
+    ValueType upperValue = (value >> numLowerBits_);
+    ValueType upperSkip = upperValue - upper_.value();
     // The average density of ones in upper bits is 1/2.
     // LIKELY here seems to make things worse, even for small skips.
     if (upperSkip < 2 * kLinearScanThreshold) {
index f2892c09b6a0d545695f9a9e0556a4eb2b507089..fd180173d8aab3d4d9f084617271f9fc316fbd1a 100644 (file)
@@ -292,7 +292,7 @@ TEST(FiberManager, addTasksNoncopyable) {
     if (!taskAdded) {
       manager.addTask([&]() {
         std::vector<std::function<std::unique_ptr<int>()>> funcs;
-        for (size_t i = 0; i < 3; ++i) {
+        for (int i = 0; i < 3; ++i) {
           funcs.push_back([i, &pendingFibers]() {
             await([&pendingFibers](Promise<int> promise) {
               pendingFibers.push_back(std::move(promise));
index 28935a218ff157fe1e0d1d605fc266ee31d99939..cf3e334437ca440899e7a468770e6b3494c1a09f 100644 (file)
@@ -586,7 +586,7 @@ collectAll(InputIterator first, InputIterator last) {
     typename std::iterator_traits<InputIterator>::value_type::value_type T;
 
   struct CollectAllContext {
-    CollectAllContext(int n) : results(n) {}
+    CollectAllContext(size_t n) : results(n) {}
     ~CollectAllContext() {
       p.setValue(std::move(results));
     }
@@ -622,7 +622,7 @@ struct CollectContext {
     Nothing,
     std::vector<Optional<T>>>::type;
 
-  explicit CollectContext(int n) : result(n) {}
+  explicit CollectContext(size_t n) : result(n) {}
   ~CollectContext() {
     if (!threw.exchange(true)) {
       // map Optional<T> -> T
index 18c7c4342d0a32f35c7857ca34932e2207f2fbc4..1e225c20512dfeedfb64b4463154c109866a668c 100644 (file)
@@ -49,7 +49,7 @@ class ClosableMPMCQueue {
   void openConsumer() { ++consumers_; }
 
   void closeInputProducer() {
-    int64_t producers = producers_--;
+    size_t producers = producers_--;
     CHECK(producers);
     if (producers == 1) { // last producer
       wakeConsumer_.notifyAll();
@@ -57,7 +57,7 @@ class ClosableMPMCQueue {
   }
 
   void closeOutputConsumer() {
-    int64_t consumers = consumers_--;
+    size_t consumers = consumers_--;
     CHECK(consumers);
     if (consumers == 1) { // last consumer
       wakeProducer_.notifyAll();
index 780d8f0810169edecb7b847237eea5681b0ef2be..4d6061bbc9b858d88aa02ea912a2ed292c9f3e37 100644 (file)
@@ -76,7 +76,7 @@ inline size_t splitPrefix(StringPiece& in,
   auto p = in.find_first_of(kCRLF);
   if (p != std::string::npos) {
     const auto in_start = in.data();
-    auto delim_len = 1;
+    size_t delim_len = 1;
     in.advance(p);
     // Either remove an MS-DOS CR-LF 2-byte newline, or eat 1 byte at a time.
     if (in.removePrefix(kCRLF)) {
index f2dc1c3eaf74ded078cfecde26b886ceff9e4cf9..9d51092e5ee361dcde61bd8fcfc1ef940467471f 100644 (file)
@@ -43,28 +43,30 @@ void Appender::vprintf(const char* fmt, va_list ap) {
   if (ret < 0) {
     throw std::runtime_error("error formatting printf() data");
   }
+  auto len = size_t(ret);
   // vsnprintf() returns the number of characters that would be printed,
   // not including the terminating nul.
-  if (size_t(ret) < length()) {
+  if (len < length()) {
     // All of the data was successfully written.
-    append(ret);
+    append(len);
     return;
   }
 
   // There wasn't enough room for the data.
   // Allocate more room, and then retry.
-  ensure(ret + 1);
+  ensure(len + 1);
   ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
                   fmt, apCopy);
   if (ret < 0) {
     throw std::runtime_error("error formatting printf() data");
   }
-  if (size_t(ret) >= length()) {
+  len = size_t(ret);
+  if (len >= length()) {
     // This shouldn't ever happen.
     throw std::runtime_error("unexpectedly out of buffer space on second "
                              "vsnprintf() attmept");
   }
-  append(ret);
+  append(len);
 }
 
 }}  // folly::io
index 80660352096376a4e4b3673b5a1322804123400d..60f297c2abce88f9782be4e0e63670845dc6be6e 100644 (file)
@@ -1313,7 +1313,7 @@ AsyncSSLSocket::performRead(void** buf, size_t* buflen, size_t* offset) {
       if (zero_return(error, bytes)) {
         return ReadResult(bytes);
       }
-      long errError = ERR_get_error();
+      auto errError = ERR_get_error();
       VLOG(6) << "AsyncSSLSocket(fd=" << fd_ << ", "
               << "state=" << state_ << ", "
               << "sslState=" << sslState_ << ", "
index 71ac6287aa9a6c07c5a1d183be032333699cc307..232fcc41a9bd79c88bd9e2c351c0b49de7d312ee 100644 (file)
@@ -127,7 +127,7 @@ bool OpenSSLUtils::validatePeerCertNames(X509* cert,
     if ((addr4 != nullptr || addr6 != nullptr) && name->type == GEN_IPADD) {
       // Extra const-ness for paranoia
       unsigned char const* const rawIpStr = name->d.iPAddress->data;
-      int const rawIpLen = name->d.iPAddress->length;
+      size_t const rawIpLen = size_t(name->d.iPAddress->length);
 
       if (rawIpLen == 4 && addr4 != nullptr) {
         if (::memcmp(rawIpStr, &addr4->sin_addr, rawIpLen) == 0) {
@@ -260,7 +260,11 @@ int OpenSSLUtils::getBioFd(BIO* b, int* fd) {
 
 void OpenSSLUtils::setBioFd(BIO* b, int fd, int flags) {
 #ifdef _WIN32
-  SOCKET sock = portability::sockets::fd_to_socket(fd);
+  SOCKET socket = portability::sockets::fd_to_socket(fd);
+  // Internally OpenSSL uses this as an int for reasons completely
+  // beyond any form of sanity, so we do the cast ourselves to avoid
+  // the warnings that would be generated.
+  int sock = int(socket);
 #else
   int sock = fd;
 #endif
index cae9c496baaf831d5c7660dc648dcfded769e51c..2dc1766c95be06d2b8d4c594fc040811a25e509c 100644 (file)
@@ -451,8 +451,8 @@ TEST(IOBuf, Appender) {
   append(head, "hello");
 
   Appender app(head.get(), 10);
-  uint32_t cap = head->capacity();
-  uint32_t len1 = app.length();
+  auto cap = head->capacity();
+  auto len1 = app.length();
   EXPECT_EQ(cap - 5, len1);
   app.ensure(len1);  // won't grow
   EXPECT_EQ(len1, app.length());
index 9fc35120963c622bc31c957a2b78e8371813b57e..bc2a8901aa286ce7dd76f217d6d07720d86faebd 100644 (file)
@@ -43,8 +43,7 @@ struct Initializer {
 };
 Initializer initializer;
 
-unique_ptr<IOBuf>
-stringToIOBuf(const char* s, uint32_t len) {
+unique_ptr<IOBuf> stringToIOBuf(const char* s, size_t len) {
   unique_ptr<IOBuf> buf = IOBuf::create(len);
   memcpy(buf->writableTail(), s, len);
   buf->append(len);
index 4ecb264fbd567b3283a121123d47905fb0f354d2..48ef486ebc87d3f17195f6d15c77d95726371121 100644 (file)
@@ -178,25 +178,20 @@ private:
  serialization_opts const& opts_;
 };
 
-  //////////////////////////////////////////////////////////////////////
-
-  struct ParseError : std::runtime_error {
-    explicit ParseError(int line)
-      : std::runtime_error(to<std::string>("json parse error on line ", line))
-    {}
-
-    explicit ParseError(int line, std::string const& context,
-        std::string const& expected)
-      : std::runtime_error(to<std::string>("json parse error on line ", line,
-          !context.empty() ? to<std::string>(" near `", context, '\'')
-                          : "",
-          ": ", expected))
-    {}
-
-    explicit ParseError(std::string const& msg)
-      : std::runtime_error("json parse error: " + msg)
-    {}
-  };
+//////////////////////////////////////////////////////////////////////
+
+struct ParseError : std::runtime_error {
+  explicit ParseError(
+      unsigned int line,
+      std::string const& context,
+      std::string const& expected)
+      : std::runtime_error(to<std::string>(
+            "json parse error on line ",
+            line,
+            !context.empty() ? to<std::string>(" near `", context, '\'') : "",
+            ": ",
+            expected)) {}
+};
 
 // Wraps our input buffer with some helper functions.
 struct Input {
index 1e363ee048f74b96609de74db392eade821e900b..4b3c664bcb3d0a2818ee631b435ade64c7dfe675 100755 (executable)
@@ -27,14 +27,16 @@ int dprintf(int fd, const char* fmt, ...) {
   va_start(args, fmt);
   SCOPE_EXIT { va_end(args); };
 
-  int len = vsnprintf(nullptr, 0, fmt, args);
-  if (len <= 0) {
+  int ret = vsnprintf(nullptr, 0, fmt, args);
+  if (ret <= 0) {
     return -1;
   }
+  size_t len = size_t(ret);
   char* buf = new char[len + 1];
   SCOPE_EXIT { delete[] buf; };
-  if (vsnprintf(buf, len + 1, fmt, args) == len && write(fd, buf, len) == len) {
-    return len;
+  if (size_t(vsnprintf(buf, len + 1, fmt, args)) == len &&
+      write(fd, buf, len) == ssize_t(len)) {
+    return ret;
   }
 
   return -1;
@@ -53,8 +55,8 @@ int vasprintf(char** dest, const char* format, va_list ap) {
   if (len <= 0) {
     return -1;
   }
-  char* buf = *dest = (char*)malloc(len + 1);
-  if (vsnprintf(buf, len + 1, format, ap) == len) {
+  char* buf = *dest = (char*)malloc(size_t(len + 1));
+  if (vsnprintf(buf, size_t(len + 1), format, ap) == len) {
     return len;
   }
   free(buf);
index ddb77569075e5371458bf07faf9ea69464b3951f..a24aff86e395537e0ed26e9fe03dcb78156ca620 100644 (file)
@@ -52,15 +52,15 @@ template <typename VT, typename CT>
 bool BucketedTimeSeries<VT, CT>::addValue(
     TimePoint now,
     const ValueType& val,
-    int64_t times) {
-  return addValueAggregated(now, val * times, times);
+    uint64_t times) {
+  return addValueAggregated(now, val * ValueType(times), times);
 }
 
 template <typename VT, typename CT>
 bool BucketedTimeSeries<VT, CT>::addValueAggregated(
     TimePoint now,
     const ValueType& total,
-    int64_t nsamples) {
+    uint64_t nsamples) {
   if (isAllTime()) {
     if (UNLIKELY(empty())) {
       firstTime_ = now;
index 0b531d8fa096cc31478f590069116b112749afca..14468d6175babcaf6f241d2c6ffff6ae90470ebe 100644 (file)
@@ -104,13 +104,13 @@ class BucketedTimeSeries {
   /*
    * Adds the value 'val' the given number of 'times' at time 'now'
    */
-  bool addValue(TimePoint now, const ValueType& val, int64_t times);
+  bool addValue(TimePoint now, const ValueType& val, uint64_t times);
 
   /*
    * Adds the value 'total' as the sum of 'nsamples' samples
    */
   bool
-  addValueAggregated(TimePoint now, const ValueType& total, int64_t nsamples);
+  addValueAggregated(TimePoint now, const ValueType& total, uint64_t nsamples);
 
   /*
    * Updates the container to the specified time, doing all the necessary
@@ -413,11 +413,11 @@ class BucketedTimeSeries {
   bool addValue(Duration now, const ValueType& val) {
     return addValueAggregated(TimePoint(now), val, 1);
   }
-  bool addValue(Duration now, const ValueType& val, int64_t times) {
+  bool addValue(Duration now, const ValueType& val, uint64_t times) {
     return addValueAggregated(TimePoint(now), val * ValueType(times), times);
   }
   bool
-  addValueAggregated(Duration now, const ValueType& total, int64_t nsamples) {
+  addValueAggregated(Duration now, const ValueType& total, uint64_t nsamples) {
     return addValueAggregated(TimePoint(now), total, nsamples);
   }
   size_t update(Duration now) {
index 8a84dd1c6542ff9290b16a5d0935bb2adb62c42f..10e41e59870e3608bf196cb6058123e486c47f2e 100644 (file)
@@ -432,7 +432,7 @@ class MultiLevelTimeSeries {
   // or flush() is called.
   TimePoint cachedTime_;
   ValueType cachedSum_;
-  int cachedCount_;
+  uint64_t cachedCount_;
 };
 
 } // folly
index dd286b924e86943cbdbd82fefab1f62f495f378f..5b6bc8fb2a164ad42039ee340cb55157f72bbd17 100644 (file)
@@ -135,7 +135,7 @@ class TimeseriesHistogram {
   }
 
   /* Total sum of values at the given timeseries level (all buckets). */
-  ValueType sum(int level) const {
+  ValueType sum(size_t level) const {
     ValueType total = ValueType();
     for (size_t b = 0; b < buckets_.getNumBuckets(); ++b) {
       total += buckets_.getByIndex(b).sum(level);
@@ -154,7 +154,7 @@ class TimeseriesHistogram {
 
   /* Average of values at the given timeseries level (all buckets). */
   template <typename ReturnType = double>
-  ReturnType avg(int level) const {
+  ReturnType avg(size_t level) const {
     auto total = ValueType();
     uint64_t nsamples = 0;
     computeAvgData(&total, &nsamples, level);
index a3b0e44c8a6eff1aec39d0e28c8a4561d18606ff..87ecb762b7524d9547b07729e09c4bfc7ce309c9 100644 (file)
@@ -43,7 +43,7 @@ static std::unordered_map<detail::Futex<DeterministicAtomic>*,
 static std::mutex futexLock;
 
 DeterministicSchedule::DeterministicSchedule(
-    const std::function<int(int)>& scheduler)
+    const std::function<size_t(size_t)>& scheduler)
     : scheduler_(scheduler), nextThreadId_(1), step_(0) {
   assert(tls_sem == nullptr);
   assert(tls_sched == nullptr);
@@ -63,16 +63,16 @@ DeterministicSchedule::~DeterministicSchedule() {
   beforeThreadExit();
 }
 
-std::function<int(int)> DeterministicSchedule::uniform(long seed) {
+std::function<size_t(size_t)> DeterministicSchedule::uniform(uint64_t seed) {
   auto rand = std::make_shared<std::ranlux48>(seed);
   return [rand](size_t numActive) {
-    auto dist = std::uniform_int_distribution<int>(0, numActive - 1);
+    auto dist = std::uniform_int_distribution<size_t>(0, numActive - 1);
     return dist(*rand);
   };
 }
 
 struct UniformSubset {
-  UniformSubset(long seed, int subsetSize, int stepsBetweenSelect)
+  UniformSubset(uint64_t seed, size_t subsetSize, size_t stepsBetweenSelect)
       : uniform_(DeterministicSchedule::uniform(seed)),
         subsetSize_(subsetSize),
         stepsBetweenSelect_(stepsBetweenSelect),
@@ -88,13 +88,13 @@ struct UniformSubset {
   }
 
  private:
-  std::function<int(int)> uniform_;
+  std::function<size_t(size_t)> uniform_;
   const size_t subsetSize_;
-  const int stepsBetweenSelect_;
+  const size_t stepsBetweenSelect_;
 
-  int stepsLeft_;
+  size_t stepsLeft_;
   // only the first subsetSize_ is properly randomized
-  std::vector<int> perm_;
+  std::vector<size_t> perm_;
 
   void adjustPermSize(size_t numActive) {
     if (perm_.size() > numActive) {
@@ -112,15 +112,14 @@ struct UniformSubset {
 
   void shufflePrefix() {
     for (size_t i = 0; i < std::min(perm_.size() - 1, subsetSize_); ++i) {
-      int j = uniform_(perm_.size() - i) + i;
+      size_t j = uniform_(perm_.size() - i) + i;
       std::swap(perm_[i], perm_[j]);
     }
   }
 };
 
-std::function<int(int)> DeterministicSchedule::uniformSubset(long seed,
-                                                             int n,
-                                                             int m) {
+std::function<size_t(size_t)>
+DeterministicSchedule::uniformSubset(uint64_t seed, size_t n, size_t m) {
   auto gen = std::make_shared<UniformSubset>(seed, n, m);
   return [=](size_t numActive) { return (*gen)(numActive); };
 }
@@ -148,7 +147,7 @@ void DeterministicSchedule::afterSharedAccess(bool success) {
   sem_post(sched->sems_[sched->scheduler_(sched->sems_.size())]);
 }
 
-int DeterministicSchedule::getRandNumber(int n) {
+size_t DeterministicSchedule::getRandNumber(size_t n) {
   if (tls_sched) {
     return tls_sched->scheduler_(n);
   }
index fbcee2744354f99b4c0299c6fb65c6f8e28fbaea..e4a6f95df19d141fe5e8505e15e7850d20fcf30f 100644 (file)
@@ -83,7 +83,8 @@ class DeterministicSchedule : boost::noncopyable {
    * DeterministicSchedule::thread on a thread participating in this
    * schedule) to participate in a deterministic schedule.
    */
-  explicit DeterministicSchedule(const std::function<int(int)>& scheduler);
+  explicit DeterministicSchedule(
+      const std::function<size_t(size_t)>& scheduler);
 
   /** Completes the schedule. */
   ~DeterministicSchedule();
@@ -95,7 +96,7 @@ class DeterministicSchedule : boost::noncopyable {
    * inter-thread communication are random variables following a poisson
    * distribution.
    */
-  static std::function<int(int)> uniform(long seed);
+  static std::function<size_t(size_t)> uniform(uint64_t seed);
 
   /**
    * Returns a scheduling function that chooses a subset of the active
@@ -103,9 +104,8 @@ class DeterministicSchedule : boost::noncopyable {
    * runnable thread.  The subset is chosen with size n, and the choice
    * is made every m steps.
    */
-  static std::function<int(int)> uniformSubset(long seed,
-                                               int n = 2,
-                                               int m = 64);
+  static std::function<size_t(size_t)>
+  uniformSubset(uint64_t seed, size_t n = 2, size_t m = 64);
 
   /** Obtains permission for the current thread to perform inter-thread
    *  communication. */
@@ -166,7 +166,7 @@ class DeterministicSchedule : boost::noncopyable {
 
   /** Used scheduler_ to get a random number b/w [0, n). If tls_sched is
    *  not set-up it falls back to std::rand() */
-  static int getRandNumber(int n);
+  static size_t getRandNumber(size_t n);
 
   /** Deterministic implemencation of getcpu */
   static int getcpu(unsigned* cpu, unsigned* node, void* unused);
@@ -194,7 +194,7 @@ class DeterministicSchedule : boost::noncopyable {
   static thread_local AuxAct tls_aux_act;
   static AuxChk aux_chk;
 
-  std::function<int(int)> scheduler_;
+  std::function<size_t(size_t)> scheduler_;
   std::vector<sem_t*> sems_;
   std::unordered_set<std::thread::id> active_;
   unsigned nextThreadId_;
index 109c63f515a51fc7a858d6f3c71a6763f63aa7b7..69e83124f8c4cc93c20b9a0391e29f8498adce86 100644 (file)
@@ -50,7 +50,7 @@ TEST(IndexedMemPool, unique_ptr) {
 
 TEST(IndexedMemPool, no_starvation) {
   const int count = 1000;
-  const int poolSize = 100;
+  const uint32_t poolSize = 100;
 
   typedef DeterministicSchedule Sched;
   Sched sched(Sched::uniform(0));
@@ -157,16 +157,16 @@ TEST(IndexedMemPool, locate_elem) {
 }
 
 struct NonTrivialStruct {
-  static FOLLY_TLS int count;
+  static FOLLY_TLS size_t count;
 
-  int elem_;
+  size_t elem_;
 
   NonTrivialStruct() {
     elem_ = 0;
     ++count;
   }
 
-  NonTrivialStruct(std::unique_ptr<std::string>&& arg1, int arg2) {
+  NonTrivialStruct(std::unique_ptr<std::string>&& arg1, size_t arg2) {
     elem_ = arg1->length() + arg2;
     ++count;
   }
@@ -176,7 +176,7 @@ struct NonTrivialStruct {
   }
 };
 
-FOLLY_TLS int NonTrivialStruct::count;
+FOLLY_TLS size_t NonTrivialStruct::count;
 
 TEST(IndexedMemPool, eager_recycle) {
   typedef IndexedMemPool<NonTrivialStruct> Pool;
index 16e23e1c919862f08c317f280ffaab48fce8f467..bfd5c2a1b6125781bf48dd496b837d11baec0151 100644 (file)
@@ -30,7 +30,7 @@ TEST(MergeTest, NonOverlapping) {
                b.begin(), b.end(),
                std::back_inserter(c));
   EXPECT_EQ(8, c.size());
-  for (int i = 0; i < 8; ++i) {
+  for (size_t i = 0; i < 8; ++i) {
     EXPECT_EQ(i, c[i]);
   }
 }