Skip to content

Commit

Permalink
- conert ringbuffer from cpp to c
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10402 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Nov 10, 2011
1 parent 77d3ba7 commit 1a75e6d
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 416 deletions.
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/math-support/Makefile.in
Expand Up @@ -14,7 +14,7 @@ FFLAGS = -O -fexceptions
OBJS = bigden.o enorm.o hybrd1.o nelmead.o qform.o r1updt.o \
biglag.o dogleg.o fdjac1.o hybrj.o newuoa.o qrfac.o trsapp.o \
dpmpar.o hybrd.o lsame.o newuob.o r1mpyq.o update.o \
simulation_events.o simulation_delay.o simulation_init.o dgesv_aux.o \
simulation_events.o ringbuffer.o simulation_delay.o simulation_init.o dgesv_aux.o \

HFILES = blaswrap.h \
matrix.h \
Expand Down
94 changes: 94 additions & 0 deletions SimulationRuntime/c/simulation/math-support/ringbuffer.c
@@ -0,0 +1,94 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2008, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#include "ringbuffer.h"

#include <assert.h>
#include <stdlib.h>
#include <memory.h>

void allocRingBuffer(RINGBUFFER* rb, int sz, int item_size)
{
rb->first_element = 0;
rb->num_element = 0;
rb->buf_size = sz > 0 ? sz : 1;
rb->item_size = item_size;
rb->buffer = calloc(rb->buf_size, rb->item_size);
assert(rb->buffer);
}

void freeRingBuffer(RINGBUFFER* rb)
{
free(rb->buffer);
}

void* getRingData(RINGBUFFER* rb, int nIndex)
{
assert(nIndex < rb->num_element);
assert(0 <= nIndex);
return ((char*)rb->buffer)+(((rb->first_element+nIndex)%rb->buf_size)*rb->item_size);
}

void expandRingBuffer(RINGBUFFER* rb)
{
int i;
void* temp = calloc(2*rb->buf_size, rb->item_size);
assert(temp);

for(i=0; i<rb->num_element; i++)
memcpy(((char*)temp)+(i*rb->item_size), getRingData(rb, i), rb->item_size);

free(rb->buffer);
rb->buffer = temp;
rb->buf_size *= 2;
rb->first_element = 0;
}

void appendRingData(RINGBUFFER* rb, void* value)
{
if(rb->buf_size < rb->num_element+1)
expandRingBuffer(rb);

memcpy(((char*)rb->buffer)+(((rb->first_element+rb->num_element)%rb->buf_size)*rb->item_size), value, rb->item_size);
++rb->num_element;
}

void dequeueNFirstRingDatas(RINGBUFFER* rb, int n)
{
assert(n <= rb->num_element);
assert(0 <= n);
rb->first_element = (rb->first_element+n)%rb->buf_size;
rb->num_element -= n;
}

int ringBufferLength(RINGBUFFER* rb)
{
return rb->num_element;
}
73 changes: 20 additions & 53 deletions SimulationRuntime/c/simulation/math-support/ringbuffer.h
Expand Up @@ -31,70 +31,37 @@
#ifndef RINGBUFFER_H
#define RINGBUFFER_H

template<typename T>

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

class ringbuffer {
private:

T* buffer;
int first_element;
int num_element;
int buf_size;

T& fast_get(const int nIndex) {
return buffer[(first_element+nIndex)%buf_size];
}

void expand_buffer() {
T* nb = new T[buf_size*2];
for (int i=0; i<num_element; i++)
nb[i] = fast_get(i);
delete buffer;
buffer = nb;
buf_size *= 2;
first_element = 0;
// fprintf(stderr, "expanded to sz %d\n", buf_size);
}

public:
ringbuffer(int sz) : first_element(0),num_element(0),buf_size(sz) {
buffer = new T[buf_size];
}
#ifdef __cplusplus
extern "C" {
#endif

~ringbuffer() {}
typedef struct RINGBUFFER
{
void* buffer;
int item_size;
int first_element;
int num_element;
int buf_size;
}RINGBUFFER;

void append(T value) {
if (buf_size < num_element+1)
expand_buffer();
buffer[(first_element+num_element)%buf_size] = value;
++num_element;
}
void allocRingBuffer(RINGBUFFER* rb, int sz, int item_size);
void freeRingBuffer(RINGBUFFER* rb);

T& operator[] (const int nIndex) {
assert(nIndex < num_element);
return fast_get(nIndex);
}
void* getRingData(RINGBUFFER* rb, int nIndex);

void dequeue_n_first(const int n) {
assert(n <= num_element);
first_element = (first_element+n)%buf_size;
num_element -= n;
}
void appendRingData(RINGBUFFER* rb, void* value);
void dequeueNFirstRingDatas(RINGBUFFER* rb, int n);

T& get(const int nIndex) {
assert(nIndex < num_element);
return fast_get(nIndex);
}
int ringBufferLength(RINGBUFFER* rb);

int length() {
return num_element;
}
};
#ifdef __cplusplus
}
#endif

#endif

0 comments on commit 1a75e6d

Please sign in to comment.