This approach has 2 adventages:
* it takes into consideration the specical case of a bufferlist
that carries solely empty bptrs while
* allowing compilers to optimize-out the load-and-check of `_len`
we already do in `rebuild()` which is called by `c_str()`.
```cpp
void buffer::list::rebuild()
{
if (_len == 0) {
_carriage = &always_empty_bptr;
_buffers.clear_and_dispose();
_num = 0;
return;
}
// ...
}
```
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
*/
char *buffer::list::c_str()
{
- switch (get_num_buffers()) {
- case 0:
- // no buffers
- return nullptr;
- case 1:
- // good, we're already contiguous.
- break;
- default:
+ if (length() == 0) {
+ return nullptr; // no non-empty buffers
+ }
+
+ auto iter = std::cbegin(_buffers);
+ ++iter;
+
+ if (iter != std::cend(_buffers)) {
rebuild();
- break;
}
return _buffers.front().c_str();
}