Skip to content

Commit

Permalink
fail over to malloc if aligned_alloc fails for some reason
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRawson authored and quickbeam123 committed Jun 13, 2024
1 parent 141d2ca commit c6d7fcd
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Lib/Allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ void *operator new(size_t size, std::align_val_t align_val) {
ALLOCATED += size;
{
Lib::TimeoutProtector tp;
if (static_cast<size_t>(align_val) > 1) {
if(void *ptr = std::aligned_alloc(align, size))
return ptr;
} else {
if(void *ptr = std::malloc(size))
return ptr;
}
if(void *ptr = std::aligned_alloc(align, size))
return ptr;

// we might be here because `aligned_alloc` is finicky (Apple, looking at you)
// so try again with `malloc` and hope for good alignment
if(void *ptr = std::malloc(size))
return ptr;

}
// no, we're actually out of memory
throw std::bad_alloc();
}

Expand Down

2 comments on commit c6d7fcd

@barracuda156
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelRawson @quickbeam123 This does not work, because aligned_alloc may not be available at all to begin with: #592

@barracuda156
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we simply need to put the following chunk inside a macro:

    if(void *ptr = std::aligned_alloc(align, size))
		
      return ptr;

So that it is not used where not supported.

Please sign in to comment.