Thomas Gleixner | e311edb | 2019-06-03 07:45:00 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 2 | /* |
| 3 | * klist.h - Some generic list helpers, extending struct list_head a bit. |
| 4 | * |
| 5 | * Implementations are found in lib/klist.c |
| 6 | * |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 7 | * Copyright (C) 2005 Patrick Mochel |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
James Bottomley | d856f1e3 | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 10 | #ifndef _LINUX_KLIST_H |
| 11 | #define _LINUX_KLIST_H |
| 12 | |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 13 | #include <linux/spinlock.h> |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 14 | #include <linux/kref.h> |
| 15 | #include <linux/list.h> |
| 16 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 17 | struct klist_node; |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 18 | struct klist { |
| 19 | spinlock_t k_lock; |
| 20 | struct list_head k_list; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 21 | void (*get)(struct klist_node *); |
| 22 | void (*put)(struct klist_node *); |
David Miller | 795abaf | 2011-02-13 16:37:07 -0800 | [diff] [blame] | 23 | } __attribute__ ((aligned (sizeof(void *)))); |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 24 | |
Tejun Heo | 1da43e4a | 2008-04-26 03:16:04 +0900 | [diff] [blame] | 25 | #define KLIST_INIT(_name, _get, _put) \ |
| 26 | { .k_lock = __SPIN_LOCK_UNLOCKED(_name.k_lock), \ |
| 27 | .k_list = LIST_HEAD_INIT(_name.k_list), \ |
| 28 | .get = _get, \ |
| 29 | .put = _put, } |
| 30 | |
| 31 | #define DEFINE_KLIST(_name, _get, _put) \ |
| 32 | struct klist _name = KLIST_INIT(_name, _get, _put) |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 33 | |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 34 | extern void klist_init(struct klist *k, void (*get)(struct klist_node *), |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 35 | void (*put)(struct klist_node *)); |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 36 | |
| 37 | struct klist_node { |
Tejun Heo | a1ed5b0 | 2008-08-25 19:50:16 +0200 | [diff] [blame] | 38 | void *n_klist; /* never access directly */ |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 39 | struct list_head n_node; |
| 40 | struct kref n_ref; |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 43 | extern void klist_add_tail(struct klist_node *n, struct klist *k); |
| 44 | extern void klist_add_head(struct klist_node *n, struct klist *k); |
Ken Helias | 0f9859c | 2014-08-06 16:09:18 -0700 | [diff] [blame] | 45 | extern void klist_add_behind(struct klist_node *n, struct klist_node *pos); |
Tejun Heo | 93dd400 | 2008-04-22 18:58:46 +0900 | [diff] [blame] | 46 | extern void klist_add_before(struct klist_node *n, struct klist_node *pos); |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 47 | |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 48 | extern void klist_del(struct klist_node *n); |
| 49 | extern void klist_remove(struct klist_node *n); |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 50 | |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 51 | extern int klist_node_attached(struct klist_node *n); |
mochel@digitalimplant.org | 8b0c250b | 2005-03-24 12:58:57 -0800 | [diff] [blame] | 52 | |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 53 | |
| 54 | struct klist_iter { |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 55 | struct klist *i_klist; |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 56 | struct klist_node *i_cur; |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 60 | extern void klist_iter_init(struct klist *k, struct klist_iter *i); |
| 61 | extern void klist_iter_init_node(struct klist *k, struct klist_iter *i, |
| 62 | struct klist_node *n); |
| 63 | extern void klist_iter_exit(struct klist_iter *i); |
Andy Shevchenko | 2e0fed7 | 2015-07-27 18:03:59 +0300 | [diff] [blame] | 64 | extern struct klist_node *klist_prev(struct klist_iter *i); |
Greg Kroah-Hartman | c3bb7fad | 2008-04-30 16:43:45 -0700 | [diff] [blame] | 65 | extern struct klist_node *klist_next(struct klist_iter *i); |
mochel@digitalimplant.org | 9a19fea | 2005-03-21 11:45:16 -0800 | [diff] [blame] | 66 | |
James Bottomley | d856f1e3 | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 67 | #endif |