How buggy this little piece of code could be? Repair strnvis() buffersize
[dragonfly.git] / sys / sys / syslink_msg2.h
1 /*
2  * Copyright (c) 2004-2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/sys/syslink_msg2.h,v 1.4 2007/08/13 17:47:20 dillon Exp $
35  */
36 /*
37  * Helper inlines and macros for syslink message generation
38  */
39
40 #ifndef _SYS_SYSLINK_MSG2_H_
41 #define _SYS_SYSLINK_MSG2_H_
42
43 #ifndef _SYS_SYSLINK_H_
44 #include <sys/syslink.h>
45 #endif
46 #ifndef _SYS_SYSLINK_MSG_H_
47 #include <sys/syslink_msg.h>
48 #endif
49
50 static __inline
51 struct syslink_elm *
52 sl_msg_init_cmd(struct syslink_msg *msg, sl_proto_t proto, sl_cmd_t cmd)
53 {
54         msg->sm_proto = proto;
55         msg->sm_bytes = 0;
56         /* rlabel will be filled in by the transport layer */
57         /* msgid will be filled in by the transport layer */
58         msg->sm_head.se_cmd = cmd;
59         msg->sm_head.se_bytes = sizeof(struct syslink_elm);
60         msg->sm_head.se_aux = 0;
61         return(&msg->sm_head);
62 }
63
64 static __inline
65 struct syslink_elm *
66 sl_msg_init_reply(struct syslink_msg *msg, struct syslink_msg *rep,
67                   int32_t error)
68 {
69         rep->sm_proto = msg->sm_proto | SM_PROTO_REPLY;
70         rep->sm_bytes = 0;
71         rep->sm_rlabel = msg->sm_rlabel;
72         rep->sm_msgid = msg->sm_msgid;
73         rep->sm_head.se_cmd = msg->sm_head.se_cmd | SE_CMDF_REPLY;
74         rep->sm_head.se_bytes = sizeof(struct syslink_elm);
75         rep->sm_head.se_aux = error;
76         return(&rep->sm_head);
77 }
78
79 static __inline
80 void
81 sl_msg_fini(struct syslink_msg *msg)
82 {
83         msg->sm_bytes = __offsetof(struct syslink_msg, sm_head) +
84                                  SL_MSG_ALIGN(msg->sm_head.se_bytes);
85 }
86
87 static __inline
88 struct syslink_elm *
89 sl_elm_make(struct syslink_elm *par, sl_cmd_t cmd, int bytes)
90 {
91         struct syslink_elm *elm = (void *)((char *)par + par->se_bytes);
92
93         KKASSERT((cmd & SE_CMDF_STRUCTURED) == 0);
94         elm->se_cmd = cmd;
95         elm->se_bytes = sizeof(*elm) + bytes;
96         elm->se_aux = 0;
97
98         par->se_bytes += SL_MSG_ALIGN(elm->se_bytes);
99         return(elm);
100 }
101
102 static __inline
103 struct syslink_elm *
104 sl_elm_make_aux(struct syslink_elm *par, sl_cmd_t cmd, int auxdata)
105 {
106         struct syslink_elm *elm = (void *)((char *)par + par->se_bytes);
107
108         KKASSERT((cmd & SE_CMDF_STRUCTURED) == 0);
109         elm->se_cmd = cmd;
110         elm->se_bytes = sizeof(*elm);
111         elm->se_aux = auxdata;
112
113         par->se_bytes += SL_MSG_ALIGN(elm->se_bytes);
114         return(elm);
115 }
116
117 /*
118  * Push a new element given the current element, creating a recursion
119  * under the current element.
120  */
121 static __inline
122 struct syslink_elm *
123 sl_elm_push(struct syslink_elm *par, sl_cmd_t cmd)
124 {
125         struct syslink_elm *elm = (void *)((char *)par + par->se_bytes);
126
127         KKASSERT(cmd & SE_CMDF_STRUCTURED);
128         elm->se_cmd = cmd;
129         elm->se_bytes = sizeof(struct syslink_elm);
130         elm->se_aux = 0;
131
132         return(elm);
133 }
134
135 /*
136  * Pop a previously pushed element.  Additional array elements may be
137  * added to the parent by continuing to push/pop using the parent.
138  */
139 static __inline
140 void
141 sl_elm_pop(struct syslink_elm *par, struct syslink_elm *elm)
142 {
143         par->se_bytes = (char *)elm - (char *)par + elm->se_bytes;
144 }
145
146 #define SL_FOREACH_ELEMENT(par, elm)                                    \
147         for (elm = par + 1; (char *)elm < (char *)par + par->se_bytes;  \
148              elm = (void *)((char *)elm + SL_MSG_ALIGN(elm->se_bytes)))
149
150 #endif
151