contrib: add an example of using list.h
[openocd.git] / contrib / list_example.c
diff --git a/contrib/list_example.c b/contrib/list_example.c
new file mode 100644 (file)
index 0000000..0f62b86
--- /dev/null
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Copyright (C) 2021 by Andreas Fritiofson <andreas.fritiofson@gmail.com> */
+
+/*
+ * Simple example of using a circular doubly linked list through list.h
+ *
+ * gcc -I ../src/ list_example.c -o list_example
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <assert.h>
+#include <helper/list.h>
+
+static LIST_HEAD(threads);
+
+struct thread {
+       int id;
+       uint64_t tcb_address;
+       struct list_head lh;
+};
+
+void insert(struct thread *t)
+{
+       list_add_tail(&t->lh, &threads);
+}
+
+void remove(struct thread *t)
+{
+       list_del(&t->lh);
+}
+
+struct thread *lookup_id(int id)
+{
+       struct thread *t;
+       list_for_each_entry(t, &threads, lh) {
+               if (t->id == id)
+                       return t;
+       }
+       return NULL;
+}
+
+struct thread *lookup_tcb(uint64_t addr)
+{
+       struct thread *t;
+       list_for_each_entry(t, &threads, lh) {
+               if (t->tcb_address == addr)
+                       return t;
+       }
+       return NULL;
+}
+
+int main(void)
+{
+       struct thread t1 = { .id = 1, .tcb_address = 111111111 };
+       struct thread t2 = { .id = 2, .tcb_address = 222222222 };
+       struct thread t3 = { .id = 3, .tcb_address = 333333333 };
+
+       insert(&t1);
+       insert(&t2);
+       assert(lookup_id(1) == &t1);
+       assert(lookup_tcb(111111111) == &t1);
+       assert(lookup_id(2) == &t2);
+       assert(lookup_id(42) == NULL);
+       remove(&t1);
+       assert(lookup_id(1) == NULL);
+       insert(&t3);
+       remove(&t2);
+       assert(lookup_id(3) == &t3);
+       assert(lookup_tcb(333333333) == &t3);
+       assert(lookup_id(2) == NULL);
+       remove(&t3);
+       assert(list_empty(&threads));
+}

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)