1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2001-2003
5 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * All rights reserved.
7 *
8 * Author: Harti Brandt <harti@freebsd.org>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * Customisation of the SSCFU code to ng_sscfu.
32 *
33 * $FreeBSD$
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)malloc((SIZE), M_NG_SSCFU, M_NOWAIT | M_ZERO))
57 #define MEMFREE(PTR) \
58 free(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)malloc((SIZE), M_NG_SSCFU_INS, M_NOWAIT | M_ZERO))
74 #define MEMFREE(PTR) \
75 free(PTR, M_NG_SSCFU_INS)
76
77 #define SIG_ALLOC(PTR) \
78 ((PTR) = malloc(sizeof(struct sscfu_sig), \
79 M_NG_SSCFU_SIG, M_NOWAIT | M_ZERO))
80 #define SIG_FREE(PTR) \
81 free(PTR, M_NG_SSCFU_SIG)
82
83 #endif
84
85 /*
86 * Signal queues
87 */
88 typedef TAILQ_ENTRY(sscfu_sig) sscfu_sigq_link_t;
89 typedef TAILQ_HEAD(sscfu_sigq, sscfu_sig) sscfu_sigq_head_t;
90 #define SIGQ_INIT(Q) TAILQ_INIT(Q)
91 #define SIGQ_APPEND(Q, S) TAILQ_INSERT_TAIL(Q, S, link)
92
93 #define SIGQ_GET(Q) ng_sscfu_sigq_get((Q))
94
95 #define DECL_SIGQ_GET \
96 static __inline struct sscfu_sig * \
97 ng_sscfu_sigq_get(struct sscfu_sigq *q) \
98 { \
99 struct sscfu_sig *s; \
100 \
101 s = TAILQ_FIRST(q); \
102 if (s != NULL) \
103 TAILQ_REMOVE(q, s, link); \
104 return (s); \
105 }
106
107 #define SIGQ_CLEAR(Q) \
108 do { \
109 struct sscfu_sig *_s1, *_s2; \
110 \
111 _s1 = TAILQ_FIRST(Q); \
112 while (_s1 != NULL) { \
113 _s2 = TAILQ_NEXT(_s1, link); \
114 if (_s1->m) \
115 MBUF_FREE(_s1->m); \
116 SIG_FREE(_s1); \
117 _s1 = _s2; \
118 } \
119 TAILQ_INIT(Q); \
120 } while (0)
121
122 /*
123 * Message buffers
124 */
125 #define MBUF_FREE(M) m_freem(M)
126
127 #ifdef SSCFU_DEBUG
128 #define ASSERT(S) KASSERT(S, (#S))
129 #else
130 #define ASSERT(S)
131 #endif
Cache object: ad9a5833fcbf826a26f972d4f6af389a
|