update moveToFbString()'s handling of flags_
authorAdam Simpkins <simpkins@fb.com>
Mon, 25 Nov 2013 00:30:22 +0000 (16:30 -0800)
committerJordan DeLong <jdelong@fb.com>
Fri, 20 Dec 2013 21:03:27 +0000 (13:03 -0800)
commitd9298481e96658f28b9df5083fef1a70a399eba8
treee5e5df06915139c43aae0b5dc1e6697b15d7e0c0
parent3d5106cd605ed237a5625dd8d45223075a57bcae
update moveToFbString()'s handling of flags_

Summary:
Improve moveToFbString()'s code which determines if we actually have a
buffer that was allocated with malloc().

The old code simply checked if flags_ == kFlagExt.  This check is rather
fragile, and relies on very specific behavior from the existing
construction methods.  It also unnecessarily forced reallocation in some
cases.

This updates the code to have more specific checks for the flags and
fields that actually matter.  In particular, even if we have an external
buffer, if sharedInfo->freeFn is set then it is not managed by malloc().
The old check happened to work because takeOwnership() was the only
function that set set freeFn, and it also set kFlagFreeSharedInfo.

This also improves the code so that it won't unnecessarily reallocate
the buffer if kFlagMaybeShared is set but the buffer isn't really
shared.  The code will also no longer unnecessarily reallocates the
buffer just because kFlagFreeSharedInfo is set.

Test Plan:
Updated the moveToFbString() test to also test with buffers created
using takeOwnership() and wrapBuffer().

Reviewed By: davejwatson@fb.com

FB internal diff: D1072304

@override-unit-failures
folly/io/IOBuf.cpp
folly/io/test/IOBufTest.cpp