Commit b8762f1a40953cfef44ff83851016d25b78e77b3

Thomas de Grivel 2018-08-03T03:41:28

wip refactor rtbuf_fun

diff --git a/Makefile b/Makefile
index 9f34a00..766bb15 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 
-SUBDIRS = rtbuf rtbuf_signal rtbuf_sndio rtbuf_glfw3
+SUBDIRS = so rtbuf rtbuf_signal rtbuf_sndio rtbuf_glfw3
 
 all: ${SUBDIRS}
 all: TARGET =
diff --git a/build/rtbuf/Makefile b/build/rtbuf/Makefile
index 3f677d2..58d001f 100644
--- a/build/rtbuf/Makefile
+++ b/build/rtbuf/Makefile
@@ -5,15 +5,14 @@ SRCDIR = ../..
 CPPFLAGS = -I${SRCDIR}/../cli/build/so
 CFLAGS = -O0 -ggdb -W -Wall -Werror
 
-LDFLAGS = -L${SRCDIR}/../cli/build/so
-LIBS = -lcli -lpthread
+LDFLAGS = -L../so -L${SRCDIR}/../cli/build/so
+LIBS = -lrtbuf -lcli -lpthread
 HEADERS = symbol.h rtbuf_defs.h rtbuf_type.h rtbuf_fun.h rtbuf.h rtbuf_lib.h
-OBJECTS = symbol.o rtbuf_type.o rtbuf_fun.o rtbuf.o rtbuf_lib.o \
-	  rtbuf_cli.o
+OBJECTS = rtbuf_lib.o rtbuf_cli.o
 
 SRC = ${HEADERS} ${OBJECTS:%.o=%.c}
 
-${PROGRAM}: ${OBJECTS}
+${PROGRAM}: ${OBJECTS} ../so/librtbuf.so
 	${CC} ${LDFLAGS} ${OBJECTS} ${LIBS} -o ${PROGRAM}
 
 ${OBJECTS}: ${HEADERS}
diff --git a/build/rtbuf_signal/Makefile b/build/rtbuf_signal/Makefile
index 6657b00..aeff511 100644
--- a/build/rtbuf_signal/Makefile
+++ b/build/rtbuf_signal/Makefile
@@ -9,7 +9,7 @@ LDFLAGS = -fPIC -shared
 LIBS = -lm
 HEADERS = symbol.h rtbuf_defs.h rtbuf_type.h rtbuf_fun.h rtbuf.h \
 	  rtbuf_lib.h rtbuf_music.h rtbuf_signal.h rtbuf_signal_type.h
-OBJECTS = symbol.o rtbuf_signal.o rtbuf_signal_sinus.o \
+OBJECTS = symbol.o rtbuf_music.o rtbuf_signal.o rtbuf_signal_sinus.o \
 	  rtbuf_signal_square.o rtbuf_signal_adsr.o rtbuf_signal_synth.o
 
 SRC = ${HEADERS} ${OBJECTS:%.o=%.c}
diff --git a/build/so/Makefile b/build/so/Makefile
new file mode 100644
index 0000000..bed3831
--- /dev/null
+++ b/build/so/Makefile
@@ -0,0 +1,30 @@
+
+PROGRAM = librtbuf.so
+SRCDIR = ../..
+
+CPPFLAGS =
+CFLAGS = -O0 -ggdb -W -Wall -Werror -fpic
+
+LDFLAGS = -fPIC -shared
+LIBS =
+HEADERS = symbol.h rtbuf_defs.h rtbuf_type.h rtbuf_fun.h rtbuf.h
+OBJECTS = symbol.o rtbuf_type.o rtbuf_fun.o rtbuf.o
+
+SRC = ${HEADERS} ${OBJECTS:%.o=%.c}
+
+${PROGRAM}: ${OBJECTS}
+	${CC} ${LDFLAGS} ${OBJECTS} ${LIBS} -o ${PROGRAM}
+
+${OBJECTS}: ${HEADERS}
+
+${SRC}: %: ${SRCDIR}/%
+	if [ -f $@ ]; then chmod u+w $@; fi
+	cp $< $@
+	chmod a-w $@
+
+CLEANFILES += ${PROGRAM} ${OBJECTS} ${SRC}
+
+clean:
+	rm -f ${CLEANFILES}
+
+.PHONY: clean
diff --git a/rtbuf.c b/rtbuf.c
index 093d849..36b4e8e 100644
--- a/rtbuf.c
+++ b/rtbuf.c
@@ -5,7 +5,6 @@
 #include <string.h>
 #include <strings.h>
 #include "rtbuf.h"
-#include "rtbuf_lib.h"
 #include "symbol.h"
 
 s_rtbuf      g_rtbuf[RTBUF_MAX];
diff --git a/rtbuf_cli.c b/rtbuf_cli.c
index aebccac..96da6b6 100644
--- a/rtbuf_cli.c
+++ b/rtbuf_cli.c
@@ -366,7 +366,7 @@ int main (int argc, char *argv[])
   symbols_init();
   rtbuf_type_init();
   rtbuf_fun_init();
-  rtbuf_lib_init();
+  rtbuf_lib_init_();
   return repl();
 }
 
diff --git a/rtbuf_defs.h b/rtbuf_defs.h
index 89cb695..469239b 100644
--- a/rtbuf_defs.h
+++ b/rtbuf_defs.h
@@ -13,5 +13,6 @@ typedef struct rtbuf_lib_fun_var s_rtbuf_lib_fun_var;
 typedef struct rtbuf_type        s_rtbuf_type;
 
 typedef int f_rtbuf_fun (s_rtbuf *rtbuf);
+typedef int f_rtbuf_lib_init (s_rtbuf_lib *lib);
 
 #endif
diff --git a/rtbuf_fun.c b/rtbuf_fun.c
index e9c32f4..36ac14b 100644
--- a/rtbuf_fun.c
+++ b/rtbuf_fun.c
@@ -1,9 +1,9 @@
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include "rtbuf.h"
-#include "rtbuf_lib.h"
 #include "symbol.h"
 
 s_rtbuf_fun g_rtbuf_fun[RTBUF_FUN_MAX];
@@ -19,39 +19,7 @@ int rtbuf_fun_p (s_rtbuf_fun *fun)
   return fun && fun->name;
 }
 
-void rtbuf_fun_new_var (s_rtbuf_fun *fun, s_rtbuf_lib_fun_var *var)
-{
-  unsigned int i = 0;
-  bzero(fun->var, sizeof(fun->var));
-  if (var)
-    while (var->name) {
-      s_rtbuf_fun_var *v = &fun->var[i];
-      v->name = symbol_intern(var->name);
-      v->type = rtbuf_type(var->type);
-      i++;
-    }
-  fun->var_n = i;
-}
-
-void rtbuf_fun_new_out (s_rtbuf_fun *fun, s_rtbuf_lib_fun_out *out)
-{
-  unsigned int i = 0;
-  bzero(fun->out, sizeof(fun->out));
-  if (out) {
-    unsigned int offset = 0;
-    while (out->name) {
-      s_rtbuf_fun_out *o = &fun->out[i];
-      o->name = symbol_intern(out->name);
-      o->type = rtbuf_type(out->type);
-      o->offset = offset;
-      offset += o->type->size;
-      i++;
-    }
-  }
-  fun->out_n = i;
-}
-
-s_rtbuf_fun * rtbuf_fun_new (s_rtbuf_lib_fun *x)
+s_rtbuf_fun * rtbuf_fun_next ()
 {
   unsigned int i = 0;
   if (g_rtbuf_fun_n == RTBUF_FUN_MAX) {
@@ -61,14 +29,8 @@ s_rtbuf_fun * rtbuf_fun_new (s_rtbuf_lib_fun *x)
   while (i < RTBUF_FUN_MAX) {
     s_rtbuf_fun *fun = &g_rtbuf_fun[i];
     if (!rtbuf_fun_p(fun)) {
-      fun->name = symbol_intern(x->name);
-      fun->f = x->f;
-      fun->start = x->start;
-      fun->stop = x->stop;
-      rtbuf_fun_new_var(&g_rtbuf_fun[i], x->var);
-      rtbuf_fun_new_out(&g_rtbuf_fun[i], x->out);
       g_rtbuf_fun_n++;
-      return &g_rtbuf_fun[i];
+      return fun;
     }
     i++;
   }
diff --git a/rtbuf_fun.h b/rtbuf_fun.h
index fb20f97..07cf066 100644
--- a/rtbuf_fun.h
+++ b/rtbuf_fun.h
@@ -38,7 +38,7 @@ extern unsigned int g_rtbuf_fun_n;
 
 extern void              rtbuf_fun_init ();
 extern int               rtbuf_fun_p (s_rtbuf_fun *fun);
-extern s_rtbuf_fun *     rtbuf_fun_new (s_rtbuf_lib_fun *x);
+extern s_rtbuf_fun *     rtbuf_fun_next ();
 extern void              rtbuf_fun_delete (s_rtbuf_fun *fun);
 extern s_rtbuf_fun *     rtbuf_fun_find (const char *x);
 extern s_rtbuf_fun_out * rtbuf_fun_out_find (s_rtbuf_fun *fun,
diff --git a/rtbuf_lib.c b/rtbuf_lib.c
index a8ac041..0a45314 100644
--- a/rtbuf_lib.c
+++ b/rtbuf_lib.c
@@ -19,7 +19,7 @@ char *g_rtbuf_lib_path[] = { "./",
                              "/usr/lib/rtbuf/",
                              0 };
 
-void rtbuf_lib_init ()
+void rtbuf_lib_init_ ()
 {
   char *in = getenv("HOME");
   char *out = g_rtbuf_lib_user_dir;
@@ -135,6 +135,7 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   s_rtbuf_lib_fun *fun;
   unsigned long *ver;
   unsigned int i = 0;
+  f_rtbuf_lib_init *init;
   if (!lib)
     return 0;
   rtbuf_lib_load_path(lib, name);
@@ -147,6 +148,11 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   assert(*ver == RTBUF_LIB_VER);
   lib->name = symbol_intern(name);
   //printf("lib_load name %s\n", lib->name);
+  if ((init = dlsym(lib->lib, "rtbuf_lib_init")))
+    if (init(lib) < 0) {
+      rtbuf_lib_delete(lib);
+      return 0;
+    }
   fun = dlsym(lib->lib, "rtbuf_lib_fun");
   lib->fun_n = 0;
   while (lib->fun_n < RTBUF_LIB_MAX &&
@@ -160,7 +166,9 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   }
   lib->fun = malloc(sizeof(s_rtbuf_fun*) * (lib->fun_n + 1));
   while (i < lib->fun_n) {
-    lib->fun[i] = rtbuf_fun_new(&fun[i]);
+    lib->fun[i] = rtbuf_fun_next();
+    assert(lib->fun[i]);
+    rtbuf_lib_fun_init_fun(lib->fun[i], &fun[i]);
     lib->fun[i]->lib = lib;
     lib->fun[i]->lib_fun = i;
     i++;
@@ -168,3 +176,50 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   lib->fun[i] = 0;
   return lib;
 }
+
+void rtbuf_lib_fun_var_init_fun (s_rtbuf_fun *fun,
+                                 s_rtbuf_lib_fun_var *var)
+{
+  unsigned int i = 0;
+  bzero(fun->var, sizeof(fun->var));
+  if (var)
+    while (var->name) {
+      s_rtbuf_fun_var *v = &fun->var[i];
+      v->name = symbol_intern(var->name);
+      v->type = rtbuf_type(var->type);
+      var++;
+      i++;
+    }
+  fun->var_n = i;
+}
+
+void rtbuf_lib_fun_out_init_fun (s_rtbuf_fun *fun,
+                                 s_rtbuf_lib_fun_out *out)
+{
+  unsigned int i = 0;
+  bzero(fun->out, sizeof(fun->out));
+  if (out) {
+    unsigned int offset = 0;
+    while (out->name) {
+      s_rtbuf_fun_out *o = &fun->out[i];
+      o->name = symbol_intern(out->name);
+      o->type = rtbuf_type(out->type);
+      assert(o->type);
+      o->offset = offset;
+      offset += o->type->size;
+      out++;
+      i++;
+    }
+  }
+  fun->out_n = i;
+}
+
+void rtbuf_lib_fun_init_fun (s_rtbuf_fun *fun, s_rtbuf_lib_fun *x)
+{
+  fun->name = symbol_intern(x->name);
+  fun->f = x->f;
+  fun->start = x->start;
+  fun->stop = x->stop;
+  rtbuf_lib_fun_var_init_fun(fun, x->var);
+  rtbuf_lib_fun_out_init_fun(fun, x->out);
+}
diff --git a/rtbuf_lib.h b/rtbuf_lib.h
index 23e8d23..0c79cc3 100644
--- a/rtbuf_lib.h
+++ b/rtbuf_lib.h
@@ -39,8 +39,14 @@ extern unsigned int g_rtbuf_lib_n;
 void          rtbuf_lib_delete (s_rtbuf_lib *rl);
 int           rtbuf_lib_find (const char *str);
 int           rtbuf_lib_find_fun (s_rtbuf_lib *rl, const char *str);
-void          rtbuf_lib_init ();
+void          rtbuf_lib_init_ ();
 s_rtbuf_lib * rtbuf_lib_load (const char *path);
 s_rtbuf_lib * rtbuf_lib_new ();
 
+void rtbuf_lib_fun_var_init_fun (s_rtbuf_fun *fun,
+                                 s_rtbuf_lib_fun_var *var);
+void rtbuf_lib_fun_out_init_fun (s_rtbuf_fun *fun,
+                                 s_rtbuf_lib_fun_out *out);
+void rtbuf_lib_fun_init_fun (s_rtbuf_fun *fun, s_rtbuf_lib_fun *x);
+
 #endif
diff --git a/rtbuf_music.c b/rtbuf_music.c
index ae58317..5a2fdba 100644
--- a/rtbuf_music.c
+++ b/rtbuf_music.c
@@ -2,6 +2,13 @@
 #include "rtbuf.h"
 #include "rtbuf_music.h"
 
+int rtbuf_music_init ()
+{
+  rtbuf_type_new(RTBUF_MUSIC_NOTES_TYPE,
+                 RTBUF_MUSIC_NOTES_SIZE);
+  return 0;
+}
+
 int rtbuf_music_notes_new (s_rtbuf_music_notes *notes)
 {
   unsigned int i = 0;
diff --git a/rtbuf_music.h b/rtbuf_music.h
index cd55445..3cdd555 100644
--- a/rtbuf_music.h
+++ b/rtbuf_music.h
@@ -15,8 +15,10 @@ typedef struct rtbuf_music_notes {
   unsigned int note_n;
 } s_rtbuf_music_notes;
 
+#define RTBUF_MUSIC_NOTES_SIZE sizeof(s_rtbuf_music_notes)
 #define RTBUF_MUSIC_NOTES_TYPE "music_notes"
 
+extern int  rtbuf_music_init ();
 void rtbuf_music_notes_init (s_rtbuf_music_notes *notes);
 int  rtbuf_music_notes_new (s_rtbuf_music_notes *notes);
 void rtbuf_music_notes_delete (s_rtbuf_music_notes *notes,
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index 57d1d5c..d366f9f 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -65,6 +65,18 @@ s_rtbuf_lib_fun rtbuf_lib_fun[] = {
     g_rtbuf_signal_synth_var, g_rtbuf_signal_synth_out },
   { 0, 0, 0, 0, 0, 0 } };
 
+int rtbuf_lib_init (s_rtbuf_lib *lib)
+{
+  (void) lib;
+  printf("rtbuf_lib_init signal\n");
+  rtbuf_music_init();
+  rtbuf_type_new(RTBUF_SIGNAL_SYNTH_NOTE_TYPE,
+                 RTBUF_SIGNAL_SYNTH_NOTE_SIZE);
+  rtbuf_type_new(RTBUF_SIGNAL_SYNTH_NOTES_TYPE,
+                 RTBUF_SIGNAL_SYNTH_NOTES_SIZE);
+  return 0;
+}
+
 double rtbuf_signal_sample (s_rtbuf *rtb, unsigned int var,
                             unsigned int i, double default_value)
 {
diff --git a/rtbuf_type.h b/rtbuf_type.h
index 37f60fe..b9905f1 100644
--- a/rtbuf_type.h
+++ b/rtbuf_type.h
@@ -14,7 +14,8 @@ 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);
+extern s_rtbuf_type * rtbuf_type_new (const char *name,
+                                      unsigned int size);
 void           rtbuf_type_delete (s_rtbuf_type *rt);
 s_rtbuf_type * rtbuf_type_find (const char *name);
 s_rtbuf_type * rtbuf_type (const char *name);