2 * Copyright 2021 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(concrt
);
30 CREATE_TYPE_INFO_VTABLE
31 CREATE_EXCEPTION_OBJECT(exception
)
33 static HMODULE msvcp140
;
35 extern const vtable_ptr exception_vtable
;
37 int CDECL
_callnewh(size_t size
);
38 void (__cdecl
*_Xmem
)(void);
39 void (__cdecl
*_Xout_of_range
)(const char*);
41 void* __cdecl
operator_new(size_t size
)
48 retval
= malloc(size
);
51 TRACE("(%Iu) returning %p\n", size
, retval
);
54 freed
= _callnewh(size
);
57 TRACE("(%Iu) out of memory\n", size
);
62 void __cdecl
operator_delete(void *mem
)
67 typedef exception runtime_error
;
68 extern const vtable_ptr runtime_error_vtable
;
70 DEFINE_THISCALL_WRAPPER(runtime_error_copy_ctor
,8)
71 runtime_error
* __thiscall
runtime_error_copy_ctor(runtime_error
*this, const runtime_error
*rhs
)
73 return __exception_copy_ctor(this, rhs
, &runtime_error_vtable
);
76 typedef exception range_error
;
77 extern const vtable_ptr range_error_vtable
;
79 DEFINE_THISCALL_WRAPPER(range_error_copy_ctor
,8)
80 range_error
* __thiscall
range_error_copy_ctor(range_error
*this, const range_error
*rhs
)
82 return __exception_copy_ctor(this, rhs
, &range_error_vtable
);
85 __ASM_BLOCK_BEGIN(vtables
)
86 __ASM_VTABLE(runtime_error
,
87 VTABLE_ADD_FUNC(exception_vector_dtor
)
88 VTABLE_ADD_FUNC(exception_what
));
89 __ASM_VTABLE(range_error
,
90 VTABLE_ADD_FUNC(exception_vector_dtor
)
91 VTABLE_ADD_FUNC(exception_what
));
94 DEFINE_CXX_DATA0( exception
, exception_dtor
)
95 DEFINE_RTTI_DATA1(runtime_error
, 0, &exception_rtti_base_descriptor
, ".?AVruntime_error@std@@")
96 DEFINE_CXX_TYPE_INFO(runtime_error
)
97 DEFINE_RTTI_DATA2(range_error
, 0, &runtime_error_rtti_base_descriptor
,
98 &exception_rtti_base_descriptor
, ".?AVrange_error@std@@")
99 DEFINE_CXX_DATA2(range_error
, &runtime_error_cxx_type_info
,
100 &exception_cxx_type_info
, exception_dtor
)
102 void DECLSPEC_NORETURN
throw_range_error(const char *str
)
105 __exception_ctor(&e
, str
, &range_error_vtable
);
106 _CxxThrowException(&e
, &range_error_exception_type
);
109 void DECLSPEC_NORETURN
throw_exception(const char* msg
)
112 __exception_ctor(&e
, msg
, &exception_vtable
);
113 _CxxThrowException(&e
, &exception_exception_type
);
116 static BOOL
init_cxx_funcs(void)
118 msvcp140
= LoadLibraryA("msvcp140.dll");
121 FIXME("Failed to load msvcp140.dll\n");
125 _Xmem
= (void*)GetProcAddress(msvcp140
, "?_Xbad_alloc@std@@YAXXZ");
126 _Xout_of_range
= (void*)GetProcAddress(msvcp140
, sizeof(void*) > sizeof(int) ?
127 "?_Xout_of_range@std@@YAXPEBD@Z" : "?_Xout_of_range@std@@YAXPBD@Z");
128 if (!_Xmem
|| !_Xout_of_range
)
130 FreeLibrary(msvcp140
);
137 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
141 case DLL_PROCESS_ATTACH
:
142 if (!init_cxx_funcs()) return FALSE
;
144 init_exception_rtti((char*)inst
);
145 init_range_error_rtti((char*)inst
);
146 init_runtime_error_rtti((char*)inst
);
147 init_type_info_rtti((char*)inst
);
149 init_exception_cxx((char*)inst
);
150 init_runtime_error_cxx_type_info((char*)inst
);
151 init_range_error_cxx((char*)inst
);
153 msvcrt_init_concurrency(inst
);
154 init_concurrency_details(inst
);
156 case DLL_PROCESS_DETACH
:
158 FreeLibrary(msvcp140
);