[syslinux] Patch sensible callback framework
Ayvaz, James
James.Ayvaz at hp.com
Tue May 11 08:45:35 PDT 2010
Is something like this what you had in mind?
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-11 05:10:12.000000000 -0500
@@ -0,0 +1,29 @@
+#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;
+
+typedef struct callback_list {
+ callback_record *head;
+ callback_record *tail;
+} callback_list;
+
+
+callback_record* register_callback(callback_list *list, callback_t callback, void *rarg);
+int unregister_callback(callback_list *list, callback_record *cb);
+int invoke_callbacks(callback_list *list, ...);
+
+#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-11 05:12:11.000000000 -0500
@@ -0,0 +1,107 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * 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_list *list, callback_t callback, void *rarg) {
+ callback_record *new;
+
+ if (!list)
+ return NULL;
+
+ new = malloc(sizeof(callback_record));
+ if (!new) {
+ return NULL;
+ }
+ new->function = callback;
+ new->rarg = rarg;
+
+ if (!list->tail) { /* first and only node */
+ new->prev = new;
+ new->next = new;
+ list->tail = list->head = new;
+ return new;
+ }
+ new->next = list->tail->next;
+ new->prev = list->tail;
+ list->tail->next->prev = new;
+ list->tail->next = new;
+ return new;
+}
+
+int unregister_callback(callback_list *list, callback_record *cb) {
+ if (!list)
+ return -1;
+ if (!cb)
+ return -1;
+
+ if (cb->next == cb)
+ list->tail = list->head = NULL;
+ else {
+ cb->next->prev = cb->prev;
+ cb->prev->next = cb->next;
+ if (cb == list->tail)
+ list->tail = cb->prev;
+ }
+
+ free(cb);
+
+ return 0;
+}
+
+
+int invoke_callbacks(callback_list *list, ...) {
+ va_list ap;
+ callback_record *curr;
+ if (!list) {
+ return -1;
+ }
+
+ curr = list->head;
+ do {
+ if (curr->function) {
+ va_start(ap, list);
+ curr->function(curr->rarg, ap);
+ va_end(ap);
+ }
+ curr = curr->next;
+ } while(curr != list->head);
+}
+
-----Original Message-----
From: syslinux-bounces at zytor.com [mailto:syslinux-bounces at zytor.com] On Behalf Of H. Peter Anvin
Sent: Wednesday, May 05, 2010 12:25 PM
To: syslinux at zytor.com
Subject: Re: [syslinux] Patch sensible callback framework
On 05/05/2010 08:40 AM, Ayvaz, James wrote:
> Resubmit based on hpa feedback
> + double linked circular linked list
> + code cleanup
>
> Hopefully I didn't miss anything
> +
> +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;
> + }
> +
This is a circularly linked list without head node, but it's a lot
cleaner to use a head node...
-hpa
_______________________________________________
Syslinux mailing list
Submissions to Syslinux at zytor.com
Unsubscribe or set options at:
http://www.zytor.com/mailman/listinfo/syslinux
Please do not send private replies to mailing list traffic.
More information about the Syslinux
mailing list