include: Add constants for CryptProtectMemory/CryptUnprotectMemory.
[wine.git] / dlls / msvcrt / cpp.c
blobf7127a392ab302b65297e885596dee06528941d9
1 /*
2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003, 2004 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winternl.h"
29 #include "wine/exception.h"
30 #include "wine/debug.h"
31 #include "msvcrt.h"
32 #include "cppexcept.h"
33 #include "mtdll.h"
34 #include "cxx.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 struct __type_info_node
40 void *memPtr;
41 struct __type_info_node* next;
44 typedef exception bad_cast;
45 typedef exception bad_typeid;
46 typedef exception __non_rtti_object;
48 extern const vtable_ptr MSVCRT_exception_vtable;
49 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
50 extern const vtable_ptr MSVCRT_bad_cast_vtable;
51 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
52 extern const vtable_ptr MSVCRT_type_info_vtable;
54 /* get the vtable pointer for a C++ object */
55 static inline const vtable_ptr *get_vtable( void *obj )
57 return *(const vtable_ptr **)obj;
60 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
62 const vtable_ptr *vtable = get_vtable( cppobj );
63 return (const rtti_object_locator *)vtable[-1];
66 #ifndef __x86_64__
67 static void dump_obj_locator( const rtti_object_locator *ptr )
69 int i;
70 const rtti_object_hierarchy *h = ptr->type_hierarchy;
72 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
73 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
74 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
75 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
76 h->signature, h->attributes, h->array_len, h->base_classes );
77 for (i = 0; i < h->array_len; i++)
79 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
80 h->base_classes->bases[i],
81 h->base_classes->bases[i]->num_base_classes,
82 h->base_classes->bases[i]->offsets.this_offset,
83 h->base_classes->bases[i]->offsets.vbase_descr,
84 h->base_classes->bases[i]->offsets.vbase_offset,
85 h->base_classes->bases[i]->attributes,
86 h->base_classes->bases[i]->type_descriptor,
87 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
91 #else
93 static void dump_obj_locator( const rtti_object_locator *ptr )
95 int i;
96 char *base = ptr->signature == 0 ? RtlPcToFileHeader((void*)ptr, (void**)&base) : (char*)ptr - ptr->object_locator;
97 const rtti_object_hierarchy *h = (const rtti_object_hierarchy*)(base + ptr->type_hierarchy);
98 const type_info *type_descriptor = (const type_info*)(base + ptr->type_descriptor);
100 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
101 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
102 type_descriptor, dbgstr_type_info(type_descriptor), h );
103 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
104 h->signature, h->attributes, h->array_len, base + h->base_classes );
105 for (i = 0; i < h->array_len; i++)
107 const rtti_base_descriptor *bases = (rtti_base_descriptor*)(base +
108 ((const rtti_base_array*)(base + h->base_classes))->bases[i]);
110 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
111 bases,
112 bases->num_base_classes,
113 bases->offsets.this_offset,
114 bases->offsets.vbase_descr,
115 bases->offsets.vbase_offset,
116 bases->attributes,
117 base + bases->type_descriptor,
118 dbgstr_type_info((const type_info*)(base + bases->type_descriptor)) );
121 #endif
123 /* Internal common ctor for exception */
124 static void EXCEPTION_ctor(exception *_this, const char** name)
126 _this->vtable = &MSVCRT_exception_vtable;
127 if (*name)
129 unsigned int name_len = strlen(*name) + 1;
130 _this->name = MSVCRT_malloc(name_len);
131 memcpy(_this->name, *name, name_len);
132 _this->do_free = TRUE;
134 else
136 _this->name = NULL;
137 _this->do_free = FALSE;
141 /******************************************************************
142 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
144 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor,8)
145 exception * __thiscall MSVCRT_exception_ctor(exception * _this, const char ** name)
147 TRACE("(%p,%s)\n", _this, *name);
148 EXCEPTION_ctor(_this, name);
149 return _this;
152 /******************************************************************
153 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
155 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc,12)
156 exception * __thiscall MSVCRT_exception_ctor_noalloc(exception * _this, char ** name, int noalloc)
158 TRACE("(%p,%s)\n", _this, *name);
159 _this->vtable = &MSVCRT_exception_vtable;
160 _this->name = *name;
161 _this->do_free = FALSE;
162 return _this;
165 /******************************************************************
166 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
168 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
169 exception * __thiscall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
171 TRACE("(%p,%p)\n", _this, rhs);
173 if (!rhs->do_free)
175 _this->vtable = &MSVCRT_exception_vtable;
176 _this->name = rhs->name;
177 _this->do_free = FALSE;
179 else
180 EXCEPTION_ctor(_this, (const char**)&rhs->name);
181 TRACE("name = %s\n", _this->name);
182 return _this;
185 /******************************************************************
186 * ??0exception@@QAE@XZ (MSVCRT.@)
188 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
189 exception * __thiscall MSVCRT_exception_default_ctor(exception * _this)
191 static const char* empty = NULL;
193 TRACE("(%p)\n", _this);
194 EXCEPTION_ctor(_this, &empty);
195 return _this;
198 /******************************************************************
199 * ??1exception@@UAE@XZ (MSVCRT.@)
201 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
202 void __thiscall MSVCRT_exception_dtor(exception * _this)
204 TRACE("(%p)\n", _this);
205 _this->vtable = &MSVCRT_exception_vtable;
206 if (_this->do_free) MSVCRT_free(_this->name);
209 /******************************************************************
210 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
212 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
213 exception * __thiscall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
215 TRACE("(%p %p)\n", _this, rhs);
216 if (_this != rhs)
218 MSVCRT_exception_dtor(_this);
219 MSVCRT_exception_copy_ctor(_this, rhs);
221 TRACE("name = %s\n", _this->name);
222 return _this;
225 /******************************************************************
226 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
228 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
229 void * __thiscall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
231 TRACE("(%p %x)\n", _this, flags);
232 if (flags & 2)
234 /* we have an array, with the number of elements stored before the first object */
235 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
237 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
238 MSVCRT_operator_delete(ptr);
240 else
242 MSVCRT_exception_dtor(_this);
243 if (flags & 1) MSVCRT_operator_delete(_this);
245 return _this;
248 /******************************************************************
249 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
251 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
252 void * __thiscall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
254 TRACE("(%p %x)\n", _this, flags);
255 MSVCRT_exception_dtor(_this);
256 if (flags & 1) MSVCRT_operator_delete(_this);
257 return _this;
260 /******************************************************************
261 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
263 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
264 const char * __thiscall MSVCRT_what_exception(exception * _this)
266 TRACE("(%p) returning %s\n", _this, _this->name);
267 return _this->name ? _this->name : "Unknown exception";
270 /******************************************************************
271 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
273 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor,8)
274 bad_typeid * __thiscall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
276 TRACE("(%p %p)\n", _this, rhs);
277 MSVCRT_exception_copy_ctor(_this, rhs);
278 _this->vtable = &MSVCRT_bad_typeid_vtable;
279 return _this;
282 /******************************************************************
283 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
285 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
286 bad_typeid * __thiscall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
288 TRACE("(%p %s)\n", _this, name);
289 EXCEPTION_ctor(_this, &name);
290 _this->vtable = &MSVCRT_bad_typeid_vtable;
291 return _this;
294 /******************************************************************
295 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
297 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor,4)
298 bad_typeid * __thiscall MSVCRT_bad_typeid_default_ctor(bad_typeid * _this)
300 return MSVCRT_bad_typeid_ctor( _this, "bad typeid" );
303 /******************************************************************
304 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
306 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
307 void __thiscall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
309 TRACE("(%p)\n", _this);
310 MSVCRT_exception_dtor(_this);
313 /******************************************************************
314 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
316 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
317 bad_typeid * __thiscall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
319 TRACE("(%p %p)\n", _this, rhs);
320 MSVCRT_exception_opequals(_this, rhs);
321 return _this;
324 /******************************************************************
325 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
327 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
328 void * __thiscall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
330 TRACE("(%p %x)\n", _this, flags);
331 if (flags & 2)
333 /* we have an array, with the number of elements stored before the first object */
334 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
336 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
337 MSVCRT_operator_delete(ptr);
339 else
341 MSVCRT_bad_typeid_dtor(_this);
342 if (flags & 1) MSVCRT_operator_delete(_this);
344 return _this;
347 /******************************************************************
348 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
350 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
351 void * __thiscall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
353 TRACE("(%p %x)\n", _this, flags);
354 MSVCRT_bad_typeid_dtor(_this);
355 if (flags & 1) MSVCRT_operator_delete(_this);
356 return _this;
359 /******************************************************************
360 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
362 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor,8)
363 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
364 const __non_rtti_object * rhs)
366 TRACE("(%p %p)\n", _this, rhs);
367 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
368 _this->vtable = &MSVCRT___non_rtti_object_vtable;
369 return _this;
372 /******************************************************************
373 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
375 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
376 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
377 const char * name)
379 TRACE("(%p %s)\n", _this, name);
380 EXCEPTION_ctor(_this, &name);
381 _this->vtable = &MSVCRT___non_rtti_object_vtable;
382 return _this;
385 /******************************************************************
386 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
388 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
389 void __thiscall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
391 TRACE("(%p)\n", _this);
392 MSVCRT_bad_typeid_dtor(_this);
395 /******************************************************************
396 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
398 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals,8)
399 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
400 const __non_rtti_object *rhs)
402 TRACE("(%p %p)\n", _this, rhs);
403 MSVCRT_bad_typeid_opequals(_this, rhs);
404 return _this;
407 /******************************************************************
408 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
410 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor,8)
411 void * __thiscall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
413 TRACE("(%p %x)\n", _this, flags);
414 if (flags & 2)
416 /* we have an array, with the number of elements stored before the first object */
417 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
419 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
420 MSVCRT_operator_delete(ptr);
422 else
424 MSVCRT___non_rtti_object_dtor(_this);
425 if (flags & 1) MSVCRT_operator_delete(_this);
427 return _this;
430 /******************************************************************
431 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
433 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor,8)
434 void * __thiscall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
436 TRACE("(%p %x)\n", _this, flags);
437 MSVCRT___non_rtti_object_dtor(_this);
438 if (flags & 1) MSVCRT_operator_delete(_this);
439 return _this;
442 /******************************************************************
443 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
444 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
446 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
447 bad_cast * __thiscall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
449 TRACE("(%p %s)\n", _this, *name);
450 EXCEPTION_ctor(_this, name);
451 _this->vtable = &MSVCRT_bad_cast_vtable;
452 return _this;
455 /******************************************************************
456 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
458 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor,8)
459 bad_cast * __thiscall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
461 TRACE("(%p %p)\n", _this, rhs);
462 MSVCRT_exception_copy_ctor(_this, rhs);
463 _this->vtable = &MSVCRT_bad_cast_vtable;
464 return _this;
467 /******************************************************************
468 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
470 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr,8)
471 bad_cast * __thiscall MSVCRT_bad_cast_ctor_charptr(bad_cast * _this, const char * name)
473 TRACE("(%p %s)\n", _this, name);
474 EXCEPTION_ctor(_this, &name);
475 _this->vtable = &MSVCRT_bad_cast_vtable;
476 return _this;
479 /******************************************************************
480 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
482 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor,4)
483 bad_cast * __thiscall MSVCRT_bad_cast_default_ctor(bad_cast * _this)
485 return MSVCRT_bad_cast_ctor_charptr( _this, "bad cast" );
488 /******************************************************************
489 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
491 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
492 void __thiscall MSVCRT_bad_cast_dtor(bad_cast * _this)
494 TRACE("(%p)\n", _this);
495 MSVCRT_exception_dtor(_this);
498 /******************************************************************
499 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
501 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
502 bad_cast * __thiscall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
504 TRACE("(%p %p)\n", _this, rhs);
505 MSVCRT_exception_opequals(_this, rhs);
506 return _this;
509 /******************************************************************
510 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
512 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
513 void * __thiscall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
515 TRACE("(%p %x)\n", _this, flags);
516 if (flags & 2)
518 /* we have an array, with the number of elements stored before the first object */
519 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
521 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
522 MSVCRT_operator_delete(ptr);
524 else
526 MSVCRT_bad_cast_dtor(_this);
527 if (flags & 1) MSVCRT_operator_delete(_this);
529 return _this;
532 /******************************************************************
533 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
535 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
536 void * __thiscall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
538 TRACE("(%p %x)\n", _this, flags);
539 MSVCRT_bad_cast_dtor(_this);
540 if (flags & 1) MSVCRT_operator_delete(_this);
541 return _this;
544 /******************************************************************
545 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
547 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals,8)
548 int __thiscall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
550 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
551 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
552 return ret;
555 /******************************************************************
556 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
558 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals,8)
559 int __thiscall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
561 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
562 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
563 return ret;
566 /******************************************************************
567 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
569 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
570 int __thiscall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
572 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
573 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
574 return ret;
577 /******************************************************************
578 * ??1type_info@@UAE@XZ (MSVCRT.@)
580 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
581 void __thiscall MSVCRT_type_info_dtor(type_info * _this)
583 TRACE("(%p)\n", _this);
584 MSVCRT_free(_this->name);
587 /******************************************************************
588 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
590 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
591 const char * __thiscall MSVCRT_type_info_name(type_info * _this)
593 if (!_this->name)
595 /* Create and set the demangled name */
596 /* Note: mangled name in type_info struct always starts with a '.', while
597 * it isn't valid for mangled name.
598 * Is this '.' really part of the mangled name, or has it some other meaning ?
600 char* name = __unDName(0, _this->mangled + 1, 0,
601 MSVCRT_malloc, MSVCRT_free, UNDNAME_NO_ARGUMENTS | UNDNAME_32_BIT_DECODE);
602 if (name)
604 unsigned int len = strlen(name);
606 /* It seems _unDName may leave blanks at the end of the demangled name */
607 while (len && name[--len] == ' ')
608 name[len] = '\0';
610 if (InterlockedCompareExchangePointer((void**)&_this->name, name, NULL))
612 /* Another thread set this member since we checked above - use it */
613 MSVCRT_free(name);
617 TRACE("(%p) returning %s\n", _this, _this->name);
618 return _this->name;
621 /******************************************************************
622 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
624 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
625 const char * __thiscall MSVCRT_type_info_raw_name(type_info * _this)
627 TRACE("(%p) returning %s\n", _this, _this->mangled);
628 return _this->mangled;
631 /* Unexported */
632 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
633 void * __thiscall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
635 TRACE("(%p %x)\n", _this, flags);
636 if (flags & 2)
638 /* we have an array, with the number of elements stored before the first object */
639 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
641 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
642 MSVCRT_operator_delete(ptr);
644 else
646 MSVCRT_type_info_dtor(_this);
647 if (flags & 1) MSVCRT_operator_delete(_this);
649 return _this;
652 #ifndef __GNUC__
653 void __asm_dummy_vtables(void) {
654 #endif
656 __ASM_VTABLE(type_info,
657 VTABLE_ADD_FUNC(MSVCRT_type_info_vector_dtor));
658 __ASM_VTABLE(exception,
659 VTABLE_ADD_FUNC(MSVCRT_exception_vector_dtor)
660 VTABLE_ADD_FUNC(MSVCRT_what_exception));
661 #if _MSVCR_VER >= 80
662 __ASM_VTABLE(exception_old,
663 VTABLE_ADD_FUNC(MSVCRT_exception_vector_dtor)
664 VTABLE_ADD_FUNC(MSVCRT_what_exception));
665 #endif
666 __ASM_VTABLE(bad_typeid,
667 VTABLE_ADD_FUNC(MSVCRT_bad_typeid_vector_dtor)
668 VTABLE_ADD_FUNC(MSVCRT_what_exception));
669 __ASM_VTABLE(bad_cast,
670 VTABLE_ADD_FUNC(MSVCRT_bad_cast_vector_dtor)
671 VTABLE_ADD_FUNC(MSVCRT_what_exception));
672 __ASM_VTABLE(__non_rtti_object,
673 VTABLE_ADD_FUNC(MSVCRT___non_rtti_object_vector_dtor)
674 VTABLE_ADD_FUNC(MSVCRT_what_exception));
676 #ifndef __GNUC__
678 #endif
680 DEFINE_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" )
681 #if _MSVCR_VER >= 80
682 DEFINE_RTTI_DATA0( exception, 0, ".?AVexception@std@@" )
683 DEFINE_RTTI_DATA0( exception_old, 0, ".?AVexception@@" )
684 DEFINE_RTTI_DATA1( bad_typeid, 0, &exception_rtti_base_descriptor, ".?AVbad_typeid@std@@" )
685 DEFINE_RTTI_DATA1( bad_cast, 0, &exception_rtti_base_descriptor, ".?AVbad_cast@std@@" )
686 DEFINE_RTTI_DATA2( __non_rtti_object, 0, &bad_typeid_rtti_base_descriptor, &exception_rtti_base_descriptor, ".?AV__non_rtti_object@std@@" )
687 #else
688 DEFINE_RTTI_DATA0( exception, 0, ".?AVexception@@" )
689 DEFINE_RTTI_DATA1( bad_typeid, 0, &exception_rtti_base_descriptor, ".?AVbad_typeid@@" )
690 DEFINE_RTTI_DATA1( bad_cast, 0, &exception_rtti_base_descriptor, ".?AVbad_cast@@" )
691 DEFINE_RTTI_DATA2( __non_rtti_object, 0, &bad_typeid_rtti_base_descriptor, &exception_rtti_base_descriptor, ".?AV__non_rtti_object@@" )
692 #endif
694 DEFINE_EXCEPTION_TYPE_INFO( exception, 0, NULL, NULL )
695 DEFINE_EXCEPTION_TYPE_INFO( bad_typeid, 1, &exception_cxx_type_info, NULL )
696 DEFINE_EXCEPTION_TYPE_INFO( bad_cast, 1, &exception_cxx_type_info, NULL )
697 DEFINE_EXCEPTION_TYPE_INFO( __non_rtti_object, 2, &bad_typeid_cxx_type_info, &exception_cxx_type_info )
699 #if _MSVCR_VER >= 80
700 typedef exception bad_alloc;
701 extern const vtable_ptr MSVCRT_bad_alloc_vtable;
703 static void bad_alloc_ctor(bad_alloc *this, const char **name)
705 MSVCRT_exception_ctor(this, name);
706 this->vtable = &MSVCRT_bad_alloc_vtable;
709 /* bad_alloc class implementation */
710 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_alloc_copy_ctor,8)
711 bad_alloc * __thiscall MSVCRT_bad_alloc_copy_ctor(bad_alloc * _this, const bad_alloc * rhs)
713 TRACE("(%p %p)\n", _this, rhs);
714 MSVCRT_exception_copy_ctor(_this, rhs);
715 _this->vtable = &MSVCRT_bad_alloc_vtable;
716 return _this;
719 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_alloc_dtor,4)
720 void __thiscall MSVCRT_bad_alloc_dtor(bad_alloc * _this)
722 TRACE("(%p)\n", _this);
723 MSVCRT_exception_dtor(_this);
726 __ASM_VTABLE(bad_alloc,
727 VTABLE_ADD_FUNC(MSVCRT_exception_vector_dtor)
728 VTABLE_ADD_FUNC(MSVCRT_what_exception));
729 DEFINE_RTTI_DATA1( bad_alloc, 0, &exception_rtti_base_descriptor, ".?AVbad_alloc@std@@" )
730 DEFINE_EXCEPTION_TYPE_INFO( bad_alloc, 1, &exception_cxx_type_info, NULL )
732 void throw_bad_alloc(const char *str)
734 bad_alloc e;
735 bad_alloc_ctor(&e, &str);
736 _CxxThrowException(&e, &bad_alloc_exception_type);
738 #endif
740 void msvcrt_init_exception(void *base)
742 #ifdef __x86_64__
743 init_type_info_rtti(base);
744 init_exception_rtti(base);
745 #if _MSVCR_VER >= 80
746 init_exception_old_rtti(base);
747 init_bad_alloc_rtti(base);
748 #endif
749 init_bad_typeid_rtti(base);
750 init_bad_cast_rtti(base);
751 init___non_rtti_object_rtti(base);
753 init_exception_cxx(base);
754 init_bad_typeid_cxx(base);
755 init_bad_cast_cxx(base);
756 init___non_rtti_object_cxx(base);
757 #if _MSVCR_VER >= 80
758 init_bad_alloc_cxx(base);
759 #endif
760 #endif
764 /******************************************************************
765 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
767 * Install a handler to be called when terminate() is called.
769 * PARAMS
770 * func [I] Handler function to install
772 * RETURNS
773 * The previously installed handler function, if any.
775 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
777 thread_data_t *data = msvcrt_get_thread_data();
778 MSVCRT_terminate_function previous = data->terminate_handler;
779 TRACE("(%p) returning %p\n",func,previous);
780 data->terminate_handler = func;
781 return previous;
784 /******************************************************************
785 * _get_terminate (MSVCRT.@)
787 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
789 thread_data_t *data = msvcrt_get_thread_data();
790 TRACE("returning %p\n", data->terminate_handler);
791 return data->terminate_handler;
794 /******************************************************************
795 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
797 * Install a handler to be called when unexpected() is called.
799 * PARAMS
800 * func [I] Handler function to install
802 * RETURNS
803 * The previously installed handler function, if any.
805 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
807 thread_data_t *data = msvcrt_get_thread_data();
808 MSVCRT_unexpected_function previous = data->unexpected_handler;
809 TRACE("(%p) returning %p\n",func,previous);
810 data->unexpected_handler = func;
811 return previous;
814 /******************************************************************
815 * _get_unexpected (MSVCRT.@)
817 MSVCRT_unexpected_function CDECL MSVCRT__get_unexpected(void)
819 thread_data_t *data = msvcrt_get_thread_data();
820 TRACE("returning %p\n", data->unexpected_handler);
821 return data->unexpected_handler;
824 /******************************************************************
825 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
827 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
829 thread_data_t *data = msvcrt_get_thread_data();
830 MSVCRT__se_translator_function previous = data->se_translator;
831 TRACE("(%p) returning %p\n",func,previous);
832 data->se_translator = func;
833 return previous;
836 /******************************************************************
837 * ?terminate@@YAXXZ (MSVCRT.@)
839 * Default handler for an unhandled exception.
841 * PARAMS
842 * None.
844 * RETURNS
845 * This function does not return. Either control resumes from any
846 * handler installed by calling set_terminate(), or (by default) abort()
847 * is called.
849 void CDECL MSVCRT_terminate(void)
851 thread_data_t *data = msvcrt_get_thread_data();
852 if (data->terminate_handler) data->terminate_handler();
853 MSVCRT_abort();
856 /******************************************************************
857 * ?unexpected@@YAXXZ (MSVCRT.@)
859 void CDECL MSVCRT_unexpected(void)
861 thread_data_t *data = msvcrt_get_thread_data();
862 if (data->unexpected_handler) data->unexpected_handler();
863 MSVCRT_terminate();
867 /******************************************************************
868 * __RTtypeid (MSVCRT.@)
870 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
872 * PARAMS
873 * cppobj [I] C++ object to get type information for.
875 * RETURNS
876 * Success: A type_info object describing cppobj.
877 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
878 * exception is thrown. If cppobj is NULL, a bad_typeid exception
879 * is thrown. In either case, this function does not return.
881 * NOTES
882 * This function is usually called by compiler generated code as a result
883 * of using one of the C++ dynamic cast statements.
885 #ifndef __x86_64__
886 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
888 const type_info *ret;
890 if (!cppobj)
892 bad_typeid e;
893 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
894 _CxxThrowException( &e, &bad_typeid_exception_type );
895 return NULL;
898 __TRY
900 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
901 ret = obj_locator->type_descriptor;
903 __EXCEPT_PAGE_FAULT
905 __non_rtti_object e;
906 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
907 _CxxThrowException( &e, &__non_rtti_object_exception_type );
908 return NULL;
910 __ENDTRY
911 return ret;
914 #else
916 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
918 const type_info *ret;
920 if (!cppobj)
922 bad_typeid e;
923 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
924 _CxxThrowException( &e, &bad_typeid_exception_type );
925 return NULL;
928 __TRY
930 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
931 char *base;
933 if(obj_locator->signature == 0)
934 base = RtlPcToFileHeader((void*)obj_locator, (void**)&base);
935 else
936 base = (char*)obj_locator - obj_locator->object_locator;
938 ret = (type_info*)(base + obj_locator->type_descriptor);
940 __EXCEPT_PAGE_FAULT
942 __non_rtti_object e;
943 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
944 _CxxThrowException( &e, &__non_rtti_object_exception_type );
945 return NULL;
947 __ENDTRY
948 return ret;
950 #endif
952 /******************************************************************
953 * __RTDynamicCast (MSVCRT.@)
955 * Dynamically cast a C++ object to one of its base classes.
957 * PARAMS
958 * cppobj [I] Any C++ object to cast
959 * unknown [I] Reserved, set to 0
960 * src [I] type_info object describing cppobj
961 * dst [I] type_info object describing the base class to cast to
962 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
964 * RETURNS
965 * Success: The address of cppobj, cast to the object described by dst.
966 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
967 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
968 * is thrown and this function does not return.
970 * NOTES
971 * This function is usually called by compiler generated code as a result
972 * of using one of the C++ dynamic cast statements.
974 #ifndef __x86_64__
975 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
976 type_info *src, type_info *dst,
977 int do_throw)
979 void *ret;
981 if (!cppobj) return NULL;
983 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
984 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
986 /* To cast an object at runtime:
987 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
988 * 2.Search for the destination type in the class hierarchy
989 * 3.If destination type is found, return base object address + dest offset
990 * Otherwise, fail the cast
992 * FIXME: the unknown parameter doesn't seem to be used for anything
994 __TRY
996 int i;
997 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
998 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
999 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1001 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1003 ret = NULL;
1004 for (i = 0; i < obj_bases->array_len; i++)
1006 const type_info *typ = base_desc[i]->type_descriptor;
1008 if (!strcmp(typ->mangled, dst->mangled))
1010 /* compute the correct this pointer for that base class */
1011 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1012 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1013 break;
1016 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1017 * to a reference, since references cannot be NULL.
1019 if (!ret && do_throw)
1021 const char *msg = "Bad dynamic_cast!";
1022 bad_cast e;
1023 MSVCRT_bad_cast_ctor( &e, &msg );
1024 _CxxThrowException( &e, &bad_cast_exception_type );
1027 __EXCEPT_PAGE_FAULT
1029 __non_rtti_object e;
1030 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1031 _CxxThrowException( &e, &__non_rtti_object_exception_type );
1032 return NULL;
1034 __ENDTRY
1035 return ret;
1038 #else
1040 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1041 type_info *src, type_info *dst,
1042 int do_throw)
1044 void *ret;
1046 if (!cppobj) return NULL;
1048 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1049 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1051 __TRY
1053 int i;
1054 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1055 const rtti_object_hierarchy *obj_bases;
1056 const rtti_base_array *base_array;
1057 char *base;
1059 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1061 if(obj_locator->signature == 0)
1062 base = RtlPcToFileHeader((void*)obj_locator, (void**)&base);
1063 else
1064 base = (char*)obj_locator - obj_locator->object_locator;
1066 obj_bases = (const rtti_object_hierarchy*)(base + obj_locator->type_hierarchy);
1067 base_array = (const rtti_base_array*)(base + obj_bases->base_classes);
1069 ret = NULL;
1070 for (i = 0; i < obj_bases->array_len; i++)
1072 const rtti_base_descriptor *base_desc = (const rtti_base_descriptor*)(base + base_array->bases[i]);
1073 const type_info *typ = (const type_info*)(base + base_desc->type_descriptor);
1075 if (!strcmp(typ->mangled, dst->mangled))
1077 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1078 ret = get_this_pointer( &base_desc->offsets, this_ptr );
1079 break;
1082 if (!ret && do_throw)
1084 const char *msg = "Bad dynamic_cast!";
1085 bad_cast e;
1086 MSVCRT_bad_cast_ctor( &e, &msg );
1087 _CxxThrowException( &e, &bad_cast_exception_type );
1090 __EXCEPT_PAGE_FAULT
1092 __non_rtti_object e;
1093 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1094 _CxxThrowException( &e, &__non_rtti_object_exception_type );
1095 return NULL;
1097 __ENDTRY
1098 return ret;
1100 #endif
1103 /******************************************************************
1104 * __RTCastToVoid (MSVCRT.@)
1106 * Dynamically cast a C++ object to a void*.
1108 * PARAMS
1109 * cppobj [I] The C++ object to cast
1111 * RETURNS
1112 * Success: The base address of the object as a void*.
1113 * Failure: NULL, if cppobj is NULL or has no RTTI.
1115 * NOTES
1116 * This function is usually called by compiler generated code as a result
1117 * of using one of the C++ dynamic cast statements.
1119 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1121 void *ret;
1123 if (!cppobj) return NULL;
1125 __TRY
1127 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1128 ret = (char *)cppobj - obj_locator->base_class_offset;
1130 __EXCEPT_PAGE_FAULT
1132 __non_rtti_object e;
1133 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1134 _CxxThrowException( &e, &__non_rtti_object_exception_type );
1135 return NULL;
1137 __ENDTRY
1138 return ret;
1142 /*********************************************************************
1143 * _CxxThrowException (MSVCRT.@)
1145 #ifndef __x86_64__
1146 void WINAPI _CxxThrowException( exception *object, const cxx_exception_type *type )
1148 ULONG_PTR args[3];
1150 args[0] = CXX_FRAME_MAGIC_VC6;
1151 args[1] = (ULONG_PTR)object;
1152 args[2] = (ULONG_PTR)type;
1153 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
1155 #else
1156 void WINAPI _CxxThrowException( exception *object, const cxx_exception_type *type )
1158 ULONG_PTR args[4];
1160 args[0] = CXX_FRAME_MAGIC_VC6;
1161 args[1] = (ULONG_PTR)object;
1162 args[2] = (ULONG_PTR)type;
1163 RtlPcToFileHeader( (void*)type, (void**)&args[3]);
1164 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 4, args );
1166 #endif
1168 /*********************************************************************
1169 * ?_is_exception_typeof@@YAHABVtype_info@@PAU_EXCEPTION_POINTERS@@@Z
1170 * ?_is_exception_typeof@@YAHAEBVtype_info@@PEAU_EXCEPTION_POINTERS@@@Z
1172 #ifndef __x86_64__
1173 int __cdecl _is_exception_typeof(const type_info *ti, EXCEPTION_POINTERS *ep)
1175 int ret = -1;
1177 TRACE("(%p %p)\n", ti, ep);
1179 __TRY
1181 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
1183 if (rec->ExceptionCode==CXX_EXCEPTION && rec->NumberParameters==3 &&
1184 (rec->ExceptionInformation[0]==CXX_FRAME_MAGIC_VC6 ||
1185 rec->ExceptionInformation[0]==CXX_FRAME_MAGIC_VC7 ||
1186 rec->ExceptionInformation[0]==CXX_FRAME_MAGIC_VC8))
1188 const cxx_type_info_table *tit = ((cxx_exception_type*)rec->ExceptionInformation[2])->type_info_table;
1189 int i;
1191 for (i=0; i<tit->count; i++) {
1192 if (ti==tit->info[i]->type_info || !strcmp(ti->mangled, tit->info[i]->type_info->mangled))
1194 ret = 1;
1195 break;
1199 if (i == tit->count)
1200 ret = 0;
1203 __EXCEPT_PAGE_FAULT
1204 __ENDTRY
1206 if(ret == -1)
1207 MSVCRT_terminate();
1208 return ret;
1210 #else
1211 int __cdecl _is_exception_typeof(const type_info *ti, EXCEPTION_POINTERS *ep)
1213 int ret = -1;
1215 TRACE("(%p %p)\n", ti, ep);
1217 __TRY
1219 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
1221 if (rec->ExceptionCode==CXX_EXCEPTION && rec->NumberParameters==4 &&
1222 (rec->ExceptionInformation[0]==CXX_FRAME_MAGIC_VC6 ||
1223 rec->ExceptionInformation[0]==CXX_FRAME_MAGIC_VC7 ||
1224 rec->ExceptionInformation[0]==CXX_FRAME_MAGIC_VC8))
1226 const cxx_exception_type *et = (cxx_exception_type*)rec->ExceptionInformation[2];
1227 const cxx_type_info_table *tit = (const cxx_type_info_table*)(rec->ExceptionInformation[3]+et->type_info_table);
1228 int i;
1230 for (i=0; i<tit->count; i++) {
1231 const cxx_type_info *cti = (const cxx_type_info*)(rec->ExceptionInformation[3]+tit->info[i]);
1232 const type_info *except_ti = (const type_info*)(rec->ExceptionInformation[3]+cti->type_info);
1233 if (ti==except_ti || !strcmp(ti->mangled, except_ti->mangled))
1235 ret = 1;
1236 break;
1240 if (i == tit->count)
1241 ret = 0;
1244 __EXCEPT_PAGE_FAULT
1245 __ENDTRY
1247 if(ret == -1)
1248 MSVCRT_terminate();
1249 return ret;
1251 #endif
1253 /*********************************************************************
1254 * __clean_type_info_names_internal (MSVCR100.@)
1256 void CDECL __clean_type_info_names_internal(void *p)
1258 FIXME("(%p) stub\n", p);
1261 /*********************************************************************
1262 * ?_name_internal_method@type_info@@QBEPBDPAU__type_info_node@@@Z (MSVCR100.@)
1264 DEFINE_THISCALL_WRAPPER(type_info_name_internal_method,8)
1265 const char * __thiscall type_info_name_internal_method(type_info * _this, struct __type_info_node *node)
1267 static int once;
1268 if (node && !once++) FIXME("type_info_node parameter ignored\n");
1270 return MSVCRT_type_info_name(_this);
1273 /* std::exception_ptr class helpers */
1274 typedef struct
1276 EXCEPTION_RECORD *rec;
1277 int *ref; /* not binary compatible with native msvcr100 */
1278 } exception_ptr;
1280 /*********************************************************************
1281 * ?__ExceptionPtrCreate@@YAXPAX@Z
1282 * ?__ExceptionPtrCreate@@YAXPEAX@Z
1284 void __cdecl __ExceptionPtrCreate(exception_ptr *ep)
1286 TRACE("(%p)\n", ep);
1288 ep->rec = NULL;
1289 ep->ref = NULL;
1292 #ifdef __i386__
1293 static inline void call_dtor(const cxx_exception_type *type, void *func, void *object)
1295 __asm__ __volatile__("call *%0" : : "m" (func), "c" (object) : "eax", "edx", "memory");
1297 #elif __x86_64__
1298 static inline void call_dtor(const cxx_exception_type *type, unsigned int dtor, void *object)
1300 char *base = RtlPcToFileHeader((void*)type, (void**)&base);
1301 void (__cdecl *func)(void*) = (void*)(base + dtor);
1302 func(object);
1304 #else
1305 #define call_dtor(type, func, object) ((void (__cdecl*)(void*))(func))(object)
1306 #endif
1308 /*********************************************************************
1309 * ?__ExceptionPtrDestroy@@YAXPAX@Z
1310 * ?__ExceptionPtrDestroy@@YAXPEAX@Z
1312 void __cdecl __ExceptionPtrDestroy(exception_ptr *ep)
1314 TRACE("(%p)\n", ep);
1316 if (!ep->rec)
1317 return;
1319 if (!InterlockedDecrement(ep->ref))
1321 if (ep->rec->ExceptionCode == CXX_EXCEPTION)
1323 const cxx_exception_type *type = (void*)ep->rec->ExceptionInformation[2];
1324 void *obj = (void*)ep->rec->ExceptionInformation[1];
1326 if (type && type->destructor) call_dtor(type, type->destructor, obj);
1327 HeapFree(GetProcessHeap(), 0, obj);
1330 HeapFree(GetProcessHeap(), 0, ep->rec);
1331 HeapFree(GetProcessHeap(), 0, ep->ref);
1335 /*********************************************************************
1336 * ?__ExceptionPtrCopy@@YAXPAXPBX@Z
1337 * ?__ExceptionPtrCopy@@YAXPEAXPEBX@Z
1339 void __cdecl __ExceptionPtrCopy(exception_ptr *ep, const exception_ptr *copy)
1341 TRACE("(%p %p)\n", ep, copy);
1343 /* don't destroy object stored in ep */
1344 *ep = *copy;
1345 if (ep->ref)
1346 InterlockedIncrement(copy->ref);
1349 /*********************************************************************
1350 * ?__ExceptionPtrRethrow@@YAXPBX@Z
1351 * ?__ExceptionPtrRethrow@@YAXPEBX@Z
1353 void __cdecl __ExceptionPtrRethrow(const exception_ptr *ep)
1355 TRACE("(%p)\n", ep);
1357 if (!ep->rec)
1359 static const char *exception_msg = "bad exception";
1360 exception e;
1362 MSVCRT_exception_ctor(&e, &exception_msg);
1363 _CxxThrowException(&e, &exception_exception_type);
1364 return;
1367 RaiseException(ep->rec->ExceptionCode, ep->rec->ExceptionFlags & (~EH_UNWINDING),
1368 ep->rec->NumberParameters, ep->rec->ExceptionInformation);
1371 #ifdef __i386__
1372 static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
1374 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
1375 if (has_vbase)
1376 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
1377 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
1378 : : "m" (func), "c" (this), "m" (src) : "eax", "edx", "memory" );
1379 else
1380 __asm__ __volatile__("pushl %2; call *%0"
1381 : : "m" (func), "c" (this), "m" (src) : "eax", "edx", "memory" );
1383 #else
1384 static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
1386 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
1387 if (has_vbase)
1388 ((void (__cdecl*)(void*, void*, BOOL))func)(this, src, 1);
1389 else
1390 ((void (__cdecl*)(void*, void*))func)(this, src);
1392 #endif
1394 /*********************************************************************
1395 * ?__ExceptionPtrCurrentException@@YAXPAX@Z
1396 * ?__ExceptionPtrCurrentException@@YAXPEAX@Z
1398 #ifndef __x86_64__
1399 void __cdecl __ExceptionPtrCurrentException(exception_ptr *ep)
1401 EXCEPTION_RECORD *rec = msvcrt_get_thread_data()->exc_record;
1403 TRACE("(%p)\n", ep);
1405 if (!rec)
1407 ep->rec = NULL;
1408 ep->ref = NULL;
1409 return;
1412 ep->rec = HeapAlloc(GetProcessHeap(), 0, sizeof(EXCEPTION_RECORD));
1413 ep->ref = HeapAlloc(GetProcessHeap(), 0, sizeof(int));
1415 *ep->rec = *rec;
1416 *ep->ref = 1;
1418 if (ep->rec->ExceptionCode == CXX_EXCEPTION)
1420 const cxx_exception_type *et = (void*)ep->rec->ExceptionInformation[2];
1421 const cxx_type_info *ti;
1422 void **data, *obj;
1424 ti = et->type_info_table->info[0];
1425 data = HeapAlloc(GetProcessHeap(), 0, ti->size);
1427 obj = (void*)ep->rec->ExceptionInformation[1];
1428 if (ti->flags & CLASS_IS_SIMPLE_TYPE)
1430 memcpy(data, obj, ti->size);
1431 if (ti->size == sizeof(void *)) *data = get_this_pointer(&ti->offsets, *data);
1433 else if (ti->copy_ctor)
1435 call_copy_ctor(ti->copy_ctor, data, get_this_pointer(&ti->offsets, obj),
1436 ti->flags & CLASS_HAS_VIRTUAL_BASE_CLASS);
1438 else
1439 memcpy(data, get_this_pointer(&ti->offsets, obj), ti->size);
1440 ep->rec->ExceptionInformation[1] = (ULONG_PTR)data;
1442 return;
1444 #else
1445 void __cdecl __ExceptionPtrCurrentException(exception_ptr *ep)
1447 EXCEPTION_RECORD *rec = msvcrt_get_thread_data()->exc_record;
1449 TRACE("(%p)\n", ep);
1451 if (!rec)
1453 ep->rec = NULL;
1454 ep->ref = NULL;
1455 return;
1458 ep->rec = HeapAlloc(GetProcessHeap(), 0, sizeof(EXCEPTION_RECORD));
1459 ep->ref = HeapAlloc(GetProcessHeap(), 0, sizeof(int));
1461 *ep->rec = *rec;
1462 *ep->ref = 1;
1464 if (ep->rec->ExceptionCode == CXX_EXCEPTION)
1466 const cxx_exception_type *et = (void*)ep->rec->ExceptionInformation[2];
1467 const cxx_type_info *ti;
1468 void **data, *obj;
1469 char *base = RtlPcToFileHeader((void*)et, (void**)&base);
1471 ti = (const cxx_type_info*)(base + ((const cxx_type_info_table*)(base + et->type_info_table))->info[0]);
1472 data = HeapAlloc(GetProcessHeap(), 0, ti->size);
1474 obj = (void*)ep->rec->ExceptionInformation[1];
1475 if (ti->flags & CLASS_IS_SIMPLE_TYPE)
1477 memcpy(data, obj, ti->size);
1478 if (ti->size == sizeof(void *)) *data = get_this_pointer(&ti->offsets, *data);
1480 else if (ti->copy_ctor)
1482 call_copy_ctor(base + ti->copy_ctor, data, get_this_pointer(&ti->offsets, obj),
1483 ti->flags & CLASS_HAS_VIRTUAL_BASE_CLASS);
1485 else
1486 memcpy(data, get_this_pointer(&ti->offsets, obj), ti->size);
1487 ep->rec->ExceptionInformation[1] = (ULONG_PTR)data;
1489 return;
1491 #endif
1493 /*********************************************************************
1494 * ?__ExceptionPtrToBool@@YA_NPBX@Z
1495 * ?__ExceptionPtrToBool@@YA_NPEBX@Z
1497 MSVCRT_bool __cdecl __ExceptionPtrToBool(exception_ptr *ep)
1499 return !!ep->rec;
1502 void* __cdecl __AdjustPointer(void *obj, const this_ptr_offsets *off)
1504 return get_this_pointer(off, obj);
1507 #if _MSVCR_VER >= 140
1508 typedef struct
1510 char *name;
1511 char mangled[1];
1512 } type_info140;
1514 typedef struct
1516 SLIST_ENTRY entry;
1517 char name[1];
1518 } type_info_entry;
1520 static void* CDECL type_info_entry_malloc(MSVCRT_size_t size)
1522 type_info_entry *ret = MSVCRT_malloc(FIELD_OFFSET(type_info_entry, name) + size);
1523 return ret->name;
1526 static void CDECL type_info_entry_free(void *ptr)
1528 ptr = (char*)ptr - FIELD_OFFSET(type_info_entry, name);
1529 MSVCRT_free(ptr);
1532 /******************************************************************
1533 * __std_type_info_compare (UCRTBASE.@)
1535 int CDECL MSVCRT_type_info_compare(const type_info140 *l, const type_info140 *r)
1537 int ret;
1539 if (l == r) ret = 0;
1540 else ret = strcmp(l->mangled + 1, r->mangled + 1);
1541 TRACE("(%p %p) returning %d\n", l, r, ret);
1542 return ret;
1545 /******************************************************************
1546 * __std_type_info_name (UCRTBASE.@)
1548 const char* CDECL MSVCRT_type_info_name_list(type_info140 *ti, SLIST_HEADER *header)
1550 if (!ti->name)
1552 char* name = __unDName(0, ti->mangled + 1, 0,
1553 type_info_entry_malloc, type_info_entry_free, UNDNAME_NO_ARGUMENTS | UNDNAME_32_BIT_DECODE);
1554 if (name)
1556 unsigned int len = strlen(name);
1558 while (len && name[--len] == ' ')
1559 name[len] = '\0';
1561 if (InterlockedCompareExchangePointer((void**)&ti->name, name, NULL))
1563 type_info_entry_free(name);
1565 else
1567 type_info_entry *entry = (type_info_entry*)(name-FIELD_OFFSET(type_info_entry, name));
1568 InterlockedPushEntrySList(header, &entry->entry);
1572 TRACE("(%p) returning %s\n", ti, ti->name);
1573 return ti->name;
1576 /******************************************************************
1577 * __std_type_info_destroy_list (UCRTBASE.@)
1579 void CDECL MSVCRT_type_info_destroy_list(SLIST_HEADER *header)
1581 SLIST_ENTRY *cur, *next;
1583 TRACE("(%p)\n", header);
1585 for(cur = InterlockedFlushSList(header); cur; cur = next)
1587 next = cur->Next;
1588 MSVCRT_free(cur);
1592 /******************************************************************
1593 * __std_type_info_hash (UCRTBASE.@)
1595 MSVCRT_size_t CDECL MSVCRT_type_info_hash(const type_info140 *ti)
1597 MSVCRT_size_t hash, fnv_prime;
1598 const char *p;
1600 #ifdef _WIN64
1601 hash = 0xcbf29ce484222325;
1602 fnv_prime = 0x100000001b3;
1603 #else
1604 hash = 0x811c9dc5;
1605 fnv_prime = 0x1000193;
1606 #endif
1608 TRACE("(%p)->%s\n", ti, ti->mangled);
1610 for(p = ti->mangled+1; *p; p++) {
1611 hash ^= *p;
1612 hash *= fnv_prime;
1615 #ifdef _WIN64
1616 hash ^= hash >> 32;
1617 #endif
1619 return hash;
1621 #endif