123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /*
- * list.h
- *
- * Copyright (C) 2015 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef __MONOLITHIUM_LIST_H__
- #define __MONOLITHIUM_LIST_H__
- #include "defs.h"
- #define LIST_INITIALIZER(name) { &name, &name }
- #define DECLARE_LIST(name) list_entry_t name = LIST_INITIALIZER(name)
- #define list_put_after list_prepend
- #define list_put_before list_append
- #define mini_list_put_after mini_list_prepend
- #define mini_list_put_before mini_list_append
- typedef struct _list_entry_t
- {
- struct _list_entry_t *next, *prev;
- } list_entry_t;
- typedef struct _mini_list_entry_t
- {
- struct _mini_list_entry_t *next;
- } mini_list_entry_t;
- static inline void list_prepend(list_entry_t *list, list_entry_t *entry)
- {
- entry->next = list->next;
- entry->prev = list;
- entry->next->prev = entry;
- entry->prev->next = entry;
- }
- static inline void list_append(list_entry_t *list, list_entry_t *entry)
- {
- entry->next = list;
- entry->prev = list->prev;
- entry->next->prev = entry;
- entry->prev->next = entry;
- }
- static inline void list_remove(list_entry_t *entry)
- {
- entry->next->prev = entry->prev;
- entry->prev->next = entry->next;
- }
- static inline void list_init(list_entry_t *list)
- {
- list->next = list->prev = list;
- }
- static inline void list_init_array(list_entry_t *list_array, size_t size)
- {
- size_t i;
- for (i = 0; i < size; i++) list_init(&list_array[i]);
- }
- static inline void mini_list_prepend(mini_list_entry_t *list, mini_list_entry_t *entry)
- {
- entry->next = list->next;
- list->next = entry;
- }
- static inline void mini_list_append(mini_list_entry_t *list, mini_list_entry_t *entry)
- {
- mini_list_entry_t *final = list->next;
- while (final->next != list) final = final->next;
- final->next = entry;
- entry->next = list;
- }
- static inline void mini_list_remove(mini_list_entry_t *entry)
- {
- mini_list_entry_t *prev = entry->next;
- while (prev->next != entry) prev = prev->next;
- prev->next = entry->next;
- }
- static inline void mini_list_init(mini_list_entry_t *list)
- {
- list->next = list;
- }
- static inline void mini_list_init_array(mini_list_entry_t *list_array, size_t size)
- {
- size_t i;
- for (i = 0; i < size; i++) mini_list_init(&list_array[i]);
- }
- #endif
|