Skip to content

Commit dad9110

Browse files
committed
Updated packaging of exception handling
1 parent c0b5bf6 commit dad9110

File tree

2 files changed

+61
-41
lines changed

2 files changed

+61
-41
lines changed

CMakeLists.txt

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
506506
endif()
507507

508508

509-
function(compile_object name ${ARGN})
510-
add_library(${name} OBJECT ${ARGN})
509+
function(compile name TYPE ${ARGN})
510+
add_library(${name} ${TYPE} ${ARGN})
511511
target_link_libraries(${name} snmalloc)
512512
set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET hidden INTERPROCEDURAL_OPTIMIZATION ${SNMALLOC_COMPILER_SUPPORT_IPO})
513513
target_compile_definitions(${name} PRIVATE "SNMALLOC_USE_${SNMALLOC_CLEANUP}")
@@ -553,12 +553,14 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
553553
install(TARGETS ${name} EXPORT snmallocConfig)
554554
endfunction()
555555

556-
function(compile_object_noexc name ${ARGN})
557-
compile_object(${name} ${ARGN})
556+
function(compile_noexc name TYPE ${ARGN})
557+
compile(${name} ${TYPE} ${ARGN})
558558

559+
target_compile_definitionS(${name} PRIVATE -DSNMALLOC_NOEXC)
559560
if(MSVC)
560561
target_compile_definitions(${name} PRIVATE -D_HAS_EXCEPTIONS=0)
561562
else()
563+
target_compile_options(${name} PRIVATE -fno-exceptions)
562564
# Ensure that we do not link against C++ stdlib when compiling shims.
563565
# If the compiler supports excluding the C++ stdlib implementation, use
564566
# it. Otherwise, fall back to linking the library as if it were C, which
@@ -573,53 +575,51 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
573575
endif()
574576
endfunction()
575577

576-
function(add_shim name type)
577-
add_library(${name} ${type})
578-
target_link_libraries(${name} ${ARGN})
579-
install(TARGETS ${name} EXPORT snmallocConfig)
580-
endfunction()
578+
# Various files for overriding libc/rust behaviours.
579+
set(MALLOC src/snmalloc/override/malloc.cc)
580+
set(NEW src/snmalloc/override/new.cc)
581+
set(MEMCPY src/snmalloc/override/memcpy.cc)
582+
set(RUST src/snmalloc/override/rust.cc)
583+
584+
set(ALLOC ${MALLOC} ${NEW})
585+
set(ALL ${ALLOC} ${MEMCPY})
586+
587+
# Exception handler code needs to be compiled with exceptions enabled.
588+
compile(snmalloc_exception_handler OBJECT src/snmalloc/override/failure.cc)
581589

582-
compile_object(snmalloc_exception_handler src/snmalloc/override/failure.cc)
583-
compile_object_noexc(snmalloc_core src/snmalloc/override/malloc.cc)
584-
compile_object_noexc(snmalloc_new src/snmalloc/override/new.cc)
585-
compile_object_noexc(snmalloc_memcpy src/snmalloc/override/memcpy.cc)
586-
compile_object_noexc(snmalloc_rust src/snmalloc/override/rust.cc)
587-
588-
compile_object(snmalloc_exception_handler_checks src/snmalloc/override/failure.cc)
589-
target_compile_definitions(snmalloc_exception_handler_checks PRIVATE SNMALLOC_CHECK_CLIENT)
590-
compile_object_noexc(snmalloc_core_checks src/snmalloc/override/malloc.cc)
591-
target_compile_definitions(snmalloc_core_checks PRIVATE SNMALLOC_CHECK_CLIENT)
592-
compile_object_noexc(snmalloc_new_checks src/snmalloc/override/new.cc)
593-
target_compile_definitions(snmalloc_new_checks PRIVATE SNMALLOC_CHECK_CLIENT)
594-
compile_object_noexc(snmalloc_memcpy_checks src/snmalloc/override/memcpy.cc)
595-
target_compile_definitions(snmalloc_memcpy_checks PRIVATE SNMALLOC_CHECK_CLIENT)
596-
compile_object_noexc(snmalloc_rust_checks src/snmalloc/override/rust.cc)
597-
target_compile_definitions(snmalloc_rust_checks PRIVATE SNMALLOC_CHECK_CLIENT)
598-
599-
add_shim(snmalloc-new-override STATIC snmalloc_new snmalloc_exception_handler)
590+
# Compile a versions without exceptions as that is what snmalloc previously did.
591+
compile_noexc(snmalloc_exception_handler_noexc OBJECT src/snmalloc/override/failure.cc)
592+
593+
compile_noexc(snmalloc-new-override STATIC ${NEW})
594+
target_link_libraries(snmalloc-new-override snmalloc_exception_handler)
600595

601596
if (SNMALLOC_STATIC_LIBRARY)
602-
add_shim(snmallocshim-static STATIC snmalloc_core snmalloc_exception_handler snmalloc_new snmalloc_memcpy)
597+
compile_noexc(snmallocshim-static STATIC src/snmalloc/override/malloc.cc src/snmalloc/override/new.cc src/snmalloc/override/memcpy.cc)
603598
target_compile_definitions(snmallocshim-static PRIVATE
604599
SNMALLOC_STATIC_LIBRARY_PREFIX=${SNMALLOC_STATIC_LIBRARY_PREFIX})
605600
endif ()
606601

607-
set(SNMALLOC_UNCHECK_OBJECTS snmalloc_core snmalloc_exception_handler snmalloc_new)
608-
set(SNMALLOC_CHECK_OBJECTS snmalloc_core_checks snmalloc_exception_handler_checks snmalloc_new_checks)
609-
610602
if(NOT WIN32)
611-
add_shim(snmallocshim SHARED ${SNMALLOC_UNCHECK_OBJECTS})
603+
compile_noexc(snmallocshim SHARED ${ALLOC})
604+
target_link_libraries(snmallocshim snmalloc_exception_handler_noexc)
605+
606+
compile(snmallocnew SHARED ${ALLOC})
607+
target_link_libraries(snmallocnew snmalloc_exception_handler)
608+
612609
if (SNMALLOC_MEMCPY_OVERRIDE)
613-
add_shim(snmallocshim-checks-memcpy-only SHARED ${SNMALLOC_UNCHECK_OBJECTS} snmalloc_memcpy)
614-
add_shim(snmallocshim-checks SHARED ${SNMALLOC_CHECK_OBJECTS} snmalloc_new_checks snmalloc_memcpy_checks)
610+
compile(snmallocshim-checks-memcpy-only SHARED ${ALL})
611+
target_link_libraries(snmallocshim-checks-memcpy-only snmalloc_exception_handler)
612+
compile(snmallocshim-checks SHARED ${ALL})
615613
else()
616-
add_shim(snmallocshim-checks SHARED ${SNMALLOC_CHECK_OBJECTS})
614+
compile(snmallocshim-checks SHARED ${MALLOC})
617615
endif()
616+
target_compile_definitions(snmallocshim-checks PRIVATE SNMALLOC_CHECK_CLIENT)
617+
target_link_libraries(snmallocshim-checks snmalloc_exception_handler)
618618
endif()
619619

620620
if(SNMALLOC_RUST_SUPPORT)
621-
add_shim(snmallocshim-rust STATIC snmalloc_rust)
622-
add_shim(snmallocshim-checks-rust STATIC snmalloc_rust_checks)
621+
compile_noexc(snmallocshim-rust STATIC ${RUST})
622+
compile_noexc(snmallocshim-checks-rust STATIC ${RUST})
623623
target_compile_definitions(snmallocshim-checks-rust PRIVATE SNMALLOC_CHECK_CLIENT)
624624
endif()
625625

src/snmalloc/override/failure.cc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,28 @@ namespace snmalloc
77
void* alloc_nothrow(size_t size);
88
void* alloc_throw(size_t size);
99

10-
template<bool ShouldThrow>
11-
class SetHandlerContinuations;
10+
#ifdef SNMALLOC_NOEXC
11+
// If we are compiling without exceptions, we cannot usefully use
12+
// new_handlers to retry allocations, so we just ignore them and
13+
// return nullptr on failure.
14+
void* failure_throw(std::size_t)
15+
{
16+
// If we are here, then the allocation failed.
17+
// Set errno to ENOMEM, as per the C standard.
18+
errno = ENOMEM;
19+
// Return nullptr on failure.
20+
return nullptr;
21+
}
1222

23+
void* failure_nothrow(std::size_t)
24+
{
25+
// If we are here, then the allocation failed.
26+
// Set errno to ENOMEM, as per the C standard.
27+
errno = ENOMEM;
28+
// Return nullptr on failure.
29+
return nullptr;
30+
}
31+
#else
1332
void* failure_throw(std::size_t size)
1433
{
1534
auto new_handler = std::get_new_handler();
@@ -20,7 +39,7 @@ namespace snmalloc
2039
// Retry now new_handler has been called.
2140
// I dislike the unbounded retrying here, but that seems to be what
2241
// other implementations do.
23-
return alloc_nothrow(size);
42+
return alloc_throw(size);
2443
}
2544

2645
// Throw std::bad_alloc on failure.
@@ -45,12 +64,13 @@ namespace snmalloc
4564
// Retry now new_handler has been called.
4665
return alloc_nothrow(size);
4766
}
48-
4967
// If we are here, then the allocation failed.
5068
// Set errno to ENOMEM, as per the C standard.
5169
errno = ENOMEM;
5270

5371
// Return nullptr on failure.
5472
return nullptr;
5573
}
74+
#endif
5675
}
76+

0 commit comments

Comments
 (0)