}
unique_ptr<IOBuf> IOBuf::takeOwnership(void* buf, uint32_t capacity,
+ uint32_t length,
FreeFunction freeFn,
void* userData,
bool freeOnError) {
uint8_t* bufPtr = static_cast<uint8_t*>(buf);
return unique_ptr<IOBuf>(new IOBuf(kExtUserSupplied, kFlagFreeSharedInfo,
bufPtr, capacity,
- bufPtr, capacity,
+ bufPtr, length,
sharedInfo));
} catch (...) {
delete sharedInfo;
* If no FreeFunction is specified, the buffer will be freed using free().
*
* The IOBuf data pointer will initially point to the start of the buffer,
- * and the length will be the full capacity of the buffer.
+ *
+ * In the first version of this function, the length of data is unspecified
+ * and is initialized to the capacity of the buffer
+ *
+ * In the second version, the user specifies the valid length of data
+ * in the buffer
*
* On error, std::bad_alloc will be thrown. If freeOnError is true (the
* default) the buffer will be freed before throwing the error.
*/
static std::unique_ptr<IOBuf> takeOwnership(void* buf, uint32_t capacity,
+ FreeFunction freeFn = NULL,
+ void* userData = NULL,
+ bool freeOnError = true) {
+ return takeOwnership(buf, capacity, capacity, freeFn,
+ userData, freeOnError);
+ }
+
+ static std::unique_ptr<IOBuf> takeOwnership(void* buf, uint32_t capacity,
+ uint32_t length,
FreeFunction freeFn = NULL,
void* userData = NULL,
bool freeOnError = true);
EXPECT_EQ(0, deleteCount);
iobuf2.reset();
EXPECT_EQ(1, deleteCount);
+
+ deleteCount = 0;
+ uint32_t size3 = 3456;
+ uint8_t *buf3 = new uint8_t[size3];
+ uint32_t length3 = 48;
+ unique_ptr<IOBuf> iobuf3(IOBuf::takeOwnership(buf3, size3, length3,
+ deleteArrayBuffer,
+ &deleteCount));
+ EXPECT_EQ(buf3, iobuf3->data());
+ EXPECT_EQ(length3, iobuf3->length());
+ EXPECT_EQ(buf3, iobuf3->buffer());
+ EXPECT_EQ(size3, iobuf3->capacity());
+ EXPECT_EQ(0, deleteCount);
+ iobuf3.reset();
+ EXPECT_EQ(1, deleteCount);
+
+
}
TEST(IOBuf, WrapBuffer) {