Debian Codegraph @

libphobos/libdruntime/rt/ehalloc.d

raw
1 /**
2 * Exception allocation, cloning, and release compiler support routines.
3 *
4 * Copyright: Copyright (c) 2017 by D Language Foundation
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Walter Bright
9 * Source: $(DRUNTIMESRC rt/_ehalloc.d)
10 */
11
12 module rt.ehalloc;
13
14 //debug = PRINTF;
15
16 debug (PRINTF) import core.stdc.stdio : printf;
17
18
19 /********************************************
20 * Delete exception instance `t` from the exception pool.
21 * Must have been allocated with `_d_newThrowable()`.
22 * This is meant to be called at the close of a catch block.
23 * It's nothrow because otherwise any function with a catch block could
24 * not be nothrow.
25 * Input:
26 * t = Throwable
27 */
28
29 nothrow extern (C) void _d_delThrowable(Throwable t)
30 {
31 if (t)
32 {
33 debug(PRINTF) printf("_d_delThrowable(%p)\n", t);
34
35 /* If allocated by the GC, don't free it.
36 * Let the GC handle it.
37 * Supporting this is necessary while transitioning
38 * to this new scheme for allocating exceptions.
39 */
40 auto refcount = t.refcount();
41 if (refcount == 0)
42 return; // it was allocated by the GC
43
44 if (refcount == 1)
45 assert(0); // no zombie objects
46
47 t.refcount() = --refcount;
48 if (refcount > 1)
49 return;
50
51 TypeInfo_Class **pc = cast(TypeInfo_Class **)t;
52 if (*pc)
53 {
54 TypeInfo_Class ci = **pc;
55
56 if (!(ci.m_flags & TypeInfo_Class.ClassFlags.noPointers))
57 {
58 // Inform the GC about the pointers in the object instance
59 import core.memory : GC;
60 GC.removeRange(cast(void*) t);
61 }
62 }
63
64 try
65 {
66 import rt.lifetime : rt_finalize;
67 rt_finalize(cast(void*) t);
68 }
69 catch (Throwable t)
70 {
71 assert(0); // should never happen since Throwable.~this() is nothrow
72 }
73 import core.stdc.stdlib : free;
74 debug(PRINTF) printf("free(%p)\n", t);
75 free(cast(void*) t);
76 }
77 }