2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Hartmut Brandt <harti@freebsd.org>
29 * $Begemot: libunimsg/libngatm/sscopcust.h,v 1.4 2004/07/08 08:21:40 brandt Exp $
31 * Customisation of the SSCOP code for the user space library.
33 #include <sys/types.h>
34 #include <sys/queue.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <netnatm/unimsg.h>
48 * Allocate zeroed or non-zeroed memory of some size and cast it.
49 * Return NULL on failure.
53 #define MEMZALLOC(PTR,CAST,SIZE) do { \
54 void *_m = malloc(SIZE); \
60 #define MEMFREE(PTR) free(PTR);
62 #define MSG_ALLOC(PTR) \
63 MEMZALLOC(PTR, struct sscop_msg *, sizeof(struct sscop_msg))
64 #define MSG_FREE(PTR) \
67 #define SIG_ALLOC(PTR) \
68 MEMZALLOC(PTR, struct sscop_sig *, sizeof(struct sscop_sig))
69 #define SIG_FREE(PTR) \
75 typedef void *sscop_timer_t;
76 #define TIMER_INIT(S,T) (S)->t_##T = NULL
77 #define TIMER_STOP(S,T) do { \
78 if ((S)->t_##T != NULL) { \
79 (S)->funcs->stop_timer((S), (S)->aarg, (S)->t_##T); \
83 #define TIMER_RESTART(S,T) do { \
84 if ((S)->t_##T != NULL) \
85 (S)->funcs->stop_timer((S), (S)->aarg, (S)->t_##T); \
86 (S)->t_##T = (S)->funcs->start_timer((S), (S)->aarg, \
87 (S)->timer##T, T##_func); \
89 #define TIMER_ISACT(S,T) ((S)->t_##T != NULL)
91 #define TIMER_FUNC(T,N) \
93 T##_func(void *varg) \
95 struct sscop *sscop = varg; \
96 VERBOSE(sscop, SSCOP_DBG_TIMER, (sscop, sscop->aarg, \
97 "timer_" #T " expired")); \
98 sscop->t_##T = NULL; \
99 sscop_signal(sscop, SIG_T_##N, NULL); \
106 typedef TAILQ_ENTRY(sscop_msg) sscop_msgq_link_t;
107 typedef TAILQ_HEAD(sscop_msgq, sscop_msg) sscop_msgq_head_t;
108 #define MSGQ_EMPTY(Q) TAILQ_EMPTY(Q)
109 #define MSGQ_INIT(Q) TAILQ_INIT(Q)
110 #define MSGQ_FOREACH(P,Q) TAILQ_FOREACH(P,Q,link)
111 #define MSGQ_REMOVE(Q,M) TAILQ_REMOVE(Q,M,link)
112 #define MSGQ_INSERT_BEFORE(B,M) TAILQ_INSERT_BEFORE(B,M,link)
113 #define MSGQ_APPEND(Q,M) TAILQ_INSERT_TAIL(Q,M,link)
114 #define MSGQ_PEEK(Q) (TAILQ_EMPTY((Q)) ? NULL : TAILQ_FIRST((Q)))
115 #define MSGQ_GET(Q) \
117 struct sscop_msg *_m = NULL; \
119 if(!TAILQ_EMPTY(Q)) { \
120 _m = TAILQ_FIRST(Q); \
121 TAILQ_REMOVE(Q, _m, link); \
126 #define MSGQ_CLEAR(Q) \
128 struct sscop_msg *_m1, *_m2; \
130 _m1 = TAILQ_FIRST(Q); \
131 while(_m1 != NULL) { \
132 _m2 = TAILQ_NEXT(_m1, link); \
133 SSCOP_MSG_FREE(_m1); \
142 typedef TAILQ_ENTRY(sscop_sig) sscop_sigq_link_t;
143 typedef TAILQ_HEAD(sscop_sigq, sscop_sig) sscop_sigq_head_t;
144 #define SIGQ_INIT(Q) TAILQ_INIT(Q)
145 #define SIGQ_APPEND(Q,S) TAILQ_INSERT_TAIL(Q, S, link)
146 #define SIGQ_EMPTY(Q) TAILQ_EMPTY(Q)
147 #define SIGQ_GET(Q) \
149 struct sscop_sig *_s = NULL; \
151 if(!TAILQ_EMPTY(Q)) { \
152 _s = TAILQ_FIRST(Q); \
153 TAILQ_REMOVE(Q, _s, link); \
158 #define SIGQ_MOVE(F,T) \
160 struct sscop_sig *_s; \
162 while(!TAILQ_EMPTY(F)) { \
163 _s = TAILQ_FIRST(F); \
164 TAILQ_REMOVE(F, _s, link); \
165 TAILQ_INSERT_TAIL(T, _s, link); \
169 #define SIGQ_PREPEND(F,T) \
171 struct sscop_sig *_s; \
173 while(!TAILQ_EMPTY(F)) { \
174 _s = TAILQ_LAST(F, sscop_sigq); \
175 TAILQ_REMOVE(F, _s, link); \
176 TAILQ_INSERT_HEAD(T, _s, link); \
180 #define SIGQ_CLEAR(Q) \
182 struct sscop_sig *_s1, *_s2; \
184 _s1 = TAILQ_FIRST(Q); \
185 while(_s1 != NULL) { \
186 _s2 = TAILQ_NEXT(_s1, link); \
187 SSCOP_MSG_FREE(_s1->msg); \
199 /* Free a buffer (if there is one) */
200 #define MBUF_FREE(M) do { if(M) uni_msg_destroy(M); } while(0)
202 /* duplicate a buffer */
203 #define MBUF_DUP(M) uni_msg_dup(M)
205 /* compute current length */
206 #define MBUF_LEN(M) uni_msg_len((M))
209 * Return the i-th word counted from the end of the buffer.
210 * i=-1 will return the last 32bit word, i=-2 the 2nd last.
211 * Assumes that there is enough space.
213 #define MBUF_TRAIL32(M,I) uni_msg_trail32((M), (I))
216 * Strip 32bit value from the end
218 #define MBUF_STRIP32(M) uni_msg_strip32((M))
221 * Strip 32bit value from head
223 #define MBUF_GET32(M) uni_msg_get32((M))
226 * Append a 32bit value to an mbuf. Failures are ignored.
228 #define MBUF_APPEND32(M,W) uni_msg_append32((M), (W))
231 * Pad a message to a multiple of four byte and return the amount of padding
232 * Failures are ignored.
234 #define MBUF_PAD4(M) \
237 while (uni_msg_len(M) % 4 != 0) { \
238 uni_msg_append8((M), 0); \
244 #define MBUF_UNPAD(M,P) do { (M)->b_wptr -= (P); } while(0)
247 * Allocate a message that will probably hold N bytes.
249 #define MBUF_ALLOC(N) uni_msg_alloc(N)
252 #define ASSERT(X) assert(X)