Skip to content
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

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions inc/mkn/kul/alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace mkn::kul {

template <typename T, std::size_t alignment = 32>
template <typename T, std::int32_t alignment = 32>
class AlignedAllocator {
using This = AlignedAllocator<T, alignment>;
public:
Expand All @@ -54,8 +54,11 @@ class AlignedAllocator {
T* allocate(std::size_t const n) const {
if (n == 0) return nullptr;

void* p = std::aligned_alloc(alignment, n * sizeof(T));
auto size = n * sizeof(T);
std::uint32_t diff = size % alignment;
if(diff > 0) diff = alignment - diff;

void* p = std::aligned_alloc(alignment, size + diff);
if (!p) throw std::bad_alloc();

return static_cast<T*>(p);
Expand All @@ -81,3 +84,4 @@ class AlignedAllocator {
} // namespace mkn::kul

#endif /*_MKN_KUL_ALLOC_HPP_*/

13 changes: 13 additions & 0 deletions inc/mkn/kul/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class Span {
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;
Comment on lines 69 to 87
Copy link

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.

Expand Down