From: Bastien Nocera <hadess@hadess.net>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ 1/3] shared: Add single-link list implementation
Date: Thu, 2 Apr 2026 17:44:41 +0200 [thread overview]
Message-ID: <20260402154520.48939-1-hadess@hadess.net> (raw)
This will allow some parts of the project to stop relying on glib
headers without linking to glib, or glib dependencies being introduced
in shared sources that need to be able not to link with glib.
ell doesn't include any linked-list API either, so it's glib or
home-made.
---
Makefile.am | 5 +++
src/shared/list.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++
src/shared/list.h | 26 ++++++++++++++
unit/test-list.c | 68 +++++++++++++++++++++++++++++++++++
4 files changed, 189 insertions(+)
create mode 100644 src/shared/list.c
create mode 100644 src/shared/list.h
create mode 100644 unit/test-list.c
diff --git a/Makefile.am b/Makefile.am
index d9de71d587d8..2d28f93d611e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -214,6 +214,7 @@ endif
shared_sources = src/shared/io.h src/shared/timeout.h \
src/shared/queue.h src/shared/queue.c \
src/shared/util.h src/shared/util.c \
+ src/shared/list.h src/shared/list.c \
src/shared/mgmt.h src/shared/mgmt.c \
src/shared/crypto.h src/shared/crypto.c \
src/shared/ecc.h src/shared/ecc.c \
@@ -700,6 +701,10 @@ unit_tests += unit/test-util
unit_test_util_LDADD = src/libshared-glib.la \
lib/libbluetooth-internal.la $(GLIB_LIBS)
+unit_tests += unit/test-list
+unit_test_list_LDADD = src/libshared-glib.la \
+ lib/libbluetooth-internal.la $(GLIB_LIBS)
+
unit_tests += unit/test-gatt
unit_test_gatt_SOURCES = unit/test-gatt.c
diff --git a/src/shared/list.c b/src/shared/list.c
new file mode 100644
index 000000000000..1eb617a9e40f
--- /dev/null
+++ b/src/shared/list.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2026 Bastien Nocera <hadess@hadess.net>
+ *
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "src/shared/list.h"
+
+#include <stddef.h>
+#include <stdlib.h>
+
+static slist_t*
+slist_last(slist_t *list)
+{
+ slist_t *last;
+
+ if (list == NULL)
+ return NULL;
+ last = list;
+ while (last->next != NULL)
+ last = last->next;
+ return last;
+}
+
+slist_t* slist_append(slist_t *list, void *data)
+{
+ slist_t *new_item;
+
+ new_item = malloc(sizeof(slist_t));
+ new_item->data = data;
+ new_item->next = NULL;
+
+ if (list != NULL) {
+ slist_t *last;
+ last = slist_last(list);
+ last->next = new_item;
+ return list;
+ }
+ return new_item;
+}
+
+void slist_clear(slist_t *list, free_func func)
+{
+ slist_t *l;
+
+ l = list;
+ while (l != NULL) {
+ slist_t *next = l->next;
+ if (func != NULL)
+ (func)(l->data);
+ free(l);
+ l = next;
+ }
+}
+
+void slist_free(slist_t *list)
+{
+ slist_clear(list, NULL);
+}
+
+slist_t* slist_find(slist_t *list, const void *data, compare_func func)
+{
+ slist_t *l;
+
+ if (func == NULL)
+ return NULL;
+ for (l = list; l != NULL; l = l->next) {
+ if ((func)(data, l->data) == 0)
+ return l;
+ }
+ return NULL;
+}
+
+void slist_foreach(slist_t *list, foreach_func func, void *user_data)
+{
+ slist_t *l;
+
+ if (func == NULL)
+ return;
+ for (l = list; l != NULL; l = l->next)
+ (func)(l->data, user_data);
+}
diff --git a/src/shared/list.h b/src/shared/list.h
new file mode 100644
index 000000000000..3a01d0425315
--- /dev/null
+++ b/src/shared/list.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2026 Bastien Nocera <hadess@hadess.net>
+ *
+ *
+ */
+
+#pragma once
+
+typedef struct slist {
+ void *next;
+ void *data;
+} slist_t;
+
+typedef void (*free_func) (void *data);
+typedef int (*compare_func) (const void *a, const void *b);
+typedef void (*foreach_func) (void *data, void *user_data);
+
+slist_t* slist_append(slist_t *list, void *data);
+void slist_clear(slist_t *list, free_func func);
+void slist_free(slist_t *list);
+slist_t* slist_find(slist_t *list, const void *data, compare_func func);
+void slist_foreach(slist_t *list, foreach_func func, void *user_data);
diff --git a/unit/test-list.c b/unit/test-list.c
new file mode 100644
index 000000000000..5dc41ebf3ebd
--- /dev/null
+++ b/unit/test-list.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2026 Bastien Nocera <hadess@hadess.net>
+ *
+ *
+ */
+
+#include <assert.h>
+
+#include "src/shared/list.h"
+#include "src/shared/util.h"
+#include "src/shared/tester.h"
+
+#define ONE 1
+#define TWO 2
+#define THREE 3
+#define ADDITION (ONE + TWO + THREE)
+
+static void add_up(void *data, void *user_data)
+{
+ int *total = (int *) user_data;
+ *total += PTR_TO_UINT(data);
+}
+
+static int int_equal(const void *a, const void *b)
+{
+ if (PTR_TO_UINT(a) == PTR_TO_UINT(b))
+ return 0;
+ return 1;
+}
+
+static void test_list_common(const void *data)
+{
+ slist_t *l;
+ slist_t *head;
+ slist_t *two;
+ int total;
+
+ l = slist_append(NULL, UINT_TO_PTR(ONE));
+ head = l;
+ l = slist_append(l, UINT_TO_PTR(TWO));
+ l = slist_append(l, UINT_TO_PTR(THREE));
+
+ assert(l == head);
+ total = 0;
+ slist_foreach(l, add_up, &total);
+ assert(total == ADDITION);
+
+ two = slist_find(l, UINT_TO_PTR(TWO), int_equal);
+ assert(two == l->next);
+
+ slist_clear(l, NULL);
+
+ tester_test_passed();
+}
+
+int main(int argc, char *argv[])
+{
+ tester_init(&argc, &argv);
+
+ tester_add("/list/common", NULL, NULL,
+ test_list_common, NULL);
+
+ return tester_run();
+}
--
2.53.0
next reply other threads:[~2026-04-02 15:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-02 15:44 Bastien Nocera [this message]
2026-04-02 15:44 ` [PATCH BlueZ 2/3] shared: Remove glib dependency from src/shared/ad.c Bastien Nocera
2026-04-02 16:23 ` Luiz Augusto von Dentz
2026-04-03 8:40 ` Bastien Nocera
2026-04-02 15:44 ` [PATCH BlueZ 3/3] shared: Remove unneeded glib includes Bastien Nocera
2026-04-02 16:33 ` [BlueZ,1/3] shared: Add single-link list implementation bluez.test.bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260402154520.48939-1-hadess@hadess.net \
--to=hadess@hadess.net \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox