Move the malloc checking from module 'list' to new module 'xlist'.
[gnulib.git] / lib / gl_xlist.h
1 /* Abstract sequential list data type, with out-of-memory checking.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _GL_XLIST_H
19 #define _GL_XLIST_H
20
21 #include "gl_list.h"
22 #include "xalloc.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 /* These functions are thin wrappers around the corresponding functions with
29    _nx_ infix from gl_list.h.  Upon out-of-memory, they invoke xalloc_die (),
30    instead of returning an error indicator.  */
31 extern gl_list_t gl_list_create_empty (gl_list_implementation_t implementation,
32                                        gl_listelement_equals_fn equals_fn,
33                                        gl_listelement_hashcode_fn hashcode_fn,
34                                        gl_listelement_dispose_fn dispose_fn,
35                                        bool allow_duplicates);
36 extern gl_list_t gl_list_create (gl_list_implementation_t implementation,
37                                  gl_listelement_equals_fn equals_fn,
38                                  gl_listelement_hashcode_fn hashcode_fn,
39                                  gl_listelement_dispose_fn dispose_fn,
40                                  bool allow_duplicates,
41                                  size_t count, const void **contents);
42 extern void gl_list_node_set_value (gl_list_t list, gl_list_node_t node,
43                                     const void *elt);
44 extern gl_list_node_t gl_list_set_at (gl_list_t list, size_t position,
45                                       const void *elt);
46 extern gl_list_node_t gl_list_add_first (gl_list_t list, const void *elt);
47 extern gl_list_node_t gl_list_add_last (gl_list_t list, const void *elt);
48 extern gl_list_node_t gl_list_add_before (gl_list_t list, gl_list_node_t node,
49                                           const void *elt);
50 extern gl_list_node_t gl_list_add_after (gl_list_t list, gl_list_node_t node,
51                                          const void *elt);
52 extern gl_list_node_t gl_list_add_at (gl_list_t list, size_t position,
53                                       const void *elt);
54 extern gl_list_node_t gl_sortedlist_add (gl_list_t list,
55                                          gl_listelement_compar_fn compar,
56                                          const void *elt);
57
58 #if HAVE_INLINE
59
60 # define gl_list_create_empty gl_list_create_empty_inline
61 static inline gl_list_t
62 gl_list_create_empty (gl_list_implementation_t implementation,
63                       gl_listelement_equals_fn equals_fn,
64                       gl_listelement_hashcode_fn hashcode_fn,
65                       gl_listelement_dispose_fn dispose_fn,
66                       bool allow_duplicates)
67 {
68   gl_list_t result =
69     gl_list_nx_create_empty (implementation, equals_fn, hashcode_fn, dispose_fn,
70                              allow_duplicates);
71   if (result == NULL)
72     xalloc_die ();
73   return result;
74 }
75
76 # define gl_list_create gl_list_create_inline
77 static inline gl_list_t
78 gl_list_create (gl_list_implementation_t implementation,
79                 gl_listelement_equals_fn equals_fn,
80                 gl_listelement_hashcode_fn hashcode_fn,
81                 gl_listelement_dispose_fn dispose_fn,
82                 bool allow_duplicates,
83                 size_t count, const void **contents)
84 {
85   gl_list_t result =
86     gl_list_nx_create (implementation, equals_fn, hashcode_fn, dispose_fn,
87                        allow_duplicates, count, contents);
88   if (result == NULL)
89     xalloc_die ();
90   return result;
91 }
92
93 # define gl_list_node_set_value gl_list_node_set_value_inline
94 static inline void
95 gl_list_node_set_value (gl_list_t list, gl_list_node_t node, const void *elt)
96 {
97   int result = gl_list_node_nx_set_value (list, node, elt);
98   if (result < 0)
99     xalloc_die ();
100 }
101
102 # define gl_list_set_at gl_list_set_at_inline
103 static inline gl_list_node_t
104 gl_list_set_at (gl_list_t list, size_t position, const void *elt)
105 {
106   gl_list_node_t result = gl_list_nx_set_at (list, position, elt);
107   if (result == NULL)
108     xalloc_die ();
109   return result;
110 }
111
112 # define gl_list_add_first gl_list_add_first_inline
113 static inline gl_list_node_t
114 gl_list_add_first (gl_list_t list, const void *elt)
115 {
116   gl_list_node_t result = gl_list_nx_add_first (list, elt);
117   if (result == NULL)
118     xalloc_die ();
119   return result;
120 }
121
122 # define gl_list_add_last gl_list_add_last_inline
123 static inline gl_list_node_t
124 gl_list_add_last (gl_list_t list, const void *elt)
125 {
126   gl_list_node_t result = gl_list_nx_add_last (list, elt);
127   if (result == NULL)
128     xalloc_die ();
129   return result;
130 }
131
132 # define gl_list_add_before gl_list_add_before_inline
133 static inline gl_list_node_t
134 gl_list_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
135 {
136   gl_list_node_t result = gl_list_nx_add_before (list, node, elt);
137   if (result == NULL)
138     xalloc_die ();
139   return result;
140 }
141
142 # define gl_list_add_after gl_list_add_after_inline
143 static inline gl_list_node_t
144 gl_list_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
145 {
146   gl_list_node_t result = gl_list_nx_add_after (list, node, elt);
147   if (result == NULL)
148     xalloc_die ();
149   return result;
150 }
151
152 # define gl_list_add_at gl_list_add_at_inline
153 static inline gl_list_node_t
154 gl_list_add_at (gl_list_t list, size_t position, const void *elt)
155 {
156   gl_list_node_t result = gl_list_nx_add_at (list, position, elt);
157   if (result == NULL)
158     xalloc_die ();
159   return result;
160 }
161
162 # define gl_sortedlist_add gl_sortedlist_add_inline
163 static inline gl_list_node_t
164 gl_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
165                    const void *elt)
166 {
167   gl_list_node_t result = gl_sortedlist_nx_add (list, compar, elt);
168   if (result == NULL)
169     xalloc_die ();
170   return result;
171 }
172
173 #endif
174
175 #ifdef __cplusplus
176 }
177 #endif
178
179 #endif /* _GL_XLIST_H */