Commit 685bdd982a9e5ce19e3204fd06ae3a67c0b0bf62

Thomas de Grivel 2020-01-24T23:32:30

wip variables

diff --git a/Makefile.am b/Makefile.am
index 4dc6c98..959ea44 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,12 +19,14 @@ librtbuf_la_SOURCES = \
 	symbol.c \
 	rtbuf_type.c \
 	rtbuf_proc.c \
+	rtbuf_var.c \
 	rtbuf.c
 include_HEADERS += \
 	symbol.h \
 	rtbuf_defs.h \
 	rtbuf_type.h \
 	rtbuf_proc.h \
+	rtbuf_var.h \
 	rtbuf.h
 
 if ENABLE_LIBDATA
diff --git a/rtbuf_defs.h b/rtbuf_defs.h
index 44455bf..b8faf3c 100644
--- a/rtbuf_defs.h
+++ b/rtbuf_defs.h
@@ -26,6 +26,9 @@ typedef struct rtbuf_lib_proc     s_rtbuf_lib_proc;
 typedef struct rtbuf_lib_proc_in  s_rtbuf_lib_proc_in;
 typedef struct rtbuf_lib_proc_out s_rtbuf_lib_proc_out;
 typedef struct rtbuf_type         s_rtbuf_type;
+typedef struct rtbuf_var          s_rtbuf_var;
+typedef union rtbuf_var_data      u_rtbuf_var_data;
+typedef enum rtbuf_var_type       e_rtbuf_var_type;
 
 typedef int f_rtbuf_proc (s_rtbuf *rtbuf);
 typedef int f_rtbuf_lib_init (s_rtbuf_lib *lib);
diff --git a/rtbuf_var.c b/rtbuf_var.c
new file mode 100644
index 0000000..894fbb6
--- /dev/null
+++ b/rtbuf_var.c
@@ -0,0 +1,67 @@
+#include <assert.h>
+#include <stdio.h>
+#include "data.h"
+#include "rtbuf_var.h"
+#include "rtbuf.h"
+
+s_data_type  g_rtbuf_var_type = {
+  sizeof(s_rtbuf_var) * 8,
+  DATA_TYPE_BITS
+};
+s_data_alloc g_rtbuf_var_alloc;
+s_rtbuf_var *g_rtbuf_var;
+
+void rtbuf_var_init (void)
+{
+  data_alloc_init(&g_rtbuf_var_alloc, &g_rtbuf_var_type, RTBUF_VAR_MAX, NULL, NULL);
+  g_rtbuf_var = g_rtbuf_var_alloc.mem;
+}
+
+s_rtbuf_var * rtbuf_var_new (const char *name)
+{
+  s_rtbuf_var *v = data_new(&g_rtbuf_var_alloc);
+  v->name = name;
+  return v;
+}
+
+s_rtbuf_var * rtbuf_var_find (const char *name)
+{
+  s_rtbuf_var *v = g_rtbuf_var + g_rtbuf_var_alloc.n;
+  unsigned i = g_rtbuf_var_alloc.n;
+  assert(name);
+  while (v--, i--) {
+    assert(v >= g_rtbuf_var);
+    if (v->name == name)
+      return v;
+  }
+  return NULL;
+}
+
+
+s_rtbuf_var * rtbuf_var_rtbuf_set (const char *name, unsigned i)
+{
+  s_rtbuf_var *v = rtbuf_var_find(name);
+  if (!v)
+    v = rtbuf_var_new(name);
+  if (v) {
+    v->type = RTBUF_VAR_RTBUF;
+    v->index = i;
+  }
+  return v;
+}
+
+void rtbuf_var_print (const s_rtbuf_var *v)
+{
+  assert(v);
+  switch (v->type) {
+  case RTBUF_VAR_NULL:
+    printf("NULL");
+    fflush(stdout);
+    break;
+  case RTBUF_VAR_RTBUF:
+    rtbuf_print_long(v->index);
+    break;
+  default:
+    assert(0);
+  }
+}
diff --git a/rtbuf_var.h b/rtbuf_var.h
new file mode 100644
index 0000000..8b6e394
--- /dev/null
+++ b/rtbuf_var.h
@@ -0,0 +1,21 @@
+#ifndef RTBUF_VAR_H
+#define RTBUF_VAR_H
+
+#include "rtbuf_defs.h"
+
+enum rtbuf_var_type {
+  RTBUF_VAR_NULL  = 0,
+  RTBUF_VAR_RTBUF = 1
+};
+
+#define RTBUF_VAR_MAX 1000
+
+struct rtbuf_var {
+  e_rtbuf_var_type type;
+  const char *name;
+  unsigned index;
+};
+
+void rtbuf_var_init (void);
+
+#endif