Skip to content

Commit

Permalink
small memory optimizations
Browse files Browse the repository at this point in the history
- free the array in List.mo
- return the input in SystemImpl__iconv if to == from
- signal that we don't want malloc_atomic in arrayCreateNoInit
  • Loading branch information
adrpo authored and OpenModelica-Hudson committed Jun 26, 2016
1 parent 499d340 commit 2ce167a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Compiler/Util/List.mo
Expand Up @@ -99,6 +99,7 @@ protected
import MetaModelica.Dangerous.{listReverseInPlace, arrayGetNoBoundsChecking, arrayUpdateNoBoundsChecking, arrayCreateNoInit};
import MetaModelica.Dangerous;
import DoubleEndedList;
import GC;

public function create<T>
"Creates a list from an element."
Expand Down Expand Up @@ -1044,6 +1045,7 @@ algorithm

arrayUpdate(arr, i, false);
end for;
GC.free(arr);
end uniqueIntN;

public function uniqueIntNArr
Expand Down
15 changes: 12 additions & 3 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -2232,19 +2232,20 @@ extern char* SystemImpl__iconv(const char * str, const char *from, const char *t
char *buf;
sz = strlen(str);
buflen = sz*8;
buf = (char*) omc_alloc_interface.malloc_atomic(buflen);
assert(buf != 0);
*buf = 0;
/* fprintf(stderr,"iconv(%s,to=%s,%s) of size %d, buflen %d\n",str,to,from,sz,buflen); */
ic = iconv_open(to, from);
if (ic == (iconv_t) -1) {
if (printError) {
char *ignore = SystemImpl__iconv__ascii(str);
const char *tokens[4] = {strerror(errno),from,to,ignore};
c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(\"%s\",to=\"%s\",from=\"%s\") failed: %s"),tokens,4);
omc_alloc_interface.free_uncollectable(ignore);
}
return (char*) "";
}
buf = (char*) omc_alloc_interface.malloc_atomic(buflen);
assert(buf != 0);
*buf = 0;
in_str = (char*) str;
out_sz = buflen-1;
res = buf;
Expand All @@ -2255,14 +2256,22 @@ extern char* SystemImpl__iconv(const char * str, const char *from, const char *t
char *ignore = SystemImpl__iconv__ascii(str);
const char *tokens[4] = {strerror(errno),from,to,ignore};
c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(\"%s\",to=\"%s\",from=\"%s\") failed: %s"),tokens,4);
omc_alloc_interface.free_uncollectable(ignore);
}
omc_alloc_interface.free_uncollectable(buf);
return (char*) "";
}
buf[(buflen-1)-out_sz] = 0;
if (strlen(buf) != (buflen-1)-out_sz) {
if (printError) c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(to=%s) failed because the character set output null bytes in the middle of the string."),&to,1);
omc_alloc_interface.free_uncollectable(buf);
return (char*) "";
}
if (!strcmp(from, to) && !strcmp(str, buf))
{
omc_alloc_interface.free_uncollectable(buf);
return (char*)str;
}
return buf;
}

Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/meta/meta_modelica_builtin.h
Expand Up @@ -158,7 +158,7 @@ static inline modelica_metatype arrayCreateNoInit(modelica_integer nelts, modeli
if (nelts < 0) {
MMC_THROW();
} else {
return (struct mmc_struct*)mmc_mk_box_no_assign(nelts, MMC_ARRAY_TAG, MMC_IS_IMMEDIATE(dummy));
return (struct mmc_struct*)mmc_mk_box_no_assign(nelts, MMC_ARRAY_TAG, 0);
}
}
#define arrayGetNoBoundsChecking(arr,ix) (MMC_STRUCTDATA((arr))[(ix)-1])
Expand Down

0 comments on commit 2ce167a

Please sign in to comment.