OMCompiler/Compiler/Util/PointerWeak.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated uniontype PointerWeak<T> | ||
| 37 | "A non-owning reference to a Pointer cell. | ||
| 38 | |||
| 39 | Breaks an ownership cycle so plain reference counting can reclaim it. The | ||
| 40 | referent must be owned somewhere else -- for `VAR_NODE.varPointer` that is | ||
| 41 | the backend's `VariablePointers`, which holds every variable. | ||
| 42 | |||
| 43 | In the bootstrapped C compiler a weak reference *is* the strong one (Boehm | ||
| 44 | reclaims cycles by tracing, so there is nothing to break) and `upgrade` can | ||
| 45 | never fail. The Rust port gives it real weak semantics, and `upgrade` fails | ||
| 46 | if the referent is already gone." | ||
| 47 | |||
| 48 | import Pointer; | ||
| 49 | |||
| 50 | 163544 | impure function downgrade | |
| 51 | "A reference that does not keep the cell alive. Never fails." | ||
| 52 | input Pointer<T> pointer; | ||
| 53 | output PointerWeak<T> weak; | ||
| 54 | external "C" weak=pointerWeakDowngrade(pointer) annotation(Include=" | ||
| 55 | static inline void* pointerWeakDowngrade(void *pointer) | ||
| 56 | { | ||
| 57 | return pointer; | ||
| 58 | } | ||
| 59 | "); | ||
| 60 | end downgrade; | ||
| 61 | |||
| 62 | 372932 | impure function upgrade | |
| 63 | "An owning cell again. Fails if the referent is already gone — which means | ||
| 64 | a weak reference outlived the structure that owned it, so the ownership | ||
| 65 | split is wrong somewhere. Dereferencing a cell never fails; only this | ||
| 66 | conversion does." | ||
| 67 | input PointerWeak<T> weak; | ||
| 68 | output Pointer<T> pointer; | ||
| 69 | external "C" pointer=pointerWeakUpgrade(weak) annotation(Include=" | ||
| 70 | static inline void* pointerWeakUpgrade(void *weak) | ||
| 71 | { | ||
| 72 | return weak; | ||
| 73 | } | ||
| 74 | "); | ||
| 75 | end upgrade; | ||
| 76 | |||
| 77 | annotation(__OpenModelica_Interface="util_datatypes_basic"); | ||
| 78 | end PointerWeak; | ||
| 79 |