libBLOC
Barry Initial commit b3106d3 (2 years, 11 months ago)
diff --git a/list/list.c b/list/list.c
new file mode 100644
index 0000000..888ff12
--- /dev/null
+++ b/list/list.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC 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 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY 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
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*
+ * This file implements object Lists. Objects can be a part of multiple lists
+ * that can be managed automatically by the library. This prevents subsystems
+ * from having to implement their own object lists for sub-objects. Lists may
+ * be created with a specific Object Type, and will not accept objects that are
+ * not of that type. If a list does not have a type, it will accept objects of
+ * any type. Lists are very little more than the default Iterable object.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/list.h>
+#include "../assert.h"
+#include "../object.h"
+#include "list.h"
+
+static void _list_new(void *);
+static void _list_delete(void *);
+
+/* List object type */
+struct ObjectType listType = {
+ .name = "List",
+ .size = sizeof(List),
+ .new = _list_new,
+ .delete = _list_delete,
+};
+
+/* Create a List object */
+static void
+_list_new(void *obj)
+{
+ List *list = obj;
+ iterable_init(&list->header);
+}
+
+/* Destroy a List object */
+static void
+_list_delete(void *obj)
+{
+ List *list = obj;
+ struct IterableEntry *entry, *next;
+ for (entry = list->header.start; entry; entry = next) {
+ next = entry->next;
+ obj_put(entry->obj);
+ free(entry);
+ }
+}
+
+/* Create an object list */
+List *
+list_create(struct ObjectType *type)
+{
+ List *list = obj_new(&listType);
+ list->type = type;
+ return list;
+}
+
+/* Count the entries in a list */
+unsigned int
+list_count(List *list)
+{
+ return list->header.entries;
+}
diff --git a/list/list.h b/list/list.h
new file mode 100644
index 0000000..4336cb8
--- /dev/null
+++ b/list/list.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC 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 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY 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
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIST_H
+#define LIST_H
+
+#include <BLOC/iterator.h>
+
+/* Structure for an object List */
+struct List {
+ struct Iterable header;
+ struct ObjectType *type;
+};
+
+#endif
diff --git a/list/list_add.c b/list/list_add.c
new file mode 100644
index 0000000..276a158
--- /dev/null
+++ b/list/list_add.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC 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 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY 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
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/list.h>
+#include "../assert.h"
+#include "../object.h"
+#include "list.h"
+
+/* Add an object to a list */
+void
+list_add(List *list, void *obj)
+{
+ ASSERT(list);
+ ASSERT(obj);
+ struct ObjectHeader *header = object_header(obj);
+ ASSERT(header->magic == OBJECT_MAGIC);
+ if (list->type && list->type != header->type)
+ return;
+
+ struct IterableEntry *entry;
+ entry = malloc(sizeof(struct IterableEntry));
+ entry->prev = entry->next = NULL;
+ entry->obj = obj_get(obj);
+
+ obj_lock(list);
+ list->header.entries++;
+ if (!list->header.start) {
+ list->header.start = list->header.end = entry;
+ } else {
+ list->header.end->next = entry;
+ entry->prev = list->header.end;
+ list->header.end = entry;
+ }
+ obj_unlock(list);
+}
diff --git a/list/list_pop.c b/list/list_pop.c
new file mode 100644
index 0000000..ceaa1d8
--- /dev/null
+++ b/list/list_pop.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC 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 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY 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
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/list.h>
+#include "../assert.h"
+#include "list.h"
+
+/* Pop the first object in a list */
+void *
+list_pop_head(List *list)
+{
+ ASSERT(list);
+ void *obj = NULL;
+ obj_lock(list);
+ if (list->header.start) {
+ obj = obj_get(list->header.start->obj);
+ list_remove(list, obj);
+ }
+ obj_unlock(list);
+ return obj;
+}
+
+/* Pop the last object in a list */
+void *
+list_pop_tail(List *list)
+{
+ ASSERT(list);
+ void *obj = NULL;
+ obj_lock(list);
+ if (list->header.end) {
+ obj = obj_get(list->header.end->obj);
+ list_remove(list, obj);
+ }
+ obj_unlock(list);
+ return obj;
+}
diff --git a/list/list_remove.c b/list/list_remove.c
new file mode 100644
index 0000000..767fb09
--- /dev/null
+++ b/list/list_remove.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC 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 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY 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
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/list.h>
+#include "../assert.h"
+#include "../object.h"
+#include "list.h"
+
+/* Remove an object from a list */
+void
+list_remove(List *list, void *obj)
+{
+ ASSERT(list);
+ ASSERT(obj);
+ struct ObjectHeader *header = object_header(obj);
+ ASSERT(header->magic == OBJECT_MAGIC);
+ if (!list->header.start)
+ return;
+ if (list->type && list->type != header->type)
+ return;
+ obj_lock(list);
+
+ /* Search for object */
+ struct IterableEntry *entry;
+ for (entry = list->header.start; entry; entry = entry->next) {
+ if (entry->obj == obj)
+ break;
+ }
+ if (!entry) {
+ obj_unlock(list);
+ return;
+ }
+
+ /* Unlink */
+ if (list->header.start == entry)
+ list->header.start = entry->next;
+ if (list->header.end == entry)
+ list->header.end = entry->prev;
+ if (entry->prev)
+ entry->prev->next = entry->next;
+ if (entry->next)
+ entry->next->prev = entry->prev;
+
+ obj_put(entry->obj);
+ list->header.entries--;
+ free(entry);
+
+ obj_unlock(list);
+}