Skip to content

Commit

Permalink
refactor: remove new handler
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Feb 11, 2024
1 parent 4a691fc commit b4a4c44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 51 deletions.
70 changes: 20 additions & 50 deletions src/ll/api/memory/MemoryOperators.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,71 +50,45 @@ void __CRTDECL operator delete[](void* p, std::size_t, std::align_val_t alignmen

_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size)
_VCRT_ALLOCATOR void* __CRTDECL operator new(std::size_t size) {
for (;;) {
if (void* const block = getDefaultAllocator().allocate(size)) {
return block;
}

if (_callnewh(size) == 0) {
if (size == SIZE_MAX) {
__scrt_throw_std_bad_array_new_length();
} else {
__scrt_throw_std_bad_alloc();
}
}

// The new handler was successful; try to allocate again...
if (void* const block = getDefaultAllocator().allocate(size)) {
return block;
}
if (size == SIZE_MAX) {
__scrt_throw_std_bad_array_new_length();
} else {
__scrt_throw_std_bad_alloc();
}
}

_NODISCARD _Ret_notnull_ _Success_(return != NULL) _Post_writable_byte_size_(size) _VCRT_ALLOCATOR void* __CRTDECL
operator new(std::size_t size, std::nothrow_t const&) noexcept {
try {
return operator new(size);
} catch (...) {
return nullptr;
}
return getDefaultAllocator().allocate(size);
}

_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size)
_VCRT_ALLOCATOR void* __CRTDECL operator new[](std::size_t size) { return operator new(size); }

_NODISCARD _Ret_notnull_ _Success_(return != NULL) _Post_writable_byte_size_(size) _VCRT_ALLOCATOR void* __CRTDECL
operator new[](std::size_t size, std::nothrow_t const&) noexcept {
try {
return operator new[](size);
} catch (...) {
return nullptr;
}
operator new[](std::size_t size, std::nothrow_t const& tag) noexcept {
return operator new(size, tag);
}


_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size)
_VCRT_ALLOCATOR void* __CRTDECL operator new(std::size_t size, std::align_val_t alignment) {
for (;;) {
if (void* const block = getDefaultAllocator().alignedAllocate(size, static_cast<size_t>(alignment))) {
return block;
}

if (_callnewh(size) == 0) {
if (size == SIZE_MAX) {
__scrt_throw_std_bad_array_new_length();
} else {
__scrt_throw_std_bad_alloc();
}
}

// The new handler was successful; try to allocate again...
if (void* const block = getDefaultAllocator().alignedAllocate(size, static_cast<size_t>(alignment))) {
return block;
}
if (size == SIZE_MAX) {
__scrt_throw_std_bad_array_new_length();
} else {
__scrt_throw_std_bad_alloc();
}
}

_NODISCARD _Ret_notnull_ _Success_(return != NULL) _Post_writable_byte_size_(size) _VCRT_ALLOCATOR void* __CRTDECL
operator new(std::size_t size, std::align_val_t alignment, std::nothrow_t const&) noexcept {
try {
return operator new(size, alignment);
} catch (...) {
return nullptr;
}
return getDefaultAllocator().alignedAllocate(size, static_cast<size_t>(alignment));
}

_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size)
Expand All @@ -123,10 +97,6 @@ _VCRT_ALLOCATOR void* __CRTDECL operator new[](std::size_t size, std::align_val_
}

_NODISCARD _Ret_notnull_ _Success_(return != NULL) _Post_writable_byte_size_(size) _VCRT_ALLOCATOR void* __CRTDECL
operator new[](std::size_t size, std::align_val_t alignment, std::nothrow_t const&) noexcept {
try {
return operator new[](size, alignment);
} catch (...) {
return nullptr;
}
operator new[](std::size_t size, std::align_val_t alignment, std::nothrow_t const& tag) noexcept {
return operator new(size, alignment, tag);
}
2 changes: 1 addition & 1 deletion src/ll/core/tweak/ModifyMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MimallocMemoryAllocatorWithCheck : public MimallocMemoryAllocator {
virtual void release(void* ptr) {
if (mi_is_in_heap_region(ptr)) [[likely]] {
mi_free(ptr);
} else {
} else if (ptr) [[unlikely]] {
free(ptr);
}
}
Expand Down

0 comments on commit b4a4c44

Please sign in to comment.