From 6b5102d2e75d260a1e9083c78fc34659527477ea Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sat, 11 Feb 2023 14:21:04 +0100 Subject: [PATCH] [Platform] [x86 Real Mode] Implement Turbo C++ 4.0J exception handler removal Full version with support for `operator new`. Part of P0230, funded by [Anonymous]. --- platform/x86real/noexcept.cpp | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 platform/x86real/noexcept.cpp diff --git a/platform/x86real/noexcept.cpp b/platform/x86real/noexcept.cpp new file mode 100644 index 00000000..28365cbf --- /dev/null +++ b/platform/x86real/noexcept.cpp @@ -0,0 +1,50 @@ +// Remove C++ exception handler bloat on Borland compilers +// (see https://community.embarcadero.com/article/technical-articles/162/14700) +// ---------------------------------------------------------------------------- + +#pragma option -zC_TEXT + +#if defined(__TURBOC__) && defined(__MSDOS__) + +#include +#include +#include "platform.h" + +extern "C" void __cdecl _ExceptInit(void) +{ +} + +new_handler _new_handler = nullptr; + +new_handler __cdecl set_new_handler(new_handler p) +{ + new_handler t = _new_handler; + _new_handler = p; + return t; +} + +void* __cdecl operator new(size_t size) +{ + void* p = nullptr; + if(size == 0) { + size = 1; + } + while(((p = malloc(size)) == nullptr) && (_new_handler != nullptr)) { + _new_handler(); + } + return p; +} + +void far* __cdecl operator new(uint32_t size) +{ + void far* p = nullptr; + if(size == 0) { + size = 1; + } + while(((p = farmalloc(size)) == nullptr) && (_new_handler != nullptr)) { + _new_handler(); + } + return p; +} + +#endif