Skip to content

Commit

Permalink
more alloc - asan warnings (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan authored Nov 19, 2023
1 parent aa5093f commit 51662eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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;
Expand Down

0 comments on commit 51662eb

Please sign in to comment.