blob: 50c3e2c5ce8e77d1398fc1f35dc56bcf5b6bdd67 [file] [log] [blame]
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -07001/******************************************************************************
2 *
3 * Copyright (C) 2016 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070019#pragma once
20
21#include <stdbool.h>
22#include <stdlib.h>
23
24struct list_node_t;
25typedef struct list_node_t list_node_t;
26
27struct list_t;
28typedef struct list_t list_t;
29
Myles Watsonb55040c2016-10-19 13:15:34 -070030typedef void (*list_free_cb)(void* data);
Andre Eisenbach760fb702016-01-13 21:20:59 -080031
32// Iterator callback prototype used for |list_foreach|.
33// |data| represents the list item currently being iterated, |context| is a
34// user defined value passed into |list_foreach|.
35// Callback must return true to continue iterating or false to stop iterating.
Myles Watsonb55040c2016-10-19 13:15:34 -070036typedef bool (*list_iter_cb)(void* data, void* context);
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070037
Myles Watsonb55040c2016-10-19 13:15:34 -070038// Returns a new, empty list. Returns NULL if not enough memory could be
Myles Watson9ca07092016-11-28 16:41:53 -080039// allocated for the list structure. The returned list must be freed with
40// |list_free|. The |callback| specifies a function to be called whenever a
41// list element is removed from the list. It can be used to release resources
42// held by the list element, e.g. memory or file descriptor. |callback| may
43// be NULL if no cleanup is necessary on element removal.
Myles Watsonb55040c2016-10-19 13:15:34 -070044list_t* list_new(list_free_cb callback);
Zach Johnson8d436962015-03-02 14:42:02 -080045
46// Frees the list. This function accepts NULL as an argument, in which case it
47// behaves like a no-op.
Myles Watsonb55040c2016-10-19 13:15:34 -070048void list_free(list_t* list);
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070049
Zach Johnson8d436962015-03-02 14:42:02 -080050// Returns true if |list| is empty (has no elements), false otherwise.
51// |list| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070052bool list_is_empty(const list_t* list);
Zach Johnson8d436962015-03-02 14:42:02 -080053
54// Returns true if the list contains |data|, false otherwise.
55// |list| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070056bool list_contains(const list_t* list, const void* data);
Zach Johnson8d436962015-03-02 14:42:02 -080057
58// Returns the length of the |list|. |list| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070059size_t list_length(const list_t* list);
Zach Johnson8d436962015-03-02 14:42:02 -080060
61// Returns the first element in the list without removing it. |list| may not
62// be NULL or empty.
Myles Watsonb55040c2016-10-19 13:15:34 -070063void* list_front(const list_t* list);
Zach Johnson8d436962015-03-02 14:42:02 -080064
65// Returns the last element in the list without removing it. |list| may not
66// be NULL or empty.
Myles Watsonb55040c2016-10-19 13:15:34 -070067void* list_back(const list_t* list);
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070068
Jakub Pawlowski270f86f2016-02-05 15:48:29 -080069// Returns the last node in the list without removing it. |list| may not
70// be NULL or empty.
Myles Watsonb55040c2016-10-19 13:15:34 -070071list_node_t* list_back_node(const list_t* list);
Jakub Pawlowski270f86f2016-02-05 15:48:29 -080072
Zach Johnson8d436962015-03-02 14:42:02 -080073// Inserts |data| after |prev_node| in |list|. |data|, |list|, and |prev_node|
74// may not be NULL. This function does not make a copy of |data| so the pointer
75// must remain valid at least until the element is removed from the list or the
76// list is freed. Returns true if |data| could be inserted, false otherwise
77// (e.g. out of memory).
Myles Watsonb55040c2016-10-19 13:15:34 -070078bool list_insert_after(list_t* list, list_node_t* prev_node, void* data);
Zach Johnson8d436962015-03-02 14:42:02 -080079
Myles Watsonb55040c2016-10-19 13:15:34 -070080// Inserts |data| at the beginning of |list|. Neither |data| nor |list| may be
81// NULL.
Zach Johnson8d436962015-03-02 14:42:02 -080082// This function does not make a copy of |data| so the pointer must remain valid
83// at least until the element is removed from the list or the list is freed.
Myles Watsonb55040c2016-10-19 13:15:34 -070084// Returns true if |data| could be inserted, false otherwise (e.g. out of
85// memory).
86bool list_prepend(list_t* list, void* data);
Zach Johnson8d436962015-03-02 14:42:02 -080087
88// Inserts |data| at the end of |list|. Neither |data| nor |list| may be NULL.
89// This function does not make a copy of |data| so the pointer must remain valid
90// at least until the element is removed from the list or the list is freed.
Myles Watsonb55040c2016-10-19 13:15:34 -070091// Returns true if |data| could be inserted, false otherwise (e.g. out of
92// memory).
93bool list_append(list_t* list, void* data);
Zach Johnson8d436962015-03-02 14:42:02 -080094
Myles Watsonb55040c2016-10-19 13:15:34 -070095// Removes |data| from the list. Neither |list| nor |data| may be NULL. If
96// |data|
97// is inserted multiple times in the list, this function will only remove the
Myles Watson9ca07092016-11-28 16:41:53 -080098// first instance. If a free function was specified in |list_new|, it will be
99// called back with |data|. This function returns true if |data| was found in
100// the list and removed, false otherwise.
Myles Watsonb55040c2016-10-19 13:15:34 -0700101bool list_remove(list_t* list, void* data);
Zach Johnson8d436962015-03-02 14:42:02 -0800102
Myles Watsonb55040c2016-10-19 13:15:34 -0700103// Removes all elements in the list. Calling this function will return the list
104// to the
Zach Johnson8d436962015-03-02 14:42:02 -0800105// same state it was in after |list_new|. |list| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -0700106void list_clear(list_t* list);
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -0700107
Myles Watsonb55040c2016-10-19 13:15:34 -0700108// Iterates through the |list| and calls |callback| for each data element.
Myles Watson9ca07092016-11-28 16:41:53 -0800109// Iteration continues until |callback| returns false. The function returns the
110// pointer to last processed element, or NULL if the list is empty, or all calls
111// to |callback| returned true. |context| is passed to |callback| on each
112// iteration.
Myles Watsonb55040c2016-10-19 13:15:34 -0700113// If the list is empty, |callback| will never be called. It is safe to mutate
Myles Watson9ca07092016-11-28 16:41:53 -0800114// the list inside the callback. If an element is added before the node being
115// visited, there will be no callback for the newly-inserted node. Neither
116// |list| nor |callback| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -0700117list_node_t* list_foreach(const list_t* list, list_iter_cb callback,
118 void* context);
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -0700119
Zach Johnson8d436962015-03-02 14:42:02 -0800120// Returns an iterator to the first element in |list|. |list| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -0700121// The returned iterator is valid as long as it does not equal the value
Myles Watson9ca07092016-11-28 16:41:53 -0800122// returned by |list_end|.
Myles Watsonb55040c2016-10-19 13:15:34 -0700123list_node_t* list_begin(const list_t* list);
Zach Johnson8d436962015-03-02 14:42:02 -0800124
125// Returns an iterator that points past the end of the list. In other words,
126// this function returns the value of an invalid iterator for the given list.
127// When an iterator has the same value as what's returned by this function, you
128// may no longer call |list_next| with the iterator. |list| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -0700129list_node_t* list_end(const list_t* list);
Zach Johnson8d436962015-03-02 14:42:02 -0800130
131// Given a valid iterator |node|, this function returns the next value for the
132// iterator. If the returned value equals the value returned by |list_end|, the
133// iterator has reached the end of the list and may no longer be used for any
134// purpose.
Myles Watsonb55040c2016-10-19 13:15:34 -0700135list_node_t* list_next(const list_node_t* node);
Zach Johnson8d436962015-03-02 14:42:02 -0800136
137// Returns the value stored at the location pointed to by the iterator |node|.
138// |node| must not equal the value returned by |list_end|.
Myles Watsonb55040c2016-10-19 13:15:34 -0700139void* list_node(const list_node_t* node);