msvcrt: Add helper for exception throwing.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Piotr Caban 2017-03-27 10:26:54 +02:00 committed by Alexandre Julliard
parent 895928860d
commit 90307e067f
5 changed files with 33 additions and 30 deletions

View File

@ -880,27 +880,27 @@ void msvcrt_init_exception(void *base)
}
#if _MSVCR_VER >= 80
void throw_bad_alloc(const char *str)
void throw_exception(exception_type et, HRESULT hr, const char *str)
{
bad_alloc e;
bad_alloc_ctor(&e, &str);
_CxxThrowException(&e, &bad_alloc_exception_type);
}
#endif
switch(et) {
case EXCEPTION_BAD_ALLOC: {
bad_alloc e;
bad_alloc_ctor(&e, &str);
_CxxThrowException(&e, &bad_alloc_exception_type);
}
#if _MSVCR_VER >= 100
void throw_scheduler_resource_allocation_error(const char *str, HRESULT hr)
{
scheduler_resource_allocation_error e;
scheduler_resource_allocation_error_ctor_name(&e, str, hr);
_CxxThrowException(&e.e, &scheduler_resource_allocation_error_exception_type);
}
void throw_improper_lock(const char *str)
{
improper_lock e;
improper_lock_ctor_str(&e, str);
_CxxThrowException(&e, &improper_lock_exception_type);
case EXCEPTION_SCHEDULER_RESOURCE_ALLOCATION_ERROR: {
scheduler_resource_allocation_error e;
scheduler_resource_allocation_error_ctor_name(&e, str, hr);
_CxxThrowException(&e.e, &scheduler_resource_allocation_error_exception_type);
}
case EXCEPTION_IMPROPER_LOCK: {
improper_lock e;
improper_lock_ctor_str(&e, str);
_CxxThrowException(&e, &improper_lock_exception_type);
}
#endif
}
}
#endif

View File

@ -151,7 +151,7 @@ void* CDECL MSVCRT_operator_new(MSVCRT_size_t size)
TRACE("(%ld) out of memory\n", size);
#if _MSVCR_VER >= 80
throw_bad_alloc("bad allocation");
throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
#endif
return NULL;
}

View File

@ -763,7 +763,7 @@ int __cdecl event_wait_for_multiple(event **events, MSVCRT_size_t count, MSVCRT_
wait = heap_alloc(FIELD_OFFSET(thread_wait, entries[count]));
if(!wait)
throw_bad_alloc("bad allocation");
throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
ret = evt_wait(wait, events, count, wait_all, timeout);
heap_free(wait);
@ -846,7 +846,7 @@ MSVCRT_bool __thiscall _Condition_variable_wait_for(_Condition_variable *this,
TRACE("(%p %p %d)\n", this, cs, timeout);
if(!(q = HeapAlloc(GetProcessHeap(), 0, sizeof(cv_queue)))) {
throw_bad_alloc("bad allocation");
throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
}
critical_section_lock(&this->lock);
@ -1026,7 +1026,7 @@ void __thiscall reader_writer_lock_lock(reader_writer_lock *this)
TRACE("(%p)\n", this);
if (this->thread_id == GetCurrentThreadId())
throw_improper_lock("Already locked");
throw_exception(EXCEPTION_IMPROPER_LOCK, 0, "Already locked");
last = InterlockedExchangePointer((void**)&this->writer_tail, &q);
if (last) {
@ -1057,7 +1057,7 @@ void __thiscall reader_writer_lock_lock_read(reader_writer_lock *this)
TRACE("(%p)\n", this);
if (this->thread_id == GetCurrentThreadId())
throw_improper_lock("Already locked as writer");
throw_exception(EXCEPTION_IMPROPER_LOCK, 0, "Already locked as writer");
do {
q.next = this->reader_head;

View File

@ -283,11 +283,14 @@ extern WORD MSVCRT__ctype [257];
void msvcrt_set_errno(int) DECLSPEC_HIDDEN;
#if _MSVCR_VER >= 80
void throw_bad_alloc(const char*) DECLSPEC_HIDDEN;
#endif
typedef enum {
EXCEPTION_BAD_ALLOC,
#if _MSVCR_VER >= 100
void throw_scheduler_resource_allocation_error(const char*, HRESULT) DECLSPEC_HIDDEN;
void throw_improper_lock(const char*) DECLSPEC_HIDDEN;
EXCEPTION_SCHEDULER_RESOURCE_ALLOCATION_ERROR,
EXCEPTION_IMPROPER_LOCK,
#endif
} exception_type;
void throw_exception(exception_type, HRESULT, const char*) DECLSPEC_HIDDEN;
#endif
void __cdecl _purecall(void);

View File

@ -99,8 +99,8 @@ static Context* get_current_context(void)
if (context_tls_index == TLS_OUT_OF_INDEXES) {
int tls_index = TlsAlloc();
if (tls_index == TLS_OUT_OF_INDEXES) {
throw_scheduler_resource_allocation_error(NULL,
HRESULT_FROM_WIN32(GetLastError()));
throw_exception(EXCEPTION_SCHEDULER_RESOURCE_ALLOCATION_ERROR,
HRESULT_FROM_WIN32(GetLastError()), NULL);
return NULL;
}