OMCompiler/SimulationRuntime/c/util/omc_string.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_string.h" | ||
| 30 | #include "../gc/omc_gc.h" | ||
| 31 | #include <string.h> | ||
| 32 | |||
| 33 | #if defined(__cplusplus) | ||
| 34 | extern "C" { | ||
| 35 | #endif | ||
| 36 | |||
| 37 | 95692 | modelica_string omc_string_alloc(mmc_uint_t len) | |
| 38 | { | ||
| 39 | modelica_string s; | ||
| 40 | |||
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 95692 times.
|
95692 | if (len == 0) { |
| 42 | ✗ | return omc_string_empty; | |
| 43 | } | ||
| 44 | 95692 | s = (modelica_string) omc_rc_alloc(sizeof(mmc_uint_t) + len + 1); | |
| 45 | 95692 | s->len = len; | |
| 46 | 95692 | s->data[len] = '\0'; | |
| 47 | 95692 | return s; | |
| 48 | } | ||
| 49 | |||
| 50 | 1193 | modelica_string omc_string_new_len(const char *str, mmc_uint_t len) | |
| 51 | { | ||
| 52 | modelica_string s; | ||
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1189 times.
|
1193 | if (len == 0) { |
| 55 | 4 | return omc_string_empty; | |
| 56 | } | ||
| 57 | 1189 | s = omc_string_alloc(len); | |
| 58 | 1189 | memcpy(s->data, str, len); | |
| 59 | 1189 | return s; | |
| 60 | } | ||
| 61 | |||
| 62 | 1193 | modelica_string omc_string_new(const char *str) | |
| 63 | { | ||
| 64 | 1193 | return omc_string_new_len(str, (mmc_uint_t) strlen(str)); | |
| 65 | } | ||
| 66 | |||
| 67 | 680765 | modelica_string omc_string_new_persist(const char *str) | |
| 68 | { | ||
| 69 | 680765 | size_t len = strlen(str); | |
| 70 | modelica_string s; | ||
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 456656 times.
✓ Branch 1 taken 224109 times.
|
680765 | if (len == 0) { |
| 73 | 456656 | return omc_string_empty; | |
| 74 | } | ||
| 75 | 224109 | s = (modelica_string) omc_alloc_interface.malloc_string_persist(sizeof(mmc_uint_t) * 2 + len + 1); | |
| 76 | 224109 | s = (modelica_string) (((mmc_uint_t*) s) + 1); | |
| 77 | 224109 | *omc_rc_hdr(s) = OMC_RC_IMMORTAL; | |
| 78 | 224109 | s->len = (mmc_uint_t) len; | |
| 79 | 224109 | memcpy(s->data, str, len + 1); | |
| 80 | 224109 | return s; | |
| 81 | } | ||
| 82 | |||
| 83 | 7509 | void omc_string_slots_store(modelica_string *dst, const modelica_string *src, size_t n) | |
| 84 | { | ||
| 85 | size_t i; | ||
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 11276 times.
✓ Branch 1 taken 7509 times.
|
18785 | for (i = 0; i < n; i++) { |
| 88 |
1/2✓ Branch 0 taken 11276 times.
✗ Branch 1 not taken.
|
11276 | omc_string_store(dst + i, src[i]); |
| 89 | } | ||
| 90 | 7509 | } | |
| 91 | |||
| 92 | 6 | void omc_string_slots_release(modelica_string *slots, size_t n) | |
| 93 | { | ||
| 94 | size_t i; | ||
| 95 | |||
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | for (i = 0; i < n; i++) { |
| 97 | ✗ | omc_rc_release_inline(slots[i]); | |
| 98 | ✗ | slots[i] = NULL; | |
| 99 | } | ||
| 100 | 6 | } | |
| 101 | |||
| 102 | #if defined(__cplusplus) | ||
| 103 | } /* end extern "C" */ | ||
| 104 | #endif | ||
| 105 |