Skip to content

Commit

Permalink
- avoid stringListStringChar like the plague in Util.stringReplaceCha…
Browse files Browse the repository at this point in the history
…r (use System.stringReplace)

- fix huge memory leak in System.stringReplace (basically the result was strdup-ed and never freed)
- enable Boehm GC in MSVC.
- still some issues with things like Util.xmlEscape as if the 
  string is rather big (40Mb or so) we will run out of memory as 
  Boehm GC will not kick in until the function returns as all the 
  temps are on the local stack!



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19729 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Mar 25, 2014
1 parent 46bac05 commit 77826cf
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Compiler/Util/Util.mo
Expand Up @@ -1858,7 +1858,8 @@ public function stringReplaceChar "Takes a string and two chars and replaces the
input String inString3;
output String outString;
algorithm
outString:=
outString := System.stringReplace(inString1, inString2, inString3);
/*
matchcontinue (inString1,inString2,inString3)
local
list<String> strList,resList;
Expand All @@ -1877,6 +1878,7 @@ algorithm
then
fail();
end matchcontinue;
*/
end stringReplaceChar;

protected function stringReplaceChar2
Expand Down
8 changes: 4 additions & 4 deletions Compiler/runtime/Dynload.cpp
Expand Up @@ -314,25 +314,25 @@ static void *name_to_path(const char *name)
if (need_replace) {
tmp = _replace(name, "__", "_");
ident = mk_scon(tmp);
free(tmp);
GC_free(tmp);
} else {
/* memcpy(&tmp, &name, sizeof(char *)); */ /* don't try this at home */
ident = mk_scon((char*)name);
}
return Absyn__IDENT(ident);
} else {
size_t len = pos - name;
tmp = (char*) malloc(len + 1);
tmp = (char*)GC_malloc_atomic(len + 1);
memcpy(tmp, name, len);
tmp[len] = '\0';
if (need_replace) {
char *tmp2 = _replace(tmp, "__", "_");
ident = mk_scon(tmp2);
free(tmp2);
GC_free(tmp2);
} else {
ident = mk_scon(tmp);
}
free(tmp);
GC_free(tmp);
return Absyn__QUALIFIED(ident, name_to_path(pos + 1));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Compiler/runtime/System_rml.c
Expand Up @@ -842,8 +842,8 @@ RML_BEGIN_LABEL(System__isSameFile)
fn2_2 = _replace(canonName2,"//","/");
//printf("Replaced form f1:%s, \nf2:%s\n",fn1_2,fn2_2);
same = strcmp(fn1_2,fn2_2) == 0;
free(fn1_2);
free(fn2_2);
GC_free(fn1_2);
GC_free(fn2_2);
if(same){
RML_TAILCALLK(rmlSC);
}
Expand Down
7 changes: 4 additions & 3 deletions Compiler/runtime/printimpl.c
Expand Up @@ -34,6 +34,7 @@

#include "errorext.h"
#include "systemimpl.h"
#include "meta_modelica.h"

/* this is defined in systemimplmisc.h/cpp */
extern char* _replace(const char* source_str, const char* search_str, const char* replace_str);
Expand Down Expand Up @@ -449,7 +450,7 @@ static int PrintImpl__writeBufConvertLines(threadData_t *threadData,const char *
"#endif\n",
#if defined(__MINGW32__) || defined(_MSC_VER)
strtmp);
free(strtmp);
GC_free(strtmp);
#else
filename);
#endif
Expand All @@ -472,7 +473,7 @@ static int PrintImpl__writeBufConvertLines(threadData_t *threadData,const char *
} else if (0==regexec(&re_end, str, 3, matches, 0)) {
if (modelicaFileName) { /* There is sometimes #endModlicaLine without a matching #modelicaLine */
#if defined(__MINGW32__) || defined(_MSC_VER)
free(modelicaFileName);
GC_free(modelicaFileName);
#endif
modelicaFileName = NULL;
fprintf(file,"#line %ld OMC_FILE\n", nlines++);
Expand All @@ -489,7 +490,7 @@ static int PrintImpl__writeBufConvertLines(threadData_t *threadData,const char *
} while (1);
#if defined(__MINGW32__) || defined(_MSC_VER)
if (modelicaFileName) {
free(modelicaFileName);
GC_free(modelicaFileName);
}
#endif
/* We do destructive updates on the print buffer; hide our tracks */
Expand Down
4 changes: 2 additions & 2 deletions Compiler/runtime/settingsimpl.c
Expand Up @@ -296,8 +296,8 @@ extern const char* SettingsImpl__getTempDirectoryPath(void)
} else {
// Must do replacement in two steps, since the _replace function can not have similar source as target.
char *str = _replace(tempDirectory, (char*)"\\", (char*)"/");
tempDirectoryPath= _replace(str, (char*)"/", (char*)"\\\\");
free(str);
tempDirectoryPath = _replace(str, (char*)"/", (char*)"\\\\");
GC_free(str);
}
#else
const char* str = getenv("TMPDIR");
Expand Down
7 changes: 6 additions & 1 deletion Compiler/runtime/systemimplmisc.cpp
Expand Up @@ -27,13 +27,18 @@ void FindAndReplace( std::string& tInput, std::string tFind, std::string tReplac
extern "C" {

#include <string.h>
#include "meta_modelica.h"

char* _replace(const char* source_str, const char* search_str, const char* replace_str)
{
string str(source_str);
size_t len;
FindAndReplace(str,string(search_str),string(replace_str));

char* res = strdup(str.c_str());
len = strlen(str.c_str());
char* res = (char *)GC_malloc_atomic_ignore_off_page(len + 1);
strcpy(res, str.c_str());
res[len] = '\0';
return res;
}

Expand Down
6 changes: 4 additions & 2 deletions SimulationRuntime/c/meta/gc/mmc_gc.h
Expand Up @@ -41,10 +41,12 @@
#ifndef META_MODELICA_GC_H_
#define META_MODELICA_GC_H_

#if !defined(_MSC_VER) /* no gc on MSVC! */
#define _MMC_USE_BOEHM_GC_
#if defined(_MSC_VER)
#include "omc_inline.h"
#endif

#define _MMC_USE_BOEHM_GC_

#if defined(__cplusplus)
extern "C" {
#endif
Expand Down

0 comments on commit 77826cf

Please sign in to comment.