OMCompiler/SimulationRuntime/c/gc/omc_rc.c
| 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 | #include "../omc_simulation_settings.h" | ||
| 30 | #include "omc_gc.h" | ||
| 31 | #include "omc_rc.h" | ||
| 32 | #include <string.h> | ||
| 33 | |||
| 34 | #if defined(__cplusplus) | ||
| 35 | extern "C" { | ||
| 36 | #endif | ||
| 37 | |||
| 38 | 133141153 | static void* rc_alloc(size_t nbytes, int zero) | |
| 39 | { | ||
| 40 | mmc_uint_t *block; | ||
| 41 | |||
| 42 | /* The runtime asks for zero-size blocks; see #7611. They still need a header | ||
| 43 | so that retain/release on the result are well defined. */ | ||
| 44 |
2/2✓ Branch 0 taken 133044286 times.
✓ Branch 1 taken 96867 times.
|
133141153 | block = (mmc_uint_t*) (zero ? calloc(1, sizeof(mmc_uint_t) + nbytes) |
| 45 | 96867 | : malloc(sizeof(mmc_uint_t) + nbytes)); | |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 133141153 times.
|
133141153 | if (!block) { |
| 47 | ✗ | mmc_do_out_of_memory(); | |
| 48 | } | ||
| 49 | 133141153 | *block = 1; | |
| 50 | 133141153 | return block + 1; | |
| 51 | } | ||
| 52 | |||
| 53 | 95692 | void* omc_rc_alloc(size_t nbytes) | |
| 54 | { | ||
| 55 | 95692 | return rc_alloc(nbytes, 0); | |
| 56 | } | ||
| 57 | |||
| 58 | 133044286 | void* omc_rc_alloc_zero(size_t nbytes) | |
| 59 | { | ||
| 60 | 133044286 | return rc_alloc(nbytes, 1); | |
| 61 | } | ||
| 62 | |||
| 63 | 1175 | char* omc_rc_strdup(const char *str) | |
| 64 | { | ||
| 65 | 1175 | size_t len = strlen(str) + 1; | |
| 66 | 1175 | return (char*) memcpy(rc_alloc(len, 0), str, len); | |
| 67 | } | ||
| 68 | |||
| 69 | ✗ | void* omc_rc_retain(void *obj) | |
| 70 | { | ||
| 71 | ✗ | return omc_rc_retain_inline(obj); | |
| 72 | } | ||
| 73 | |||
| 74 | 15 | void omc_rc_release(void *obj) | |
| 75 | { | ||
| 76 | 15 | omc_rc_release_inline(obj); | |
| 77 | 15 | } | |
| 78 | |||
| 79 | 141 | int omc_rc_is_last(void *obj) | |
| 80 | { | ||
| 81 | #if defined(OMC_METAMODELICA_RUNTIME) | ||
| 82 | /* Collected, not counted: the collector owns that decision. */ | ||
| 83 | (void) obj; | ||
| 84 | ✗ | return 0; | |
| 85 | #else | ||
| 86 |
1/2✓ Branch 0 taken 141 times.
✗ Branch 1 not taken.
|
141 | if (obj) { |
| 87 | 141 | mmc_uint_t rc = *omc_rc_hdr(obj); | |
| 88 | 141 | return rc != OMC_RC_IMMORTAL && rc == 1; | |
| 89 | } | ||
| 90 | return 0; | ||
| 91 | #endif | ||
| 92 | } | ||
| 93 | |||
| 94 | 2333 | static void rc_init(void) | |
| 95 | { | ||
| 96 | 2333 | } | |
| 97 | |||
| 98 | 5 | static int rc_collect_a_little(void) | |
| 99 | { | ||
| 100 | 5 | return 0; | |
| 101 | } | ||
| 102 | |||
| 103 | 7038 | static void* rc_malloc_uncollectable(size_t sz) | |
| 104 | { | ||
| 105 |
2/2✓ Branch 0 taken 7020 times.
✓ Branch 1 taken 18 times.
|
7038 | void *addr = sz ? calloc(1, sz) : NULL; |
| 106 | |||
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7038 times.
|
7038 | if (sz && !addr) { |
| 108 | ✗ | mmc_do_out_of_memory(); | |
| 109 | } | ||
| 110 | 7038 | return addr; | |
| 111 | } | ||
| 112 | |||
| 113 | #define OMC_RC_ALLOC_INTERFACE \ | ||
| 114 | rc_init, \ | ||
| 115 | omc_rc_alloc_zero, \ | ||
| 116 | omc_rc_alloc_zero, \ | ||
| 117 | (char*(*)(size_t)) omc_rc_alloc_zero, \ | ||
| 118 | omc_rc_strdup, \ | ||
| 119 | rc_collect_a_little, \ | ||
| 120 | rc_malloc_uncollectable, \ | ||
| 121 | free, \ | ||
| 122 | malloc, \ | ||
| 123 | free | ||
| 124 | |||
| 125 | omc_alloc_interface_t omc_alloc_interface_rc = { OMC_RC_ALLOC_INTERFACE }; | ||
| 126 | |||
| 127 | #if !defined(OMC_METAMODELICA_RUNTIME) | ||
| 128 | omc_alloc_interface_t omc_alloc_interface = { OMC_RC_ALLOC_INTERFACE }; | ||
| 129 | #endif | ||
| 130 | |||
| 131 | #if defined(__cplusplus) | ||
| 132 | } /* end extern "C" */ | ||
| 133 | #endif | ||
| 134 |