blob: 1e9ba1366dbf5cffdbefe6e3b629c76955ba3db1 [file] [log] [blame]
Anurag Singh6ec12062012-10-02 09:59:01 -07001/*
2 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
Duy Truong70222452013-02-10 06:35:11 -080013 * * Neither the name of The Linux Foundation nor the names of its
Anurag Singh6ec12062012-10-02 09:59:01 -070014 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "list.h"
Rashed Abdel-Tawab9c14c9f2017-12-26 23:11:03 +020035#include <log/log.h>
Anurag Singh6ec12062012-10-02 09:59:01 -070036
Anurag Singh6ec12062012-10-02 09:59:01 -070037struct list_node *add_list_node(struct list_node *head, void *data)
38{
39 /* Create a new list_node. And put 'data' into it. */
40 struct list_node *new_node;
41
42 if (head == NULL) {
43 return NULL;
44 }
45
46 if (!(new_node = malloc(sizeof(struct list_node)))) {
47 return NULL;
48 }
49
50 new_node->data = data;
51 new_node->next = head->next;
52 new_node->compare = head->compare;
53 new_node->dump = head->dump;
54 head->next = new_node;
55
56 return new_node;
57}
58
Anurag Singh6ec12062012-10-02 09:59:01 -070059/*
60 * Delink and de-allocate 'node'.
61 */
62int remove_list_node(struct list_node *head, struct list_node *del_node)
63{
64 struct list_node *current_node;
65 struct list_node *saved_node;
66
67 if (head == NULL || head->next == NULL) {
68 return -1;
69 }
70
71 current_node = head->next;
72 saved_node = head;
73
74 while (current_node && current_node != del_node) {
75 saved_node = current_node;
76 current_node = current_node->next;
77 }
78
79 if (saved_node) {
80 if (current_node) {
81 saved_node->next = current_node->next;
82 } else {
83 /* Node not found. */
84 return -1;
85 }
86 }
87
88 if (del_node) {
89 free(del_node);
90 }
91
92 return 0;
93}
94
Anurag Singh6ec12062012-10-02 09:59:01 -070095struct list_node *find_node(struct list_node *head, void *comparison_data)
96{
97 struct list_node *current_node = head;
98
99 if (head == NULL)
100 return NULL;
101
102 while ((current_node = current_node->next)) {
103 if (current_node->compare) {
104 if (current_node->compare(current_node->data,
105 comparison_data) == 0) {
106 /* Match found. Return current_node. */
107 return current_node;
108 }
109 }
110 }
111
112 /* No match found. */
113 return NULL;
114}