libBLOC
Barry Initial commit b3106d3 (2 years, 11 months ago)
diff --git a/include/BLOC/iterator.h b/include/BLOC/iterator.h
new file mode 100644
index 0000000..62bdd6b
--- /dev/null
+++ b/include/BLOC/iterator.h
@@ -0,0 +1,51 @@
+/*
+ * 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 _BLOC_ITERATOR_H
+#define _BLOC_ITERATOR_H
+
+typedef struct Iterator Iterator;
+
+/* Structure for an Iterable object header */
+struct Iterable {
+ unsigned int magic;
+ struct IterableEntry *start, *end;
+ unsigned int entries;
+};
+
+/* Structure for an Iterable object entry header */
+struct IterableEntry {
+ struct IterableEntry *prev, *next;
+ void *obj;
+};
+
+void iterable_init(struct Iterable *header);
+
+Iterator *iterator_create(void *iterable);
+void *iterator_first(Iterator *iter);
+void *iterator_last(Iterator *iter);
+void *iterator_next(Iterator *iter);
+void *iterator_prev(Iterator *iter);
+
+#define foreach(l,i) for (Iterator *(__l_##i) = iterator_create((void *) (l)); \
+ (__l_##i); obj_put(__l_##i), (__l_##i) = NULL) \
+ for ((i) = iterator_first(__l_##i); (i); \
+ (i) = iterator_next(__l_##i))
+
+#endif
diff --git a/include/BLOC/list.h b/include/BLOC/list.h
new file mode 100644
index 0000000..964362b
--- /dev/null
+++ b/include/BLOC/list.h
@@ -0,0 +1,32 @@
+/*
+ * 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 _BLOC_LIST_H
+#define _BLOC_LIST_H
+
+typedef struct List List;
+
+List *list_create(struct ObjectType *type);
+void list_add(List *list, void *obj);
+void list_remove(List *list, void *obj);
+void *list_pop_head(List *list);
+void *list_pop_tail(List *list);
+unsigned int list_count(List *list);
+
+#endif
diff --git a/include/BLOC/object.h b/include/BLOC/object.h
new file mode 100644
index 0000000..9c221f5
--- /dev/null
+++ b/include/BLOC/object.h
@@ -0,0 +1,47 @@
+/*
+ * 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 _BLOC_OBJECT_H
+#define _BLOC_OBJECT_H
+
+#include <stddef.h>
+
+typedef _Atomic unsigned int refcount_t;
+
+/* Object Type */
+struct ObjectType {
+ refcount_t usage;
+ const char *name;
+ size_t size;
+ void (*new)(void *);
+ void (*delete)(void *);
+};
+
+unsigned int obj_usage(void *addr);
+void *obj_get(void *addr);
+void obj_put(void *addr);
+void *obj_new(struct ObjectType *type);
+
+#define obj_drop(v) ({ obj_put(v); v = NULL; })
+#define obj_swap(v,o) ({ obj_put(v); v = obj_get(o); })
+
+void obj_lock(void *addr);
+void obj_unlock(void *addr);
+
+#endif
diff --git a/include/BLOC/tree.h b/include/BLOC/tree.h
new file mode 100644
index 0000000..785751f
--- /dev/null
+++ b/include/BLOC/tree.h
@@ -0,0 +1,30 @@
+/*
+ * 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 _BLOC_TREE_H
+#define _BLOC_TREE_H
+
+typedef struct Tree Tree;
+
+Tree *tree_create(struct ObjectType *type, int (*compare)(void *, void *));
+void tree_add(Tree *tree, void *obj);
+void tree_remove(Tree *tree, void *obj);
+unsigned int tree_count(Tree *tree);
+
+#endif