Skip to content

Commit

Permalink
Fixes for bootstrapping
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15315 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 25, 2013
1 parent d8e9db8 commit efacaf7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Compiler/Util/Print.mo
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ encapsulated package Print

public function saveAndClearBuf "saves and clears content of buffer and return a handle to the saved buffer so it can be restored by restorBuf later on"
output Integer handle;
external "C";
external "C" handle = Print_saveAndClearBuf() annotation(Library = "omcruntime");
end saveAndClearBuf;

public function restoreBuf
input Integer handle;
external "C";
external "C" Print_restoreBuf(handle) annotation(Library = "omcruntime");
end restoreBuf;

public function setBufSize
Expand Down
15 changes: 6 additions & 9 deletions Compiler/runtime/Print_omc.cpp → Compiler/runtime/Print_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@
#include <stdio.h>
#include <stdlib.h>
#include "meta_modelica.h"
extern "C" {

#include "printimpl.c"

extern int Print_saveAndClearBuf()
{
// TODO: copy impl. from Print_rml.c
MMC_THROW();
return 0;
int handle = PrintImpl__saveAndClearBuf();
if (handle) MMC_THROW();
return handle;
}

extern int Print_restoreBuf(int handle)
extern void Print_restoreBuf(int handle)
{
// TODO: copy impl. from Print_rml.c
MMC_THROW();
if (PrintImpl__restoreBuf(handle))
MMC_THROW();
}

extern void Print_printErrorBuf(const char* str)
Expand Down Expand Up @@ -117,5 +116,3 @@ extern void Print_writeBuf(const char* filename)
if (PrintImpl__writeBuf(filename))
MMC_THROW();
}

}
68 changes: 6 additions & 62 deletions Compiler/runtime/Print_rml.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,75 +48,19 @@ void Print_5finit(void)

RML_BEGIN_LABEL(Print__saveAndClearBuf)
{
long freeHandle,foundHandle=0;

if (! savedBuffers) {
savedBuffers = (char**)malloc(MAXSAVEDBUFFERS*sizeof(char*));
if (!savedBuffers) {
fprintf(stderr, "Internal error allocating savedBuffers in Print.saveAndClearBuf\n");
RML_TAILCALLK(rmlFC);
}
memset(savedBuffers,0,MAXSAVEDBUFFERS);
}
if (! savedCurSize) {
savedCurSize = (long*)malloc(MAXSAVEDBUFFERS*sizeof(long*));
if (!savedCurSize) {
fprintf(stderr, "Internal error allocating savedCurSize in Print.saveAndClearBuf\n");
RML_TAILCALLK(rmlFC);
}
memset(savedCurSize,0,MAXSAVEDBUFFERS);
}
if (! savedNfilled) {
savedNfilled = (long*)malloc(MAXSAVEDBUFFERS*sizeof(long*));
if (!savedNfilled) {
fprintf(stderr, "Internal error allocating savedNfilled in Print.saveAndClearBuf\n");
RML_TAILCALLK(rmlFC);
}
memset(savedNfilled,0,MAXSAVEDBUFFERS);
}
for (freeHandle=0; freeHandle< MAXSAVEDBUFFERS; freeHandle++) {
if (savedBuffers[freeHandle]==0)
{
foundHandle = 1;
break;
}
}
if (!foundHandle) {
fprintf(stderr,"Internal error, can not save more than %d buffers, increase MAXSAVEDBUFFERS in printimpl.c\n",MAXSAVEDBUFFERS);
RML_TAILCALLK(rmlFC);
}
savedBuffers[freeHandle] = buf;
savedCurSize[freeHandle] = cursize;
savedNfilled[freeHandle] = nfilled;
buf = (char*)malloc(INITIAL_BUFSIZE*sizeof(char));
nfilled=0;
cursize=0;
rmlA0 = mk_icon(freeHandle);
long handle = PrintImpl__saveAndClearBuf();
if (handle < 0)
RML_TAILCALLK(rmlFC);
rmlA0 = mk_icon(handle);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(Print__restoreBuf)
{
long handle = (long)RML_UNTAGFIXNUM(rmlA0);

if (handle < 0 || handle > MAXSAVEDBUFFERS-1) {
fprintf(stderr,"Internal error, hanlde %d out of range. Should be in [%d,&d]\n",handle,0,MAXSAVEDBUFFERS-1);
if (PrintImpl__restoreBuf((long)RML_UNTAGFIXNUM(rmlA0)))
RML_TAILCALLK(rmlFC);
} else {
if (buf) { free(buf);}
buf = savedBuffers[handle];
cursize = savedCurSize[handle];
nfilled = savedNfilled[handle];
savedBuffers[handle] = 0;
savedCurSize[handle] = 0;
savedNfilled[handle] = 0;
if (buf == 0) {
fprintf(stderr,"Internal error, handle %d does not contain a valid buffer pointer\n",handle);
RML_TAILCALLK(rmlFC);
}
RML_TAILCALLK(rmlSC);
}
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

Expand Down
69 changes: 69 additions & 0 deletions Compiler/runtime/printimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,72 @@ static int PrintImpl__hasBufNewLineAtEnd(void)
{
return (nfilled > 0 && buf[nfilled-1] == '\n') ? 1 : 0;
}

static int PrintImpl__restoreBuf(long handle)
{
if (handle < 0 || handle > MAXSAVEDBUFFERS-1) {
fprintf(stderr,"Internal error, hanlde %d out of range. Should be in [%d,&d]\n",handle,0,MAXSAVEDBUFFERS-1);
return 1;
} else {
if (buf) { free(buf);}
buf = savedBuffers[handle];
cursize = savedCurSize[handle];
nfilled = savedNfilled[handle];
savedBuffers[handle] = 0;
savedCurSize[handle] = 0;
savedNfilled[handle] = 0;
if (buf == 0) {
fprintf(stderr,"Internal error, handle %d does not contain a valid buffer pointer\n",handle);
return 1;
}
return 0;
}
}

static long PrintImpl__saveAndClearBuf()
{
long freeHandle,foundHandle=0;

if (! savedBuffers) {
savedBuffers = (char**)malloc(MAXSAVEDBUFFERS*sizeof(char*));
if (!savedBuffers) {
fprintf(stderr, "Internal error allocating savedBuffers in Print.saveAndClearBuf\n");
return -1;
}
memset(savedBuffers,0,MAXSAVEDBUFFERS);
}
if (! savedCurSize) {
savedCurSize = (long*)malloc(MAXSAVEDBUFFERS*sizeof(long*));
if (!savedCurSize) {
fprintf(stderr, "Internal error allocating savedCurSize in Print.saveAndClearBuf\n");
return -1;
}
memset(savedCurSize,0,MAXSAVEDBUFFERS);
}
if (! savedNfilled) {
savedNfilled = (long*)malloc(MAXSAVEDBUFFERS*sizeof(long*));
if (!savedNfilled) {
fprintf(stderr, "Internal error allocating savedNfilled in Print.saveAndClearBuf\n");
return -1;
}
memset(savedNfilled,0,MAXSAVEDBUFFERS);
}
for (freeHandle=0; freeHandle< MAXSAVEDBUFFERS; freeHandle++) {
if (savedBuffers[freeHandle]==0)
{
foundHandle = 1;
break;
}
}
if (!foundHandle) {
fprintf(stderr,"Internal error, can not save more than %d buffers, increase MAXSAVEDBUFFERS in printimpl.c\n",MAXSAVEDBUFFERS);
return -1;
}
savedBuffers[freeHandle] = buf;
savedCurSize[freeHandle] = cursize;
savedNfilled[freeHandle] = nfilled;
buf = (char*)malloc(INITIAL_BUFSIZE*sizeof(char));
nfilled=0;
cursize=0;
return freeHandle;
}

0 comments on commit efacaf7

Please sign in to comment.