msvcp: Remove MSVCP_ prefix from RTTI and vtable related code.
[wine/zf.git] / dlls / msvcp140_1 / msvcp140_1.c
blob823adedf3ac442ed49c657348b56ec40a9e46c68
1 /*
2 * Copyright 2021 Arkadiusz Hiler 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
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include <malloc.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wine/debug.h"
26 #include "winnls.h"
27 #include "cxx.h"
29 #define NEW_ALIGNMENT (2*sizeof(void*))
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
33 CREATE_TYPE_INFO_VTABLE
35 static HMODULE msvcp140;
37 int CDECL _callnewh(size_t size);
38 void (__cdecl *throw_bad_alloc)(void);
40 /* non-static, needed by type_info */
41 void* __cdecl MSVCRT_operator_new(size_t size)
43 void *retval;
44 int freed;
48 retval = malloc(size);
49 if (retval)
51 TRACE("(%Iu) returning %p\n", size, retval);
52 return retval;
54 freed = _callnewh(size);
55 } while (freed);
57 TRACE("(%Iu) out of memory\n", size);
58 throw_bad_alloc();
59 return NULL;
62 void __cdecl MSVCRT_operator_delete(void *mem)
64 free(mem);
67 static void* __cdecl MSVCRT_operator_new_aligned(size_t size, size_t alignment)
69 void *retval;
70 int freed;
74 retval = _aligned_malloc(size, alignment);
75 if (retval)
77 TRACE("(%Iu) returning %p\n", size, retval);
78 return retval;
80 freed = _callnewh(size);
81 } while (freed);
83 TRACE("(%Iu) out of memory\n", size);
84 throw_bad_alloc();
85 return NULL;
88 static void __cdecl MSVCRT_operator_delete_aligned(void *mem, size_t alignment)
90 _aligned_free(mem);
93 typedef struct {
94 const vtable_ptr *vtable;
95 } memory_resource;
97 extern const vtable_ptr aligned_resource_vtable;
98 extern const vtable_ptr unaligned_resource_vtable;
99 extern const vtable_ptr null_resource_vtable;
101 __ASM_BLOCK_BEGIN(vtables)
102 __ASM_VTABLE(aligned_resource,
103 VTABLE_ADD_FUNC(nop_dtor)
104 VTABLE_ADD_FUNC(aligned_do_allocate)
105 VTABLE_ADD_FUNC(aligned_do_deallocate)
106 VTABLE_ADD_FUNC(do_is_equal));
107 __ASM_VTABLE(unaligned_resource,
108 VTABLE_ADD_FUNC(nop_dtor)
109 VTABLE_ADD_FUNC(unaligned_do_allocate)
110 VTABLE_ADD_FUNC(unaligned_do_deallocate)
111 VTABLE_ADD_FUNC(do_is_equal));
112 __ASM_VTABLE(null_resource,
113 VTABLE_ADD_FUNC(nop_dtor)
114 VTABLE_ADD_FUNC(null_do_allocate)
115 VTABLE_ADD_FUNC(null_do_deallocate)
116 VTABLE_ADD_FUNC(do_is_equal));
117 __ASM_BLOCK_END
119 DEFINE_RTTI_BASE(base_memory_resource, 0, ".?AVmemory_resource@pmr@std@@")
120 DEFINE_RTTI_BASE(_Identity_equal_resource, 0, ".?AV_Identity_equal_resource@pmr@std@@")
121 DEFINE_RTTI_DATA2(aligned_resource, 0, &_Identity_equal_resource_rtti_base_descriptor,
122 &base_memory_resource_rtti_base_descriptor,
123 ".?AV_Aligned_new_delete_resource_impl@pmr@std@@")
124 DEFINE_RTTI_DATA2(unaligned_resource, 0, &_Identity_equal_resource_rtti_base_descriptor,
125 &base_memory_resource_rtti_base_descriptor,
126 ".?AV_Unaligned_new_delete_resource_impl@pmr@std@@")
127 DEFINE_RTTI_DATA2(null_resource, 0, &_Identity_equal_resource_rtti_base_descriptor,
128 &base_memory_resource_rtti_base_descriptor,
129 ".?AV_Null_resource@?1??null_memory_resource@@YAPAVmemory_resource@pmr@std@@XZ")
131 DEFINE_THISCALL_WRAPPER(nop_dtor, 4)
132 void __thiscall nop_dtor(void *this)
134 /* nop */
137 DEFINE_THISCALL_WRAPPER(do_is_equal, 8)
138 bool __thiscall do_is_equal(memory_resource *this, memory_resource *other)
140 return this == other;
143 DEFINE_THISCALL_WRAPPER(aligned_do_allocate, 12)
144 void* __thiscall aligned_do_allocate(memory_resource *this, size_t bytes, size_t alignment)
146 if (alignment > NEW_ALIGNMENT)
147 return MSVCRT_operator_new_aligned(bytes, alignment);
148 else
149 return MSVCRT_operator_new(bytes);
152 DEFINE_THISCALL_WRAPPER(aligned_do_deallocate, 16)
153 void __thiscall aligned_do_deallocate(memory_resource *this,
154 void *p, size_t bytes, size_t alignment)
156 if (alignment > NEW_ALIGNMENT)
157 MSVCRT_operator_delete_aligned(p, alignment);
158 else
159 MSVCRT_operator_delete(p);
162 DEFINE_THISCALL_WRAPPER(unaligned_do_allocate, 12)
163 void* __thiscall unaligned_do_allocate(memory_resource *this,
164 size_t bytes, size_t alignment)
166 if (alignment > NEW_ALIGNMENT)
167 throw_bad_alloc();
169 return MSVCRT_operator_new(bytes);
172 DEFINE_THISCALL_WRAPPER(unaligned_do_deallocate, 16)
173 void __thiscall unaligned_do_deallocate(memory_resource *this,
174 void *p, size_t bytes, size_t alignment)
176 MSVCRT_operator_delete(p);
179 DEFINE_THISCALL_WRAPPER(null_do_allocate, 12)
180 void* __thiscall null_do_allocate(memory_resource *this,
181 size_t bytes, size_t alignment)
183 throw_bad_alloc();
184 return NULL;
187 DEFINE_THISCALL_WRAPPER(null_do_deallocate, 16)
188 void __thiscall null_do_deallocate(memory_resource *this,
189 void *p, size_t bytes, size_t alignment)
191 /* nop */
194 static memory_resource *default_resource;
196 /* EXPORTS */
198 memory_resource* __cdecl _Aligned_new_delete_resource(void)
200 static memory_resource impl = { &aligned_resource_vtable };
201 return &impl;
204 memory_resource* __cdecl _Unaligned_new_delete_resource(void)
206 static memory_resource impl = { &unaligned_resource_vtable };
207 return &impl;
210 memory_resource* __cdecl _Aligned_get_default_resource(void)
212 if (default_resource) return default_resource;
213 return _Aligned_new_delete_resource();
216 memory_resource* __cdecl _Aligned_set_default_resource(memory_resource *res)
218 memory_resource *ret = InterlockedExchangePointer((void**)&default_resource, res);
219 if (!ret) ret = _Aligned_new_delete_resource();
220 return ret;
223 memory_resource* __cdecl _Unaligned_get_default_resource(void)
225 if (default_resource) return default_resource;
226 return _Unaligned_new_delete_resource();
229 memory_resource* __cdecl _Unaligned_set_default_resource(memory_resource *res)
231 memory_resource *ret = InterlockedExchangePointer((void**)&default_resource, res);
232 if (!ret) ret = _Unaligned_new_delete_resource();
233 return ret;
236 memory_resource* __cdecl null_memory_resource(void)
238 static memory_resource impl = { &null_resource_vtable };
239 return &impl;
242 /* DLL INIT */
244 static BOOL init_cxx_funcs(void)
246 msvcp140 = LoadLibraryA("msvcp140.dll");
247 if (!msvcp140)
249 FIXME("Failed to load msvcp140.dll\n");
250 return FALSE;
253 throw_bad_alloc = (void*)GetProcAddress(msvcp140, "?_Xbad_alloc@std@@YAXXZ");
254 if (!throw_bad_alloc)
256 FIXME("Failed to get address of ?_Xbad_alloc@std@@YAXXZ\n");
257 FreeLibrary(msvcp140);
258 return FALSE;
261 return TRUE;
264 static void init_rtti(void *base)
266 #ifdef __x86_64__
267 init_type_info_rtti(base);
268 init_base_memory_resource_rtti(base);
269 init__Identity_equal_resource_rtti(base);
270 init_aligned_resource_rtti(base);
271 init_unaligned_resource_rtti(base);
272 init_null_resource_rtti(base);
273 #endif
276 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
278 switch (reason)
280 case DLL_PROCESS_ATTACH:
281 if (!init_cxx_funcs()) return FALSE;
282 init_rtti(inst);
283 break;
284 case DLL_PROCESS_DETACH:
285 if (reserved) break;
286 FreeLibrary(msvcp140);
287 break;
289 return TRUE;