1139823Simp/*-
2121461Sharti * Copyright (c) 2001-2003
3121461Sharti *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4121461Sharti * 	All rights reserved.
5121461Sharti *
6121461Sharti * Author: Harti Brandt <harti@freebsd.org>
7121461Sharti *
8121461Sharti * Redistribution and use in source and binary forms, with or without
9121461Sharti * modification, are permitted provided that the following conditions
10121461Sharti * are met:
11121461Sharti * 1. Redistributions of source code must retain the above copyright
12121461Sharti *    notice, this list of conditions and the following disclaimer.
13121461Sharti * 2. Redistributions in binary form must reproduce the above copyright
14121461Sharti *    notice, this list of conditions and the following disclaimer in the
15121461Sharti *    documentation and/or other materials provided with the distribution.
16121461Sharti *
17121461Sharti * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18121461Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19121461Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20121461Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21121461Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22121461Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23121461Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24121461Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25121461Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26121461Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27121461Sharti * SUCH DAMAGE.
28121461Sharti *
29121461Sharti * Customisation of the SSCFU code to ng_sscfu.
30121461Sharti *
31121461Sharti * $FreeBSD$
32121461Sharti */
33121461Sharti#include <sys/param.h>
34121461Sharti#include <sys/types.h>
35121461Sharti#include <sys/kernel.h>
36121461Sharti#include <sys/mbuf.h>
37121461Sharti#include <sys/queue.h>
38121461Sharti#include <sys/callout.h>
39121461Sharti#include <sys/systm.h>
40121461Sharti#include <sys/malloc.h>
41121461Sharti#include <machine/stdarg.h>
42121461Sharti
43121461Sharti/*
44121461Sharti * Allocate zeroed or non-zeroed memory of some size and cast it.
45121461Sharti * Return NULL on failure.
46121461Sharti */
47121461Sharti#ifndef SSCFU_DEBUG
48121461Sharti
49121461Sharti#define	MEMINIT() \
50121461Sharti	MALLOC_DECLARE(M_NG_SSCFU); \
51121461Sharti	DECL_SIGQ_GET
52121461Sharti
53121461Sharti#define	MEMZALLOC(PTR, CAST, SIZE) \
54121461Sharti	((PTR) = (CAST)malloc((SIZE), M_NG_SSCFU, M_NOWAIT | M_ZERO))
55121461Sharti#define	MEMFREE(PTR) \
56121461Sharti	free(PTR, M_NG_SSCFU)
57121461Sharti
58121461Sharti#define	SIG_ALLOC(PTR) \
59121461Sharti	MEMZALLOC(PTR, struct sscfu_sig *, sizeof(struct sscfu_sig))
60121461Sharti#define	SIG_FREE(PTR) \
61121461Sharti	MEMFREE(PTR)
62121461Sharti
63121461Sharti#else
64121461Sharti
65121461Sharti#define	MEMINIT() 							\
66121461Sharti	MALLOC_DEFINE(M_NG_SSCFU_INS, "sscfu_ins", "SSCFU instances");	\
67121461Sharti	MALLOC_DEFINE(M_NG_SSCFU_SIG, "sscfu_sig", "SSCFU signals");	\
68121461Sharti	DECL_SIGQ_GET
69121461Sharti
70121461Sharti#define	MEMZALLOC(PTR, CAST, SIZE)					\
71121461Sharti	((PTR) = (CAST)malloc((SIZE), M_NG_SSCFU_INS, M_NOWAIT | M_ZERO))
72121461Sharti#define	MEMFREE(PTR)							\
73184205Sdes	free(PTR, M_NG_SSCFU_INS)
74121461Sharti
75121461Sharti#define	SIG_ALLOC(PTR)							\
76121461Sharti	((PTR) = malloc(sizeof(struct sscfu_sig),			\
77121461Sharti	    M_NG_SSCFU_SIG, M_NOWAIT | M_ZERO))
78121461Sharti#define	SIG_FREE(PTR)							\
79184205Sdes	free(PTR, M_NG_SSCFU_SIG)
80121461Sharti
81121461Sharti#endif
82121461Sharti
83121461Sharti
84121461Sharti/*
85121461Sharti * Signal queues
86121461Sharti */
87121461Shartitypedef TAILQ_ENTRY(sscfu_sig) sscfu_sigq_link_t;
88121461Shartitypedef TAILQ_HEAD(sscfu_sigq, sscfu_sig) sscfu_sigq_head_t;
89121461Sharti#define	SIGQ_INIT(Q) 		TAILQ_INIT(Q)
90121461Sharti#define	SIGQ_APPEND(Q, S)	TAILQ_INSERT_TAIL(Q, S, link)
91121461Sharti
92121461Sharti#define	SIGQ_GET(Q) ng_sscfu_sigq_get((Q))
93121461Sharti
94121461Sharti#define	DECL_SIGQ_GET							\
95121461Shartistatic __inline struct sscfu_sig *					\
96121461Sharting_sscfu_sigq_get(struct sscfu_sigq *q)					\
97121461Sharti{									\
98121461Sharti	struct sscfu_sig *s;						\
99121461Sharti									\
100121461Sharti	s = TAILQ_FIRST(q);						\
101121461Sharti	if (s != NULL)							\
102121461Sharti		TAILQ_REMOVE(q, s, link);				\
103121461Sharti	return (s);							\
104121461Sharti}
105121461Sharti
106121461Sharti#define	SIGQ_CLEAR(Q)							\
107121461Sharti    do {								\
108121461Sharti	struct sscfu_sig *_s1, *_s2;					\
109121461Sharti									\
110121461Sharti	_s1 = TAILQ_FIRST(Q);						\
111121461Sharti	while (_s1 != NULL) {						\
112121461Sharti		_s2 = TAILQ_NEXT(_s1, link);				\
113121461Sharti		if (_s1->m)						\
114121461Sharti			MBUF_FREE(_s1->m);				\
115121461Sharti		SIG_FREE(_s1);						\
116121461Sharti		_s1 = _s2;						\
117121461Sharti	}								\
118121461Sharti	TAILQ_INIT(Q);							\
119121461Sharti    } while (0)
120121461Sharti
121121461Sharti
122121461Sharti/*
123121461Sharti * Message buffers
124121461Sharti */
125121461Sharti#define	MBUF_FREE(M)	m_freem(M)
126121461Sharti
127121461Sharti#ifdef SSCFU_DEBUG
128121461Sharti#define	ASSERT(S)	KASSERT(S, (#S))
129121461Sharti#else
130121461Sharti#define	ASSERT(S)
131121461Sharti#endif
132