Skip to content

Commit f8e50e1

Browse files
committed
- some utility functions
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10459 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent f7ef635 commit f8e50e1

File tree

6 files changed

+221
-194
lines changed

6 files changed

+221
-194
lines changed

SimulationRuntime/c/math-support/delay.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "delay.h"
3535
#include "simulation_runtime.h"
3636
#include "ringbuffer.h"
37+
#include "error.h"
3738

3839
#include <assert.h>
3940
#include <stdio.h>
@@ -71,16 +72,16 @@ void initDelay(double startTime)
7172
assert(delayStructure);
7273

7374
for(i=0; i<numDelayExpressionIndex; i++)
74-
{
75-
delayStructure[i] = (RINGBUFFER*)malloc(sizeof(RINGBUFFER));
76-
allocRingBuffer(delayStructure[i], 1024, sizeof(t_TimeAndValue));
77-
}
75+
delayStructure[i] = allocRingBuffer(1024, sizeof(t_TimeAndValue));
7876

77+
DEBUG_INFO(DF_SOLVER, "initDelay called with startTime = %f\n", startTime);
78+
79+
/*
7980
if(sim_verbose >= LOG_SOLVER)
8081
{
8182
fprintf(stdout, "initDelay called with startTime = %f\n", startTime);
8283
fflush(NULL);
83-
}
84+
}*/
8485
}
8586

8687
void deinitDelay()
@@ -102,15 +103,15 @@ void deinitDelay()
102103
* the buffer in 'delayStruct' is not empty
103104
* 'time' is smaller than the last entry in 'delayStruct'
104105
*/
105-
static int findTime(double time, RINGBUFFER delayStruct)
106+
static int findTime(double time, RINGBUFFER *delayStruct)
106107
{
107108
int start = 0;
108-
int end = ringBufferLength(&delayStruct);
109+
int end = ringBufferLength(delayStruct);
109110
double t;
110111
do
111112
{
112113
int i = (start + end) / 2;
113-
t = ((t_TimeAndValue*)getRingData(&delayStruct, i))->time;
114+
t = ((t_TimeAndValue*)getRingData(delayStruct, i))->time;
114115
if(t > time)
115116
end = i;
116117
else
@@ -215,7 +216,7 @@ double delayImpl(int exprNumber, double exprValue, double time, double delayTime
215216
}
216217
else
217218
{
218-
i = findTime(timeStamp, *delayStruct);
219+
i = findTime(timeStamp, delayStruct);
219220
assert(i < length);
220221
time0 = ((t_TimeAndValue*)getRingData(delayStruct, i))->time;
221222
value0 = ((t_TimeAndValue*)getRingData(delayStruct, i))->value;
Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,108 @@
11
/*
2-
*This file is part of OpenModelica.
3-
*
4-
*Copyright (c) 1998-2008, Linköpings University,
5-
*Department of Computer and Information Science,
6-
*SE-58183 Linköping, Sweden.
7-
*
8-
*All rights reserved.
9-
*
10-
*THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11-
*LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12-
*THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13-
*PUBLIC LICENSE.
14-
*
15-
*The OpenModelica software and the Open Source Modelica
16-
*Consortium (OSMC) Public License (OSMC-PL) are obtained
17-
*from Linköpings University, either from the above address,
18-
*from the URL: http://www.ida.liu.se/projects/OpenModelica
19-
*and in the OpenModelica distribution.
20-
*
21-
*This program is distributed WITHOUT ANY WARRANTY; without
22-
*even the implied warranty of MERCHANTABILITY or FITNESS
23-
*FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24-
*IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25-
*OF OSMC-PL.
26-
*
27-
*See the full OSMC Public License conditions for more details.
28-
*
29-
*/
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2008, Linköpings University,
5+
* Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11+
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12+
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13+
* PUBLIC LICENSE.
14+
*
15+
* The OpenModelica software and the Open Source Modelica
16+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17+
* from Linköpings University, either from the above address,
18+
* from the URL: http://www.ida.liu.se/projects/OpenModelica
19+
* and in the OpenModelica distribution.
20+
*
21+
* This program is distributed WITHOUT ANY WARRANTY; without
22+
* even the implied warranty of MERCHANTABILITY or FITNESS
23+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25+
* OF OSMC-PL.
26+
*
27+
* See the full OSMC Public License conditions for more details.
28+
*
29+
*/
3030

3131
#include "ringbuffer.h"
3232

3333
#include <assert.h>
3434
#include <stdlib.h>
3535
#include <memory.h>
3636

37-
void allocRingBuffer(RINGBUFFER *rb, int sz, int item_size)
37+
struct RINGBUFFER
3838
{
39-
rb->first_element = 0;
40-
rb->num_element = 0;
41-
rb->buf_size = sz > 0 ? sz : 1;
42-
rb->item_size = item_size;
43-
rb->buffer = calloc(rb->buf_size, rb->item_size);
44-
assert(rb->buffer);
39+
void *buffer; /* buffer itself */
40+
int itemSize; /* size of one item in bytes */
41+
int firstElement; /* position of first element in buffer */
42+
int nElements; /* number of elements in buffer */
43+
int bufferSize; /* number of elements which could be stored in buffer */
44+
};
45+
46+
RINGBUFFER *allocRingBuffer(int bufferSize, int itemSize)
47+
{
48+
RINGBUFFER *rb = (RINGBUFFER*)malloc(sizeof(RINGBUFFER));
49+
50+
rb->firstElement = 0;
51+
rb->nElements = 0;
52+
rb->bufferSize = bufferSize > 0 ? bufferSize : 1;
53+
rb->itemSize = itemSize;
54+
rb->buffer = calloc(rb->bufferSize, rb->itemSize);
55+
ASSERT(rb->buffer, "out of memory");
4556
}
4657

4758
void freeRingBuffer(RINGBUFFER *rb)
4859
{
49-
free(rb->buffer);
50-
rb = NULL;
60+
free(rb->buffer);
61+
free(rb);
5162
}
5263

53-
void *getRingData(RINGBUFFER *rb, int nIndex)
64+
void *getRingData(RINGBUFFER *rb, int i)
5465
{
55-
assert(nIndex < rb->num_element);
56-
assert(0 <= nIndex);
57-
return ((char*)rb->buffer)+(((rb->first_element+nIndex)%rb->buf_size)*rb->item_size);
66+
ASSERT(i < rb->nElements, "index out of range");
67+
ASSERT(-rb->nElements < i, "index out of range");
68+
return ((char*)rb->buffer)+(((rb->firstElement+i)%rb->bufferSize)*rb->itemSize);
5869
}
5970

6071
void expandRingBuffer(RINGBUFFER *rb)
6172
{
62-
int i;
63-
void *temp = calloc(2*rb->buf_size, rb->item_size);
64-
assert(temp);
73+
int i;
6574

66-
for(i=0; i<rb->num_element; i++)
67-
memcpy(((char*)temp)+(i*rb->item_size), getRingData(rb, i), rb->item_size);
75+
void *temp = calloc(2*rb->bufferSize, rb->itemSize);
76+
ASSERT(temp, "out of memory");
6877

69-
free(rb->buffer);
70-
rb->buffer = temp;
71-
rb->buf_size *= 2;
72-
rb->first_element = 0;
78+
for(i=0; i<rb->nElements; i++)
79+
memcpy(((char*)temp)+(i*rb->itemSize), getRingData(rb, i), rb->itemSize);
80+
81+
free(rb->buffer);
82+
rb->buffer = temp;
83+
rb->bufferSize *= 2;
84+
rb->firstElement = 0;
7385
}
7486

7587
void appendRingData(RINGBUFFER *rb, void *value)
7688
{
77-
if(rb->buf_size < rb->num_element+1)
78-
expandRingBuffer(rb);
79-
80-
memcpy(((char*)rb->buffer)+(((rb->first_element+rb->num_element)%rb->buf_size)*rb->item_size), value, rb->item_size);
81-
++rb->num_element;
89+
if(rb->bufferSize < rb->nElements+1)
90+
expandRingBuffer(rb);
91+
92+
memcpy(((char*)rb->buffer)+(((rb->firstElement+rb->nElements)%rb->bufferSize)*rb->itemSize), value, rb->itemSize);
93+
++rb->nElements;
8294
}
8395

8496
void dequeueNFirstRingDatas(RINGBUFFER *rb, int n)
8597
{
86-
assert(n <= rb->num_element);
87-
assert(0 <= n);
88-
rb->first_element = (rb->first_element+n)%rb->buf_size;
89-
rb->num_element -= n;
98+
ASSERT(n <= rb->nElements, "index out of range");
99+
ASSERT(0 <= n, "index out of range)");
100+
101+
rb->firstElement = (rb->firstElement+n)%rb->bufferSize;
102+
rb->nElements -= n;
90103
}
91104

92105
int ringBufferLength(RINGBUFFER *rb)
93106
{
94-
return rb->num_element;
107+
return rb->nElements;
95108
}

SimulationRuntime/c/math-support/ringbuffer.h

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,58 @@
11
/*
2-
*This file is part of OpenModelica.
2+
* This file is part of OpenModelica.
33
*
4-
*Copyright (c) 1998-2008, Linköpings University,
5-
*Department of Computer and Information Science,
6-
*SE-58183 Linköping, Sweden.
4+
* Copyright (c) 1998-2008, Linköpings University,
5+
* Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
77
*
8-
*All rights reserved.
8+
* All rights reserved.
99
*
10-
*THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11-
*LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12-
*THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13-
*PUBLIC LICENSE.
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11+
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12+
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13+
* PUBLIC LICENSE.
1414
*
15-
*The OpenModelica software and the Open Source Modelica
16-
*Consortium (OSMC) Public License (OSMC-PL) are obtained
17-
*from Linköpings University, either from the above address,
18-
*from the URL: http://www.ida.liu.se/projects/OpenModelica
19-
*and in the OpenModelica distribution.
15+
* The OpenModelica software and the Open Source Modelica
16+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17+
* from Linköpings University, either from the above address,
18+
* from the URL: http://www.ida.liu.se/projects/OpenModelica
19+
* and in the OpenModelica distribution.
2020
*
21-
*This program is distributed WITHOUT ANY WARRANTY; without
22-
*even the implied warranty of MERCHANTABILITY or FITNESS
23-
*FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24-
*IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25-
*OF OSMC-PL.
21+
* This program is distributed WITHOUT ANY WARRANTY; without
22+
* even the implied warranty of MERCHANTABILITY or FITNESS
23+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25+
* OF OSMC-PL.
2626
*
27-
*See the full OSMC Public License conditions for more details.
27+
* See the full OSMC Public License conditions for more details.
2828
*
2929
*/
3030

3131
#ifndef RINGBUFFER_H
3232
#define RINGBUFFER_H
3333

3434
/*
35-
*This is an expanding ring buffer.
36-
*When it gets full, it doubles in size.
37-
*It's basically a queue which has get(ix) instead of get_first()/delete_first().
35+
* This is an expanding ring buffer.
36+
* When it gets full, it doubles in size.
37+
* It's basically a queue which has get(ix) instead of get_first()/delete_first().
3838
*/
3939

4040
#ifdef __cplusplus
4141
extern "C" {
4242
#endif
4343

44-
typedef struct RINGBUFFER
45-
{
46-
void *buffer;
47-
int item_size;
48-
int first_element;
49-
int num_element;
50-
int buf_size;
51-
}RINGBUFFER;
44+
struct RINGBUFFER;
45+
typedef struct RINGBUFFER RINGBUFFER;
5246

53-
void allocRingBuffer(RINGBUFFER *rb, int sz, int item_size);
54-
void freeRingBuffer(RINGBUFFER *rb);
47+
RINGBUFFER *allocRingBuffer(int bufferSize, int itemSize);
48+
void freeRingBuffer(RINGBUFFER *rb);
5549

56-
void *getRingData(RINGBUFFER *rb, int nIndex);
50+
void *getRingData(RINGBUFFER *rb, int nIndex);
5751

58-
void appendRingData(RINGBUFFER *rb, void *value);
59-
void dequeueNFirstRingDatas(RINGBUFFER *rb, int n);
52+
void appendRingData(RINGBUFFER *rb, void *value);
53+
void dequeueNFirstRingDatas(RINGBUFFER *rb, int n);
6054

61-
int ringBufferLength(RINGBUFFER *rb);
55+
int ringBufferLength(RINGBUFFER *rb);
6256

6357
#ifdef __cplusplus
6458
}

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ int verboseLevel(int argc, char**argv) {
479479
}
480480
if (flags->find("LOG_SOLVER", 0) != string::npos) {
481481
res |= LOG_SOLVER;
482+
globalDebugFlags |= DF_SOLVER;
482483
}
483484
if (flags->find("LOG_EVENTS", 0) != string::npos) {
484485
res |= LOG_EVENTS;
@@ -1117,6 +1118,10 @@ int _main_SimulationRuntime(int argc, char**argv, _X_DATA *data)
11171118
int retVal = -1;
11181119
if(!setjmp(globalJmpbuf) )
11191120
{
1121+
/*MSG("info", stdout, "test");
1122+
DEBUG_INFO(1, "test der DEBUG_INFO %d, %s, %f", 42, "hello world", 0.123f);
1123+
THROW("ENDE");*/
1124+
11201125
if (initRuntimeAndSimulation(argc, argv, data)) //initRuntimeAndSimulation returns 1 if an error occurs
11211126
return 1;
11221127
/* sighandler_t oldhandler = different type on all platforms... */

0 commit comments

Comments
 (0)