blob: 953f283f8451daef34283770ff3c9c0a14db7804 [file] [log] [blame]
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08001/*
2 * klist.h - Some generic list helpers, extending struct list_head a bit.
3 *
4 * Implementations are found in lib/klist.c
5 *
6 *
7 * Copyright (C) 2005 Patrick Mochel
8 *
9 * This file is rleased under the GPL v2.
10 */
11
James Bottomleyd856f1e32005-08-19 09:14:01 -040012#ifndef _LINUX_KLIST_H
13#define _LINUX_KLIST_H
14
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080015#include <linux/spinlock.h>
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080016#include <linux/kref.h>
17#include <linux/list.h>
18
James Bottomley34bb61f2005-09-06 16:56:51 -070019struct klist_node;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080020struct klist {
21 spinlock_t k_lock;
22 struct list_head k_list;
James Bottomley34bb61f2005-09-06 16:56:51 -070023 void (*get)(struct klist_node *);
24 void (*put)(struct klist_node *);
David Miller795abaf2011-02-13 16:37:07 -080025} __attribute__ ((aligned (sizeof(void *))));
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080026
Tejun Heo1da43e42008-04-26 03:16:04 +090027#define KLIST_INIT(_name, _get, _put) \
28 { .k_lock = __SPIN_LOCK_UNLOCKED(_name.k_lock), \
29 .k_list = LIST_HEAD_INIT(_name.k_list), \
30 .get = _get, \
31 .put = _put, }
32
33#define DEFINE_KLIST(_name, _get, _put) \
34 struct klist _name = KLIST_INIT(_name, _get, _put)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080035
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070036extern void klist_init(struct klist *k, void (*get)(struct klist_node *),
James Bottomley34bb61f2005-09-06 16:56:51 -070037 void (*put)(struct klist_node *));
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080038
39struct klist_node {
Tejun Heoa1ed5b02008-08-25 19:50:16 +020040 void *n_klist; /* never access directly */
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080041 struct list_head n_node;
42 struct kref n_ref;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080043};
44
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070045extern void klist_add_tail(struct klist_node *n, struct klist *k);
46extern void klist_add_head(struct klist_node *n, struct klist *k);
Ken Helias0f9859c2014-08-06 16:09:18 -070047extern void klist_add_behind(struct klist_node *n, struct klist_node *pos);
Tejun Heo93dd4002008-04-22 18:58:46 +090048extern void klist_add_before(struct klist_node *n, struct klist_node *pos);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080049
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070050extern void klist_del(struct klist_node *n);
51extern void klist_remove(struct klist_node *n);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080052
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070053extern int klist_node_attached(struct klist_node *n);
mochel@digitalimplant.org8b0c2502005-03-24 12:58:57 -080054
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080055
56struct klist_iter {
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070057 struct klist *i_klist;
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070058 struct klist_node *i_cur;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080059};
60
61
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070062extern void klist_iter_init(struct klist *k, struct klist_iter *i);
63extern void klist_iter_init_node(struct klist *k, struct klist_iter *i,
64 struct klist_node *n);
65extern void klist_iter_exit(struct klist_iter *i);
Andy Shevchenko2e0fed72015-07-27 18:03:59 +030066extern struct klist_node *klist_prev(struct klist_iter *i);
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070067extern struct klist_node *klist_next(struct klist_iter *i);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080068
James Bottomleyd856f1e32005-08-19 09:14:01 -040069#endif