afs: afs_osi_Alloc_NoSleep() cleanup
[openafs.git] / src / rx / xdr.h
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
30 /* This file has the contents of Sun's orginal types.h file added. */
31
32 /*      @(#)types.h 1.1 86/02/03 SMI      */
33
34 /*
35  * Rpc additions to <sys/types.h>
36  */
37
38 #ifndef __XDR_INCLUDE__
39 #define __XDR_INCLUDE__
40 #include <afs/param.h>
41 #ifdef AFS_NT40_ENV
42 #ifndef _MFC_VER
43 #include <winsock2.h>
44 #endif /* _MFC_VER */
45 #endif
46 #ifndef bool_t
47 #define bool_t  int
48 #define enum_t  int
49 #endif
50 #ifndef FALSE
51 #define FALSE   0
52 #endif /* !FALSE */
53 #ifndef TRUE
54 #define TRUE    1
55 #endif /* !TRUE */
56 #define __dontcare__    -1
57
58 #if defined(AFS_NBSD50_ENV)
59 # if !defined(DEF_CADDR_T)
60 typedef char * caddr_t;
61 #define DEF_CADDR_T
62 # endif
63 #endif
64
65 #ifndef mem_alloc
66 #define mem_alloc(bsize)        malloc(bsize)
67 #endif
68
69 #ifndef mem_free
70 #define mem_free(ptr, bsize)    free(ptr)
71 #endif
72
73 #if defined(AFS_AMD64_LINUX24_ENV) || defined(AFS_DARWIN_ENV)
74 #define xdr_alloc afs_xdr_alloc
75 #define xdr_free afs_xdr_free
76 #define xdr_void afs_xdr_void
77 #define xdr_int afs_xdr_int
78 #define xdr_u_int afs_xdr_u_int
79 #define xdr_short afs_xdr_short
80 #define xdr_u_short afs_xdr_u_short
81 #define xdr_long afs_xdr_long
82 #define xdr_u_long afs_xdr_u_long
83 #define xdr_char afs_xdr_char
84 #define xdr_u_char afs_xdr_u_char
85 #define xdr_bool afs_xdr_bool
86 #define xdr_enum afs_xdr_enum
87 #define xdr_array afs_xdr_array
88 #define xdr_arrayN afs_xdr_arrayN
89 #define xdr_bytes afs_xdr_bytes
90 #define xdr_opaque afs_xdr_opaque
91 #define xdr_string afs_xdr_string
92 #define xdr_union afs_xdr_union
93 #define xdr_float afs_xdr_float
94 #define xdr_double afs_xdr_double
95 #define xdr_reference afs_xdr_reference
96 #define xdr_wrapstring afs_xdr_wrapstring
97 #define xdr_vector afs_xdr_vector
98 #define xdr_int64 afs_xdr_int64
99 #define xdr_uint64 afs_xdr_uint64
100 #define xdr_pointer afs_xdr_pointer
101 #define xdrmem_create afs_xdrmem_create
102 #endif
103
104 #ifdef  KERNEL
105 #define osi_alloc               afs_osi_Alloc
106 #define osi_free                afs_osi_Free
107
108 /* keep here for now, 64 bit issues */
109 #ifndef AFS_OBSD44_ENV
110 extern void *afs_osi_Alloc(size_t x);
111 extern void afs_osi_Free(void *x, size_t asize);
112 #endif
113
114 #endif
115 #ifndef major                   /* ouch! */
116 #include <sys/types.h>
117 #endif
118
119 /*      @(#)xdr.h 1.1 86/02/03 SMI      */
120
121 /*
122  * xdr.h, External Data Representation Serialization Routines.
123  *
124  * Copyright (C) 1984, Sun Microsystems, Inc.
125  */
126
127 /*
128  * XDR provides a conventional way for converting between C data
129  * types and an external bit-string representation.  Library supplied
130  * routines provide for the conversion on built-in C data types.  These
131  * routines and utility routines defined here are used to help implement
132  * a type encode/decode routine for each user-defined type.
133  *
134  * Each data type provides a single procedure which takes two arguments:
135  *
136  *      bool_t
137  *      xdrproc(xdrs, argresp)
138  *              XDR *xdrs;
139  *              <type> *argresp;
140  *
141  * xdrs is an instance of a XDR handle, to which or from which the data
142  * type is to be converted.  argresp is a pointer to the structure to be
143  * converted.  The XDR handle contains an operation field which indicates
144  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
145  *
146  * XDR_DECODE may allocate space if the pointer argresp is null.  This
147  * data can be freed with the XDR_FREE operation.
148  *
149  * We write only one procedure per data type to make it easy
150  * to keep the encode and decode procedures for a data type consistent.
151  * In many cases the same code performs all operations on a user defined type,
152  * because all the hard work is done in the component type routines.
153  * decode as a series of calls on the nested data types.
154  */
155
156 /*
157  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
158  * stream.  XDR_DECODE causes the type to be extracted from the stream.
159  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
160  * request.
161  */
162 enum xdr_op {
163     XDR_ENCODE = 0,
164     XDR_DECODE = 1,
165     XDR_FREE = 2
166 };
167
168 /*
169  * This is the number of bytes per unit of external data.
170  */
171 #define BYTES_PER_XDR_UNIT      (4)
172
173 /*
174  * A xdrproc_t exists for each data type which is to be encoded or decoded.
175  *
176  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
177  * The opaque pointer generally points to a structure of the data type
178  * to be decoded.  If this pointer is 0, then the type routines should
179  * allocate dynamic storage of the appropriate size and return it.
180  * bool_t       (*xdrproc_t)(XDR *, caddr_t *);
181  */
182
183 /* We need a different prototype for i386 Linux kernel code, because it
184  * uses a register (rather than stack) based calling convention. The
185  * normal va_args prototype results in the arguments being placed on the
186  * stack, where they aren't accessible to the 'real' function.
187  */
188 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)
189 typedef bool_t(*xdrproc_t) (void *, caddr_t, u_int);
190 #else
191 typedef bool_t(*xdrproc_t) (void *, ...);
192 #endif
193
194 /*
195  * The XDR handle.
196  * Contains operation which is being applied to the stream,
197  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
198  * and two private fields for the use of the particular impelementation.
199  */
200
201 #if defined(KERNEL) && ((defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV))
202 /* NOTE: SGI 6.1 adds two routines to the xdr_ops if the size of a long is
203  * 64 bits. I've only done this for the kernel, since other changes may
204  * be necessary if we make a 64 bit user version of AFS.
205  */
206 #define AFS_XDR_64BITOPS 1
207 #endif
208
209 typedef struct __afs_xdr {
210     enum xdr_op x_op;           /* operation; fast additional param */
211     struct xdr_ops {
212 #ifdef AFS_XDR_64BITOPS
213         bool_t(*x_getint64) (struct __afs_xdr *xdrs, afs_int64 * lp);   /* get 32 bits into a long */
214         bool_t(*x_putint64) (struct __afs_xdr *xdrs, afs_int64 * lp);   /* send 32 bits of a long */
215 #endif
216 #if !(defined(KERNEL) && defined(AFS_SUN57_ENV))
217         bool_t(*x_getint32) (struct __afs_xdr *xdrs, afs_int32 * lp);   /* get an afs_int32 from underlying stream */
218         bool_t(*x_putint32) (struct __afs_xdr *xdrs, afs_int32 * lp);   /* put an afs_int32 to " */
219 #endif
220         bool_t(*x_getbytes) (struct __afs_xdr *xdrs, caddr_t addr, u_int len);  /* get some bytes from " */
221         bool_t(*x_putbytes) (struct __afs_xdr *xdrs, caddr_t addr, u_int len);  /* put some bytes to " */
222         u_int(*x_getpostn) (struct __afs_xdr *xdrs);    /* returns bytes off from beginning */
223         bool_t(*x_setpostn) (struct __afs_xdr *xdrs, u_int pos);        /* lets you reposition the stream */
224         afs_int32 *(*x_inline) (struct __afs_xdr *xdrs, u_int len);     /* buf quick ptr to buffered data */
225         void (*x_destroy) (struct __afs_xdr *xdrs);     /* free privates of this xdr_stream */
226 #if defined(KERNEL) && defined(AFS_SUN57_ENV)
227           bool_t(*x_control) (struct __afs_xdr *xdrs);
228           bool_t(*x_getint32) (struct __afs_xdr *xdrs, afs_int32 * lp);
229           bool_t(*x_putint32) (struct __afs_xdr *xdrs, afs_int32 * lp);
230 #endif
231     } *x_ops;
232     caddr_t x_public;           /* users' data */
233     caddr_t x_private;          /* pointer to private data */
234     caddr_t x_base;             /* private used for position info */
235     int x_handy;                /* extra private word */
236 } XDR;
237
238 /*
239  * Operations defined on a XDR handle
240  *
241  * XDR          *xdrs;
242  * afs_int32            *int32p;
243  * caddr_t       addr;
244  * u_int         len;
245  * u_int         pos;
246  */
247 #ifdef AFS_XDR_64BITOPS
248 #define XDR_GETINT64(xdrs, int64p)                      \
249         (*(xdrs)->x_ops->x_getint64)(xdrs, int64p)
250 #define xdr_getint64(xdrs, int64p)                      \
251         (*(xdrs)->x_ops->x_getint64)(xdrs, int64p)
252
253 #define XDR_PUTINT64(xdrs, int64p)                      \
254         (*(xdrs)->x_ops->x_putint64)(xdrs, int64p)
255 #define xdr_putint64(xdrs, int64p)                      \
256         (*(xdrs)->x_ops->x_putint64)(xdrs, int64p)
257 #endif /* AFS_XDR_64BITOPS */
258
259 #define XDR_GETINT32(xdrs, int32p)                      \
260         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
261 #define xdr_getint32(xdrs, int32p)                      \
262         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
263
264 #define XDR_PUTINT32(xdrs, int32p)                      \
265         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
266 #define xdr_putint32(xdrs, int32p)                      \
267         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
268
269 #define XDR_GETBYTES(xdrs, addr, len)                   \
270         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
271 #define xdr_getbytes(xdrs, addr, len)                   \
272         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
273
274 #define XDR_PUTBYTES(xdrs, addr, len)                   \
275         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
276 #define xdr_putbytes(xdrs, addr, len)                   \
277         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
278
279 #define XDR_GETPOS(xdrs)                                \
280         (*(xdrs)->x_ops->x_getpostn)(xdrs)
281 #define xdr_getpos(xdrs)                                \
282         (*(xdrs)->x_ops->x_getpostn)(xdrs)
283
284 #define XDR_SETPOS(xdrs, pos)                           \
285         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
286 #define xdr_setpos(xdrs, pos)                           \
287         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
288
289 #define XDR_INLINE(xdrs, len)                           \
290         (*(xdrs)->x_ops->x_inline)(xdrs, len)
291 #define xdr_inline(xdrs, len)                           \
292         (*(xdrs)->x_ops->x_inline)(xdrs, len)
293
294 #define XDR_DESTROY(xdrs)                               \
295         if ((xdrs)->x_ops->x_destroy)                   \
296                 (*(xdrs)->x_ops->x_destroy)(xdrs)
297 #define xdr_destroy(xdrs)                               \
298         if ((xdrs)->x_ops->x_destroy)                   \
299                 (*(xdrs)->x_ops->x_destroy)(xdrs)
300
301 /*
302  * Support struct for discriminated unions.
303  * You create an array of xdrdiscrim structures, terminated with
304  * a entry with a null procedure pointer.  The xdr_union routine gets
305  * the discriminant value and then searches the array of structures
306  * for a matching value.  If a match is found the associated xdr routine
307  * is called to handle that part of the union.  If there is
308  * no match, then a default routine may be called.
309  * If there is no match and no default routine it is an error.
310  */
311 #define NULL_xdrproc_t ((xdrproc_t)0)
312 struct xdr_discrim {
313     int value;
314     xdrproc_t proc;
315 };
316
317 /*
318  * In-line routines for fast encode/decode of primitve data types.
319  * Caveat emptor: these use single memory cycles to get the
320  * data from the underlying buffer, and will fail to operate
321  * properly if the data is not aligned.  The standard way to use these
322  * is to say:
323  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
324  *              return (FALSE);
325  *      <<< macro calls >>>
326  * where ``count'' is the number of bytes of data occupied
327  * by the primitive data types.
328  *
329  * N.B. and frozen for all time: each data type here uses 4 bytes
330  * of external representation.
331  */
332 #define IXDR_GET_INT32(buf)             ntohl(*buf++)
333 #define IXDR_PUT_INT32(buf, v)          (*buf++ = htonl(v))
334
335 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_INT32(buf))
336 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_INT32(buf))
337 #define IXDR_GET_U_INT32(buf)           ((afs_uint32)IXDR_GET_INT32(buf))
338 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_INT32(buf))
339 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_INT32(buf))
340
341 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_INT32((buf), ((afs_int32)(v)))
342 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_INT32((buf), ((afs_int32)(v)))
343 #define IXDR_PUT_U_INT32(buf, v)        IXDR_PUT_INT32((buf), ((afs_int32)(v)))
344 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_INT32((buf), ((afs_int32)(v)))
345 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_INT32((buf), ((afs_int32)(v)))
346
347 #include "xdr_prototypes.h"
348 #endif /* __XDR_INCLUDE__ */