File: list.h

package info (click to toggle)
ndctl 82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,460 kB
  • sloc: ansic: 42,027; sh: 3,974; makefile: 28
file content (39 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2022 Intel Corporation. All rights reserved. */
#ifndef _NDCTL_LIST_H_
#define _NDCTL_LIST_H_

#include <ccan/list/list.h>

#define list_add_sorted(head, n, node, cmp)                                    \
	do {                                                                   \
		struct list_head *__head = (head);                             \
		typeof(n) __iter, __next;                                      \
		typeof(n) __new = (n);                                         \
                                                                               \
		if (list_empty(__head)) {                                      \
			list_add(__head, &__new->node);                        \
			break;                                                 \
		}                                                              \
                                                                               \
		list_for_each (__head, __iter, node) {                         \
			if (cmp(__new, __iter) < 0) {                          \
				list_add_before(__head, &__iter->node,         \
						&__new->node);                 \
				break;                                         \
			}                                                      \
			__next = list_next(__head, __iter, node);              \
			if (!__next) {                                         \
				list_add_after(__head, &__iter->node,          \
					       &__new->node);                  \
				break;                                         \
			}                                                      \
			if (cmp(__new, __next) < 0) {                          \
				list_add_before(__head, &__next->node,         \
						&__new->node);                 \
				break;                                         \
			}                                                      \
		}                                                              \
	} while (0)

#endif /* _NDCTL_LIST_H_ */