[syslinux] [Syslinux] Patch sensible callback framework

Ayvaz, James James.Ayvaz at hp.com
Thu Jun 10 09:52:25 PDT 2010


Here is my latest, I had to split it back into a .h/.c files to get address compiler warnings about variable argument lists.


>From 40116afc402f0f311bbd1d263fd40aec6bd856fd Mon Sep 17 00:00:00 2001
From: root <root at linux.site>
Date: Thu, 10 Jun 2010 06:19:46 -0500
Subject: [PATCH] added callback framework

---
 com32/include/syslinux/callback.h |   67 ++++++++++++++++++++++++++++++++
 com32/lib/syslinux/callback.c     |   76 +++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+), 0 deletions(-)
 create mode 100644 com32/include/syslinux/callback.h
 create mode 100644 com32/lib/syslinux/callback.c

diff --git a/com32/include/syslinux/callback.h b/com32/include/syslinux/callback.h
new file mode 100644
index 0000000..40de8f4
--- /dev/null
+++ b/com32/include/syslinux/callback.h
@@ -0,0 +1,67 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *   Copyright 2007-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.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/callback.h
+ *
+ * sensible callback framework
+ */
+
+#ifndef _SYSLINUX_CALLBACK_H
+#define _SYSLINUX_CALLBACK_H
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <assert.h>
+
+struct callback_record;
+
+typedef void (*callback_t)(const void *rarg, va_list ap);
+
+#define DECLARE_CALLBACK(name) 		\
+struct callback_record name = { 	\
+	.function 	= NULL,		\
+	.next		= &name,	\
+	.prev		= &name,	\
+}
+
+
+struct callback_record  {
+  callback_t function;
+  struct callback_record *next;
+  struct callback_record *prev;
+  const void *rarg;
+};
+
+
+struct callback_record* register_callback(struct callback_record *head, callback_t callback, const void *rarg);
+int unregister_callback(struct callback_record *cb);
+int invoke_callbacks(struct callback_record *head, ...);
+
+#endif /* _SYSLINUX_CALLBACK_H */
diff --git a/com32/lib/syslinux/callback.c b/com32/lib/syslinux/callback.c
new file mode 100644
index 0000000..60d6025
--- /dev/null
+++ b/com32/lib/syslinux/callback.c
@@ -0,0 +1,76 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *   Copyright 2007-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.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/callback.c
+ *
+ * sensible callback framework
+ */
+
+#include <syslinux/callback.h>
+
+struct callback_record* register_callback(struct callback_record *head, callback_t callback, const void *rarg)
+{
+    struct callback_record *new;
+
+    new = malloc(sizeof(struct callback_record));
+    if (!new)
+        return NULL;
+
+    new->function = callback;
+    new->rarg = rarg;
+    new->next = head->next;
+    new->prev = head;
+    head->next->prev = new;
+    head->next = new;
+    return new;
+}
+
+int unregister_callback(struct callback_record *cb)
+{
+    cb->next->prev = cb->prev;
+    cb->prev->next = cb->next;
+    free(cb);
+    return 0;
+}
+
+int invoke_callbacks(struct callback_record *head, ...)
+{
+   va_list ap;
+   struct callback_record *curr;
+
+   curr = head;
+   do {
+       if (curr->function) {
+           va_start(ap, head);
+           curr->function(curr->rarg, ap);
+           va_end(ap);
+       }
+       curr = curr->next;
+   } while(curr != head);
+   return 0;
+}
-- 
1.6.0.2


>From ffc0741ffe06baf02a0267b249e37edde266d8bd Mon Sep 17 00:00:00 2001
From: root <root at linux.site>
Date: Thu, 10 Jun 2010 06:23:26 -0500
Subject: [PATCH] updated to include callback.o

---
 com32/lib/Makefile |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/com32/lib/Makefile b/com32/lib/Makefile
index 93643ce..6923605 100644
--- a/com32/lib/Makefile
+++ b/com32/lib/Makefile
@@ -105,6 +105,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			\
-- 
1.6.0.2



-----Original Message-----
From: H. Peter Anvin [mailto:hpa at zytor.com] 
Sent: Monday, June 07, 2010 5:00 PM
To: For discussion of Syslinux and tftp-hpa
Cc: Ayvaz, James
Subject: Re: [syslinux] [Syslinux] Patch sensible callback framework

On 06/01/2010 05:21 AM, Ayvaz, James wrote:
> Following up on this.  Were there any more changes that I need to make to get this patch accepted?  Thanks.
> +
> +static inline struct callback_record* register_callback(struct callback_record *head, callback_t callback, void *rarg) {

Brackets go on the line after the function declaration, please.

The one thing I'd ask is that you make this patch against the
"pathbased" branch on the git tree, or equivalently against the
4.00-pre47 release.  Other than that, it's starting to look really good.

However, the INIT_CALLBACK() macro is confused.  The right way to use a
macro like that is to set it up as follows:

#define DECLARE_CALLBACK(name) 		\
struct callback_record name = { 	\
	.function 	= NULL,		\
	.next		= &name,	\
	.prev		= &name,	\
}

Then use it at the point of declaration:

DECLARE_CALLBACK(my_callback);

This allows the callback to be initialized statically.

	-hpa




More information about the Syslinux mailing list