Envoy: Where did envoy allocate the read/write buffer?

Created on 10 Nov 2020  路  7Comments  路  Source: envoyproxy/envoy

When checking the buffer implementation, I traced the implementation of OwnedSlice and Slice, and find the initialization

OwnedSlice(uint64_t size) : Slice(0, 0, size) { base_ = storage_; }

But by search the whole codebase, I have no clues about where storage_ is assigned with the allocated memory, which confused me for quite a long time. Is there any clues for that?

Thanks, @brian-pane

question stale

All 7 comments

Is this related to the placement-new stuff? Like
std::unique_ptr<OwnedSlice> slice(new (slice_capacity) OwnedSlice(slice_capacity));

This is probably a c++ question.
storage_ is an array name but not a pointer.
Actually array name is a non-modifiable lvalue. You cannot assign it value.

This is probably a c++ question.
storage_ is an array name but not a pointer.
Actually array name is a non-modifiable lvalue. You cannot assign it value.

The declaration of storage_ is uint8_t storage_[], so where did envoy allocate the memory for this array? (There should be some stuff like new/malloc, or memory pool to associate with storage_, but I did not find it).

Yes, OwnedSlice uses an exotic, custom C++ new operator defined by the InlineStorage mixin base class which allocates a memory block for for the size of the object plus an extra variable size region. Note the OwnedSlice::storage_ array data member that doesn't have a pre-defined size, it's address is at the start of the variable length region after the OwnedSlice's fixed size data members.

OwnedSlice::create is in charge of allocating the variable size object and feeding the size of the variable size section to the object's constructor. To complicate things further, OwnedSlice uses sliceSize to round the requested capacity passed in to the create function so that the allocated OwnedSlice always consumes a multiple of the page size, which Envoy assumes is 4kb in size.

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted" or "no stalebot". Thank you for your contributions.

I changed how slice allocation works a while ago. It's less subtle now.

See https://github.com/envoyproxy/envoy/pull/14111 and https://github.com/envoyproxy/envoy/pull/14282

Was this page helpful?
0 / 5 - 0 ratings