|
| 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 src/rt/_dwarfeh.d) |
| 10 | + */ |
| 11 | + |
| 12 | +module rt.ehalloc; |
| 13 | + |
| 14 | +//debug = PRINTF; |
| 15 | + |
| 16 | +debug(PRINTF) |
| 17 | +{ |
| 18 | + import core.stdc.stdio; |
| 19 | +} |
| 20 | + |
| 21 | +/********************************************** |
| 22 | + * Allocate an exception of type `ci` from the exception pool. |
| 23 | + * It has the same interface as `rt.lifetime._d_newclass()`. |
| 24 | + * The class type must be Throwable or derived from it, |
| 25 | + * and cannot be a COM or C++ class. The compiler must enforce |
| 26 | + * this. |
| 27 | + * Returns: |
| 28 | + * default initialized instance of the type |
| 29 | + */ |
| 30 | + |
| 31 | +extern (C) Throwable _d_newThrowable(const TypeInfo_Class ci) |
| 32 | +{ |
| 33 | + debug(PRINTF) printf("_d_newThrowable(ci = %p, %s)\n", ci, cast(char *)ci.name); |
| 34 | + |
| 35 | + assert(!(ci.m_flags & TypeInfo_Class.ClassFlags.isCOMclass)); |
| 36 | + assert(!(ci.m_flags & TypeInfo_Class.ClassFlags.isCPPclass)); |
| 37 | + |
| 38 | + import core.stdc.stdlib : malloc; |
| 39 | + void* p = malloc(ci.initializer.length); |
| 40 | + if (!p) |
| 41 | + { |
| 42 | + import core.exception : onOutOfMemoryError; |
| 43 | + onOutOfMemoryError(); |
| 44 | + } |
| 45 | + |
| 46 | + debug(PRINTF) printf(" p = %p\n", p); |
| 47 | + |
| 48 | + // initialize it |
| 49 | + p[0 .. ci.initializer.length] = ci.initializer[]; |
| 50 | + |
| 51 | + if (!(ci.m_flags & TypeInfo_Class.ClassFlags.noPointers)) |
| 52 | + { |
| 53 | + // Inform the GC about the pointers in the object instance |
| 54 | + import core.memory : GC; |
| 55 | + |
| 56 | + GC.addRange(p, ci.initializer.length, ci); |
| 57 | + } |
| 58 | + |
| 59 | + debug(PRINTF) printf("initialization done\n"); |
| 60 | + Throwable t = cast(Throwable)p; |
| 61 | + t.refcount() = 1; |
| 62 | + return t; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +/******************************************** |
| 67 | + * Delete exception instance `t` from the exception pool. |
| 68 | + * Must have been allocated with `_d_newThrowable()`. |
| 69 | + * This is meant to be called at the close of a catch block. |
| 70 | + * It's nothrow because otherwise any function with a catch block could |
| 71 | + * not be nothrow. |
| 72 | + * Input: |
| 73 | + * t = Throwable |
| 74 | + */ |
| 75 | + |
| 76 | +nothrow extern (C) void _d_delThrowable(Throwable t) |
| 77 | +{ |
| 78 | + if (t) |
| 79 | + { |
| 80 | + debug(PRINTF) printf("_d_delThrowable(%p)\n", t); |
| 81 | + |
| 82 | + /* If allocated by the GC, don't free it. |
| 83 | + * Let the GC handle it. |
| 84 | + * Supporting this is necessary while transitioning |
| 85 | + * to this new scheme for allocating exceptions. |
| 86 | + */ |
| 87 | + auto refcount = t.refcount(); |
| 88 | + if (refcount == 0) |
| 89 | + return; // it was allocated by the GC |
| 90 | + |
| 91 | + if (refcount == 1) |
| 92 | + assert(0); // no zombie objects |
| 93 | + |
| 94 | + t.refcount() = --refcount; |
| 95 | + if (refcount > 1) |
| 96 | + return; |
| 97 | + |
| 98 | + TypeInfo_Class **pc = cast(TypeInfo_Class **)t; |
| 99 | + if (*pc) |
| 100 | + { |
| 101 | + TypeInfo_Class ci = **pc; |
| 102 | + |
| 103 | + if (!(ci.m_flags & TypeInfo_Class.ClassFlags.noPointers)) |
| 104 | + { |
| 105 | + // Inform the GC about the pointers in the object instance |
| 106 | + import core.memory : GC; |
| 107 | + GC.removeRange(cast(void*) t); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + try |
| 112 | + { |
| 113 | + import rt.lifetime : rt_finalize; |
| 114 | + rt_finalize(cast(void*) t); |
| 115 | + } |
| 116 | + catch (Throwable t) |
| 117 | + { |
| 118 | + assert(0); // should never happen since Throwable.~this() is nothrow |
| 119 | + } |
| 120 | + import core.stdc.stdlib : free; |
| 121 | + debug(PRINTF) printf("free(%p)\n", t); |
| 122 | + free(cast(void*) t); |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments