From f711e58f8f635d0afcf3f02da83780bef83c4347 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Sat, 13 Jan 2007 12:27:09 +0000 Subject: Rename struct slist to GSList and slist_* to g_slist_* --- common/Makefile.am | 2 +- common/dbus.c | 29 +++--- common/glib-ectomy.c | 256 +++++++++++++++++++++++++++++++++++++++++++++------ common/glib-ectomy.h | 30 ++++++ common/list.c | 234 ---------------------------------------------- common/list.h | 54 ----------- 6 files changed, 273 insertions(+), 332 deletions(-) delete mode 100644 common/list.c delete mode 100644 common/list.h (limited to 'common') diff --git a/common/Makefile.am b/common/Makefile.am index 3774edf9..eb6e62b0 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -15,7 +15,7 @@ endif noinst_LIBRARIES = libhelper.a -libhelper_a_SOURCES = oui.h oui.c list.h list.c \ +libhelper_a_SOURCES = oui.h oui.c \ textfile.h textfile.c helper.h helper.c \ logging.h logging.c dbus.h dbus.c \ sdp-xml.h sdp-xml.c $(sdp_sources) \ diff --git a/common/dbus.c b/common/dbus.c index eb8c77f3..2162feab 100644 --- a/common/dbus.c +++ b/common/dbus.c @@ -37,13 +37,12 @@ #include "glib-ectomy.h" #include "dbus.h" #include "logging.h" -#include "list.h" #define DISPATCH_TIMEOUT 0 static int name_listener_initialized = 0; -static struct slist *name_listeners = NULL; +static GSList *name_listeners = NULL; typedef struct { uint32_t id; @@ -68,12 +67,12 @@ struct name_callback { struct name_data { char *name; - struct slist *callbacks; + GSList *callbacks; }; static struct name_data *name_data_find(const char *name) { - struct slist *current; + GSList *current; for (current = name_listeners; current != NULL; current = current->next) { struct name_data *data = current->data; @@ -84,10 +83,10 @@ static struct name_data *name_data_find(const char *name) return NULL; } -static struct name_callback *name_callback_find(struct slist *callbacks, +static struct name_callback *name_callback_find(GSList *callbacks, name_cb_t func, void *user_data) { - struct slist *current; + GSList *current; for (current = callbacks; current != NULL; current = current->next) { struct name_callback *cb = current->data; @@ -100,12 +99,12 @@ static struct name_callback *name_callback_find(struct slist *callbacks, static void name_data_free(struct name_data *data) { - struct slist *l; + GSList *l; for (l = data->callbacks; l != NULL; l = l->next) free(l->data); - slist_free(data->callbacks); + g_slist_free(data->callbacks); if (data->name) free(data->name); @@ -142,10 +141,10 @@ static int name_data_add(const char *name, name_cb_t func, void *user_data) if (!data->name) goto failed; - name_listeners = slist_append(name_listeners, data); + name_listeners = g_slist_append(name_listeners, data); done: - data->callbacks = slist_append(data->callbacks, cb); + data->callbacks = g_slist_append(data->callbacks, cb); return first; failed: @@ -169,12 +168,12 @@ static void name_data_remove(const char *name, name_cb_t func, void *user_data) cb = name_callback_find(data->callbacks, func, user_data); if (cb) { - data->callbacks = slist_remove(data->callbacks, cb); + data->callbacks = g_slist_remove(data->callbacks, cb); free(cb); } if (!data->callbacks) { - name_listeners = slist_remove(name_listeners, data); + name_listeners = g_slist_remove(name_listeners, data); name_data_free(data); } } @@ -182,7 +181,7 @@ static void name_data_remove(const char *name, name_cb_t func, void *user_data) static DBusHandlerResult name_exit_filter(DBusConnection *connection, DBusMessage *message, void *user_data) { - struct slist *l; + GSList *l; struct name_data *data; char *name, *old, *new; @@ -214,7 +213,7 @@ static DBusHandlerResult name_exit_filter(DBusConnection *connection, cb->func(name, cb->user_data); } - name_listeners = slist_remove(name_listeners, data); + name_listeners = g_slist_remove(name_listeners, data); name_data_free(data); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; @@ -283,7 +282,7 @@ int name_listener_remove(DBusConnection *connection, const char *name, return -1; } - data->callbacks = slist_remove(data->callbacks, cb); + data->callbacks = g_slist_remove(data->callbacks, cb); free(cb); /* Don't remove the filter if other callbacks exist */ diff --git a/common/glib-ectomy.c b/common/glib-ectomy.c index 46d92154..b45d26f4 100644 --- a/common/glib-ectomy.c +++ b/common/glib-ectomy.c @@ -13,7 +13,6 @@ #include #include -#include "list.h" #include "glib-ectomy.h" struct timeout { @@ -34,12 +33,12 @@ struct _GMainContext { guint next_id; glong next_timeout; - struct slist *timeouts; - struct slist *proc_timeouts; + GSList *timeouts; + GSList *proc_timeouts; gboolean timeout_lock; - struct slist *watches; - struct slist *proc_watches; + GSList *watches; + GSList *proc_watches; gboolean watch_lock; }; @@ -174,7 +173,7 @@ static GMainContext *g_main_context_default() void g_io_remove_watch(guint id) { GMainContext *context = g_main_context_default(); - struct slist *l; + GSList *l; struct watch *w; if (!context) @@ -186,7 +185,7 @@ void g_io_remove_watch(guint id) if (w->id != id) continue; - context->watches = slist_remove(context->watches, w); + context->watches = g_slist_remove(context->watches, w); watch_free(w); return; @@ -198,7 +197,7 @@ void g_io_remove_watch(guint id) if (w->id != id) continue; - context->proc_watches = slist_remove(context->proc_watches, w); + context->proc_watches = g_slist_remove(context->proc_watches, w); watch_free(w); return; @@ -210,7 +209,7 @@ int watch_prio_cmp(struct watch *w1, struct watch *w2) return w1->priority - w2->priority; } -#define watch_list_add(l, w) slist_insert_sorted((l), (w), (cmp_func_t) watch_prio_cmp) +#define watch_list_add(l, w) g_slist_insert_sorted((l), (w), (GCompareFunc) watch_prio_cmp) guint g_io_add_watch_full(GIOChannel *channel, gint priority, GIOCondition condition, GIOFunc func, @@ -273,7 +272,7 @@ GMainLoop *g_main_loop_new(GMainContext *context, gboolean is_running) static void timeout_handlers_prepare(GMainContext *context) { - struct slist *l; + GSList *l; struct timeval tv; glong msec, timeout = LONG_MAX; @@ -314,8 +313,8 @@ static void timeout_handlers_check(GMainContext *context) gboolean ret; if (timercmp(&tv, &t->expiration, <)) { - context->timeouts = slist_remove(context->timeouts, t); - context->proc_timeouts = slist_append(context->proc_timeouts, t); + context->timeouts = g_slist_remove(context->timeouts, t); + context->proc_timeouts = g_slist_append(context->proc_timeouts, t); continue; } @@ -323,10 +322,10 @@ static void timeout_handlers_check(GMainContext *context) /* Check if the handler was removed/freed by the callback * function */ - if (!slist_find(context->timeouts, t, ptr_cmp)) + if (!g_slist_find_custom(context->timeouts, t, ptr_cmp)) continue; - context->timeouts = slist_remove(context->timeouts, t); + context->timeouts = g_slist_remove(context->timeouts, t); if (!ret) { free(t); @@ -344,7 +343,7 @@ static void timeout_handlers_check(GMainContext *context) t->expiration.tv_sec++; } - context->proc_timeouts = slist_append(context->proc_timeouts, t); + context->proc_timeouts = g_slist_append(context->proc_timeouts, t); } context->timeouts = context->proc_timeouts; @@ -366,7 +365,7 @@ void g_main_loop_run(GMainLoop *loop) while (loop->is_running) { int nfds; - struct slist *l; + GSList *l; struct watch *w; for (nfds = 0, l = context->watches; l != NULL; l = l->next, nfds++) { @@ -391,7 +390,7 @@ void g_main_loop_run(GMainLoop *loop) w = context->watches->data; if (!*w->revents) { - context->watches = slist_remove(context->watches, w); + context->watches = g_slist_remove(context->watches, w); context->proc_watches = watch_list_add(context->proc_watches, w); continue; } @@ -400,10 +399,10 @@ void g_main_loop_run(GMainLoop *loop) /* Check if the watch was removed/freed by the callback * function */ - if (!slist_find(context->watches, w, ptr_cmp)) + if (!g_slist_find_custom(context->watches, w, ptr_cmp)) continue; - context->watches = slist_remove(context->watches, w); + context->watches = g_slist_remove(context->watches, w); if (!ret) { watch_free(w); @@ -434,11 +433,11 @@ void g_main_loop_unref(GMainLoop *loop) if (!loop->context) return; - slist_foreach(loop->context->watches, (slist_func_t)watch_free, NULL); - slist_free(loop->context->watches); + g_slist_foreach(loop->context->watches, (GFunc)watch_free, NULL); + g_slist_free(loop->context->watches); - slist_foreach(loop->context->timeouts, (slist_func_t)free, NULL); - slist_free(loop->context->timeouts); + g_slist_foreach(loop->context->timeouts, (GFunc)free, NULL); + g_slist_free(loop->context->timeouts); free(loop->context); loop->context = NULL; @@ -482,9 +481,9 @@ guint g_timeout_add(guint interval, GSourceFunc function, gpointer data) t->id = context->next_id++; if (context->timeout_lock) - context->proc_timeouts = slist_prepend(context->proc_timeouts, t); + context->proc_timeouts = g_slist_prepend(context->proc_timeouts, t); else - context->timeouts = slist_prepend(context->timeouts, t); + context->timeouts = g_slist_prepend(context->timeouts, t); return t->id; } @@ -492,7 +491,7 @@ guint g_timeout_add(guint interval, GSourceFunc function, gpointer data) gint g_timeout_remove(const guint id) { GMainContext *context = g_main_context_default(); - struct slist *l; + GSList *l; struct timeout *t; if (!context) @@ -507,7 +506,7 @@ gint g_timeout_remove(const guint id) if (t->id != id) continue; - context->timeouts = slist_remove(context->timeouts, t); + context->timeouts = g_slist_remove(context->timeouts, t); free(t); return 0; @@ -522,7 +521,7 @@ gint g_timeout_remove(const guint id) if (t->id != id) continue; - context->proc_timeouts = slist_remove(context->proc_timeouts, t); + context->proc_timeouts = g_slist_remove(context->proc_timeouts, t); free(t); return 0; @@ -616,4 +615,205 @@ failed: return FALSE; } +/* GSList functions */ +GSList *g_slist_append(GSList *list, void *data) +{ + GSList *entry, *tail; + + entry = malloc(sizeof(GSList)); + /* FIXME: this currently just silently fails */ + if (!entry) + return list; + + entry->data = data; + entry->next = NULL; + + if (!list) + return entry; + + /* Find the end of the list */ + for (tail = list; tail->next; tail = tail->next); + + tail->next = entry; + + return list; +} + +GSList *g_slist_prepend(GSList *list, void *data) +{ + GSList *entry; + + entry = malloc(sizeof(GSList)); + /* FIXME: this currently just silently fails */ + if (!entry) + return list; + + entry->data = data; + entry->next = list; + + return entry; +} + +GSList *g_slist_insert_sorted(GSList *list, void *data, GCompareFunc cmp_func) +{ + GSList *tmp, *prev, *entry; + int cmp; + + entry = malloc(sizeof(GSList)); + if (!entry) + return list; + + entry->data = data; + entry->next = NULL; + + if (!list) + return entry; + + prev = NULL; + tmp = list; + + cmp = cmp_func(data, tmp->data); + + while (tmp->next && cmp > 0) { + prev = tmp; + tmp = tmp->next; + + cmp = cmp_func(data, tmp->data); + } + + if (!tmp->next && cmp > 0) { + tmp->next = entry; + return list; + } + + if (prev) { + prev->next = entry; + entry->next = tmp; + return list; + } else { + entry->next = list; + return entry; + } +} + +GSList *g_slist_remove(GSList *list, void *data) +{ + GSList *l, *next, *prev = NULL, *match = NULL; + + if (!list) + return NULL; + + for (l = list; l != NULL; l = l->next) { + if (l->data == data) { + match = l; + break; + } + prev = l; + } + + if (!match) + return list; + + next = match->next; + + free(match); + + /* If the head was removed, return the next element */ + if (!prev) + return next; + + prev->next = next; + + return list; +} + +GSList *g_slist_find_custom(GSList *list, const void *data, + GCompareFunc cmp_func) +{ + GSList *l; + + for (l = list; l != NULL; l = l->next) { + if (!cmp_func(l->data, data)) + return l; + } + + return NULL; +} + +static GSList *g_slist_sort_merge(GSList *l1, GSList *l2, + GCompareFunc cmp_func) +{ + GSList list, *l; + int cmp; + + l = &list; + + while (l1 && l2) { + cmp = cmp_func(l1->data, l2->data); + + if (cmp <= 0) { + l = l->next = l1; + l1 = l1->next; + } else { + l = l->next = l2; + l2 = l2->next; + } + } + + l->next = l1 ? l1 : l2; + + return list.next; +} + +GSList *g_slist_sort(GSList *list, GCompareFunc cmp_func) +{ + GSList *l1, *l2; + + if (!list || !list->next) + return list; + + l1 = list; + l2 = list->next; + + while ((l2 = l2->next) != NULL) { + if ((l2 = l2->next) == NULL) + break; + l1 = l1->next; + } + + l2 = l1->next; + l1->next = NULL; + + return g_slist_sort_merge(g_slist_sort(list, cmp_func), + g_slist_sort(l2, cmp_func), cmp_func); +} + +int g_slist_length(GSList *list) +{ + int len; + + for (len = 0; list != NULL; list = list->next) + len++; + + return len; +} + +void g_slist_foreach(GSList *list, GFunc func, void *user_data) +{ + while (list) { + GSList *next = list->next; + func(list->data, user_data); + list = next; + } +} + +void g_slist_free(GSList *list) +{ + GSList *l, *next; + + for (l = list; l != NULL; l = next) { + next = l->next; + free(l); + } +} diff --git a/common/glib-ectomy.h b/common/glib-ectomy.h index 8502deb5..02fa0230 100644 --- a/common/glib-ectomy.h +++ b/common/glib-ectomy.h @@ -112,6 +112,36 @@ gboolean g_utf8_validate(const gchar *str, gssize max_len, const gchar **end); #define g_main_quit(loop) g_main_loop_quit(loop) #define g_main_unref(loop) g_main_loop_unref(loop) +/* Begin GSList declarations */ + +typedef struct _GSList { + void *data; + struct _GSList *next; +} GSList; + +typedef int (*GCompareFunc)(const void *a, const void *b); +typedef void (*GFunc)(void *data, void *user_data); + +GSList *g_slist_append(GSList *list, void *data); + +GSList *g_slist_prepend(GSList *list, void *data); + +GSList *g_slist_insert_sorted(GSList *list, void *data, GCompareFunc cmp_func); + +GSList *g_slist_remove(GSList *list, void *data); + +GSList *g_slist_find_custom(GSList *list, const void *data, + GCompareFunc cmp_func); + +GSList *g_slist_sort(GSList *list, GCompareFunc cmp_func); + +int g_slist_length(GSList *list); + +void g_slist_foreach(GSList *list, GFunc func, void *user_data); +void g_slist_free(GSList *list); + +/* End GSList declarations */ + #endif #endif /* __GLIB_ECTOMY_H */ diff --git a/common/list.c b/common/list.c deleted file mode 100644 index 827f1789..00000000 --- a/common/list.c +++ /dev/null @@ -1,234 +0,0 @@ -/* - * - * BlueZ - Bluetooth protocol stack for Linux - * - * Copyright (C) 2004-2006 Marcel Holtmann - * Copyright (C) 2005-2006 Johan Hedberg - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include - -#include "list.h" - -struct slist *slist_append(struct slist *list, void *data) -{ - struct slist *entry, *tail; - - entry = malloc(sizeof(struct slist)); - /* FIXME: this currently just silently fails */ - if (!entry) - return list; - - entry->data = data; - entry->next = NULL; - - if (!list) - return entry; - - /* Find the end of the list */ - for (tail = list; tail->next; tail = tail->next); - - tail->next = entry; - - return list; -} - -struct slist *slist_prepend(struct slist *list, void *data) -{ - struct slist *entry; - - entry = malloc(sizeof(struct slist)); - /* FIXME: this currently just silently fails */ - if (!entry) - return list; - - entry->data = data; - entry->next = list; - - return entry; -} - -struct slist *slist_insert_sorted(struct slist *list, void *data, cmp_func_t cmp_func) -{ - struct slist *tmp, *prev, *entry; - int cmp; - - entry = malloc(sizeof(struct slist)); - if (!entry) - return list; - - entry->data = data; - entry->next = NULL; - - if (!list) - return entry; - - prev = NULL; - tmp = list; - - cmp = cmp_func(data, tmp->data); - - while (tmp->next && cmp > 0) { - prev = tmp; - tmp = tmp->next; - - cmp = cmp_func(data, tmp->data); - } - - if (!tmp->next && cmp > 0) { - tmp->next = entry; - return list; - } - - if (prev) { - prev->next = entry; - entry->next = tmp; - return list; - } else { - entry->next = list; - return entry; - } -} - -struct slist *slist_remove(struct slist *list, void *data) -{ - struct slist *l, *next, *prev = NULL, *match = NULL; - - if (!list) - return NULL; - - for (l = list; l != NULL; l = l->next) { - if (l->data == data) { - match = l; - break; - } - prev = l; - } - - if (!match) - return list; - - next = match->next; - - free(match); - - /* If the head was removed, return the next element */ - if (!prev) - return next; - - prev->next = next; - - return list; -} - -struct slist *slist_find(struct slist *list, const void *data, - cmp_func_t cmp_func) -{ - struct slist *l; - - for (l = list; l != NULL; l = l->next) { - if (!cmp_func(l->data, data)) - return l; - } - - return NULL; -} - -static struct slist *slist_sort_merge(struct slist *l1, struct slist *l2, - cmp_func_t cmp_func) -{ - struct slist list, *l; - int cmp; - - l = &list; - - while (l1 && l2) { - cmp = cmp_func(l1->data, l2->data); - - if (cmp <= 0) { - l = l->next = l1; - l1 = l1->next; - } else { - l = l->next = l2; - l2 = l2->next; - } - } - - l->next = l1 ? l1 : l2; - - return list.next; -} - -struct slist *slist_sort(struct slist *list, cmp_func_t cmp_func) -{ - struct slist *l1, *l2; - - if (!list || !list->next) - return list; - - l1 = list; - l2 = list->next; - - while ((l2 = l2->next) != NULL) { - if ((l2 = l2->next) == NULL) - break; - l1 = l1->next; - } - - l2 = l1->next; - l1->next = NULL; - - return slist_sort_merge(slist_sort(list, cmp_func), - slist_sort(l2, cmp_func), cmp_func); -} - -int slist_length(struct slist *list) -{ - int len; - - for (len = 0; list != NULL; list = list->next) - len++; - - return len; -} - -void slist_foreach(struct slist *list, slist_func_t func, void *user_data) -{ - while (list) { - struct slist *next = list->next; - func(list->data, user_data); - list = next; - } -} - -void slist_free(struct slist *list) -{ - struct slist *l, *next; - - for (l = list; l != NULL; l = next) { - next = l->next; - free(l); - } -} diff --git a/common/list.h b/common/list.h deleted file mode 100644 index c11424ca..00000000 --- a/common/list.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * BlueZ - Bluetooth protocol stack for Linux - * - * Copyright (C) 2004-2006 Marcel Holtmann - * Copyright (C) 2005-2006 Johan Hedberg - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifndef __LIST_H -#define __LIST_H - -struct slist { - void *data; - struct slist *next; -}; - -typedef int (*cmp_func_t)(const void *a, const void *b); -typedef void (*slist_func_t)(void *data, void *user_data); - -struct slist *slist_append(struct slist *list, void *data); - -struct slist *slist_prepend(struct slist *list, void *data); - -struct slist *slist_insert_sorted(struct slist *list, void *data, cmp_func_t cmp_func); - -struct slist *slist_remove(struct slist *list, void *data); - -struct slist *slist_find(struct slist *list, const void *data, - cmp_func_t cmp_func); - -struct slist *slist_sort(struct slist *list, cmp_func_t cmp_func); - -int slist_length(struct slist *list); - -void slist_foreach(struct slist *list, slist_func_t func, void *user_data); -void slist_free(struct slist *list); - -#endif /* __LIST_H */ -- cgit