2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/io/IOBuf.h"
18 #include "folly/io/TypedIOBuf.h"
20 // googletest requires std::tr1::tuple, not std::tuple
23 #include <gflags/gflags.h>
24 #include <boost/random.hpp>
25 #include <gtest/gtest.h>
27 #include "folly/Malloc.h"
28 #include "folly/Range.h"
30 using folly::fbstring;
31 using folly::fbvector;
33 using folly::TypedIOBuf;
34 using folly::StringPiece;
35 using folly::ByteRange;
36 using std::unique_ptr;
38 void append(std::unique_ptr<IOBuf>& buf, StringPiece str) {
39 EXPECT_LE(str.size(), buf->tailroom());
40 memcpy(buf->writableData(), str.data(), str.size());
41 buf->append(str.size());
44 void prepend(std::unique_ptr<IOBuf>& buf, StringPiece str) {
45 EXPECT_LE(str.size(), buf->headroom());
46 memcpy(buf->writableData() - str.size(), str.data(), str.size());
47 buf->prepend(str.size());
51 unique_ptr<IOBuf> buf(IOBuf::create(100));
52 uint32_t cap = buf->capacity();
54 EXPECT_EQ(0, buf->headroom());
55 EXPECT_EQ(0, buf->length());
56 EXPECT_EQ(cap, buf->tailroom());
60 EXPECT_EQ(10, buf->headroom());
61 EXPECT_EQ(5, buf->length());
62 EXPECT_EQ(cap - 15, buf->tailroom());
64 prepend(buf, "hello ");
65 EXPECT_EQ(4, buf->headroom());
66 EXPECT_EQ(11, buf->length());
67 EXPECT_EQ(cap - 15, buf->tailroom());
69 const char* p = reinterpret_cast<const char*>(buf->data());
70 EXPECT_EQ("hello world", std::string(p, buf->length()));
73 EXPECT_EQ(0, buf->headroom());
74 EXPECT_EQ(0, buf->length());
75 EXPECT_EQ(cap, buf->tailroom());
79 void testAllocSize(uint32_t requestedCapacity) {
80 unique_ptr<IOBuf> iobuf(IOBuf::create(requestedCapacity));
81 EXPECT_GE(iobuf->capacity(), requestedCapacity);
84 TEST(IOBuf, AllocSizes) {
85 // Try with a small allocation size that should fit in the internal buffer
88 // Try with a large allocation size that will require an external buffer.
91 // 220 bytes is currently the cutoff
92 // (It would be nice to use the IOBuf::kMaxInternalDataSize constant,
93 // but it's private and it doesn't seem worth making it public just for this
100 void deleteArrayBuffer(void *buf, void* arg) {
101 uint32_t* deleteCount = static_cast<uint32_t*>(arg);
103 uint8_t* bufPtr = static_cast<uint8_t*>(buf);
107 TEST(IOBuf, TakeOwnership) {
109 uint8_t *buf1 = static_cast<uint8_t*>(malloc(size1));
110 unique_ptr<IOBuf> iobuf1(IOBuf::takeOwnership(buf1, size1));
111 EXPECT_EQ(buf1, iobuf1->data());
112 EXPECT_EQ(size1, iobuf1->length());
113 EXPECT_EQ(buf1, iobuf1->buffer());
114 EXPECT_EQ(size1, iobuf1->capacity());
116 uint32_t deleteCount = 0;
117 uint32_t size2 = 4321;
118 uint8_t *buf2 = new uint8_t[size2];
119 unique_ptr<IOBuf> iobuf2(IOBuf::takeOwnership(buf2, size2,
122 EXPECT_EQ(buf2, iobuf2->data());
123 EXPECT_EQ(size2, iobuf2->length());
124 EXPECT_EQ(buf2, iobuf2->buffer());
125 EXPECT_EQ(size2, iobuf2->capacity());
126 EXPECT_EQ(0, deleteCount);
128 EXPECT_EQ(1, deleteCount);
131 uint32_t size3 = 3456;
132 uint8_t *buf3 = new uint8_t[size3];
133 uint32_t length3 = 48;
134 unique_ptr<IOBuf> iobuf3(IOBuf::takeOwnership(buf3, size3, length3,
137 EXPECT_EQ(buf3, iobuf3->data());
138 EXPECT_EQ(length3, iobuf3->length());
139 EXPECT_EQ(buf3, iobuf3->buffer());
140 EXPECT_EQ(size3, iobuf3->capacity());
141 EXPECT_EQ(0, deleteCount);
143 EXPECT_EQ(1, deleteCount);
148 TEST(IOBuf, WrapBuffer) {
149 const uint32_t size1 = 1234;
151 unique_ptr<IOBuf> iobuf1(IOBuf::wrapBuffer(buf1, size1));
152 EXPECT_EQ(buf1, iobuf1->data());
153 EXPECT_EQ(size1, iobuf1->length());
154 EXPECT_EQ(buf1, iobuf1->buffer());
155 EXPECT_EQ(size1, iobuf1->capacity());
157 uint32_t size2 = 0x1234;
158 unique_ptr<uint8_t[]> buf2(new uint8_t[size2]);
159 unique_ptr<IOBuf> iobuf2(IOBuf::wrapBuffer(buf2.get(), size2));
160 EXPECT_EQ(buf2.get(), iobuf2->data());
161 EXPECT_EQ(size2, iobuf2->length());
162 EXPECT_EQ(buf2.get(), iobuf2->buffer());
163 EXPECT_EQ(size2, iobuf2->capacity());
166 void fillBuf(uint8_t* buf, uint32_t length, boost::mt19937& gen) {
167 for (uint32_t n = 0; n < length; ++n) {
168 buf[n] = static_cast<uint8_t>(gen() & 0xff);
172 void fillBuf(IOBuf* buf, boost::mt19937& gen) {
174 fillBuf(buf->writableData(), buf->length(), gen);
177 void checkBuf(const uint8_t* buf, uint32_t length, boost::mt19937& gen) {
178 // Rather than using EXPECT_EQ() to check each character,
179 // count the number of differences and the first character that differs.
180 // This way on error we'll report just that information, rather than tons of
181 // failed checks for each byte in the buffer.
182 uint32_t numDifferences = 0;
183 uint32_t firstDiffIndex = 0;
184 uint8_t firstDiffExpected = 0;
185 for (uint32_t n = 0; n < length; ++n) {
186 uint8_t expected = static_cast<uint8_t>(gen() & 0xff);
187 if (buf[n] == expected) {
191 if (numDifferences == 0) {
193 firstDiffExpected = expected;
198 EXPECT_EQ(0, numDifferences);
199 if (numDifferences > 0) {
200 // Cast to int so it will be printed numerically
201 // rather than as a char if the check fails
202 EXPECT_EQ(static_cast<int>(buf[firstDiffIndex]),
203 static_cast<int>(firstDiffExpected));
207 void checkBuf(IOBuf* buf, boost::mt19937& gen) {
208 checkBuf(buf->data(), buf->length(), gen);
211 void checkBuf(ByteRange buf, boost::mt19937& gen) {
212 checkBuf(buf.data(), buf.size(), gen);
215 void checkChain(IOBuf* buf, boost::mt19937& gen) {
216 IOBuf *current = buf;
218 checkBuf(current->data(), current->length(), gen);
219 current = current->next();
220 } while (current != buf);
223 TEST(IOBuf, Chaining) {
224 uint32_t fillSeed = 0x12345678;
225 boost::mt19937 gen(fillSeed);
227 // An IOBuf with external storage
228 uint32_t headroom = 123;
229 unique_ptr<IOBuf> iob1(IOBuf::create(2048));
230 iob1->advance(headroom);
232 fillBuf(iob1.get(), gen);
234 // An IOBuf with internal storage
235 unique_ptr<IOBuf> iob2(IOBuf::create(20));
237 fillBuf(iob2.get(), gen);
239 // An IOBuf around a buffer it doesn't own
240 uint8_t localbuf[1234];
241 fillBuf(localbuf, 1234, gen);
242 unique_ptr<IOBuf> iob3(IOBuf::wrapBuffer(localbuf, sizeof(localbuf)));
244 // An IOBuf taking ownership of a user-supplied buffer
245 uint32_t heapBufSize = 900;
246 uint8_t* heapBuf = static_cast<uint8_t*>(malloc(heapBufSize));
247 fillBuf(heapBuf, heapBufSize, gen);
248 unique_ptr<IOBuf> iob4(IOBuf::takeOwnership(heapBuf, heapBufSize));
250 // An IOBuf taking ownership of a user-supplied buffer with
251 // a custom free function
252 uint32_t arrayBufSize = 321;
253 uint8_t* arrayBuf = new uint8_t[arrayBufSize];
254 fillBuf(arrayBuf, arrayBufSize, gen);
255 uint32_t arrayBufFreeCount = 0;
256 unique_ptr<IOBuf> iob5(IOBuf::takeOwnership(arrayBuf, arrayBufSize,
258 &arrayBufFreeCount));
260 EXPECT_FALSE(iob1->isChained());
261 EXPECT_FALSE(iob2->isChained());
262 EXPECT_FALSE(iob3->isChained());
263 EXPECT_FALSE(iob4->isChained());
264 EXPECT_FALSE(iob5->isChained());
266 EXPECT_FALSE(iob1->isSharedOne());
267 EXPECT_FALSE(iob2->isSharedOne());
268 EXPECT_TRUE(iob3->isSharedOne()); // since we own the buffer
269 EXPECT_FALSE(iob4->isSharedOne());
270 EXPECT_FALSE(iob5->isSharedOne());
272 // Chain the buffers all together
273 // Since we are going to relinquish ownership of iob2-5 to the chain,
274 // store raw pointers to them so we can reference them later.
275 IOBuf* iob2ptr = iob2.get();
276 IOBuf* iob3ptr = iob3.get();
277 IOBuf* iob4ptr = iob4.get();
278 IOBuf* iob5ptr = iob5.get();
280 iob1->prependChain(std::move(iob2));
281 iob1->prependChain(std::move(iob4));
282 iob2ptr->appendChain(std::move(iob3));
283 iob1->prependChain(std::move(iob5));
285 EXPECT_EQ(iob2ptr, iob1->next());
286 EXPECT_EQ(iob3ptr, iob2ptr->next());
287 EXPECT_EQ(iob4ptr, iob3ptr->next());
288 EXPECT_EQ(iob5ptr, iob4ptr->next());
289 EXPECT_EQ(iob1.get(), iob5ptr->next());
291 EXPECT_EQ(iob5ptr, iob1->prev());
292 EXPECT_EQ(iob1.get(), iob2ptr->prev());
293 EXPECT_EQ(iob2ptr, iob3ptr->prev());
294 EXPECT_EQ(iob3ptr, iob4ptr->prev());
295 EXPECT_EQ(iob4ptr, iob5ptr->prev());
297 EXPECT_TRUE(iob1->isChained());
298 EXPECT_TRUE(iob2ptr->isChained());
299 EXPECT_TRUE(iob3ptr->isChained());
300 EXPECT_TRUE(iob4ptr->isChained());
301 EXPECT_TRUE(iob5ptr->isChained());
303 uint64_t fullLength = (iob1->length() + iob2ptr->length() +
304 iob3ptr->length() + iob4ptr->length() +
306 EXPECT_EQ(5, iob1->countChainElements());
307 EXPECT_EQ(fullLength, iob1->computeChainDataLength());
309 // Since iob3 is shared, the entire buffer should report itself as shared
310 EXPECT_TRUE(iob1->isShared());
312 iob3ptr->unshareOne();
313 EXPECT_FALSE(iob3ptr->isSharedOne());
314 // Now everything in the chain should be unshared.
315 // Check on all members of the chain just for good measure
316 EXPECT_FALSE(iob1->isShared());
317 EXPECT_FALSE(iob2ptr->isShared());
318 EXPECT_FALSE(iob3ptr->isShared());
319 EXPECT_FALSE(iob4ptr->isShared());
320 EXPECT_FALSE(iob5ptr->isShared());
325 for (auto buf : *iob1) {
331 // Clone one of the IOBufs in the chain
332 unique_ptr<IOBuf> iob4clone = iob4ptr->cloneOne();
334 checkBuf(iob1.get(), gen);
335 checkBuf(iob2ptr, gen);
336 checkBuf(iob3ptr, gen);
337 checkBuf(iob4clone.get(), gen);
338 checkBuf(iob5ptr, gen);
340 EXPECT_TRUE(iob1->isShared());
341 EXPECT_TRUE(iob2ptr->isShared());
342 EXPECT_TRUE(iob3ptr->isShared());
343 EXPECT_TRUE(iob4ptr->isShared());
344 EXPECT_TRUE(iob5ptr->isShared());
346 EXPECT_FALSE(iob1->isSharedOne());
347 EXPECT_FALSE(iob2ptr->isSharedOne());
348 EXPECT_FALSE(iob3ptr->isSharedOne());
349 EXPECT_TRUE(iob4ptr->isSharedOne());
350 EXPECT_FALSE(iob5ptr->isSharedOne());
352 // Unshare that clone
353 EXPECT_TRUE(iob4clone->isSharedOne());
354 iob4clone->unshare();
355 EXPECT_FALSE(iob4clone->isSharedOne());
356 EXPECT_FALSE(iob4ptr->isSharedOne());
357 EXPECT_FALSE(iob1->isShared());
361 // Create a clone of a different IOBuf
362 EXPECT_FALSE(iob1->isShared());
363 EXPECT_FALSE(iob3ptr->isSharedOne());
365 unique_ptr<IOBuf> iob3clone = iob3ptr->cloneOne();
367 checkBuf(iob1.get(), gen);
368 checkBuf(iob2ptr, gen);
369 checkBuf(iob3clone.get(), gen);
370 checkBuf(iob4ptr, gen);
371 checkBuf(iob5ptr, gen);
373 EXPECT_TRUE(iob1->isShared());
374 EXPECT_TRUE(iob3ptr->isSharedOne());
375 EXPECT_FALSE(iob1->isSharedOne());
377 // Delete the clone and make sure the original is unshared
379 EXPECT_FALSE(iob1->isShared());
380 EXPECT_FALSE(iob3ptr->isSharedOne());
383 // Clone the entire chain
384 unique_ptr<IOBuf> chainClone = iob1->clone();
385 // Verify that the data is correct.
386 EXPECT_EQ(fullLength, chainClone->computeChainDataLength());
388 checkChain(chainClone.get(), gen);
390 // Check that the buffers report sharing correctly
391 EXPECT_TRUE(chainClone->isShared());
392 EXPECT_TRUE(iob1->isShared());
394 EXPECT_TRUE(iob1->isSharedOne());
395 // since iob2 has a small internal buffer, it will never be shared
396 EXPECT_FALSE(iob2ptr->isSharedOne());
397 EXPECT_TRUE(iob3ptr->isSharedOne());
398 EXPECT_TRUE(iob4ptr->isSharedOne());
399 EXPECT_TRUE(iob5ptr->isSharedOne());
401 // Unshare the cloned chain
402 chainClone->unshare();
403 EXPECT_FALSE(chainClone->isShared());
404 EXPECT_FALSE(iob1->isShared());
406 // Make sure the unshared result still has the same data
407 EXPECT_EQ(fullLength, chainClone->computeChainDataLength());
409 checkChain(chainClone.get(), gen);
411 // Destroy this chain
416 EXPECT_FALSE(iob1->isShared());
417 chainClone = iob1->clone();
418 EXPECT_TRUE(iob1->isShared());
419 EXPECT_TRUE(chainClone->isShared());
421 // Delete the original chain
423 EXPECT_FALSE(chainClone->isShared());
425 // Coalesce the chain
427 // Coalescing this chain will create a new buffer and release the last
428 // refcount on the original buffers we created. Also make sure
429 // that arrayBufFreeCount increases to one to indicate that arrayBuf was
431 EXPECT_EQ(5, chainClone->countChainElements());
432 EXPECT_EQ(0, arrayBufFreeCount);
434 // Buffer lengths: 1500 20 1234 900 321
435 // Coalesce the first 3 buffers
436 chainClone->gather(1521);
437 EXPECT_EQ(3, chainClone->countChainElements());
438 EXPECT_EQ(0, arrayBufFreeCount);
440 // Make sure the data is still the same after coalescing
441 EXPECT_EQ(fullLength, chainClone->computeChainDataLength());
443 checkChain(chainClone.get(), gen);
445 // Coalesce the entire chain
446 chainClone->coalesce();
447 EXPECT_EQ(1, chainClone->countChainElements());
448 EXPECT_EQ(1, arrayBufFreeCount);
450 // Make sure the data is still the same after coalescing
451 EXPECT_EQ(fullLength, chainClone->computeChainDataLength());
453 checkChain(chainClone.get(), gen);
455 // Make a new chain to test the unlink and pop operations
456 iob1 = IOBuf::create(1);
458 IOBuf *iob1ptr = iob1.get();
459 iob2 = IOBuf::create(3);
461 iob2ptr = iob2.get();
462 iob3 = IOBuf::create(5);
464 iob3ptr = iob3.get();
465 iob4 = IOBuf::create(7);
467 iob4ptr = iob4.get();
468 iob1->appendChain(std::move(iob2));
469 iob1->prev()->appendChain(std::move(iob3));
470 iob1->prev()->appendChain(std::move(iob4));
471 EXPECT_EQ(4, iob1->countChainElements());
472 EXPECT_EQ(16, iob1->computeChainDataLength());
474 // Unlink from the middle of the chain
475 iob3 = iob3ptr->unlink();
476 EXPECT_TRUE(iob3.get() == iob3ptr);
477 EXPECT_EQ(3, iob1->countChainElements());
478 EXPECT_EQ(11, iob1->computeChainDataLength());
480 // Unlink from the end of the chain
481 iob4 = iob1->prev()->unlink();
482 EXPECT_TRUE(iob4.get() == iob4ptr);
483 EXPECT_EQ(2, iob1->countChainElements());
484 EXPECT_TRUE(iob1->next() == iob2ptr);
485 EXPECT_EQ(4, iob1->computeChainDataLength());
487 // Pop from the front of the chain
489 EXPECT_TRUE(iob1.get() == iob1ptr);
490 EXPECT_EQ(1, iob1->countChainElements());
491 EXPECT_EQ(1, iob1->computeChainDataLength());
492 EXPECT_TRUE(iob2.get() == iob2ptr);
493 EXPECT_EQ(1, iob2->countChainElements());
494 EXPECT_EQ(3, iob2->computeChainDataLength());
497 void testFreeFn(void* buffer, void* ptr) {
498 uint32_t* freeCount = static_cast<uint32_t*>(ptr);;
499 delete[] static_cast<uint8_t*>(buffer);
505 TEST(IOBuf, Reserve) {
506 uint32_t fillSeed = 0x23456789;
507 boost::mt19937 gen(fillSeed);
509 // Reserve does nothing if empty and doesn't have to grow the buffer
512 unique_ptr<IOBuf> iob(IOBuf::create(2000));
513 EXPECT_EQ(0, iob->headroom());
514 const void* p1 = iob->buffer();
516 EXPECT_LE(5, iob->headroom());
517 EXPECT_EQ(p1, iob->buffer());
520 // Reserve doesn't reallocate if we have enough total room
523 unique_ptr<IOBuf> iob(IOBuf::create(2000));
525 fillBuf(iob.get(), gen);
526 EXPECT_EQ(0, iob->headroom());
527 EXPECT_EQ(100, iob->length());
528 const void* p1 = iob->buffer();
529 const uint8_t* d1 = iob->data();
530 iob->reserve(100, 1800);
531 EXPECT_LE(100, iob->headroom());
532 EXPECT_EQ(p1, iob->buffer());
533 EXPECT_EQ(d1 + 100, iob->data());
535 checkBuf(iob.get(), gen);
538 // Reserve reallocates if we don't have enough total room.
539 // NOTE that, with jemalloc, we know that this won't reallocate in place
540 // as the size is less than jemallocMinInPlaceExpanadable
543 unique_ptr<IOBuf> iob(IOBuf::create(2000));
545 fillBuf(iob.get(), gen);
546 EXPECT_EQ(0, iob->headroom());
547 EXPECT_EQ(100, iob->length());
548 const void* p1 = iob->buffer();
549 const uint8_t* d1 = iob->data();
550 iob->reserve(100, 2512); // allocation sizes are multiples of 256
551 EXPECT_LE(100, iob->headroom());
552 if (folly::usingJEMalloc()) {
553 EXPECT_NE(p1, iob->buffer());
556 checkBuf(iob.get(), gen);
559 // Test reserve from internal buffer, this used to segfault
561 unique_ptr<IOBuf> iob(IOBuf::create(0));
562 iob->reserve(0, 2000);
563 EXPECT_EQ(0, iob->headroom());
564 EXPECT_LE(2000, iob->tailroom());
567 // Test reserving from a user-allocated buffer.
569 uint8_t* buf = static_cast<uint8_t*>(malloc(100));
570 auto iob = IOBuf::takeOwnership(buf, 100);
571 iob->reserve(0, 2000);
572 EXPECT_EQ(0, iob->headroom());
573 EXPECT_LE(2000, iob->tailroom());
576 // Test reserving from a user-allocated with a custom free function.
578 uint32_t freeCount{0};
579 uint8_t* buf = new uint8_t[100];
580 auto iob = IOBuf::takeOwnership(buf, 100, testFreeFn, &freeCount);
581 iob->reserve(0, 2000);
582 EXPECT_EQ(0, iob->headroom());
583 EXPECT_LE(2000, iob->tailroom());
584 EXPECT_EQ(1, freeCount);
588 TEST(IOBuf, copyBuffer) {
589 std::string s("hello");
590 auto buf = IOBuf::copyBuffer(s.data(), s.size(), 1, 2);
591 EXPECT_EQ(1, buf->headroom());
592 EXPECT_EQ(s, std::string(reinterpret_cast<const char*>(buf->data()),
594 EXPECT_LE(2, buf->tailroom());
596 buf = IOBuf::copyBuffer(s, 5, 7);
597 EXPECT_EQ(5, buf->headroom());
598 EXPECT_EQ(s, std::string(reinterpret_cast<const char*>(buf->data()),
600 EXPECT_LE(7, buf->tailroom());
603 buf = IOBuf::copyBuffer(empty, 3, 6);
604 EXPECT_EQ(3, buf->headroom());
605 EXPECT_EQ(0, buf->length());
606 EXPECT_LE(6, buf->tailroom());
609 TEST(IOBuf, maybeCopyBuffer) {
610 std::string s("this is a test");
611 auto buf = IOBuf::maybeCopyBuffer(s, 1, 2);
612 EXPECT_EQ(1, buf->headroom());
613 EXPECT_EQ(s, std::string(reinterpret_cast<const char*>(buf->data()),
615 EXPECT_LE(2, buf->tailroom());
618 buf = IOBuf::maybeCopyBuffer("", 5, 7);
619 EXPECT_EQ(nullptr, buf.get());
621 buf = IOBuf::maybeCopyBuffer("");
622 EXPECT_EQ(nullptr, buf.get());
627 int customDeleterCount = 0;
628 int destructorCount = 0;
629 struct OwnershipTestClass {
630 explicit OwnershipTestClass(int v = 0) : val(v) { }
631 ~OwnershipTestClass() {
637 typedef std::function<void(OwnershipTestClass*)> CustomDeleter;
639 void customDelete(OwnershipTestClass* p) {
640 ++customDeleterCount;
644 void customDeleteArray(OwnershipTestClass* p) {
645 ++customDeleterCount;
651 TEST(IOBuf, takeOwnershipUniquePtr) {
654 std::unique_ptr<OwnershipTestClass> p(new OwnershipTestClass());
656 EXPECT_EQ(1, destructorCount);
660 std::unique_ptr<OwnershipTestClass[]> p(new OwnershipTestClass[2]);
662 EXPECT_EQ(2, destructorCount);
666 std::unique_ptr<OwnershipTestClass> p(new OwnershipTestClass());
667 std::unique_ptr<IOBuf> buf(IOBuf::takeOwnership(std::move(p)));
668 EXPECT_EQ(sizeof(OwnershipTestClass), buf->length());
669 EXPECT_EQ(0, destructorCount);
671 EXPECT_EQ(1, destructorCount);
675 std::unique_ptr<OwnershipTestClass[]> p(new OwnershipTestClass[2]);
676 std::unique_ptr<IOBuf> buf(IOBuf::takeOwnership(std::move(p), 2));
677 EXPECT_EQ(2 * sizeof(OwnershipTestClass), buf->length());
678 EXPECT_EQ(0, destructorCount);
680 EXPECT_EQ(2, destructorCount);
682 customDeleterCount = 0;
685 std::unique_ptr<OwnershipTestClass, CustomDeleter>
686 p(new OwnershipTestClass(), customDelete);
687 std::unique_ptr<IOBuf> buf(IOBuf::takeOwnership(std::move(p)));
688 EXPECT_EQ(sizeof(OwnershipTestClass), buf->length());
689 EXPECT_EQ(0, destructorCount);
691 EXPECT_EQ(1, destructorCount);
692 EXPECT_EQ(1, customDeleterCount);
694 customDeleterCount = 0;
697 std::unique_ptr<OwnershipTestClass[], CustomDeleter>
698 p(new OwnershipTestClass[2], customDeleteArray);
699 std::unique_ptr<IOBuf> buf(IOBuf::takeOwnership(std::move(p), 2));
700 EXPECT_EQ(2 * sizeof(OwnershipTestClass), buf->length());
701 EXPECT_EQ(0, destructorCount);
703 EXPECT_EQ(2, destructorCount);
704 EXPECT_EQ(1, customDeleterCount);
707 TEST(IOBuf, Alignment) {
708 // max_align_t doesn't exist in gcc 4.6.2
711 } __attribute__((aligned));
712 size_t alignment = alignof(MaxAlign);
714 std::vector<size_t> sizes {0, 1, 64, 256, 1024, 1 << 10};
715 for (size_t size : sizes) {
716 auto buf = IOBuf::create(size);
717 uintptr_t p = reinterpret_cast<uintptr_t>(buf->data());
718 EXPECT_EQ(0, p & (alignment - 1)) << "size=" << size;
722 TEST(TypedIOBuf, Simple) {
723 auto buf = IOBuf::create(0);
724 TypedIOBuf<uint64_t> typed(buf.get());
725 const uint64_t n = 10000;
727 EXPECT_LE(n, typed.capacity());
728 for (uint64_t i = 0; i < n; i++) {
729 *typed.writableTail() = i;
732 EXPECT_EQ(n, typed.length());
733 for (uint64_t i = 0; i < n; i++) {
734 EXPECT_EQ(i, typed.data()[i]);
739 TAKE_OWNERSHIP_MALLOC,
740 TAKE_OWNERSHIP_CUSTOM,
744 // chain element size, number of elements in chain, shared
745 class MoveToFbStringTest
746 : public ::testing::TestWithParam<std::tr1::tuple<int, int, bool, BufType>> {
749 std::tr1::tie(elementSize_, elementCount_, shared_, type_) = GetParam();
751 for (int i = 0; i < elementCount_ - 1; ++i) {
752 buf_->prependChain(makeBuf());
754 EXPECT_EQ(elementCount_, buf_->countChainElements());
755 EXPECT_EQ(elementCount_ * elementSize_, buf_->computeChainDataLength());
757 buf2_ = buf_->clone();
758 EXPECT_EQ(elementCount_, buf2_->countChainElements());
759 EXPECT_EQ(elementCount_ * elementSize_, buf2_->computeChainDataLength());
763 std::unique_ptr<IOBuf> makeBuf() {
764 unique_ptr<IOBuf> buf;
767 buf = IOBuf::create(elementSize_);
768 buf->append(elementSize_);
770 case TAKE_OWNERSHIP_MALLOC: {
771 void* data = malloc(elementSize_);
773 throw std::bad_alloc();
775 buf = IOBuf::takeOwnership(data, elementSize_);
778 case TAKE_OWNERSHIP_CUSTOM: {
779 uint8_t* data = new uint8_t[elementSize_];
780 buf = IOBuf::takeOwnership(data, elementSize_, testFreeFn);
784 unique_ptr<uint8_t[]> data(new uint8_t[elementSize_]);
785 buf = IOBuf::wrapBuffer(data.get(), elementSize_);
786 ownedBuffers_.emplace_back(std::move(data));
790 throw std::invalid_argument("unexpected buffer type parameter");
793 memset(buf->writableData(), 'x', elementSize_);
797 void check(std::unique_ptr<IOBuf>& buf) {
798 fbstring str = buf->moveToFbString();
799 EXPECT_EQ(elementCount_ * elementSize_, str.size());
800 EXPECT_EQ(elementCount_ * elementSize_, strspn(str.c_str(), "x"));
801 EXPECT_EQ(0, buf->length());
802 EXPECT_EQ(1, buf->countChainElements());
803 EXPECT_EQ(0, buf->computeChainDataLength());
804 EXPECT_FALSE(buf->isChained());
811 std::unique_ptr<IOBuf> buf_;
812 std::unique_ptr<IOBuf> buf2_;
813 std::vector<std::unique_ptr<uint8_t[]>> ownedBuffers_;
816 TEST_P(MoveToFbStringTest, Simple) {
823 INSTANTIATE_TEST_CASE_P(
827 ::testing::Values(0, 1, 24, 256, 1 << 10, 1 << 20), // element size
828 ::testing::Values(1, 2, 10), // element count
829 ::testing::Bool(), // shared
830 ::testing::Values(CREATE, TAKE_OWNERSHIP_MALLOC,
831 TAKE_OWNERSHIP_CUSTOM, USER_OWNED)));
833 TEST(IOBuf, getIov) {
834 uint32_t fillSeed = 0xdeadbeef;
835 boost::mt19937 gen(fillSeed);
839 auto buf = IOBuf::create(len + 1);
840 buf->append(rand() % len + 1);
841 fillBuf(buf.get(), gen);
843 for (size_t i = 0; i < count - 1; i++) {
844 auto buf2 = IOBuf::create(len + 1);
845 buf2->append(rand() % len + 1);
846 fillBuf(buf2.get(), gen);
847 buf->prependChain(std::move(buf2));
849 EXPECT_EQ(count, buf->countChainElements());
851 auto iov = buf->getIov();
852 EXPECT_EQ(count, iov.size());
854 IOBuf const* p = buf.get();
855 for (size_t i = 0; i < count; i++, p = p->next()) {
856 EXPECT_EQ(p->data(), iov[i].iov_base);
857 EXPECT_EQ(p->length(), iov[i].iov_len);
860 // an empty buf should be skipped in the iov.
861 buf->next()->clear();
863 EXPECT_EQ(count - 1, iov.size());
864 EXPECT_EQ(buf->next()->next()->data(), iov[1].iov_base);
866 // same for the first one being empty
869 EXPECT_EQ(count - 2, iov.size());
870 EXPECT_EQ(buf->next()->next()->data(), iov[0].iov_base);
873 buf->prev()->clear();
875 EXPECT_EQ(count - 3, iov.size());
878 int main(int argc, char** argv) {
879 testing::InitGoogleTest(&argc, argv);
880 google::ParseCommandLineFlags(&argc, &argv, true);
882 return RUN_ALL_TESTS();