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
25 #include "wine/debug.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
)
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
MSVCRT_operator_delete(void *mem
)
67 static void* __cdecl
MSVCRT_operator_new_aligned(size_t size
, size_t alignment
)
74 retval
= _aligned_malloc(size
, alignment
);
77 TRACE("(%Iu) returning %p\n", size
, retval
);
80 freed
= _callnewh(size
);
83 TRACE("(%Iu) out of memory\n", size
);
88 static void __cdecl
MSVCRT_operator_delete_aligned(void *mem
, size_t alignment
)
94 const vtable_ptr
*vtable
;
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
));
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)
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
);
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
);
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
)
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
)
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
)
194 static memory_resource
*default_resource
;
198 memory_resource
* __cdecl
_Aligned_new_delete_resource(void)
200 static memory_resource impl
= { &aligned_resource_vtable
};
204 memory_resource
* __cdecl
_Unaligned_new_delete_resource(void)
206 static memory_resource impl
= { &unaligned_resource_vtable
};
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();
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();
236 memory_resource
* __cdecl
null_memory_resource(void)
238 static memory_resource impl
= { &null_resource_vtable
};
244 static BOOL
init_cxx_funcs(void)
246 msvcp140
= LoadLibraryA("msvcp140.dll");
249 FIXME("Failed to load msvcp140.dll\n");
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
);
264 static void init_rtti(void *base
)
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
);
276 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
280 case DLL_PROCESS_ATTACH
:
281 if (!init_cxx_funcs()) return FALSE
;
284 case DLL_PROCESS_DETACH
:
286 FreeLibrary(msvcp140
);