Skip to content

Commit 05e4ab6

Browse files
committed
- Make it possible to change ModelicaAllocateString to use garbage collected allocation in the bootstrapped compiler
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16836 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 2ceed8e commit 05e4ab6

File tree

15 files changed

+62
-28
lines changed

15 files changed

+62
-28
lines changed

Compiler/Template/CodegenC.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9740,6 +9740,7 @@ int rml_execution_failed(mmc_GC_local_state_type local_GC_state)
97409740

97419741
int main(int argc, char **argv)
97429742
{
9743+
OpenModelica_ExternalC_allocation_function = mmc_mk_scon_len_ret_ptr;
97439744
init_metamodelica_segv_handler();
97449745
if (!mmc_GC_state)
97459746
{

Compiler/runtime/Corba_omc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
extern "C" {
3535

3636
#include "meta_modelica.h"
37+
#include "ModelicaUtilities.h"
3738

3839
extern int Corba_haveCorba()
3940
{
@@ -52,7 +53,8 @@ extern void Corba_setSessionName(const char* _inSessionName)
5253

5354
extern const char* Corba_waitForCommand()
5455
{
55-
return strdup(CorbaImpl__waitForCommand());
56+
const char *res = CorbaImpl__waitForCommand();
57+
return strcpy(ModelicaAllocateString(strlen(res)), res);
5658
}
5759

5860
extern void Corba_initialize()

Compiler/runtime/Dynload_omc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern "C" {
4141
#include "rml_compatibility.h"
4242
#include "OpenModelicaBootstrappingHeader.h"
4343
#include "Dynload.cpp"
44+
#include "ModelicaUtilities.h"
4445

4546
extern void* DynLoad_executeFunction(int _inFuncHandle, void* _inValLst, int _inPrintDebug)
4647
{
@@ -59,7 +60,7 @@ extern void* omc_Absyn_pathString2(void*,void*);
5960
static const char* path_to_name(void* path, char del)
6061
{
6162
char delStr[2] = {del,'\0'};
62-
return strdup(MMC_STRINGDATA(omc_Absyn_pathString2(path, mmc_mk_scon(delStr))));
63+
return MMC_STRINGDATA(omc_Absyn_pathString2(path, mmc_mk_scon(delStr)));
6364
}
6465

6566
}

Compiler/runtime/Error_omc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ extern void* Error_getMessages()
6868
extern const char* Error_printErrorsNoWarning()
6969
{
7070
std::string res = ErrorImpl__printErrorsNoWarning();
71-
return MMC_STRINGDATA(mmc_mk_scon(res.c_str()));
71+
return strcpy(ModelicaAllocateString(res.size()), res.c_str());
7272
}
7373

7474
extern const char* Error_printMessagesStr()
7575
{
7676
std::string res = ErrorImpl__printMessagesStr();
77-
return MMC_STRINGDATA(mmc_mk_scon(res.c_str()));
77+
return strcpy(ModelicaAllocateString(res.size()), res.c_str());
7878
}
7979

8080
extern void Error_addSourceMessage(int _id, void *msg_type, void *severity, int _sline, int _scol, int _eline, int _ecol, int _read_only, const char* _filename, const char* _msg, void* tokenlst)

Compiler/runtime/Print_omc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "meta_modelica.h"
3434

3535
#include "printimpl.c"
36+
#include "ModelicaUtilities.h"
3637

3738
extern int Print_saveAndClearBuf()
3839
{
@@ -76,15 +77,15 @@ extern const char* Print_getString(void)
7677
if (res == NULL)
7778
MMC_THROW();
7879
// fprintf(stderr, "Print_getString: %s##\n", res);fflush(NULL);
79-
return res; // strdup(res);
80+
return strcpy(ModelicaAllocateString(strlen(res)), res);
8081
}
8182

8283
extern const char* Print_getErrorString(void)
8384
{
8485
const char* res = PrintImpl__getErrorString();
8586
if (res == NULL)
8687
MMC_THROW();
87-
return res; // strdup(res);
88+
return strcpy(ModelicaAllocateString(strlen(res)), res);
8889
}
8990

9091
extern void Print_clearErrorBuf(void)

Compiler/runtime/Settings_omc.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#endif
3636

3737
#include "meta_modelica.h"
38+
#include "ModelicaUtilities.h"
3839

3940
extern "C" {
4041

@@ -45,7 +46,7 @@ extern const char* Settings_getInstallationDirectoryPath()
4546
const char *path = SettingsImpl__getInstallationDirectoryPath();
4647
if (path == NULL)
4748
MMC_THROW();
48-
return strdup(path);
49+
return strcpy(ModelicaAllocateString(strlen(path)), path);
4950
}
5051

5152
extern const char* Settings_getModelicaPath(int runningTestsuite)
@@ -58,7 +59,8 @@ extern const char* Settings_getModelicaPath(int runningTestsuite)
5859

5960
extern const char* Settings_getCompileCommand()
6061
{
61-
return strdup(SettingsImpl__getCompileCommand());
62+
const char *res = SettingsImpl__getCompileCommand();
63+
return strcpy(ModelicaAllocateString(strlen(res)), res);
6264
}
6365

6466
extern void Settings_setPlotCommand(const char* _inString);

Compiler/runtime/System_omc.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern "C"
4444
#include "openmodelica.h"
4545
#include "meta_modelica.h"
4646
#include "rml_compatibility.h"
47+
#include "ModelicaUtilities.h"
4748
#define ADD_METARECORD_DEFINTIONS static
4849
#include "OpenModelicaBootstrappingHeader.h"
4950
#include "systemimpl.c"
@@ -93,7 +94,7 @@ extern const char* System_stringFindString(const char* str, const char* searchSt
9394
const char *found = strstr(str, searchStr);
9495
if (found == NULL)
9596
MMC_THROW();
96-
return strdup(found);
97+
return strcpy(ModelicaAllocateString(strlen(found)), found);
9798
}
9899

99100
extern void System_realtimeTick(int ix)
@@ -127,27 +128,27 @@ extern const char* System_getRTLibs()
127128

128129
extern const char* System_getCCompiler()
129130
{
130-
return strdup(cc);
131+
return strcpy(ModelicaAllocateString(strlen(cc)), cc);
131132
}
132133

133134
extern const char* System_getCXXCompiler()
134135
{
135-
return strdup(cxx);
136+
return strcpy(ModelicaAllocateString(strlen(cxx)), cxx);
136137
}
137138

138139
extern const char* System_getLinker()
139140
{
140-
return strdup(linker);
141+
return strcpy(ModelicaAllocateString(strlen(linker)), linker);
141142
}
142143

143144
extern const char* System_getLDFlags()
144145
{
145-
return strdup(ldflags);
146+
return strcpy(ModelicaAllocateString(strlen(ldflags)), ldflags);
146147
}
147148

148149
extern const char* System_getCFlags()
149150
{
150-
return strdup(cflags);
151+
return strcpy(ModelicaAllocateString(strlen(cflags)), cflags);
151152
}
152153

153154
extern const char* System_getExeExt()
@@ -184,7 +185,8 @@ extern const char* System_trimChar(const char* str, const char* char_to_remove)
184185

185186
extern const char* System_basename(const char* str)
186187
{
187-
return strdup(SystemImpl__basename(str));
188+
char *res = SystemImpl__basename(str);
189+
return strcpy(ModelicaAllocateString(strlen(res)), res);
188190
}
189191

190192
extern const char* System_dirname(const char* str)
@@ -195,10 +197,11 @@ extern const char* System_dirname(const char* str)
195197
char drive[_MAX_DRIVE], dir[_MAX_DIR], filename[_MAX_FNAME], extension[_MAX_EXT];
196198
_splitpath(str, drive, dir, filename, extension);
197199
sprintf(cpy, "%s/%s/",drive,dir);
198-
res = strdup(cpy);
200+
res = cpy;
199201
#else
200-
res = strdup(dirname(cpy));
202+
res = dirname(cpy);
201203
#endif
204+
res = strcpy(ModelicaAllocateString(strlen(res)), res);
202205
free(cpy);
203206
return res;
204207
}
@@ -347,7 +350,7 @@ extern char* System_substring(const char *str, int start, int stop)
347350
extern char* System_toupper(const char *str)
348351
{
349352
int i;
350-
char* strToUpper = strdup(str);
353+
char* strToUpper = strcpy(ModelicaAllocateString(strlen(str)),str);
351354
for (i = 0; i < strlen(strToUpper); i++)
352355
{
353356
strToUpper[i] = toupper(strToUpper[i]);
@@ -358,7 +361,7 @@ extern char* System_toupper(const char *str)
358361
extern char* System_tolower(const char *str)
359362
{
360363
int i;
361-
char* strToLower = strdup(str);
364+
char* strToLower = strcpy(ModelicaAllocateString(strlen(str)),str);
362365
for (i = 0; i < strlen(strToLower); i++)
363366
{
364367
strToLower[i] = tolower(strToLower[i]);
@@ -369,7 +372,7 @@ extern char* System_tolower(const char *str)
369372
const char* System_getClassnamesForSimulation()
370373
{
371374
if(class_names_for_simulation)
372-
return strdup(class_names_for_simulation);
375+
return strcpy(ModelicaAllocateString(strlen(class_names_for_simulation)),class_names_for_simulation);
373376
else
374377
return "{}";
375378
}
@@ -469,7 +472,7 @@ extern const char* System_readEnv(const char *envname)
469472
{
470473
char *envvalue = getenv(envname);
471474
if (envvalue == NULL) MMC_THROW();
472-
return strdup(envvalue);
475+
return strcpy(ModelicaAllocateString(strlen(envvalue)),envvalue);
473476
}
474477

475478
extern void System_getCurrentDateTime(int* sec, int* min, int* hour, int* mday, int* mon, int* year)
@@ -488,7 +491,8 @@ extern void System_getCurrentDateTime(int* sec, int* min, int* hour, int* mday,
488491

489492
extern const char* System_getUUIDStr()
490493
{
491-
return strdup(SystemImpl__getUUIDStr());
494+
char *res = SystemImpl__getUUIDStr();
495+
return strcpy(ModelicaAllocateString(strlen(res)),res);
492496
}
493497

494498
extern int System_loadLibrary(const char *name, int printDebug)
@@ -672,7 +676,9 @@ extern void System_getLoadModelPath(const char *className, void *prios, void *mp
672676
{
673677
*name = NULL;
674678
if (SystemImpl__getLoadModelPath(className,prios,mps,dir,name,isDir)) MMC_THROW();
675-
/* TODO: Do not strdup in parent... */
679+
char *res = strcpy(ModelicaAllocateString(strlen(name)),name);
680+
free(*name);
681+
*name = res;
676682
}
677683

678684
extern const char* System_getMakeCommand()

Compiler/runtime/UnitParserExt_omc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* External interface for UnitParserExt module */
22
#include "unitparser.h"
33
#include "unitparserext.cpp"
4+
#include "ModelicaUtilities.h"
45

56
extern "C"
67
{
@@ -34,7 +35,7 @@ const char* UnitParserExt_unit2str(void *nums, void *denoms, void *tpnoms, void
3435
//string res = unitParser->unit2str(unit);
3536
string res = unitParser->prettyPrintUnit2str(unit);
3637

37-
return strdup(res.c_str());
38+
return strcpy(ModelicaAllocateString(res.size()), res.c_str());
3839
}
3940

4041
void UnitParserExt_str2unit(const char *inStr, void **nums, void **denoms, void **tpnoms, void **tpdenoms, void **tpstrs)

Compiler/runtime/systemimpl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern "C" {
4343

4444
#include "meta_modelica.h"
4545
#include <limits.h>
46+
#include "ModelicaUtilities.h"
4647

4748
#include <stdio.h>
4849
#include <stdlib.h>

Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bootstrap-from-compiled:
102102
$(MAKE) -C testsuite/openmodelica/bootstrapping -f LinkMain.makefile bootstrap-from-compiled
103103
$(MAKE) omlibrary
104104

105-
bootstrap-dependencies: omc-diff interactive docs fmi fmil opencl_rt runtimeCPPinstall
105+
bootstrap-dependencies: omc-diff interactive docs fmi fmil opencl_rt
106106
$(MAKE) -C Compiler/runtime install
107107
$(MAKE) -C Compiler builtin install_scripts
108108
$(MAKE) -C Parser install

0 commit comments

Comments
 (0)