Merge branch 'vendor/LDNS'
[dragonfly.git] / sys / netgraph7 / atm / sscfu / ng_sscfu_cust.h
1 /*-
2  * Copyright (c) 2001-2003
3  *      Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *      All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Customisation of the SSCFU code to ng_sscfu.
30  *
31  * $FreeBSD: src/sys/netgraph/atm/sscfu/ng_sscfu_cust.h,v 1.2 2005/01/07 01:45:41 imp Exp $
32  * $DragonFly: src/sys/netgraph7/atm/sscfu/ng_sscfu_cust.h,v 1.2 2008/06/26 23:05:39 dillon Exp $
33  * $DragonFly: src/sys/netgraph7/atm/sscfu/ng_sscfu_cust.h,v 1.2 2008/06/26 23:05:39 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/kernel.h>
38 #include <sys/mbuf.h>
39 #include <sys/queue.h>
40 #include <sys/callout.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <machine/stdarg.h>
44
45 /*
46  * Allocate zeroed or non-zeroed memory of some size and cast it.
47  * Return NULL on failure.
48  */
49 #ifndef SSCFU_DEBUG
50
51 #define MEMINIT() \
52         MALLOC_DECLARE(M_NG_SSCFU); \
53         DECL_SIGQ_GET
54
55 #define MEMZALLOC(PTR, CAST, SIZE) \
56         ((PTR) = (CAST)kmalloc((SIZE), M_NG_SSCFU, M_WAITOK | M_NULLOK | M_ZERO))
57 #define MEMFREE(PTR) \
58         kfree(PTR, M_NG_SSCFU)
59
60 #define SIG_ALLOC(PTR) \
61         MEMZALLOC(PTR, struct sscfu_sig *, sizeof(struct sscfu_sig))
62 #define SIG_FREE(PTR) \
63         MEMFREE(PTR)
64
65 #else
66
67 #define MEMINIT()                                                       \
68         MALLOC_DEFINE(M_NG_SSCFU_INS, "sscfu_ins", "SSCFU instances");  \
69         MALLOC_DEFINE(M_NG_SSCFU_SIG, "sscfu_sig", "SSCFU signals");    \
70         DECL_SIGQ_GET
71
72 #define MEMZALLOC(PTR, CAST, SIZE)                                      \
73         ((PTR) = (CAST)kmalloc((SIZE), M_NG_SSCFU_INS, M_WAITOK | M_NULLOK | M_ZERO))
74 #define MEMFREE(PTR)                                                    \
75         FREE(PTR, M_NG_SSCFU_INS)
76
77 #define SIG_ALLOC(PTR)                                                  \
78         ((PTR) = kmalloc(sizeof(struct sscfu_sig),                      \
79             M_NG_SSCFU_SIG, M_WAITOK | M_NULLOK | M_ZERO))
80 #define SIG_FREE(PTR)                                                   \
81         FREE(PTR, M_NG_SSCFU_SIG)
82
83 #endif
84
85
86 /*
87  * Signal queues
88  */
89 typedef TAILQ_ENTRY(sscfu_sig) sscfu_sigq_link_t;
90 typedef TAILQ_HEAD(sscfu_sigq, sscfu_sig) sscfu_sigq_head_t;
91 #define SIGQ_INIT(Q)            TAILQ_INIT(Q)
92 #define SIGQ_APPEND(Q, S)       TAILQ_INSERT_TAIL(Q, S, link)
93
94 #define SIGQ_GET(Q) ng_sscfu_sigq_get((Q))
95
96 #define DECL_SIGQ_GET                                                   \
97 static __inline struct sscfu_sig *                                      \
98 ng_sscfu_sigq_get(struct sscfu_sigq *q)                                 \
99 {                                                                       \
100         struct sscfu_sig *s;                                            \
101                                                                         \
102         s = TAILQ_FIRST(q);                                             \
103         if (s != NULL)                                                  \
104                 TAILQ_REMOVE(q, s, link);                               \
105         return (s);                                                     \
106 }
107
108 #define SIGQ_CLEAR(Q)                                                   \
109     do {                                                                \
110         struct sscfu_sig *_s1, *_s2;                                    \
111                                                                         \
112         _s1 = TAILQ_FIRST(Q);                                           \
113         while (_s1 != NULL) {                                           \
114                 _s2 = TAILQ_NEXT(_s1, link);                            \
115                 if (_s1->m)                                             \
116                         MBUF_FREE(_s1->m);                              \
117                 SIG_FREE(_s1);                                          \
118                 _s1 = _s2;                                              \
119         }                                                               \
120         TAILQ_INIT(Q);                                                  \
121     } while (0)
122
123
124 /*
125  * Message buffers
126  */
127 #define MBUF_FREE(M)    m_freem(M)
128
129 #ifdef SSCFU_DEBUG
130 #define ASSERT(S)       KASSERT(S, (#S))
131 #else
132 #define ASSERT(S)
133 #endif