[syslinux] Patch sensible callback framework

Ayvaz, James James.Ayvaz at hp.com
Wed May 5 08:40:36 PDT 2010


Resubmit based on hpa feedback
	+ double linked circular linked list
	+ code cleanup

Hopefully I didn't miss anything

diff -uprN syslinux-3.86-vanilla/com32/include/syslinux/callback.h syslinux-3.86/com32/include/syslinux/callback.h
--- syslinux-3.86-vanilla/com32/include/syslinux/callback.h	1969-12-31 18:00:00.000000000 -0600
+++ syslinux-3.86/com32/include/syslinux/callback.h	2010-05-04 03:06:31.000000000 -0500
@@ -0,0 +1,25 @@
+#ifndef LIBUTIL_CALLBACK_H
+#define LIBUTIL_CALLBACK_H
+
+#include <stddef.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+struct callback_record;
+
+typedef void (*callback_t)(void *rarg, va_list ap);
+
+typedef struct callback_record  {
+  callback_t function;
+  struct callback_record *prev;
+  struct callback_record *next;
+  void *rarg;
+} callback_record;
+
+
+
+callback_record* register_callback(callback_record **type, callback_t callback, void *rarg);
+int unregister_callback(callback_record **head, callback_record *cb);
+int invoke_callbacks(callback_record **head, ...);
+
+#endif
diff -uprN syslinux-3.86-vanilla/com32/lib/Makefile syslinux-3.86/com32/lib/Makefile
--- syslinux-3.86-vanilla/com32/lib/Makefile	2010-03-31 11:24:25.000000000 -0500
+++ syslinux-3.86/com32/lib/Makefile	2010-04-26 04:57:20.000000000 -0500
@@ -102,6 +102,8 @@ LIBOBJS = \
 	syslinux/run_default.o syslinux/run_command.o			\
 	syslinux/cleanup.o syslinux/localboot.o	syslinux/runimage.o	\
 	\
+	syslinux/callback.o                                             \
+	\
 	syslinux/loadfile.o syslinux/floadfile.o syslinux/zloadfile.o	\
 	\
 	syslinux/load_linux.o syslinux/initramfs.o			\
diff -uprN syslinux-3.86-vanilla/com32/lib/syslinux/callback.c syslinux-3.86/com32/lib/syslinux/callback.c
--- syslinux-3.86-vanilla/com32/lib/syslinux/callback.c	1969-12-31 18:00:00.000000000 -0600
+++ syslinux-3.86/com32/lib/syslinux/callback.c	2010-05-05 05:04:22.000000000 -0500
@@ -0,0 +1,110 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *   Copyright 2005-2008 H. Peter Anvin - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * callback.c
+ *
+ * generic callback handlers
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "syslinux/callback.h"
+
+callback_record* register_callback(callback_record **type, callback_t callback, void *rarg) {
+    callback_record *new;
+    new = malloc(sizeof(callback_record));
+    if (!new) {
+        return NULL;
+    }
+    new->function = callback;
+    new->rarg = rarg;
+
+    if (*type == NULL) { /* first and only node */
+        new->next = new;
+        new->prev = new;
+        *type = new;
+        return new;
+    }
+
+    (*type)->next->prev = new;
+    new->next = (*type)->next;
+    (*type)->next = new;
+    new->prev = *type;
+    return new;
+}
+
+int unregister_callback(callback_record **head, callback_record *cb) {
+    if (!head)
+        return -1;
+    if (!*head)
+        return -1;
+    if (!cb)
+        return -1;
+
+    cb->prev->next = cb->next;
+    cb->next->prev = cb->prev;
+
+    /* move head pointer */
+    if (cb == *head)
+        *head = cb->next;
+    free(cb);
+
+    /* reset head pointer */
+    if (cb == *head)
+        *head = NULL;
+
+    return 0;
+}
+
+
+int invoke_callbacks(callback_record **head, ...) {
+   va_list ap;
+   callback_record *curr;
+   if (!head) {
+       return -1;
+   }
+   if (!*head) {
+       return -1;
+   }
+
+   curr = *head;
+   do {
+       if (curr->function) {
+           va_start(ap, head);
+           curr->function(curr->rarg, ap);
+           va_end(ap);
+       }
+       curr = curr->next;
+   } while(curr != *head);
+}
+




More information about the Syslinux mailing list