Commit 6ceea9414fb3ad27db7152761dadcaf0a0b59f5b

Thomas de Grivel 2018-08-28T18:30:46

libdata for rtbuf_fun

diff --git a/rtbuf_fun.c b/rtbuf_fun.c
index a7378e5..2f8b17b 100644
--- a/rtbuf_fun.c
+++ b/rtbuf_fun.c
@@ -15,18 +15,24 @@
  */
 
 #include <assert.h>
+#include <data.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include "rtbuf.h"
 #include "symbol.h"
 
-s_rtbuf_fun g_rtbuf_fun[RTBUF_FUN_MAX];
-unsigned int g_rtbuf_fun_n = 0;
+s_data_type g_rtbuf_fun_type = {
+  sizeof(s_rtbuf_fun) * 8,
+  DATA_TYPE_BITS
+};
+s_data_alloc g_rtbuf_fun_alloc;
+s_rtbuf_fun *g_rtbuf_fun;
 
 void rtbuf_fun_init ()
 {
-  bzero(g_rtbuf_fun, sizeof(g_rtbuf_fun));
+  data_alloc_init(&g_rtbuf_fun_alloc, &g_rtbuf_fun_type,
+                  RTBUF_FUN_MAX, 0, 0);
 }
 
 int rtbuf_fun_p (s_rtbuf_fun *fun)
@@ -34,30 +40,16 @@ int rtbuf_fun_p (s_rtbuf_fun *fun)
   return fun && fun->name;
 }
 
-s_rtbuf_fun * rtbuf_fun_next ()
+s_rtbuf_fun * rtbuf_fun_new ()
 {
-  unsigned int i = 0;
-  if (g_rtbuf_fun_n == RTBUF_FUN_MAX) {
-    fprintf(stderr, "RTBUF_FUN_MAX exceeded\n");
-    return 0;
-  }
-  while (i < RTBUF_FUN_MAX) {
-    s_rtbuf_fun *fun = &g_rtbuf_fun[i];
-    if (!rtbuf_fun_p(fun)) {
-      g_rtbuf_fun_n++;
-      return fun;
-    }
-    i++;
-  }
-  return 0;
+  s_rtbuf_fun *rf = data_new(&g_rtbuf_fun_alloc);
+  return rf;
 }
 
 void rtbuf_fun_delete (s_rtbuf_fun *fun)
 {
-  if (fun && fun->name) {
-    bzero(fun, sizeof(s_rtbuf_fun));
-    g_rtbuf_fun_n--;
-  }
+  assert(fun);
+  data_delete(&g_rtbuf_fun_alloc, fun);
 }
 
 s_rtbuf_fun * rtbuf_fun_find (const char *x)
@@ -65,11 +57,13 @@ s_rtbuf_fun * rtbuf_fun_find (const char *x)
   const char *sym = symbol_find(x);
   if (sym) {
     unsigned int i = 0;
-    unsigned int n = g_rtbuf_fun_n;
-    while (i < RTBUF_FUN_MAX && n > 0) {
-      if (rtbuf_fun_p(&g_rtbuf_fun[i]) &&
-          sym == g_rtbuf_fun[i].name)
-        return &g_rtbuf_fun[i];
+    unsigned int n = g_rtbuf_alloc.n - g_rtbuf_alloc.free_n;
+    while (i < g_rtbuf_fun_alloc.n && n > 0) {
+      if (rtbuf_fun_p(&g_rtbuf_fun[i])) {
+        if (sym == g_rtbuf_fun[i].name)
+          return &g_rtbuf_fun[i];
+        n--;
+      }
       i++;
     }
   }
diff --git a/rtbuf_fun.h b/rtbuf_fun.h
index 771d14b..abaf1df 100644
--- a/rtbuf_fun.h
+++ b/rtbuf_fun.h
@@ -16,6 +16,7 @@
 #ifndef RTBUF_FUN_H
 #define RTBUF_FUN_H
 
+#include <data.h>
 #include "rtbuf_defs.h"
 #include "symbol.h"
 
@@ -48,12 +49,12 @@ struct rtbuf_fun {
 };
 
 #define RTBUF_FUN_MAX 1024
-extern s_rtbuf_fun g_rtbuf_fun[RTBUF_FUN_MAX];
-extern unsigned int g_rtbuf_fun_n;
+s_data_alloc g_rtbuf_fun_alloc;
+s_rtbuf_fun *g_rtbuf_fun;
 
 void              rtbuf_fun_init ();
 int               rtbuf_fun_p (s_rtbuf_fun *fun);
-s_rtbuf_fun *     rtbuf_fun_next ();
+s_rtbuf_fun *     rtbuf_fun_new ();
 void              rtbuf_fun_delete (s_rtbuf_fun *fun);
 s_rtbuf_fun *     rtbuf_fun_find (const char *x);
 int               rtbuf_fun_out_find (s_rtbuf_fun *fun,