Skip to content

Commit 3dea19d

Browse files
committed
update raii.h, cthread.c and exception.c
1 parent fecb031 commit 3dea19d

File tree

3 files changed

+19
-52
lines changed

3 files changed

+19
-52
lines changed

include/raii.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ DO NOT FREE, will `throw/panic` if memory request fails. */
268268

269269
/* Defer execution `LIFO` of given function with argument,
270270
execution begins when current `guard` scope exits or panic/throw. */
271-
#define _defer(func, ptr) raii_recover_by(_$##__FUNCTION__, func, ptr)
271+
#define _defer(func, ptr) raii_recover_by(_$##__FUNCTION__, (func_t)func, ptr)
272272

273273
/* Compare `err` to scoped error condition, will mark exception handled, if `true`. */
274274
#define _recover(err) raii_is_caught(raii_init()->arena, err)
275275

276-
/* Compare `err` to scoped error condition, will mark exception handled, if `true`. */
276+
/* Compare `err` to scoped error condition,
277+
will mark exception handled, if `true`.
278+
DO NOT PUT `err` in quote's like "err". */
277279
#define _is_caught(err) raii_is_caught(raii_init()->arena, EX_STR(err))
278280

279281
/* Get scoped error condition string. */
@@ -370,7 +372,7 @@ On exit will begin executing deferred functions.
370372
#define finality ex_finality
371373
#define end_trying ex_end_trying
372374
#else
373-
#define finality ex_finally
375+
#define finality catch_any ex_finally
374376
#define end_trying ex_end_try
375377
#endif
376378

src/cthread.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ freely, subject to the following restrictions:
2424
SPDX-License-Identifier: Zlib
2525
*/
2626

27+
#include "raii.h"
2728
#ifndef HAS_C11_THREADS
28-
#include "cthread.h"
29-
#include <stdlib.h>
30-
3129
/* Platform specific includes */
3230
#if defined(_TTHREAD_POSIX_)
3331
#include <signal.h>
@@ -581,7 +579,7 @@ static void * _thrd_wrapper_function(void * aArg)
581579
arg = ti->mArg;
582580

583581
/* The thread is responsible for freeing the startup information */
584-
free((void *)ti);
582+
RAII_FREE((void *)ti);
585583

586584
/* Call the actual client thread function */
587585
res = fun(arg);
@@ -671,7 +669,7 @@ static void _remove_thread_data(DWORD thread_id)
671669
if (node->thread_id == thread_id)
672670
{
673671
_cthread_thread_head = node->next;
674-
free(node);
672+
RAII_FREE(node);
675673
ReleaseSRWLockExclusive(&_cthread_thread_head_srwlock);
676674
return;
677675
}
@@ -681,7 +679,7 @@ static void _remove_thread_data(DWORD thread_id)
681679
{
682680
struct CThreadThrdData* needle = node->next;
683681
node->next = needle->next;
684-
free(needle);
682+
RAII_FREE(needle);
685683
ReleaseSRWLockExclusive(&_cthread_thread_head_srwlock);
686684
return;
687685
}
@@ -696,7 +694,7 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
696694
{
697695
/* Fill out the thread startup information (passed to the thread wrapper,
698696
which will eventually free it) */
699-
_thread_start_info* ti = (_thread_start_info*)malloc(sizeof(_thread_start_info));
697+
_thread_start_info* ti = (_thread_start_info*)RAII_MALLOC(sizeof(_thread_start_info));
700698
if (ti == NULL)
701699
{
702700
return thrd_nomem;
@@ -707,17 +705,17 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
707705
/* Create the thread */
708706
#if defined(_TTHREAD_WIN32_)
709707
struct CThreadThrdData* data;
710-
data = (struct CThreadThrdData*)malloc(sizeof(struct CThreadThrdData));
708+
data = (struct CThreadThrdData*)RAII_MALLOC(sizeof(struct CThreadThrdData));
711709
if (data == NULL)
712710
{
713-
free(ti);
711+
RAII_FREE(ti);
714712
return thrd_nomem;
715713
}
716714
HANDLE handle = CreateThread(NULL, 0, _thrd_wrapper_function, (LPVOID)ti, 0, NULL);
717715
if (handle == NULL)
718716
{
719-
free(ti);
720-
free(data);
717+
RAII_FREE(ti);
718+
RAII_FREE(data);
721719
return thrd_error;
722720
}
723721
data->handle = handle;
@@ -728,7 +726,7 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
728726
#elif defined(_TTHREAD_POSIX_)
729727
if(pthread_create(thr, NULL, _thrd_wrapper_function, (void *)ti) != 0)
730728
{
731-
free(ti);
729+
RAII_FREE(ti);
732730
return thrd_error;
733731
}
734732
#endif
@@ -965,7 +963,7 @@ int tss_set(tss_t key, void *val)
965963
struct CThreadTSSData* data = (struct CThreadTSSData*)TlsGetValue(key);
966964
if (data == NULL)
967965
{
968-
data = (struct CThreadTSSData*)malloc(sizeof(struct CThreadTSSData));
966+
data = (struct CThreadTSSData*)RAII_MALLOC(sizeof(struct CThreadTSSData));
969967
if (data == NULL)
970968
{
971969
return thrd_error;

src/exception.c

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,12 @@
22
#undef _FORTIFY_SOURCE
33
#endif
44
#include "raii.h"
5-
/*
6-
o---------------------------------------------------------------------o
7-
|
8-
| Exception in C
9-
|
10-
| Copyright (c) 2001+ Laurent Deniau, [email protected]
11-
|
12-
| For more information, see:
13-
| http://cern.ch/laurent.deniau/oopc.html
14-
|
15-
o---------------------------------------------------------------------o
16-
|
17-
| Exception in C is free software; you can redistribute it and/or
18-
| modify it under the terms of the GNU Lesser General Public License
19-
| as published by the Free Software Foundation; either version 2.1 of
20-
| the License, or (at your option) any later version.
21-
|
22-
| The C Object System is distributed in the hope that it will be
23-
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24-
| of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25-
| Lesser General Public License for more details.
26-
|
27-
| You should have received a copy of the GNU Lesser General Public
28-
| License along with this library; if not, write to the Free
29-
| Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30-
| Boston, MA 02110-1301 USA
31-
|
32-
o---------------------------------------------------------------------o
33-
|
34-
| $Id$
35-
|
36-
*/
375

386
/* Some common exception */
397
EX_EXCEPTION(invalid_type);
408
EX_EXCEPTION(range_error);
419
EX_EXCEPTION(divide_by_zero);
10+
EX_EXCEPTION(division_by_zero);
4211
EX_EXCEPTION(out_of_memory);
4312
EX_EXCEPTION(panic);
4413

@@ -387,11 +356,9 @@ void ex_signal_reset(int sig) {
387356
*/
388357
ex_sig_sa.sa_handler = SIG_DFL;
389358
if (sigemptyset(&ex_sig_sa.sa_mask) != 0)
390-
fprintf(stderr, "Cannot setup handler for signal no %d (%s)\n",
391-
sig, ex);
359+
fprintf(stderr, "Cannot setup handler for signal no %d\n", sig);
392360
else if (sigaction(sig, &ex_sig_sa, NULL) != 0)
393-
fprintf(stderr, "Cannot restore handler for signal no %d (%s)\n",
394-
sig, ex);
361+
fprintf(stderr, "Cannot restore handler for signal no %d\n", sig);
395362
#endif
396363
exception_signal_set = false;
397364
}

0 commit comments

Comments
 (0)