Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 92.9% 13 / 0 / 14
Functions: -% 0 / 3 / 3
Branches: 28.6% 10 / 0 / 35

OMCompiler/SimulationRuntime/c/util/omc_string.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_STRING_H_
30 #define OMC_STRING_H_
31
32 #include "../openmodelica_types.h"
33 #include "../gc/omc_rc.h"
34 /* meta/meta_modelica_string.h defines the same names for MetaModelica. */
35 #if !defined(OMC_METAMODELICA_RUNTIME)
36
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40
41 /* The simulation's String. A counted object like any other, so the reference
42 * count sits in the word before it (see gc/omc_rc.h):
43 *
44 * [ rc : mmc_uint_t ][ len : mmc_uint_t ][ bytes ... '\0' ]
45 *
46 * The pointer is untagged and the slot holds nothing but a string, so
47 * retain/release need not decode it. Its own type, so a mix-up with
48 * modelica_metatype is a compile error. */
49
50 struct omc_string_s {
51 mmc_uint_t len;
52 char data[1]; /* len bytes plus the terminator */
53 };
54
55 /* A literal: static, never freed, never written to. */
56 #define OMC_DEFSTRINGLIT(NAME,LEN,VAL) \
57 struct { \
58 mmc_uint_t rc; \
59 mmc_uint_t len; \
60 const char data[(LEN)+1]; \
61 } NAME = { OMC_RC_IMMORTAL, (LEN), VAL }
62 #define OMC_REFSTRINGLIT(NAME) ((modelica_string) &((NAME).len))
63
64 static inline char* omc_string_data(modelica_string s)
65 {
66
2/9
✓ Branch 0 taken 2 times.
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
87093 return s->data;
67 }
68
69 static inline mmc_uint_t omc_string_len(modelica_string s)
70 {
71
4/10
✗ Branch 0 not taken.
✓ Branch 1 taken 86927 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
86967 return s->len;
72 }
73
74 static inline modelica_string omc_string_retain(modelica_string s)
75 {
76 omc_rc_retain_inline(s);
77 return s;
78 }
79
80 static inline void omc_string_release(modelica_string s)
81 {
82 212 omc_rc_release_inline(s);
83 ✗ }
84
85 /* Uninitialised string of `len` bytes (count 1, terminator written). */
86 modelica_string omc_string_alloc(mmc_uint_t len);
87 modelica_string omc_string_new(const char *str);
88 modelica_string omc_string_new_len(const char *str, mmc_uint_t len);
89 /* Outlives the heap: used for strings handed to code that never releases. */
90 modelica_string omc_string_new_persist(const char *str);
91
92 /* store copies (takes its own reference), move takes over the caller's.
93 Retaining before releasing is what makes s := s safe. */
94 static inline void omc_string_store(modelica_string *slot, modelica_string s)
95 {
96
2/8
<unknown function 66>:
✗ Branch 0 not taken.
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 287 times.
✗ Branch 3 not taken.
<unknown function 96>:
✓ Branch 0 taken 11276 times.
✗ Branch 1 not taken.
11563 modelica_string old = *slot;
97 omc_rc_retain_inline(s);
98 11563 *slot = s;
99 11563 omc_rc_release_inline(old);
100 }
101
102 static inline void omc_string_move(modelica_string *slot, modelica_string s)
103 {
104 4 modelica_string old = *slot;
105 4 *slot = s;
106
2/8
✗ 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 taken 2 times.
✗ Branch 7 not taken.
4 if (old != s) {
107 4 omc_rc_release_inline(old);
108 }
109 }
110
111 /* The caller takes the reference, so the release at _return: finds nothing. */
112 #define omc_string_disown(X) ((void) ((X) = NULL))
113
114 /* n slots at a time: the ring buffer of past values, which holds the same
115 strings as the current step. */
116 void omc_string_slots_store(modelica_string *dst, const modelica_string *src, size_t n);
117 void omc_string_slots_release(modelica_string *slots, size_t n);
118
119 DLLDataDirection extern modelica_string omc_string_empty;
120 /* The 256 one-byte strings, shared and immortal. */
121 DLLDataDirection extern modelica_string omc_strings_len1[256];
122
123 #if defined(__cplusplus)
124 } /* end extern "C" */
125 #endif
126
127 #endif /* !OMC_METAMODELICA_RUNTIME */
128 #endif /* OMC_STRING_H_ */
129