1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bli
24  */
25 
26 #include "BLI_compiler_attrs.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct BLI_mempool;
33 struct MemArena;
34 
35 typedef void (*LinkNodeFreeFP)(void *link);
36 typedef void (*LinkNodeApplyFP)(void *link, void *userdata);
37 
38 typedef struct LinkNode {
39   struct LinkNode *next;
40   void *link;
41 } LinkNode;
42 
43 /**
44  * Use for append (single linked list, storing the last element).
45  *
46  * \note list manipulation functions don't operate on this struct.
47  * This is only to be used while appending.
48  */
49 typedef struct LinkNodePair {
50   LinkNode *list, *last_node;
51 } LinkNodePair;
52 
53 int BLI_linklist_count(const LinkNode *list) ATTR_WARN_UNUSED_RESULT;
54 int BLI_linklist_index(const LinkNode *list, void *ptr) ATTR_WARN_UNUSED_RESULT;
55 
56 LinkNode *BLI_linklist_find(LinkNode *list, int index) ATTR_WARN_UNUSED_RESULT;
57 LinkNode *BLI_linklist_find_last(LinkNode *list) ATTR_WARN_UNUSED_RESULT;
58 
59 void BLI_linklist_reverse(LinkNode **listp) ATTR_NONNULL(1);
60 
61 void BLI_linklist_move_item(LinkNode **listp, int curr_index, int new_index) ATTR_NONNULL(1);
62 
63 void BLI_linklist_prepend_nlink(LinkNode **listp, void *ptr, LinkNode *nlink) ATTR_NONNULL(1, 3);
64 void BLI_linklist_prepend(LinkNode **listp, void *ptr) ATTR_NONNULL(1);
65 void BLI_linklist_prepend_arena(LinkNode **listp, void *ptr, struct MemArena *ma)
66     ATTR_NONNULL(1, 3);
67 void BLI_linklist_prepend_pool(LinkNode **listp, void *ptr, struct BLI_mempool *mempool)
68     ATTR_NONNULL(1, 3);
69 
70 /* use LinkNodePair to avoid full search */
71 void BLI_linklist_append_nlink(LinkNodePair *list_pair, void *ptr, LinkNode *nlink)
72     ATTR_NONNULL(1, 3);
73 void BLI_linklist_append(LinkNodePair *list_pair, void *ptr) ATTR_NONNULL(1);
74 void BLI_linklist_append_arena(LinkNodePair *list_pair, void *ptr, struct MemArena *ma)
75     ATTR_NONNULL(1, 3);
76 void BLI_linklist_append_pool(LinkNodePair *list_pair, void *ptr, struct BLI_mempool *mempool)
77     ATTR_NONNULL(1, 3);
78 
79 void *BLI_linklist_pop(LinkNode **listp) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
80 void *BLI_linklist_pop_pool(LinkNode **listp, struct BLI_mempool *mempool) ATTR_WARN_UNUSED_RESULT
81     ATTR_NONNULL(1, 2);
82 void BLI_linklist_insert_after(LinkNode **listp, void *ptr) ATTR_NONNULL(1);
83 
84 void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc);
85 void BLI_linklist_freeN(LinkNode *list);
86 void BLI_linklist_free_pool(LinkNode *list, LinkNodeFreeFP freefunc, struct BLI_mempool *mempool);
87 void BLI_linklist_apply(LinkNode *list, LinkNodeApplyFP applyfunc, void *userdata);
88 LinkNode *BLI_linklist_sort(LinkNode *list,
89                             int (*cmp)(const void *, const void *)) ATTR_WARN_UNUSED_RESULT
90     ATTR_NONNULL(2);
91 LinkNode *BLI_linklist_sort_r(LinkNode *list,
92                               int (*cmp)(void *, const void *, const void *),
93                               void *thunk) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
94 
95 #define BLI_linklist_prepend_alloca(listp, ptr) \
96   BLI_linklist_prepend_nlink(listp, ptr, alloca(sizeof(LinkNode)))
97 #define BLI_linklist_append_alloca(list_pair, ptr) \
98   BLI_linklist_append_nlink(list_pair, ptr, alloca(sizeof(LinkNode)))
99 
100 #ifdef __cplusplus
101 }
102 #endif
103