Boost logo

Boost :

From: Aniket Pugaonkar (aniket.pugaonkar_at_[hidden])
Date: 2022-12-09 15:36:31


Hello,

I was checking the implementation of boost streambuf and noticed that
buffer_delta value is defaulted to 128, which results in the capacity of
the container to 128 bytes by default,
and based on that, there is no way to change it even if we provide max size
as some custom number (say 9000 for receiving an encapsulated packet on
socket).
The initial allocation (buffer_.resize is done with 128. But when we call
*buf.prepare(9000)*, it again results in resize(n) operation (through
reserve(n) function call.

Is there any other way to change this other than modifying the constructor
in our codebase to provide a user defined value of buffer_delta
Not sure if this needs to be enhanced. the maximum_size argument seems to
be of no use in this case.

Kindly advise for the same.

For time being, I modified the library code to the following to ensure it
does not break the default argument, but accepts user defined value.
Alternatively, I can change buffer delta to be of larger value, but
don't want to end up with a large buffer for no reason.

if (max_size_ == std::numeric_limits<std::size_t>::max())
        pend = (std::min<std::size_t>)(max_size_, buffer_delta);
else
        pend = std::max<std::size_t>(max_size_, buffer_delta);

Regards
Aniket.

-------------------- original code ----------------
enum { buffer_delta = 128 };

  /// Construct a basic_streambuf object.
  /**
   * Constructs a streambuf with the specified maximum size. The initial
size
   * of the streambuf's input sequence is 0.
   */
  explicit basic_streambuf(
      std::size_t maximum_size = (std::numeric_limits<std::size_t>::max)(),
      const Allocator& allocator = Allocator())
    : max_size_(maximum_size),
      buffer_(allocator)
  {
    *std::size_t pend = (std::min<std::size_t>)(max_size_, buffer_delta);*
    *buffer_.resize((std::max<std::size_t>)(pend, 1));*
    setg(&buffer_[0], &buffer_[0], &buffer_[0]);
    setp(&buffer_[0], &buffer_[0] + pend);
  }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk