folly.git
8 years agoMake advanced init function for test optional
Denis Samoylov [Fri, 29 Jan 2016 18:18:01 +0000 (10:18 -0800)]
Make advanced init function for test optional

Summary: In order to avoid mandatory dependncy on elf, dwarf, unwind made new init function optional

Reviewed By: markisaa

Differential Revision: D2878545

fb-gh-sync-id: f66d3884a531dcf56fc1432330325ab45b149d7f

8 years agoExclude based on __APPLE__
Michael Lee [Fri, 29 Jan 2016 15:23:19 +0000 (07:23 -0800)]
Exclude based on __APPLE__

Summary: This was showing up on a Linux compile to my sadness.

Reviewed By: francis-ma

Differential Revision: D2876010

fb-gh-sync-id: f638d593712c0aeb50177d96aaacb26575820359

8 years agoOptimize getline(istream&, fbstring&) implementation
Giuseppe Ottaviano [Fri, 29 Jan 2016 15:02:58 +0000 (07:02 -0800)]
Optimize getline(istream&, fbstring&) implementation

Summary:
Current `getline` implementation in `fbstring` always allocates, even if the passed string is already large enough. Furthermore, the growing strategy relies on outdated assumptions about the allocator.
This implementation reuses the existing allocation as much as possible, then uses exponential growth.

Reviewed By: luciang, philippv

Differential Revision: D2871976

fb-gh-sync-id: 8db9512030be3f4953efa8f008747827504c032c

8 years agoA common init function for binaries and a default main function for tests
Denis Samoylov [Thu, 28 Jan 2016 19:18:54 +0000 (11:18 -0800)]
A common init function for binaries and a default main function for tests

Summary: Added initialization routines to test main function that can help with debugging tests

Reviewed By: markisaa, yfeldblum

Differential Revision: D2839759

fb-gh-sync-id: 71cad45f3747336c8c7f8706db139cd060e1442b

8 years agofolly: ubsan: replace undefined call through reinterpret-cast fn-pointer with std...
Lucian Grijincu [Thu, 28 Jan 2016 19:07:44 +0000 (11:07 -0800)]
folly: ubsan: replace undefined call through reinterpret-cast fn-pointer with std::function

Summary:
This code casts function pointers to void(*fn)(void*) and calls
functions through that type. Calling functions through a different
pointer type is undefined behavior. Casts were used to avoid memory
allocations.

`std::bind` needs a memory allocation - so it's avoided in EventBase.

  std::function<void()> x = std::bind(fn, args);

`std::function` should use small object optimizations when possible and embed the functor in the body of `std::function`.

On GCC 5.0 and forward small lambdas don't need memory allocations
(lambdas being tiny structures - two pointers in this case - and a
function operator).

  std::function<void()> y = [=] { fn(args); };

On GCC 4.7 .. 4.9 functors for which __is_location_invariant<Func> is
true also don't need memory allocations.

Remove undefined behavior by using a `SmallFunctor` which leverages `std::function`'s small object optimization.

Reviewed By: philippv

Differential Revision: D2864895

fb-gh-sync-id: ab40f60b3519ce38f43fecebf88ccdbf09d9bea9

8 years agoAdd (void)ssl back to SSLContext for -Wunused-parameter
Michael Lee [Thu, 28 Jan 2016 18:52:42 +0000 (10:52 -0800)]
Add (void)ssl back to SSLContext for -Wunused-parameter

Summary:
I'm not sure what accepted practice is to make the
-Wunused-parameter not balk.

Reviewed By: igorsugak

Differential Revision: D2874705

fb-gh-sync-id: 8765bae504fd90dfd8896857f4bf865ca8f64a8a

8 years agoFix unused parameter errors under -Werror
Marcus Holland-Moritz [Thu, 28 Jan 2016 16:05:32 +0000 (08:05 -0800)]
Fix unused parameter errors under -Werror

Summary:
D2872406 enables -Werror=unused-parameter, which in conjunction with mode/opt
(which eliminates asserts) causes two files in folly/io/async to fail to compile.
This change works around the error.

Reviewed By: vchalyshev

Differential Revision: D2874625

fb-gh-sync-id: 97104679f964390c5df88ee7831af7df243a152a

8 years agofolly: build with -Wunused-parameter
Igor Sugak [Thu, 28 Jan 2016 03:34:02 +0000 (19:34 -0800)]
folly: build with -Wunused-parameter

Summary: Mechanical changes (using custom clang-tidy) to fix all of the `-Wunused-parameter` violations in folly.

Reviewed By: yfeldblum

Differential Revision: D2872406

fb-gh-sync-id: bdb1941f3dadf6ab854e7a9f271f50fda93f9480

8 years agofolly/test/FBVectorTestBenchmarks.cpp.h: remove unused argument
Igor Sugak [Thu, 28 Jan 2016 03:32:21 +0000 (19:32 -0800)]
folly/test/FBVectorTestBenchmarks.cpp.h: remove unused argument

Summary: Remove unused argument `iters`.

Reviewed By: meyering

Differential Revision: D2872551

fb-gh-sync-id: 0989e5b256d4d80dd3ad02401e3fc65d0f749ba6

8 years agoFix FOLLY_TLS under macosx and clang
Francis Ma [Thu, 28 Jan 2016 00:33:36 +0000 (16:33 -0800)]
Fix FOLLY_TLS under macosx and clang

Summary: Only include guard iphone simulator and iphone devices for folly_tls

Reviewed By: ldemailly

Differential Revision: D2872383

fb-gh-sync-id: 00fb8c1ee03a97037e92d20aeda75f2435d71f5a

8 years agofolly: use -Wheader-hygiene with clang
Igor Sugak [Thu, 28 Jan 2016 00:12:47 +0000 (16:12 -0800)]
folly: use -Wheader-hygiene with clang

Summary: `-Wheader-hygiene` warns on using namespace directive in global context in header.  Fix all of the violations.

Reviewed By: yfeldblum

Differential Revision: D2867655

fb-gh-sync-id: 46840f8ece99e7af262058e631635d870bd51149

8 years agoFix EventBase destruction race in FiberManagerMap
Andrii Grynenko [Thu, 28 Jan 2016 00:08:08 +0000 (16:08 -0800)]
Fix EventBase destruction race in FiberManagerMap

Summary:
Previously we could be reading from thread-local FiberManagerMap while it was modified.
This is now fixed by keeping a per-thread list of EventBases which need to be removed from local maps. On the fast-path no action is taken, since list will be empty.

Reviewed By: yfeldblum

Differential Revision: D2853921

fb-gh-sync-id: f05e1924dd2b97bfb359537de1909bbe193e0cb9

8 years agoMake folly::detail::CacheLocality portable on apple
Francis Ma [Wed, 27 Jan 2016 22:37:05 +0000 (14:37 -0800)]
Make folly::detail::CacheLocality portable on apple

Summary:
This is one of the series steps to port folly::future on ios. Apple doesn't support __thread. Adding a HashingThreadId as a fallback
on apple.

Reviewed By: nbronson

Differential Revision: D2832068

fb-gh-sync-id: c3389245f3c0bbd36de6260680f7ac6110b3206c

8 years agofolly: ubsan: reduce vector size to avoid UBSAN timeout
Lucian Grijincu [Wed, 27 Jan 2016 22:33:34 +0000 (14:33 -0800)]
folly: ubsan: reduce vector size to avoid UBSAN timeout

Summary:
Based on diff where this was introduced in {D360195} it seems like

```
  // This value should we multiple of word size.
  static size_t const kHeapifyCapacitySize = sizeof(
    typename std::aligned_storage<
      sizeof(InternalSizeType),
      alignof(value_type)
    >::type);
  // Threshold to control capacity heapifying.
  static size_t const kHeapifyCapacityThreshold =
    100 * kHeapifyCapacitySize;
```

So anything above 100*sizeof(SizeType) should do.

Reviewed By: philippv

Differential Revision: D2871422

fb-gh-sync-id: a69e47286c53887ac05e89dab565b9d609e183a0

8 years agoImplement a generalized mechanism for pushing/popping warnings
Orvid King [Wed, 27 Jan 2016 21:57:12 +0000 (13:57 -0800)]
Implement a generalized mechanism for pushing/popping warnings

Summary: Folly synchronized requires disabling the shadow warning in a macro, but that doesn't work under MSVC, so abstract a mechansim out that allows them to be handled gracefully.

Reviewed By: yfeldblum

Differential Revision: D2870357

Pulled By: Orvid

fb-gh-sync-id: a4b0e425736ddd5293f020b360244554571d397f

8 years agoAdd mechanizm for caching local and peer addresses in AsyncSSLSocket.
Maxim Georgiev [Wed, 27 Jan 2016 21:08:50 +0000 (13:08 -0800)]
Add mechanizm for caching local and peer addresses in AsyncSSLSocket.

Summary: This change adds a flag to AsyncSSLSocket which forces the socket to cache local and remote addresses right after TCP connection is established. Cached address values will be available after the connection is closed. Caching addresses can be halpful in SSL handshake failure investigations.

Reviewed By: yfeldblum

Differential Revision: D2863274

fb-gh-sync-id: d7b415292988c2fb187a80422e8ccbf8ba8ab0e3

8 years agoFix typo in comment
Giuseppe Ottaviano [Wed, 27 Jan 2016 09:54:59 +0000 (01:54 -0800)]
Fix typo in comment

Reviewed By: yfeldblum

Differential Revision: D2869494

fb-gh-sync-id: 889957a92dd7f59c4b9564d1946e9f4058293839

8 years agoExtract endianness checks into Portability.h
Yedidya Feldblum [Wed, 27 Jan 2016 07:18:09 +0000 (23:18 -0800)]
Extract endianness checks into Portability.h

Summary: [Folly] Extract endianness checks into Portability.h.

Reviewed By: fugalh

Differential Revision: D2857924

fb-gh-sync-id: 23ecd2a3cad661024acb62769cd85df394786c59

8 years agoUse FOLLY_DEPRECATED rather than directly using GCC specific attributes.
Christopher Dykes [Tue, 26 Jan 2016 16:59:32 +0000 (08:59 -0800)]
Use FOLLY_DEPRECATED rather than directly using GCC specific attributes.

Summary: Without this MSVC can't compile.

Reviewed By: lbrandy

Differential Revision: D2856598

fb-gh-sync-id: 0e146afe844b0ce5d3782528ed9c3de53f7c8b05

8 years agoDebian DEBs build script
Arjen Roodselaar [Tue, 26 Jan 2016 01:39:11 +0000 (17:39 -0800)]
Debian DEBs build script

Summary:
This scripts uses fpm to build a somewhat reproducible set of debs which can be deployed to Ubuntu 14.04 hosts. The main library package carries the Folly major version number, allowing multiple versions to be installed alongside. The -dev deb is simply called libfolly-dev and will be upgraded as Folly moves forward. In accordance to the Debian packaging policies the shared libraries have their (debug) symbols stripped and saved to external symbol files, contained in the -dev deb.

sgolemon, yfeldblum you guys are my best guess to review this. Feel free to suggest additional folks if needed.

Reviewed By: yfeldblum

Differential Revision: D2806082

fb-gh-sync-id: 42605acccdec781f7a6b59a925121e6ed7c7cdf5

8 years agoInitialize LifoSem's padding to allow for its constexpr constructor.
Christopher Dykes [Mon, 25 Jan 2016 20:21:24 +0000 (12:21 -0800)]
Initialize LifoSem's padding to allow for its constexpr constructor.

Summary: MSVC correctly gives an error about the constexpr constructor not initializing all members.

Reviewed By: yfeldblum

Differential Revision: D2856806

fb-gh-sync-id: cef97639906dd3c39e3d3dc2ba939021e15edcb9

8 years agoThread-safe RequestContext putIfAbsent operation
Michael Bejda [Mon, 25 Jan 2016 17:45:49 +0000 (09:45 -0800)]
Thread-safe RequestContext putIfAbsent operation

Summary:
Adds a thread-safe putIfAbsent operation to the RequestContext. The current setContextData() is not sufficent to do it safely.
Just like setContextData, this method is unfair, as a high volume of reads will block the spinlock.

Reviewed By: fugalh

Differential Revision: D2850752

fb-gh-sync-id: 2ff22ea9e9bd8f27f6ae7a57214a6dbc4fdcd4c5

8 years agoavoid aggressive optimization
David Callahan [Fri, 22 Jan 2016 21:34:32 +0000 (13:34 -0800)]
avoid aggressive optimization

Summary: GCC will now dead-code eliminate the folly-based version of this test without a mechanisms to force the result to be live.

Reviewed By: ttsugriy

Differential Revision: D2854633

fb-gh-sync-id: 0e3841ed22c040fda7653bcfb5a3f19ca3d1f835

8 years agoAdd PrintTo for dynamic
Tom Jackson [Thu, 21 Jan 2016 22:19:37 +0000 (14:19 -0800)]
Add PrintTo for dynamic

Summary: Making `EXPECT_EQ(dyn1, dyn2)` easier to debug

Reviewed By: luciang, ot, yfeldblum

Differential Revision: D2848318

fb-gh-sync-id: 0c7cdd292665a493f2b792798df4e6966c1f28db

8 years agoAdd getPeerCert() to AsyncTransport.
Kyle Nekritz [Thu, 21 Jan 2016 21:54:28 +0000 (13:54 -0800)]
Add getPeerCert() to AsyncTransport.

Reviewed By: elindsey

Differential Revision: D2850760

fb-gh-sync-id: 60dbc3117e658d2fd083a87884892924bf313019

8 years agoAdding OpenSSLPtrTypes.h.
Kyle Nekritz [Thu, 21 Jan 2016 21:54:26 +0000 (13:54 -0800)]
Adding OpenSSLPtrTypes.h.

Summary:
So that these deleter and unique_ptr types don't have to be redeclared every single place they are used.
To be expanded on.

Reviewed By: mzlee

Differential Revision: D2850376

fb-gh-sync-id: e7f8bba320163b8b12a93b5cf3cd9a5921d38edc

8 years agoHaswell-specific implementation of Select64 routine.
Dmitry Pleshkov [Thu, 21 Jan 2016 06:23:30 +0000 (22:23 -0800)]
Haswell-specific implementation of Select64 routine.

Summary: k-th bit selection could be efficiently implemented via new BMI2 instruction set.

Reviewed By: ot, philippv

Differential Revision: D2843311

fb-gh-sync-id: 4c0cf52176a03422aef276ce5f677080f67f5fdf

8 years agoMove StringTest benchmarks into StringBenchmark.cpp
Michael Lee [Wed, 20 Jan 2016 15:10:31 +0000 (07:10 -0800)]
Move StringTest benchmarks into StringBenchmark.cpp

Summary: StringTest.cpp is a mix of benchmarks and normal unittests. Fixing this.

Reviewed By: yfeldblum

Differential Revision: D2840804

fb-gh-sync-id: d3efc357f5f09385e9f69b70e38b64d59045ff0d

8 years agoRemove unecessary main functions.
Michael Lee [Wed, 20 Jan 2016 15:09:04 +0000 (07:09 -0800)]
Remove unecessary main functions.

Summary: Starting with this, but I will keep digging through these. The tests will be compiled into one combined test with a single main.

Reviewed By: yfeldblum

Differential Revision: D2841391

fb-gh-sync-id: 78fd153e282f1ca2dbe7ada942dc04fc8ba5d42d

8 years agoAdd Range::erase
Alan Frindell [Tue, 19 Jan 2016 22:21:15 +0000 (14:21 -0800)]
Add Range::erase

Summary: Needed to use StringPiece with boost::algorithm::trim()

Reviewed By: yfeldblum

Differential Revision: D2833657

fb-gh-sync-id: 3430b1a2540279b2f69f04c871df3bca748f2cb1

8 years agoPrint null correctly
Alexey Spiridonov [Fri, 15 Jan 2016 20:53:27 +0000 (12:53 -0800)]
Print null correctly

Summary: Null by itself was printing as 0, now it prints as null, which is consistent with the 'pseudo json' output.

Reviewed By: yfeldblum

Differential Revision: D2789284

fb-gh-sync-id: f318b8d0f8349f4b36f868c419842fb50bee9517

8 years agoFix buck build for SSLContext
Aaron Balsara [Fri, 15 Jan 2016 20:02:40 +0000 (12:02 -0800)]
Fix buck build for SSLContext

Summary: D2800746 broke buck with an unsigned/signed compare

Reviewed By: dkgi

Differential Revision: D2835102

fb-gh-sync-id: a0b8311b38a199e089d3ed5a69b12f8f8abe38b1

8 years agoAllow SSLContext to read certificates and keys from memory
Aaron Balsara [Fri, 15 Jan 2016 18:50:33 +0000 (10:50 -0800)]
Allow SSLContext to read certificates and keys from memory

Summary: Added the ability for SSLContext to load X509 Certificates and private keys from memory

Reviewed By: yfeldblum

Differential Revision: D2800746

fb-gh-sync-id: 14cad74f8d761b9b0f07e2827b155cec9ba27f50

8 years agoDecouple future and fiber for mobile
Francis Ma [Thu, 14 Jan 2016 22:07:21 +0000 (14:07 -0800)]
Decouple future and fiber for mobile

Summary:
folly::fibers have a bunch of dependencies that are shaky on mobiles.
Decoupling it by using folly::Baton instead of folly::fibers::Baton. Should have
zero effect on fbcode cases.

Reviewed By: djwatson, yfeldblum

Differential Revision: D2821603

fb-gh-sync-id: 0ce3472c9eedf97224e8584d25e04515396cd18e

8 years agomatch new signal text from prior diff; squash colors (!)
Rachel Kroll [Thu, 14 Jan 2016 01:40:01 +0000 (17:40 -0800)]
match new signal text from prior diff; squash colors (!)

Reviewed By: yfeldblum

Differential Revision: D2828789

fb-gh-sync-id: 71e45ffe31061220e06d7e7762058f8a1501b5cc

8 years agoAdd a preprocessor guard around __STDC_LIMIT_MACROS in IOBuf.cpp
Michael Lee [Wed, 13 Jan 2016 00:22:47 +0000 (16:22 -0800)]
Add a preprocessor guard around __STDC_LIMIT_MACROS in IOBuf.cpp

Summary:
Adding the standard preprocessor guard around the `#define
__STD_LIMIT_MACROS` in IOBuf.cpp.

public

Reviewed By: knekritz

Differential Revision: D2825816

fb-gh-sync-id: 191824ec8d1f837e2b8380a2f8451972c421ceaa

8 years agoUse PRIuMAX for Android format strings.
Michael Lee [Wed, 13 Jan 2016 00:22:02 +0000 (16:22 -0800)]
Use PRIuMAX for Android format strings.

Summary: Provide a more Android compatible version of sformat.

Reviewed By: sgolemon

Differential Revision: D2825105

fb-gh-sync-id: ce328d17b9f8008d81bc7dd5bf7f0905e560dfe1

8 years agoLog pid/uid of sending process in signal handler, too
Rachel Kroll [Tue, 12 Jan 2016 22:57:55 +0000 (14:57 -0800)]
Log pid/uid of sending process in signal handler, too

Reviewed By: yfeldblum

Differential Revision: D2816450

fb-gh-sync-id: 3d3b0bf20a3f20570f847e3c5ec91a5e3786a370

8 years agoAdd security protocol trace event.
Kyle Nekritz [Mon, 11 Jan 2016 00:33:45 +0000 (16:33 -0800)]
Add security protocol trace event.

Reviewed By: siyengar

Differential Revision: D2805183

fb-gh-sync-id: 9ed320dd3762658e993daa658100d0c3c617d210

8 years agoSingeltonVault::ScopedExpunger - RAII class to clear singletons
Yedidya Feldblum [Sat, 9 Jan 2016 02:40:42 +0000 (18:40 -0800)]
SingeltonVault::ScopedExpunger - RAII class to clear singletons

Summary:
[Folly] `SingeltonVault::ScopedExpunger` - RAII class to clear singletons.

Clears all singletons in the given vault at ctor and dtor times. Useful for unit-tests that need to clear the world.

This need can arise when a unit-test needs to swap out an object used by a singleton for a test-double, but the singleton needing its dependency to be swapped has a type or a tag local to some other translation unit and unavailable in the current translation unit.

Other, better approaches to this need are "plz 2 refactor" ....

Reviewed By: andriigrynenko

Differential Revision: D2802459

fb-gh-sync-id: c24cebd3a464ed5da29ea1d9e7b86c51d61cf631

8 years agoFix Python FiberTask to properly init/reset Python thread state
Andrii Grynenko [Sat, 9 Jan 2016 01:15:21 +0000 (17:15 -0800)]
Fix Python FiberTask to properly init/reset Python thread state

Reviewed By: alikhtarov

Differential Revision: D2813735

fb-gh-sync-id: ab3c3e18618ed8bf15f478bcfca008786834e65c

8 years agofolly::Symbolizer can be enhanced for dumpStackTrace
Pallab Bhattacharya [Fri, 8 Jan 2016 04:45:32 +0000 (20:45 -0800)]
folly::Symbolizer can be enhanced for dumpStackTrace

Summary:
added functionality in Symbolizer.cpp to handle multi-segment binary and to allow symbol lookup via /proc/self/exe when some or whole text is relocated to ANON pages such as during hugification. An example of multi-segment binary : P56071432

cc markw65 - a part of the change include logic pulled from hphp/runtime/base/stack-logger.cpp:symbolize_huge_text so that hugified consumers of dumpStackTrace need not fork off.

Reviewed By: edwardc

Differential Revision: D2802837

fb-gh-sync-id: 577ab1b4ef8f22059894bfdd9c0526a22ee89ca8

8 years agoFix two fbstring operator+ overloads
Giuseppe Ottaviano [Fri, 8 Jan 2016 01:34:42 +0000 (17:34 -0800)]
Fix two fbstring operator+ overloads

Summary: `insert()` returns `fbstring` in most cases, but `iterator` (that is, `value_type*`) when the first argument is an iterator. Two overloads of `operator+` used `insert` as if it returned `fbstring`, which by chance works anyway unless the resulting string contains a `'\0'` (plus it does an extra string copy). This diff fixes the bug.

Reviewed By: philippv, luciang, Gownta

Differential Revision: D2813713

fb-gh-sync-id: 015188b72813da2dabe23980f50f00832d62aa14

8 years agoparse args for -json flag
David Callahan [Wed, 6 Jan 2016 04:27:15 +0000 (20:27 -0800)]
parse args for -json flag

Reviewed By: yfeldblum

Differential Revision: D2803911

fb-gh-sync-id: 65023cae7fd8e06c30fed100826b4b834ee2e9b1

8 years agoEnable GroupVarint on PPC64
Gustavo Serra Scalet [Tue, 5 Jan 2016 20:18:23 +0000 (12:18 -0800)]
Enable GroupVarint on PPC64

Summary:
This PR is necessary for both Folly and HHVM to compile on PPC64 (https://github.com/PPC64/hhvm) and other platforms that are not compatible with SSE instructions.

It also removes GroupVarint32 tables generator dependency on x86 platform.
Closes https://github.com/facebook/folly/pull/339

Reviewed By: mxw

Differential Revision: D2760156

fb-gh-sync-id: b4e93b54b62d6f68ccf684d34840678f677bf276

8 years agoparse args for -json flag
David Callahan [Tue, 5 Jan 2016 19:38:11 +0000 (11:38 -0800)]
parse args for -json flag

Reviewed By: lbrandy

Differential Revision: D2803778

fb-gh-sync-id: 0cb21413bb1c78d256b4408322f8d585f53cb0ac

8 years agoAdd canAdvance to cursor and tests
Neel Goyal [Tue, 5 Jan 2016 17:33:55 +0000 (09:33 -0800)]
Add canAdvance to cursor and tests

Summary:
Determine if the cursor can advance N bytes. This is useful if
applications want to check before reading so an exception isn't thrown.
It tries to walk the minimal amount of links needed in the chain.

I had a task that could have used this, though caching totalLength and
macro magic ended up being the implementation chosen. I think this just
adds to the cursor API.

Reviewed By: djwatson

Differential Revision: D2728498

fb-gh-sync-id: 8657653b82a48828cccab143653dc169ef715702

8 years agoSwitch back to SYS_gettid, and fix
Michael Lee [Mon, 4 Jan 2016 23:35:02 +0000 (15:35 -0800)]
Switch back to SYS_gettid, and fix

Summary:
SYS_gettid is different on Linux vs. OSX.  `__NR_gettid` is
only sometimes present and `SYS_gettid` is only sometimes present, but
we can pick one name and just follow that one.

Reviewed By: dcolascione

Differential Revision: D2800515

fb-gh-sync-id: 4245de4b9184ac4233ade9da297409c1031869a3

8 years agoClean up folly tests.
Michael Lee [Mon, 4 Jan 2016 17:42:21 +0000 (09:42 -0800)]
Clean up folly tests.

Summary: Clean up and remove unecessary gflags use.

Reviewed By: yfeldblum

Differential Revision: D2795904

fb-gh-sync-id: 99cccb4dc32a051b3d552b72cbc9243e20ba8127

8 years agoClose AsyncServerSocket sockets in reverse order
Giuseppe Ottaviano [Wed, 30 Dec 2015 20:41:32 +0000 (12:41 -0800)]
Close AsyncServerSocket sockets in reverse order

Summary:
When a large number of processes concurrently bind and close `AsyncServerSocket`s with `port=0` (for example in tests) binding the IPv4 socket on the same port bound with the IPv6 socket can currently fail, because sockets are closed in the same order as they are opened, which makes the following scenario possible:

```
P0: close IPv6 port
P1: open IPv6 port (succeed)
P1: open IPv4 port (FAIL)
P0: close IPv4 port
```

This diff reverses the closing order, and also fixes a couple of outdated comments.

Reviewed By: philippv

Differential Revision: D2795920

fb-gh-sync-id: 0b5157a56dfed84aed4a590c103050a2727847b3

8 years agoAdd Notification Queue Size in verbose logs of EventBase
Samrat Bhattacharya [Mon, 28 Dec 2015 23:03:16 +0000 (15:03 -0800)]
Add Notification Queue Size in verbose logs of EventBase

Summary: As above

Reviewed By: alandau

Differential Revision: D2791829

fb-gh-sync-id: 8ae5b875464dfa910b88dc12465879a9420d398c

8 years agowrapper for nullable attribute
Dominik Gabi [Wed, 23 Dec 2015 23:43:26 +0000 (15:43 -0800)]
wrapper for nullable attribute

Reviewed By: yfeldblum

Differential Revision: D2784472

fb-gh-sync-id: 84c7426cc82edabb04c662fa699764ffc0864b7e

8 years agoclang attribute wrappers
Dominik Gabi [Wed, 23 Dec 2015 23:43:24 +0000 (15:43 -0800)]
clang attribute wrappers

Summary: adding GCC compatible wrappers for clang attributes to folly.

Reviewed By: yfeldblum

Differential Revision: D2706913

fb-gh-sync-id: 17db34089d48a6a01c20904321a47ff98141c2fe

8 years agoChange `SYS_gettid` to __NR_gettid instead
Michael Lee [Wed, 23 Dec 2015 20:38:29 +0000 (12:38 -0800)]
Change `SYS_gettid` to __NR_gettid instead

Summary: SYS_gettid is not available on all platforms, but __NR_gettid should be.

Reviewed By: dcolascione

Differential Revision: D2787003

fb-gh-sync-id: ee024437ac95281a3573e2440653847f6f7d1738

8 years agoFix test Dynamic.ArrayReassignment with Clang 3.7
Giuseppe Ottaviano [Wed, 23 Dec 2015 20:07:16 +0000 (12:07 -0800)]
Fix test Dynamic.ArrayReassignment with Clang 3.7

Summary:
`dynamic` is one of the few unfortunate recursive types that have a constructor of the form `T(initializer_list<T>)`. After the Defect Report 95 (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1467) a statement `T t = {v};` where `v` is of type `T` invokes the copy constructor instead of the initializer list constructor. Clang 3.7 implements the new behavior, and GCC probably will soon.
This diff only fixes a test where the ambiguous syntax is used, but a better solution would be preferable.

Reviewed By: philippv

Differential Revision: D2786455

fb-gh-sync-id: 7fa5235e4041cbc8dd3ce685b5a0c23c343f78d2

8 years agoAdded missing instantiation for HistogramBuckets::computeTotalCount()
Mike Kolupaev [Wed, 23 Dec 2015 10:05:01 +0000 (02:05 -0800)]
Added missing instantiation for HistogramBuckets::computeTotalCount()

Summary:
D2078239 added a template method to HistogramBuckets but didn't add an instantiation for it, similar to getPercentileBucketIdx() and getPercentileEstimate() (see comment in Instantiations.cpp). This diff adds it, making `computeTotalCount()` usable without including `Histogram-defs.h`.

Also removes the weird `const` in return type.

Reviewed By: simpkins

Differential Revision: D2783534

fb-gh-sync-id: 9226489820116e0cbcb1f6a631b389439558061e

8 years agoRemove extra FLAGS_seed definition
Michael Lee [Tue, 22 Dec 2015 23:48:46 +0000 (15:48 -0800)]
Remove extra FLAGS_seed definition

Summary: This is not used within the file.  Cleaning up.

Reviewed By: yfeldblum

Differential Revision: D2784500

fb-gh-sync-id: 60270cb20c40b79f988c5536fa00e0a0f1d08e46

8 years agoMove ThreadLocal.h into the implementation
Michael Lee [Mon, 21 Dec 2015 15:55:45 +0000 (07:55 -0800)]
Move ThreadLocal.h into the implementation

Summary: Move ThreadLocal.h out of Random.h so we don't over include.

Reviewed By: yfeldblum

Differential Revision: D2770085

fb-gh-sync-id: e6934d606236c2b5cdde728639f372641ee716dc

8 years agofixed build with g++ 5+ and ASAN enabled
Kosov Eugene [Fri, 18 Dec 2015 18:17:19 +0000 (10:17 -0800)]
fixed build with g++ 5+ and ASAN enabled

Summary:
Fixing by deleting unnecessary macro checks - folly already requires at least g++4.8
Closes https://github.com/facebook/folly/pull/338

Reviewed By: yfeldblum

Differential Revision: D2742024

Pulled By: fredemmott

fb-gh-sync-id: c0ecc7edceeebd1f972b0be4f4740214211f1c24

8 years agoFix EventBaseLoopController destruction races
Andrii Grynenko [Thu, 17 Dec 2015 21:03:15 +0000 (13:03 -0800)]
Fix EventBaseLoopController destruction races

Summary:
Existing scheduleThreadSafe implementation had 2 potential races on destruction:
1. (very unlikely) insertHead is complete, but fiber loop is already running on another thread, so it finishes processing all of the fibers, destroys FiberManager or EventBase or both. By the time we get to scheduleThreadSafe EventBaseLoopController is already destoyed
2. (more likely) scheduleThreadSafe is complete, but FiberManager loop which is already running, picks complete fiber, finishes the processing. After that FiberManager may be destoyed. So when EventBase actually executes the callback FiberManager is already dead.

This solution fixes both races. Holding the alive shared_ptr when completing sheduleThreadSafe assures EventBase can't be destoyed until its completed (or it won't try to schedule anything after EventBase was destroyed). Locking alive weak_ptr in the EventBase loop callback ensures FiberManager and thus EventBaseLoopController were not destroyed yet (they can be destoyed only by the same thread which is running EventBase loop).

Reviewed By: spalamarchuk

Differential Revision: D2763206

fb-gh-sync-id: 1972d6c0c11aa931747ebdaed4029a209130f69c

8 years agoAllow override for unaligned reads in SpookyHashV2
Michael Lee [Thu, 17 Dec 2015 18:57:47 +0000 (10:57 -0800)]
Allow override for unaligned reads in SpookyHashV2

Summary: Add configuration for disabling unaligned reads in SpookyHashV2

Reviewed By: yfeldblum

Differential Revision: D2750885

fb-gh-sync-id: 9f48dcdfd5af05478a38e354f6fa0332b1332c14

8 years agoPut optimized memcpy into folly
Bin Liu [Thu, 17 Dec 2015 00:48:11 +0000 (16:48 -0800)]
Put optimized memcpy into folly

Summary: There is an optimized assembler version of memcpy that is showing 1.5% gain on TAO, add it to folly

Reviewed By: yfeldblum

Differential Revision: D2218473

fb-gh-sync-id: d5ac7f5ab30ff6febe7e94b017766c68dbd8934d

8 years agoSupport gzip in folly/io:compression
Lovro Puzar [Tue, 15 Dec 2015 16:13:53 +0000 (08:13 -0800)]
Support gzip in folly/io:compression

Summary: gzip files can be easier to work with operationally than raw zlib thanks to gzip+gunzip.  zlib supports adding/stripping the gzip header; expose that through a new CodecType.

Reviewed By: chipturner

Differential Revision: D2759554

fb-gh-sync-id: 67e0f5f88e087d363db1c349cabb909fb3f00417

8 years agoUpdate SSLContext to use folly::Random and discrete_distribution
Neel Goyal [Tue, 15 Dec 2015 14:35:14 +0000 (06:35 -0800)]
Update SSLContext to use folly::Random and discrete_distribution

Summary:
Change next pick logic to use `std::discrete_distribution`.  Use folly::Random::DefaultGenerator as the generator.
Update the TARGETS to have sslcontext depend on random (which was the build break in D2741855)

Reviewed By: siyengar

Differential Revision: D2749166

fb-gh-sync-id: 7a6b400173b1e8c69a961f9e75e97d4777d5467a

8 years agoAdd a per-socket buffer callback
Yang Chi [Mon, 14 Dec 2015 23:33:49 +0000 (15:33 -0800)]
Add a per-socket buffer callback

Summary: this is way simpler than D2623385 + D2709121. There will be a followup diff to clean the existing per-write call BufferCallback. The new one is on per-socket basis, much straightforward. I will only setup this in HTTPUpstreamSession.

Reviewed By: afrind

Differential Revision: D2723493

fb-gh-sync-id: 6b1c21a719281b9693330b6a4074f7149d7c342a

8 years agoRemove per-write buffer callback from AsyncSocket
Yang Chi [Mon, 14 Dec 2015 23:33:46 +0000 (15:33 -0800)]
Remove per-write buffer callback from AsyncSocket

Summary: Remove per-write buffer callback from AsyncSocket

Reviewed By: afrind

Differential Revision: D2733142

fb-gh-sync-id: 91214a8c833bbd479bf049c2bb72d660e0c30f50

8 years agofolly::dynamic::operator= for std::initializer_list
Louis Brandy [Fri, 11 Dec 2015 17:21:43 +0000 (09:21 -0800)]
folly::dynamic::operator= for std::initializer_list

Summary:
If we're going to have a constructor for it, we should have operator=.

Otherwise, gcc-4.9 goes via the copy constructor effectively "ignoring" the attempted nesting for e.g..

```
d = { other_dynamic };  // Should be ARRAY containing dynamic
```

NOTE: this only fixes gcc-4.9, there's still issues in clang.

Reviewed By: yfeldblum

Differential Revision: D2745180

fb-gh-sync-id: 667787c788fc7c131d8a34c608c355f5b875be50

8 years agoFix RefCountTest and RCURefCount race
Andrii Grynenko [Fri, 11 Dec 2015 02:16:51 +0000 (18:16 -0800)]
Fix RefCountTest and RCURefCount race

Reviewed By: alikhtarov

Differential Revision: D2741459

fb-gh-sync-id: c4bd068cf735ae25364edba40960096fb35e8c43

8 years agosetup signal handler with SA_ONSTACK
Philip Pronin [Fri, 11 Dec 2015 00:15:37 +0000 (16:15 -0800)]
setup signal handler with SA_ONSTACK

Summary:
By default signal handlers are run on the signaled thread's stack.
In case of stack overflow running the `SIGSEGV` signal handler on
the same stack leads to another `SIGSEGV` and crashes the program
Use `SA_ONSTACK`, so alternate stack is used (only if configured via
`sigaltstack`).

Reviewed By: luciang

Differential Revision: D2747021

fb-gh-sync-id: 48388acd6147e2919412ec32acfca1ca76f22a16

8 years agoUse SharedMutex as the default mutex type in Synchronized
Yedidya Feldblum [Thu, 10 Dec 2015 20:15:41 +0000 (12:15 -0800)]
Use SharedMutex as the default mutex type in Synchronized

Summary:
[Folly] Use `SharedMutex` as the default mutex type in `Synchronized`.

`folly::SharedMutex` is, overall, a faster mutex than `boost::shared_mutex`. Let's upgrade.

Reviewed By: nbronson

Differential Revision: D2743132

fb-gh-sync-id: e36881495e8e1002cd481607e05e555c5da19b9a

8 years agoD2741855 broke my wangle. Reverting
Neel Goyal [Thu, 10 Dec 2015 15:10:49 +0000 (07:10 -0800)]
D2741855 broke my wangle. Reverting

Summary: Revert D2741855

Reviewed By: mzlee

Differential Revision: D2744015

fb-gh-sync-id: b1e9b0a5ab95cb988d2b5c08c86139452b092465

8 years agoUnit test showing array assignment problem on gcc 4.9
Barney Moss [Thu, 10 Dec 2015 10:37:18 +0000 (02:37 -0800)]
Unit test showing array assignment problem on gcc 4.9

Summary: Unit test showing array assignment problem on gcc 4.9

Reviewed By: lbrandy

Differential Revision: D2739900

fb-gh-sync-id: 6b0fde956672b8248fbd4e620fd112195c45c646

8 years agoAdd ReadMostlySharedPtr and TLRefCount to Makefile.am
Andrii Grynenko [Thu, 10 Dec 2015 04:03:34 +0000 (20:03 -0800)]
Add ReadMostlySharedPtr and TLRefCount to Makefile.am

Reviewed By: pavlo-fb

Differential Revision: D2743230

fb-gh-sync-id: 4a52d1758a43fd103318bbe0913776340eaa6af7

8 years agoFix automake build
Arjen Roodselaar [Thu, 10 Dec 2015 01:51:27 +0000 (17:51 -0800)]
Fix automake build

Summary: There is slight drift in external deps between the TARGETS and automake files, keeping tests from linking in the 'make check' step when building folly outside of fbcode.

Reviewed By: yfeldblum

Differential Revision: D2740482

fb-gh-sync-id: ae6243f9bb112a7d2680017b3d577a1f23ef4f60

8 years agoUpdate SSLContext to use discrete_distribution
Neel Goyal [Thu, 10 Dec 2015 00:21:19 +0000 (16:21 -0800)]
Update SSLContext to use discrete_distribution

Summary: Update the protocol pick logic to use discrete_distribution

Reviewed By: siyengar

Differential Revision: D2741855

fb-gh-sync-id: 244bd087124a7a9584a1108fe8f8150093275878

8 years agoSwitch folly::Singleton to ReadMostlySharedPtr
Andrii Grynenko [Wed, 9 Dec 2015 23:09:05 +0000 (15:09 -0800)]
Switch folly::Singleton to ReadMostlySharedPtr

Summary:
This also introduces a new try_get_fast() API which returns ReadMostlySharedPtr.
We should eventually migrate all users onto this API.

Reviewed By: elsteveogrande

Differential Revision: D2706745

fb-gh-sync-id: d558d705f431fc6147334c188fd0c6156644ba5c

8 years agoAdd support for ALPN
Alan Frindell [Wed, 9 Dec 2015 19:55:40 +0000 (11:55 -0800)]
Add support for ALPN

Summary: With openssl-1.0.2 and later add support for ALPN.  Clients can request NPN only, but the default is to support either (client will send ALPN list, server will send NPN advertisement if ALPN is not negotiated).

Reviewed By: siyengar

Differential Revision: D2710441

fb-gh-sync-id: a8efe69e1869bbecb4ed9e0a513448fcfdb21ca6

8 years agonotification queue read -> readNoInt
Shijin Kong [Tue, 8 Dec 2015 23:29:19 +0000 (15:29 -0800)]
notification queue read -> readNoInt

Summary: EINTR should be retried

Reviewed By: afrind

Differential Revision: D2735879

fb-gh-sync-id: 6f25cd26e29ab8e7e13a3d63f24bf2d104c14809

8 years agoAvoid cost of cancelAll
Chad Parry [Tue, 8 Dec 2015 22:37:00 +0000 (14:37 -0800)]
Avoid cost of cancelAll

Summary:
This is an experimental diff that may improve the performance of `HHWheelTimer::cancelAll`. If there are no timers to cancel, then it can bail early.

Since perflab is unlikely to be conclusive, and since this diff looks like a strict improvement, I'll proceed with landing it.

Reviewed By: djwatson

Differential Revision: D2735297

fb-gh-sync-id: 9f5a811ee6d9fa9434576e9abd3ef3443a7579b7

8 years agoFix TLRefCount race around thread local destruction and fix RefCount unit test
Andrii Grynenko [Tue, 8 Dec 2015 20:51:40 +0000 (12:51 -0800)]
Fix TLRefCount race around thread local destruction and fix RefCount unit test

Reviewed By: pavlo-fb

Differential Revision: D2708425

fb-gh-sync-id: 665d077210503df4f4e8aa8f88ce5b9b277582f3

8 years agoUpdate zstd to 0.4.2
Chip Turner [Tue, 8 Dec 2015 15:52:02 +0000 (07:52 -0800)]
Update zstd to 0.4.2

Summary: New version.

Reviewed By: cyan

Differential Revision: D2730844

fb-gh-sync-id: 4305bdfba2e8d25eba295bbf3f13a140c0d04a1c

8 years agoMark some methods virtual on SSL Context.
Neel Goyal [Tue, 8 Dec 2015 13:18:02 +0000 (05:18 -0800)]
Mark some methods virtual on SSL Context.

Summary:
Mark some methods as virtual.  This makes writing some
better unit tests on the associated task possible.

Reviewed By: siyengar

Differential Revision: D2730126

fb-gh-sync-id: f79f7632e873cb218bb49883d9b53beea7bb96e7

8 years agoadd type info to broken promise what()
John Sherwood [Mon, 7 Dec 2015 20:05:28 +0000 (12:05 -0800)]
add type info to broken promise what()

Summary: when a promise is broken, add type info to the message. This will allow for some small improvements to debugging errors from broken promises, namely giving developers a slightly more refined target to grep for.

Reviewed By: fugalh

Differential Revision: D2725575

fb-gh-sync-id: b92edbde874e63eaeab97395da7bb52f76968800

8 years agoFix segfaults in AsyncSSLSocket when used in unencrypted mode.
Kyle Nekritz [Mon, 7 Dec 2015 19:04:13 +0000 (11:04 -0800)]
Fix segfaults in AsyncSSLSocket when used in unencrypted mode.

Reviewed By: djwatson

Differential Revision: D2720027

fb-gh-sync-id: 1d56aabedeaf65a5d48e152eaee5c81695be7620

8 years agojson: Add option to parse numbers as strings
Bartek Ryniec [Fri, 4 Dec 2015 21:07:15 +0000 (13:07 -0800)]
json: Add option to parse numbers as strings

Summary: Adding a flag in `folly::json::serialization_opts` that allows you to request all numbers to be parsed into strings. This saves you from exceptions being thrown by `folly::parseJson` if it encounters numbers that wouldn't fit in int64.

Reviewed By: yfeldblum

Differential Revision: D2713039

fb-gh-sync-id: 4f3d0d20f3012efd6229adf895200a3901bb4493

8 years agoMake getWrappedTransport/getUnderlyingTransport const.
Kyle Nekritz [Fri, 4 Dec 2015 17:18:44 +0000 (09:18 -0800)]
Make getWrappedTransport/getUnderlyingTransport const.

Reviewed By: mzlee

Differential Revision: D2709222

fb-gh-sync-id: 67a00a49bbcef5572b6092d7bfba1842d985e643

8 years agoRemove getBytesRead and getBytesWritten from folly::AsyncSSLSocket.
Kyle Nekritz [Fri, 4 Dec 2015 17:18:39 +0000 (09:18 -0800)]
Remove getBytesRead and getBytesWritten from folly::AsyncSSLSocket.

Summary: getRawBytesReceived and getRawBytesWritten should be used instead.

Reviewed By: mzlee

Differential Revision: D2708684

fb-gh-sync-id: 2de87dc2b8c3466f94293a3d7df89fc5fce3a00f

8 years agoAdd an "after fork, before exec" callback
Alexey Spiridonov [Fri, 4 Dec 2015 09:35:41 +0000 (01:35 -0800)]
Add an "after fork, before exec" callback

Summary:
In rare cases, it is required to run code **in the child process**, before it calls `exec()` because you need to change the state of the process **before** the child binary gets to run. This diff adds a callback to permit this. The callback is deliberately harder to use than an `std::function`, because you **REALLY HAVE TO KNOW WHAT YOU ARE DOING** to use this. And you have to check your work three times. Even glog `LOG()` must not be called from inside this callback. Your random library of choice is also probably unsafe.

This diff is primarily applicable to job managers and shells. For example, these tasks benefit from this callback: adding a child to a Linux `cgroup`, twiddling its various POSIX process attributes.

Implementing a correct callback for the post-vfork environment is fraught with peril. Read http://ewontfix.com/7/, and the docstrings in this diff.

Reviewed By: yfeldblum

Differential Revision: D2688323

fb-gh-sync-id: aae49e2b3957ca845895acca26e9cb44df1afc07

8 years agofix for negative delay in runAfterDelay
Umair Sadiq [Fri, 4 Dec 2015 05:38:07 +0000 (21:38 -0800)]
fix for negative delay in runAfterDelay

Summary: Currently, the tryRunAfterDelay implicitly converts a negative delay in int to unsigned int. With this change, the api makes it clear than only unsinged int is accepted as valid delay

Reviewed By: yfeldblum

Differential Revision: D2710034

fb-gh-sync-id: cb3c6b91bb86bbe07fa0e590b2ea871242d19745

8 years agoThreadLocalDetail: OSX fixes
Steve O'Brien [Thu, 3 Dec 2015 03:13:22 +0000 (19:13 -0800)]
ThreadLocalDetail: OSX fixes

Summary:
Change mutex to MicroSpinLock, for constexpr-ctor-ness on OSX.

Fixes this error seen on mac:

  ThreadLocalDetail.h:202:38: note: non-literal type 'std::mutex' cannot be used in a constant expression

Reviewed By: ldemailly

Differential Revision: D2713511

fb-gh-sync-id: bc1c34b4a0ee21347278aa368b408f286345e050

8 years agoThreadLocalDetail: fix bug just introduced w/ recent change to constexpr-ctor style...
Steve O'Brien [Wed, 2 Dec 2015 17:17:38 +0000 (09:17 -0800)]
ThreadLocalDetail: fix bug just introduced w/ recent change to constexpr-ctor style class

Summary: Small, silly bug which is triggered when there are no keys to unregister, invalidating the current size of the structure.

Reviewed By: yzhan

Differential Revision: D2712604

fb-gh-sync-id: f6f031936d8e4e458b49c08714f42cb2eec4c4fe

8 years agoAdd Optional::value_type
Tudor Bosman [Wed, 2 Dec 2015 16:57:22 +0000 (08:57 -0800)]
Add Optional::value_type

Summary: Comes in handy, and std::experimental::optional has it.

Reviewed By: yfeldblum

Differential Revision: D2708324

fb-gh-sync-id: 87cdf2449f356d3baf9fb56a608e9c43d8fe79ae

8 years agofolly: ThreadLocalDetail: make PthreadKeyUnregister constexpr-constructible, avoid...
Steve O'Brien [Wed, 2 Dec 2015 14:06:32 +0000 (06:06 -0800)]
folly: ThreadLocalDetail: make PthreadKeyUnregister constexpr-constructible, avoid SIOF

Summary:
Since this is used in so many places during the program's static-initialization (at startup), this class itself could be (has been demonstrated to be) a point of SIOF problems itself.

Made this class constexpr-constructible, so it doesn't need to be part of static initialization, making it SIOF-proof.

Reviewed By: luciang

Differential Revision: D2709231

fb-gh-sync-id: f248c9f2848c09045e000cfdc03636d847e522c9

8 years agoRecommend a different exception_handler idiom
Chad Parry [Wed, 2 Dec 2015 07:36:55 +0000 (23:36 -0800)]
Recommend a different exception_handler idiom

Summary: This is a //tiny// documentation change. I noticed that there are lots of places in the codebase where people follow this recommended usage of `with_exception` in an if–else chain, (e.g., https://fburl.com/181606454). IMHO, a short-circuiting `||` operator is easier to read and write.

Reviewed By: yfeldblum

Differential Revision: D2687840

fb-gh-sync-id: 6354555aeba81dcfe7adf17e7e573de0b1206b37

8 years agoProperly init collectDone_
Andrii Grynenko [Wed, 2 Dec 2015 01:23:48 +0000 (17:23 -0800)]
Properly init collectDone_

Summary: facepaw

Reviewed By: djwatson

Differential Revision: D2709030

fb-gh-sync-id: 20fe50bc70e97c4fedeae8fd271a4de70fa0e805

8 years agoAvoid requiring a suppression (unevaluated-expression)
Yedidya Feldblum [Tue, 1 Dec 2015 20:04:24 +0000 (12:04 -0800)]
Avoid requiring a suppression (unevaluated-expression)

Summary: [Folly] Avoid requiring a suppression (`unevaluated-expression`).

I am not a fan of suppressing warnings. This warning comes from the internals of gtest, but anyway.

Replace with something that does not trigger the warning.

Reviewed By: Gownta

Differential Revision: D2706279

fb-gh-sync-id: 2ecac1d230308a8fd21a69565193c52585526448

8 years agoEnable find/emplace for key types other than KeyT.
Daniel Andersson [Tue, 1 Dec 2015 15:46:00 +0000 (07:46 -0800)]
Enable find/emplace for key types other than KeyT.

Summary: Add template parameters to support arbitrary types when looking up a key.

This is useful to avoid the cost of 'materializing' a key which is likely to be present in the array (or, alternatively, switching on a tagged union KeyT).

Use the new capability to greatly simplify the lookup logic in HHVMs static string table.

Reviewed By: nbronson

Differential Revision: D2662451

fb-gh-sync-id: 707fa033f350b80ca8080af17f1a8a74c59f2e88

8 years agofolly/Conv.h: suppress -Wfloat-conversion warnings
Jim Meyering [Tue, 1 Dec 2015 15:27:54 +0000 (07:27 -0800)]
folly/Conv.h: suppress -Wfloat-conversion warnings

Summary: Using -Werror and -Wfloat-conversion was causing failure in code that is deliberately
performing that conversion.  Add explicit casts to avoid the warnings/errors.
Here are two of the diagnostics:

  ./folly/Conv.h: In instantiation of "typename std::enable_if<std::is_floating_point<_Tp>::value, Tgt>::type folly::to(folly::StringPiece) [with Tgt = float; typename std::enable_if<std::is_floating_point<_Tp>::value, Tgt>::type = float; folly::StringPiece = folly::Range<const char*>]":
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:687:28:   required from "T apache::thrift::SimpleJSONProtocolReader::castIntegral(const string&) [with T = float; std::string = std::basic_fbstring<char>]"
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:680:26:   required from "uint32_t apache::thrift::SimpleJSONProtocolReader::readJSONKey(T&) [with T = float; uint32_t = unsigned int]"
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:654:16:   required from "uint32_t apache::thrift::SimpleJSONProtocolReader::readInContext(T&) [with T = float; uint32_t = unsigned int]"
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:1035:34:   required from here
  ./folly/Conv.h:1222:31: error: conversion to "float" from "std::enable_if<true, double>::type {aka double}" may alter its value [-Werror=float-conversion]

  ./folly/Conv.h: In instantiation of "typename std::enable_if<((std::is_integral<_Tp2>::value && std::is_floating_point<_Tp>::value) || (std::is_floating_point<_DInputType>::value && std::is_integral<_Tp>::value)), Tgt>::type folly::to(const Src&) [with Tgt = long int; Src = double; typename std::enable_if<((std::is_integral<_Tp2>::value && std::is_floating_point<_Tp>::value) || (std::is_floating_point<_DInputType>::value && std::is_integral<_Tp>::value)), Tgt>::type = long int]":
  ./folly/dynamic-inl.h:636:51:   required from "T folly::dynamic::asImpl() const [with T = long int]"
  ./folly/dynamic-inl.h:384:64:   required from here
  ./folly/Conv.h:1245:16: error: conversion to "long int" from "double" may alter its value [-Werror=float-conversion]

This change deserves a little more explanation:

  -  return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10);
  +  return (unsigned int)(ceil(sizeof(IntegerType) * CHAR_BIT * M_LN2 / M_LN10));

In addition to adding the cast to destination type, I have also removed the unnecessary cast to double of the first product (there is not risk that it will overflow, and no need to promote to double, there), and I have also removed the grouping parentheses, since the standard left-to-right evaluation works just fine here.

Reviewed By: Gownta, yfeldblum

Differential Revision: D2703928

fb-gh-sync-id: 23b49281d9120d106f1fdbd30921e8f39c01367d

8 years agoCodeMod: Use the ExceptionWrapper::with_exception variant that deduces exception...
Yedidya Feldblum [Tue, 1 Dec 2015 06:54:20 +0000 (22:54 -0800)]
CodeMod: Use the ExceptionWrapper::with_exception variant that deduces exception types

Summary: CodeMod: Use the `ExceptionWrapper::with_exception` variant that deduces exception types.

Since we must specify the exception type in the lambda arg, and there is a variant of `ExceptionWrapper::with_exception` that deduces the type of the exception from the type of the lambda arg, we don't need to specify the exception type again as a template parameter anymore.

Reviewed By: meyering

Differential Revision: D2694895

fb-gh-sync-id: 505469f9008973a315e836f356e5db97df4ec921

8 years agofolly/Conv.h: estimateSpaceNeeded: avoid undefined behavior
Jim Meyering [Tue, 1 Dec 2015 05:17:20 +0000 (21:17 -0800)]
folly/Conv.h: estimateSpaceNeeded: avoid undefined behavior

Summary: Do not negate signed numbers like INT_MIN or INTMAX_MIN, since
that would evoke undefined behavior.  Otherwise, the test (below)
would fail with this run-time error:

  [ RUN      ] Conv.Integral2String
  folly/Conv.h:521:47: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself

Reviewed By: markisaa

Differential Revision: D2704195

fb-gh-sync-id: 4036437fb972109672004163880078127e7df797