-
Notifications
You must be signed in to change notification settings - Fork 5.3k
remove use of variable length arrays #4870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #pragma once | ||
|
|
||
| #if !defined(WIN32) | ||
| #include <alloca.h> | ||
|
|
||
| #else | ||
| #include <malloc.h> | ||
| #endif | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| #include "common/common/assert.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| // This macro is intended to be used as a replacement for variable-length arrays. | ||
| // Note that the StackArray wrapper object will be destructed and each element's | ||
| // destructor will be called when it leaves scope. However, the memory containing | ||
| // the array won't be deallocated until the function containing the macro returns. | ||
| // We can't call alloca in the StackArray constructor since the memory would | ||
| // be freed when the constructor returns. | ||
| #define STACK_ARRAY(var, type, num) StackArray<type> var(::alloca(sizeof(type) * num), num) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about this, do we actually need this macro? Couldn't we just specify the template instantiation directly with type and num for each var and actually have
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct, if we called
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK makes sense thanks. |
||
|
|
||
| template <typename T> class StackArray { | ||
| public: | ||
| StackArray(void* buf, size_t num_items) { | ||
| ASSERT(buf != nullptr, "StackArray received null pointer"); | ||
| begin_ = static_cast<T*>(buf); | ||
| end_ = static_cast<T*>(buf) + num_items; | ||
| for (T& ref : *this) { | ||
| new (&ref) T; | ||
| } | ||
| } | ||
|
|
||
| ~StackArray() { | ||
| for (T& ref : *this) { | ||
| ref.~T(); | ||
| } | ||
| } | ||
|
|
||
| T* begin() { return begin_; } | ||
|
|
||
| T* end() { return end_; } | ||
|
|
||
| T& operator[](size_t idx) { return begin_[idx]; } | ||
|
|
||
| void* operator new(size_t) = delete; | ||
|
|
||
| void* operator new[](size_t) = delete; | ||
|
|
||
| private: | ||
| T* begin_; | ||
| T* end_; | ||
| }; | ||
|
|
||
| } // namespace Envoy | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop this blank line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like the
tools/check_format.py fixscript is adding this new line backThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see... np then.