Commit 09fcb4baa4d668d2b4c2e46b66808487ccda4efe

Thomas de Grivel 2018-08-28T17:43:35

start using libdata

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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);