Skip to content

Commit

Permalink
Refactor new/delete overrides (#660)
Browse files Browse the repository at this point in the history
Produce static library that only overrides new and delete. This allows for a static library to be added that only overrides new and delete.
  • Loading branch information
mjp41 committed Jun 6, 2024
1 parent 846a926 commit 2dba088
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,11 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)

endfunction()

set(SHIM_FILES src/snmalloc/override/new.cc)
set(SHIM_FILES src/snmalloc/override/malloc.cc src/snmalloc/override/new.cc)
set(SHIM_FILES_MEMCPY src/snmalloc/override/memcpy.cc)

add_shim(snmalloc-new-override STATIC src/snmalloc/override/new.cc)

if (SNMALLOC_STATIC_LIBRARY)
add_shim(snmallocshim-static STATIC ${SHIM_FILES})
target_compile_definitions(snmallocshim-static PRIVATE
Expand Down
16 changes: 7 additions & 9 deletions src/snmalloc/override/new.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "malloc.cc"
#include "libc.h"

#ifdef _WIN32
# ifdef __clang__
Expand All @@ -16,8 +16,6 @@
# endif
#endif

using namespace snmalloc;

void* operator new(size_t size)
{
return snmalloc::libc::malloc(size);
Expand Down Expand Up @@ -70,25 +68,25 @@ void operator delete[](void* p, std::nothrow_t&)

void* operator new(size_t size, std::align_val_t val)
{
size = aligned_size(size_t(val), size);
size = snmalloc::aligned_size(size_t(val), size);
return snmalloc::libc::malloc(size);
}

void* operator new[](size_t size, std::align_val_t val)
{
size = aligned_size(size_t(val), size);
size = snmalloc::aligned_size(size_t(val), size);
return snmalloc::libc::malloc(size);
}

void* operator new(size_t size, std::align_val_t val, std::nothrow_t&)
{
size = aligned_size(size_t(val), size);
size = snmalloc::aligned_size(size_t(val), size);
return snmalloc::libc::malloc(size);
}

void* operator new[](size_t size, std::align_val_t val, std::nothrow_t&)
{
size = aligned_size(size_t(val), size);
size = snmalloc::aligned_size(size_t(val), size);
return snmalloc::libc::malloc(size);
}

Expand All @@ -104,12 +102,12 @@ void operator delete[](void* p, std::align_val_t) EXCEPTSPEC

void operator delete(void* p, size_t size, std::align_val_t val) EXCEPTSPEC
{
size = aligned_size(size_t(val), size);
size = snmalloc::aligned_size(size_t(val), size);
snmalloc::libc::free_sized(p, size);
}

void operator delete[](void* p, size_t size, std::align_val_t val) EXCEPTSPEC
{
size = aligned_size(size_t(val), size);
size = snmalloc::aligned_size(size_t(val), size);
snmalloc::libc::free_sized(p, size);
}

0 comments on commit 2dba088

Please sign in to comment.