diff --git a/Makefile.am b/Makefile.am
index 586f800..c91bdc8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -16,6 +16,7 @@ AM_CFLAGS = -DNDEBUG -O2
endif
lib_LTLIBRARIES += librtbuf.la
+librtbuf_la_LIBADD = -ldata
librtbuf_la_SOURCES = \
symbol.c \
rtbuf_type.c \
diff --git a/configure.ac b/configure.ac
index 5f751fb..761c4ca 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
AC_PREREQ([2.59])
-AC_INIT([rtbuf], [0.1.0], [kmx.io], [rtbuf], [http://kmx.io/])
+AC_INIT([rtbuf], [0.2], [kmx.io], [rtbuf], [http://kmx.io/])
CFLAGS="$CFLAGS -W -Wall -Werror"
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([build-aux/m4])
@@ -29,13 +29,14 @@ if test -d /usr/lib; then
LDFLAGS="$LDFLAGS -L/usr/lib"
fi
-enable_rtbuf=true
-AM_CONDITIONAL([ENABLE_RTBUF], [test x"$enable_rtbuf" = x"true"])
-AC_SEARCH_LIBS([data_init], [data],
- [RTBUF_LIBS="$RTBUF_LIBS -ldata"],
+AC_SEARCH_LIBS([libdata_init], [data],
+ [LIBRTBUF_LIBS="$LIBRTBUF_LIBS -ldata"],
[if test x"$enable_rtbuf" = x"true"; then
AC_MSG_ERROR([missing libdata for rtbuf"])
fi])
+
+enable_rtbuf=true
+AM_CONDITIONAL([ENABLE_RTBUF], [test x"$enable_rtbuf" = x"true"])
AC_SEARCH_LIBS([cli_init], [cli],
[RTBUF_LIBS="$RTBUF_LIBS -lcli"],
[if test x"$enable_rtbuf" = x"true"; then
diff --git a/rtbuf.c b/rtbuf.c
index e5d1079..a713b25 100644
--- a/rtbuf.c
+++ b/rtbuf.c
@@ -15,6 +15,7 @@
*/
#include <assert.h>
+#include <data.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -23,40 +24,29 @@
#include "rtbuf_lib.h"
#include "symbol.h"
-s_rtbuf g_rtbuf[RTBUF_MAX];
-unsigned int g_rtbuf_max = RTBUF_MAX;
-unsigned int g_rtbuf_n = 0;
-int g_rtbuf_run = 0;
-unsigned int g_rtbuf_sort = 0;
-int g_rtbuf_sorted[RTBUF_MAX];
-unsigned int g_rtbuf_sorted_n = 0;
-int g_rtbuf_delete = 0;
-
-int rtbuf_init ()
+s_data_type g_rtbuf_type = {
+ sizeof(s_rtbuf) * 8,
+ DATA_TYPE_BITS
+};
+s_data_alloc g_rtbuf_alloc;
+s_rtbuf *g_rtbuf;
+int g_rtbuf_run = 0;
+unsigned int g_rtbuf_sort = 0;
+unsigned int g_rtbuf_sorted[RTBUF_MAX];
+unsigned int g_rtbuf_sorted_n = 0;
+
+int librtbuf_init ()
{
- bzero(g_rtbuf, sizeof(g_rtbuf));
+ libdata_init();
bzero(g_rtbuf_sorted, sizeof(g_rtbuf_sorted));
+ data_alloc_init(&g_rtbuf_alloc, &g_rtbuf_type, RTBUF_MAX, 0, 0);
+ g_rtbuf = g_rtbuf_alloc.mem;
+ rtbuf_type_init();
+ rtbuf_fun_init();
+ rtbuf_lib_init_();
return 0;
}
-int rtbuf_new_ (void *data)
-{
- s_rtbuf *rtb = g_rtbuf;
- unsigned int i = 0;
- if (g_rtbuf_n == RTBUF_MAX)
- return rtbuf_err("RTBUF_MAX exhausted");
- while (i < RTBUF_MAX) {
- if (rtb->data == 0) {
- rtb->data = data;
- g_rtbuf_n++;
- return i;
- }
- rtb++;
- i++;
- }
- return -1;
-}
-
int rtbuf_new (s_rtbuf_fun *rf)
{
int i;
@@ -68,9 +58,10 @@ int rtbuf_new (s_rtbuf_fun *rf)
if (!data)
return rtbuf_err("malloc failed");
bzero(data, rf->out_bytes);
- if ((i = rtbuf_new_(data)) < 0)
+ if ((i = data_new_i(&g_rtbuf_alloc)) < 0)
return -1;
rtb = &g_rtbuf[i];
+ rtb->data = data;
rtb->flags = 0;
rtb->fun = rf;
while (j < RTBUF_FUN_VAR_MAX) {
@@ -116,7 +107,7 @@ void rtbuf_unbind_all_out (s_rtbuf *rtb)
{
unsigned int rtb_i;
unsigned int i = 0;
- unsigned int n = g_rtbuf_n;
+ unsigned int n = g_rtbuf_alloc.n;
assert(rtb);
assert(g_rtbuf <= rtb);
assert(rtb < g_rtbuf + RTBUF_MAX);
@@ -149,7 +140,7 @@ void rtbuf_delete_ (s_rtbuf *rtb)
bzero(rtb->data, rtb->fun->out_bytes);
free(rtb->data);
bzero(rtb, sizeof(s_rtbuf));
- g_rtbuf_n--;
+ data_delete(&g_rtbuf_alloc, rtb);
}
void rtbuf_delete (s_rtbuf *rtb)
@@ -196,7 +187,7 @@ int rtbuf_data_set (s_rtbuf *rtb, symbol name, void *value,
int out_i = rtbuf_fun_out_find(rtb->fun, name);
if (out_i >= 0) {
s_rtbuf_fun_out *out = &rtb->fun->out[out_i];
- if (out->type->size == size) {
+ if (out->type->t.bits == size * 8) {
void *data = rtb->data + out->offset;
memcpy(data, value, size);
return size;
@@ -262,7 +253,7 @@ void rtbuf_find_roots (s_rtbuf_var_stack *rvs)
{
s_rtbuf *rtb = g_rtbuf;
unsigned int i = 0;
- unsigned int n = g_rtbuf_n;
+ unsigned int n = g_rtbuf_alloc.n;
unsigned int c = 0;
//printf("rtbuf_find_roots\n");
while (i < RTBUF_MAX && n > 0) {
@@ -295,7 +286,7 @@ void rtbuf_sort_push_child (s_rtbuf_var_stack *rvs,
ptr->var++;
if (rtb >= 0) {
while (i < g_rtbuf_sorted_n && !found) {
- if (g_rtbuf_sorted[i] == rtb)
+ if (g_rtbuf_sorted[i] == (unsigned) rtb)
found = 1;
i++;
}
@@ -315,7 +306,7 @@ void rtbuf_sort ()
s_rtbuf_var_stack rvs;
s_rtbuf_var_ptr *ptr;
printf("rtbuf_sort\n");
- if (g_rtbuf_n == 0)
+ if (g_rtbuf_alloc.n == 0)
return;
rtbuf_var_stack_init(&rvs);
rtbuf_find_roots(&rvs);
@@ -389,7 +380,8 @@ void rtbuf_stop ()
int rtbuf_find (const char *x)
{
int i = atoi(x);
- if (0 <= i && i < RTBUF_MAX && g_rtbuf[i].data && g_rtbuf[i].fun)
+ if (0 <= i && (unsigned int) i < g_rtbuf_alloc.n &&
+ g_rtbuf[i].data && g_rtbuf[i].fun)
return i;
return -1;
}
@@ -447,7 +439,7 @@ int rtbuf_out_int (s_rtbuf *rtb, unsigned int out, int default_value)
assert(out < rtb->fun->out_n);
o = &rtb->fun->out[out];
assert(o->type);
- if (o->type->size >= sizeof(int)) {
+ if (o->type->t.bits >= sizeof(int) * 8) {
int *i = (int*)(rtb->data + o->offset);
return *i;
}
diff --git a/rtbuf.h b/rtbuf.h
index f4a4bc0..1e08cd3 100644
--- a/rtbuf.h
+++ b/rtbuf.h
@@ -16,6 +16,7 @@
#ifndef RTBUF_H
#define RTBUF_H
+#include <data.h>
#include "rtbuf_defs.h"
#include "rtbuf_type.h"
#include "rtbuf_fun.h"
@@ -41,16 +42,13 @@ struct rtbuf
#define RTBUF_MAX 1000
-extern s_rtbuf g_rtbuf[];
-extern unsigned int g_rtbuf_max;
-extern unsigned int g_rtbuf_n;
-extern int g_rtbuf_run;
-extern unsigned int g_rtbuf_sort;
-extern int g_rtbuf_sorted[];
-extern unsigned int g_rtbuf_sorted_n;
+s_data_alloc g_rtbuf_alloc;
+s_rtbuf *g_rtbuf;
+int g_rtbuf_run;
+
+int librtbuf_init ();
int rtbuf_err (const char *msg);
-int rtbuf_init ();
int rtbuf_new (s_rtbuf_fun *rf);
void rtbuf_var_unbind (s_rtbuf *rtb, unsigned int var);
void rtbuf_unbind_all (s_rtbuf *rtb);
diff --git a/rtbuf_cli.c b/rtbuf_cli.c
index a4df987..79e7ae8 100644
--- a/rtbuf_cli.c
+++ b/rtbuf_cli.c
@@ -74,7 +74,7 @@ int rtbuf_cli_load (int argc, const char *argv[])
int rtbuf_cli_buffers (int argc, const char *argv[])
{
unsigned int i = 0;
- unsigned int n = g_rtbuf_n;
+ unsigned int n = g_rtbuf_alloc.n;
assert(argc == 0);
(void) argv;
printf("Listing %i buffers :\n", n);
@@ -297,9 +297,7 @@ int main (int argc, char *argv[])
(void) argc;
(void) argv;
symbols_init();
- rtbuf_type_init();
- rtbuf_fun_init();
- rtbuf_lib_init_();
+ librtbuf_init();
return repl();
}
diff --git a/rtbuf_lib.c b/rtbuf_lib.c
index 167d314..6d7dbb1 100644
--- a/rtbuf_lib.c
+++ b/rtbuf_lib.c
@@ -227,7 +227,7 @@ void rtbuf_lib_fun_out_init_fun (s_rtbuf_fun *fun,
o->type = rtbuf_type(out->type);
assert(o->type);
o->offset = offset;
- offset += o->type->size;
+ offset += (o->type->t.bits + 7) / 8;
out++;
i++;
}
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index d3b28cf..9b8a687 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -83,7 +83,7 @@ rtbuf_signal_sample (s_rtbuf *rtb,
assert(v->out < src->fun->out_n);
out = &src->fun->out[v->out];
assert(out->type);
- if (out->type->size >= sizeof(t_rtbuf_signal_sample)) {
+ if (out->type->t.bits >= sizeof(t_rtbuf_signal_sample) * 8) {
t_rtbuf_signal_sample *sample = (t_rtbuf_signal_sample*)
(src->data + out->offset);
return *sample;
@@ -132,9 +132,9 @@ void rtbuf_signal_fun (s_rtbuf *rtb,
assert(v->out < dest->fun->out_n);
out = &dest->fun->out[v->out];
assert(out->type);
- if (out->type->size >= sizeof(t_rtbuf_signal_sample))
+ if (out->type->t.bits >= sizeof(t_rtbuf_signal_sample) * 8)
rsf->signal = (double*)(dest->data + out->offset);
- if (out->type->size >= sizeof(t_rtbuf_signal))
+ if (out->type->t.bits >= sizeof(t_rtbuf_signal) * 8)
rsf->sample_fun = rtbuf_signal_sample_from_signal;
}
}
diff --git a/rtbuf_type.c b/rtbuf_type.c
index c3fb714..01721d3 100644
--- a/rtbuf_type.c
+++ b/rtbuf_type.c
@@ -14,6 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -21,13 +22,18 @@
#include "rtbuf_type.h"
#include "symbol.h"
-s_rtbuf_type g_rtbuf_type[RTBUF_TYPE_MAX];
-unsigned int g_rtbuf_type_n = 0;
+s_data_type g_rtbuf_data_type_type = {
+ sizeof(s_rtbuf_type) * 8,
+ DATA_TYPE_BITS
+};
+s_data_alloc g_rtbuf_data_type_alloc;
+s_rtbuf_type *g_rtbuf_data_type;
void rtbuf_type_init ()
{
- bzero(g_rtbuf_type, sizeof(g_rtbuf_type));
- g_rtbuf_type_n = 0;
+ data_alloc_init(&g_rtbuf_data_type_alloc, &g_rtbuf_data_type_type,
+ RTBUF_TYPE_MAX, 0, 0);
+ g_rtbuf_data_type = g_rtbuf_data_type_alloc.mem;
rtbuf_type_define("char" , sizeof(char));
rtbuf_type_define("unsigned char" , sizeof(unsigned char));
rtbuf_type_define("short" , sizeof(short));
@@ -42,36 +48,29 @@ void rtbuf_type_init ()
s_rtbuf_type * rtbuf_type_new (const char *name, unsigned int size)
{
- unsigned int i = 0;
- while (i < RTBUF_TYPE_MAX) {
- if (g_rtbuf_type[i].name == 0) {
- g_rtbuf_type[i].name = symbol_intern(name);
- g_rtbuf_type[i].size = size;
- g_rtbuf_type_n++;
- return &g_rtbuf_type[i];
- }
- i++;
- }
- return 0;
+ s_rtbuf_type *rt = data_new(&g_rtbuf_data_type_alloc);
+ if (!rt)
+ return 0;
+ rt->name = symbol_intern(name);
+ rt->t.bits = size * 8;
+ rt->t.type = DATA_TYPE_BITS;
+ return rt;
}
void rtbuf_type_delete (s_rtbuf_type *rt)
{
- if (rt->name) {
- rt->name = 0;
- rt->size = 0;
- g_rtbuf_type_n--;
- }
+ assert(rt);
+ data_delete(&g_rtbuf_data_type_alloc, rt);
}
s_rtbuf_type * rtbuf_type_find (symbol name)
{
unsigned int i = 0;
- unsigned int n = g_rtbuf_type_n;
- while (i < RTBUF_TYPE_MAX && n > 0) {
- if (g_rtbuf_type[i].name) {
- if (name == g_rtbuf_type[i].name)
- return &g_rtbuf_type[i];
+ unsigned int n = g_rtbuf_data_type_alloc.n;
+ while (i < g_rtbuf_data_type_alloc.max && n > 0) {
+ if (g_rtbuf_data_type[i].name) {
+ if (name == g_rtbuf_data_type[i].name)
+ return &g_rtbuf_data_type[i];
n--;
}
i++;
@@ -82,7 +81,7 @@ s_rtbuf_type * rtbuf_type_find (symbol name)
s_rtbuf_type * rtbuf_type_define (const char *name, unsigned int size)
{
s_rtbuf_type *found = rtbuf_type_find(name);
- if (found && found->size == size)
+ if (found && found->t.bits == size * 8)
return found;
return rtbuf_type_new(name, size);
}
@@ -103,7 +102,7 @@ s_rtbuf_type * rtbuf_type_parse_array (const char *name)
return 0;
buf[rb] = 0;
size = atoi(&buf[lb + 1]);
- return rtbuf_type_new(name, element_type->size * size);
+ return rtbuf_type_new(name, element_type->t.bits / 8 * size);
}
return 0;
}
diff --git a/rtbuf_type.h b/rtbuf_type.h
index d6883ab..9113619 100644
--- a/rtbuf_type.h
+++ b/rtbuf_type.h
@@ -16,19 +16,18 @@
#ifndef RTBUF_TYPE_H
#define RTBUF_TYPE_H
+#include <data.h>
#include "rtbuf_defs.h"
#include "symbol.h"
struct rtbuf_type {
- const char *name; /* symbol */
- unsigned int size;
+ symbol name;
+ s_data_type t;
+ s_data_alloc alloc;
};
#define RTBUF_TYPE_MAX 1024
-s_rtbuf_type g_rtbuf_type[RTBUF_TYPE_MAX];
-unsigned int g_rtbuf_type_n;
-
void rtbuf_type_init ();
s_rtbuf_type * rtbuf_type_new (const char *name,
unsigned int size);