fbstring IOBuf::moveToFbString() { // malloc-allocated buffers are just fine, everything else needs // to be turned into one. if (!sharedInfo() || // user owned, not ours to give up sharedInfo()->freeFn || // not malloc()-ed headroom() != 0 || // malloc()-ed block doesn't start at beginning tailroom() == 0 || // no room for NUL terminator isShared() || // shared isChained()) { // chained // We might as well get rid of all head and tailroom if we're going // to reallocate; we need 1 byte for NUL terminator. coalesceAndReallocate(0, computeChainDataLength(), this, 1); } // Ensure NUL terminated *writableTail() = 0; fbstring str(reinterpret_cast<char*>(writableData()), length(), capacity(), AcquireMallocatedString()); if (flags() & kFlagFreeSharedInfo) { delete sharedInfo(); } // Reset to a state where we can be deleted cleanly flagsAndSharedInfo_ = 0; buf_ = nullptr; clear(); return str; }
fbstring demangle(const char* name) { int status; size_t len = 0; // malloc() memory for the demangled type name char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status); if (status != 0) { return name; } // len is the length of the buffer (including NUL terminator and maybe // other junk) return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString()); }