userspace fun: maze game and a new device to support it
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / ldga.h
index c436798477775dafb5574d6121ad07c98c2a1150..a9b1430662fbebece728b1757744860d1422643e 100644 (file)
 #define __LUNAIX_LDGA_H
 
 #include <lunaix/types.h>
+#include <lunaix/compiler.h>
+
+#define ldga_el_id(ga_name, el_name) __lga_##ga_name##_##el_name
+#define ldga_section(ga_name) __attribute__((section(".lga." ga_name)))
 
 #define export_ldga_el(ga_name, el_name, type, val)                            \
-    type __attribute__((section(".lga." #ga_name)))                            \
-    __lga_##ga_name##_##el_name = (type)(val)
+    type ldga_section(#ga_name) must_emit ldga_el_id(ga_name, el_name) = (type)(val)
+
+#define export_ldga_el_sfx(ga_name, el_name, type, val, suffix)                \
+    type ldga_section(#ga_name "." #suffix) must_emit ldga_el_id(ga_name, el_name) =     \
+      (type)(val)
+
+#define export_ldga_el_idx(ga_name, i, type, val)                              \
+    export_ldga_el(ga_name, i, type, val)
+#define export_ldga_el_anon(ga_name, type, val)                                \
+    export_ldga_el_idx(ga_name, __COUNTER__, type, val)
 
 #define ldga_foreach(ga_name, el_type, index, el)                              \
     extern el_type __lga_##ga_name##_start[], __lga_##ga_name##_end;           \
          (ptr_t)&__lga_##ga_name##_end;                                        \
          el = __lga_##ga_name##_start[++index])
 
+/**
+ * @brief Invoke all elements in the array named `ga_name` of parameterless
+ * function pointers
+ *
+ */
+#define ldga_invoke_fn0(ga_name)                                               \
+    ({                                                                         \
+        int i = 0;                                                             \
+        ptr_t fn0;                                                             \
+        ldga_foreach(ga_name, ptr_t, i, fn0)                                   \
+        {                                                                      \
+            ((void (*)())fn0)();                                               \
+        }                                                                      \
+    })
+
 #endif /* __LUNAIX_LDGA_H */