OMCompiler/SimulationRuntime/c/gc/omc_gc.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 | /* | ||
| 30 | * Adrian Pop [Adrian.Pop@liu.se] | ||
| 31 | * This file defines the MetaModelica garbage collector (GC) interface | ||
| 32 | * We use Boehm GC mark-and-sweep collector. | ||
| 33 | * | ||
| 34 | * | ||
| 35 | */ | ||
| 36 | |||
| 37 | #include "../omc_simulation_settings.h" | ||
| 38 | #include "omc_gc.h" | ||
| 39 | #include "../util/omc_error.h" | ||
| 40 | #include "../util/omc_file.h" | ||
| 41 | #include "../util/omc_init.h" | ||
| 42 | |||
| 43 | static mmc_GC_state_type x_mmc_GC_state = {0}; | ||
| 44 | mmc_GC_state_type *mmc_GC_state = &x_mmc_GC_state; | ||
| 45 | |||
| 46 | #if defined(OMC_RECORD_ALLOC_WORDS) | ||
| 47 | #include <stdlib.h> | ||
| 48 | #include <stdio.h> | ||
| 49 | #include "util/uthash.h" | ||
| 50 | #include <sys/types.h> | ||
| 51 | #include <unistd.h> | ||
| 52 | |||
| 53 | typedef struct { | ||
| 54 | const char *pos; /* key */ | ||
| 55 | size_t count; | ||
| 56 | UT_hash_handle hh; | ||
| 57 | } word_ht; | ||
| 58 | |||
| 59 | static const char *curPos="<nofile>:0"; | ||
| 60 | static word_ht *table=NULL; | ||
| 61 | static size_t totalAlloc=0; | ||
| 62 | static void print_words() | ||
| 63 | { | ||
| 64 | word_ht *entry, *tmp; | ||
| 65 | pid_t pid = getpid(); | ||
| 66 | char str[50]; | ||
| 67 | sprintf(str, "omc-memory.%d.txt", pid); | ||
| 68 | FILE *fout = omc_fopen(str, "w"); | ||
| 69 | HASH_ITER(hh, table, entry, tmp) { | ||
| 70 | fprintf(fout, "%ld: %s\n", (long) entry->count, entry->pos); | ||
| 71 | } | ||
| 72 | fprintf(fout, "%ld: Total alloc\n", (long) totalAlloc); | ||
| 73 | fclose(fout); | ||
| 74 | printf("*** Printed memory consumption to file: %s\n", str); | ||
| 75 | } | ||
| 76 | |||
| 77 | void mmc_record_alloc_words(size_t n) | ||
| 78 | { | ||
| 79 | static int init=0; | ||
| 80 | word_ht *entry; | ||
| 81 | totalAlloc += n; | ||
| 82 | if (!init) { | ||
| 83 | init=1; | ||
| 84 | atexit(print_words); | ||
| 85 | } | ||
| 86 | HASH_FIND_STR(table, curPos, entry); | ||
| 87 | if (entry) { | ||
| 88 | entry->count += n; | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | entry = malloc(sizeof(word_ht)); | ||
| 92 | entry->pos = curPos; | ||
| 93 | entry->count = n; | ||
| 94 | HASH_ADD_KEYPTR( hh, table, entry->pos, strlen(entry->pos), entry); | ||
| 95 | } | ||
| 96 | void mmc_set_current_pos(const char *pos) | ||
| 97 | { | ||
| 98 | curPos = pos; | ||
| 99 | } | ||
| 100 | #endif | ||
| 101 | ✗ | void mmc_do_out_of_memory(void) | |
| 102 | { | ||
| 103 | ✗ | threadData_t *threadData = (threadData_t*)pthread_getspecific(mmc_thread_data_key); | |
| 104 | #if (defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME)) | ||
| 105 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Out of memory!"); | |
| 106 | #else | ||
| 107 | ✗ | omc_assert_warning(omc_dummyFileInfo, "Out of memory! Faking a stack overflow."); | |
| 108 | ✗ | mmc_do_stackoverflow(threadData); | |
| 109 | #endif | ||
| 110 | ✗ | abort(); // Silence invalid noreturn warning. This is never reached. | |
| 111 | } | ||
| 112 | |||
| 113 | #if !defined(OMC_NO_BOEHM_GC) | ||
| 114 | /* The MetaModelica runtime's allocator: Boehm. The counted one a simulation | ||
| 115 | gets instead is in gc/omc_rc.c. */ | ||
| 116 | ✗ | static int GC_collect_a_little_or_not(void) | |
| 117 | { | ||
| 118 | ✗ | return 0; | |
| 119 | } | ||
| 120 | |||
| 121 | ✗ | static void nofree(void *ptr) | |
| 122 | { | ||
| 123 | (void) ptr; | ||
| 124 | ✗ | } | |
| 125 | |||
| 126 | omc_alloc_interface_t omc_alloc_interface = { | ||
| 127 | GC_init, | ||
| 128 | GC_malloc, | ||
| 129 | GC_malloc_atomic, | ||
| 130 | (char*(*)(size_t)) GC_malloc_atomic, | ||
| 131 | GC_strdup, | ||
| 132 | GC_collect_a_little_or_not, | ||
| 133 | GC_malloc_uncollectable, | ||
| 134 | GC_free, | ||
| 135 | GC_malloc_atomic, | ||
| 136 | nofree | ||
| 137 | }; | ||
| 138 | |||
| 139 | /* Work-around for Boehm GC not exposing the maximum heap size */ | ||
| 140 | static size_t max_heap_size = 0; | ||
| 141 | ✗ | void omc_GC_set_max_heap_size(size_t sz) | |
| 142 | { | ||
| 143 | ✗ | max_heap_size = sz; | |
| 144 | ✗ | GC_set_max_heap_size(sz); | |
| 145 | ✗ | } | |
| 146 | ✗ | size_t omc_GC_get_max_heap_size(void) | |
| 147 | { | ||
| 148 | ✗ | return max_heap_size; | |
| 149 | } | ||
| 150 | #endif | ||
| 151 |