blob: 9a74b3fe23c35d8d315f3cbd62629603acdab4f4 [file] [log] [blame]
Jacob Erlbeckdfef28d2015-05-11 14:13:47 +02001/* cxx_linuxlist.h
2 *
3 * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH
4 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#pragma once
22
23extern "C" {
24 #include <osmocom/core/linuxlist.h>
25}
26
27template <typename T>
28struct LListHead {
29 typedef T entry_t;
30
31 /* This must match the declaration of struct llist_head */
32 LListHead<T> *next;
33 LListHead<T> *prev;
34
35 LListHead() : m_back(0) { INIT_LLIST_HEAD(this); }
36 LListHead(T* entry) : m_back(entry) {
37 next = (LListHead<T> *)LLIST_POISON1;
38 prev = (LListHead<T> *)LLIST_POISON2;
39 }
40
41 T *entry() {return m_back;}
42 const T *entry() const {return m_back;}
43
44 llist_head &llist() {
45 return *static_cast<llist_head *>(static_cast<void *>(this));
46 }
47 const llist_head &llist() const {
Jacob Erlbeckaf387e22015-08-21 12:05:30 +020048 return *static_cast<const llist_head *>(static_cast<const void *>(this));
Jacob Erlbeckdfef28d2015-05-11 14:13:47 +020049 }
50
51private:
52 T *const m_back;
53};
54
55/* Define a family of casting functions */
56template <typename T>
57llist_head &llist(LListHead<T> &l)
58{
59 return l->llist();
60}
61
62template <typename T>
63const llist_head &llist(const LListHead<T> &l)
64{
65 return l->llist();
66}
67
68template <typename T>
69llist_head *llptr(LListHead<T> *l)
70{
71 return &(l->llist());
72}
73
74template <typename T>
75const llist_head *llptr(const LListHead<T> *l)
76{
77 return &(l->llist());
78}
79
80/* Define type-safe wrapper for the existing linux_list.h functions */
81template <typename T>
82inline void llist_add(LListHead<T> *new_, LListHead<T> *head)
83{
84 llist_add(llptr(new_), llptr(head));
85}
86
87template <typename T>
88inline void llist_add_tail(LListHead<T> *new_, LListHead<T> *head)
89{
90 llist_add_tail(llptr(new_), llptr(head));
91}
92
93template <typename T>
94inline void llist_del(LListHead<T> *entry)
95{
96 llist_del(llptr(entry));
97}
98
99template <typename T>
100inline void llist_del_init(LListHead<T> *entry)
101{
102 llist_del_init(llptr(entry));
103}
104
105template <typename T>
106inline void llist_move(LListHead<T> *list, LListHead<T> *head)
107{
108 llist_move(llptr(list), llptr(head));
109}
110
111template <typename T>
112inline void llist_move_tail(LListHead<T> *list, LListHead<T> *head)
113{
114 llist_move_tail(llptr(list), llptr(head));
115}
116
117template <typename T>
118inline int llist_empty(const LListHead<T> *head)
119{
120 return llist_empty(llptr(head));
121}
122
123template <typename T>
124inline void llist_splice(LListHead<T> *list, LListHead<T> *head)
125{
126 llist_splice(llptr(list), llptr(head));
127}
128
129template <typename T>
130inline void llist_splice_init(LListHead<T> *list, LListHead<T> *head)
131{
132 llist_splice_init(llptr(list), llptr(head));
133}