-
Notifications
You must be signed in to change notification settings - Fork 3
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
more alloc - asan warnings #36
Conversation
WalkthroughThe recent updates involve enhancing memory alignment handling in the allocation header and extending the functionality of the Changes
TipsChat with CodeRabbit Bot (
|
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.
Review Status
Actionable comments generated: 1
Configuration used: CodeRabbit UI
Files selected for processing (2)
- inc/mkn/kul/alloc.hpp (3 hunks)
- inc/mkn/kul/span.hpp (1 hunks)
Additional comments: 3
inc/mkn/kul/alloc.hpp (3)
39-42: Changing the template parameter
alignment
fromstd::size_t
tostd::int32_t
could potentially introduce issues if the alignment is expected to be larger than whatstd::int32_t
can hold. However, since the alignment is typically a small power of two, this is unlikely to be a problem in practice. Still, it's important to ensure that all uses ofAlignedAllocator
are checked to confirm that astd::int32_t
is sufficient and that no negative values are being used for alignment, as that would be nonsensical and could lead to undefined behavior.54-64: The changes to the
allocate
function look correct and ensure that the allocated memory is properly aligned. The calculation ofdiff
ensures that the allocated size is always a multiple of the alignment, which is important for alignment guarantees. The use ofstd::aligned_alloc
is appropriate here, and the check for a null pointer before throwingstd::bad_alloc
is a good practice. However, it's important to note thatstd::aligned_alloc
requires the size to be a multiple of the alignment, which is ensured by the added calculation. This change should be safe as long as the alignment value is a power of two, which is a common requirement for memory alignment.84-87: No issues with the closing of the namespace and the include guard. These are standard practices in C++ to prevent symbol redefinition and to ensure that the file is only included once during compilation.
auto end() const { return ptr + s; } | ||
SIZE const& size() const { return s; } | ||
|
||
template <typename C, std::enable_if_t<is_span_like_v<C>, bool> = 0> | ||
void reset(C& c) { | ||
ptr = c.data(); | ||
s = c.size(); | ||
} | ||
|
||
template <typename C, std::enable_if_t<is_span_like_v<C>, bool> = 0> | ||
auto& operator=(C& c) { | ||
reset(c); | ||
return *this; | ||
} | ||
|
||
|
||
private: | ||
T* ptr = nullptr; | ||
SIZE s = 0; |
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.
The implementation of the reset
and operator=
functions for the Span
class is correct and follows the expected behavior of resetting the internal pointer and size to match the provided container. The use of std::enable_if_t
with the is_span_like_v
concept ensures that these functions are only available for types that have data()
and size()
member functions, which is a good use of SFINAE (Substitution Failure Is Not An Error) to enforce type constraints.
However, it's important to note that the size()
method in line 75 should be cast to SIZE
to ensure type consistency, especially if c.size()
returns a type that is different from SIZE
. This is to prevent any potential type narrowing or conversion issues.
Also, the reset
and operator=
functions do not check if the container c
is empty before assigning its data pointer to ptr
. If c
is empty, c.data()
might return nullptr
, and ptr
would also be set to nullptr
. This behavior should be documented or handled if it's not the intended behavior.
Summary by CodeRabbit
Span
class with new member functions for resetting and assigning from compatible containers, increasing flexibility in managing span-like data structures.