OMCompiler/SimulationRuntime/c/gc/omc_rc.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | |||
| 29 | #ifndef OMC_RC_H_ | ||
| 30 | #define OMC_RC_H_ | ||
| 31 | |||
| 32 | #include <stdlib.h> | ||
| 33 | #include "../openmodelica_types.h" | ||
| 34 | |||
| 35 | #if defined(__cplusplus) | ||
| 36 | extern "C" { | ||
| 37 | #endif | ||
| 38 | |||
| 39 | /* A counted block is [ rc : mmc_uint_t ][ object ... ]; the pointer handed out | ||
| 40 | * is the object. rc == OMC_RC_IMMORTAL is never written and never freed. | ||
| 41 | * | ||
| 42 | * The counts are not atomic: only --hpcom shares a modelica_string between | ||
| 43 | * threads. Define OMC_RC_ATOMIC for that, at a locked op per retain. */ | ||
| 44 | |||
| 45 | |||
| 46 | #if defined(OMC_RC_ATOMIC) | ||
| 47 | #if !defined(__GNUC__) && !defined(__clang__) | ||
| 48 | #error "OMC_RC_ATOMIC needs the GCC/Clang __atomic builtins" | ||
| 49 | #endif | ||
| 50 | #define OMC_RC_LOAD(rc) __atomic_load_n((rc), __ATOMIC_RELAXED) | ||
| 51 | #define OMC_RC_INC(rc) ((void) __atomic_fetch_add((rc), 1, __ATOMIC_RELAXED)) | ||
| 52 | #define OMC_RC_DEC(rc) (__atomic_sub_fetch((rc), 1, __ATOMIC_ACQ_REL)) | ||
| 53 | #else | ||
| 54 | #define OMC_RC_LOAD(rc) (*(rc)) | ||
| 55 | #define OMC_RC_INC(rc) ((void) (*(rc))++) | ||
| 56 | #define OMC_RC_DEC(rc) (--(*(rc))) | ||
| 57 | #endif | ||
| 58 | |||
| 59 | /* rc = 1. malloc_atomic and malloc are the same block; nothing scans it. */ | ||
| 60 | void* omc_rc_alloc(size_t nbytes); | ||
| 61 | void* omc_rc_alloc_zero(size_t nbytes); | ||
| 62 | char* omc_rc_strdup(const char *str); | ||
| 63 | |||
| 64 | /* Out of line for generated code, which emits them whichever heap it gets. */ | ||
| 65 | void* omc_rc_retain(void *obj); | ||
| 66 | void omc_rc_release(void *obj); | ||
| 67 | |||
| 68 | /* Whether releasing `obj` would free it; always 0 on a collected heap. */ | ||
| 69 | int omc_rc_is_last(void *obj); | ||
| 70 | |||
| 71 | static inline mmc_uint_t* omc_rc_hdr(void *obj) | ||
| 72 | { | ||
| 73 | 228266922 | return ((mmc_uint_t*)obj) - 1; | |
| 74 | } | ||
| 75 | |||
| 76 | /* A collected object has no count in front of it to touch. */ | ||
| 77 | #if defined(OMC_METAMODELICA_RUNTIME) | ||
| 78 | |||
| 79 | static inline void* omc_rc_retain_inline(void *obj) { return obj; } | ||
| 80 | static inline void omc_rc_release_inline(void *obj) { (void) obj; } | ||
| 81 | |||
| 82 | #else | ||
| 83 | |||
| 84 | static inline void* omc_rc_retain_inline(void *obj) | ||
| 85 | { | ||
| 86 |
3/12<unknown function 73>:
✗ Branch 0 not taken.
✓ Branch 0 taken 1899996 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 37610590 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 287 times.
✗ Branch 7 not taken.
<unknown function 86>:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
39510873 | if (obj) { |
| 87 | mmc_uint_t *rc = omc_rc_hdr(obj); | ||
| 88 |
7/26<unknown function 73>:
✓ Branch 0 taken 1888832 times.
✓ Branch 1 taken 11164 times.
✓ Branch 2 taken 1902839 times.
✓ Branch 3 taken 35707751 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 75 times.
✓ Branch 7 taken 212 times.
<unknown function 86>:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
<unknown function 88>:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
39510875 | if (OMC_RC_LOAD(rc) != OMC_RC_IMMORTAL) { |
| 89 | 3791748 | OMC_RC_INC(rc); | |
| 90 | } | ||
| 91 | } | ||
| 92 | return obj; | ||
| 93 | } | ||
| 94 | |||
| 95 | 289444620 | static inline void omc_rc_release_inline(void *obj) | |
| 96 | { | ||
| 97 |
2/2✓ Branch 0 taken 228266922 times.
✓ Branch 1 taken 61177698 times.
|
289444620 | if (obj) { |
| 98 | mmc_uint_t *rc = omc_rc_hdr(obj); | ||
| 99 |
4/4✓ Branch 0 taken 134857341 times.
✓ Branch 1 taken 93409581 times.
✓ Branch 2 taken 131065599 times.
✓ Branch 3 taken 3791742 times.
|
228266922 | if (OMC_RC_LOAD(rc) != OMC_RC_IMMORTAL && OMC_RC_DEC(rc) == 0) { |
| 100 | 131065599 | free(rc); | |
| 101 | } | ||
| 102 | } | ||
| 103 | 289444620 | } | |
| 104 | |||
| 105 | #endif /* OMC_METAMODELICA_RUNTIME */ | ||
| 106 | |||
| 107 | #if defined(__cplusplus) | ||
| 108 | } /* end extern "C" */ | ||
| 109 | #endif | ||
| 110 | |||
| 111 | #endif /* OMC_RC_H_ */ | ||
| 112 |