xref: /csrg-svn/lib/librpc/rpc/pmap_prot2.c (revision 45085)
1*45085Smckusick /* @(#)pmap_prot2.c	2.1 88/07/29 4.0 RPCSRC */
2*45085Smckusick /*
3*45085Smckusick  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4*45085Smckusick  * unrestricted use provided that this legend is included on all tape
5*45085Smckusick  * media and as a part of the software program in whole or part.  Users
6*45085Smckusick  * may copy or modify Sun RPC without charge, but are not authorized
7*45085Smckusick  * to license or distribute it to anyone else except as part of a product or
8*45085Smckusick  * program developed by the user.
9*45085Smckusick  *
10*45085Smckusick  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11*45085Smckusick  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12*45085Smckusick  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13*45085Smckusick  *
14*45085Smckusick  * Sun RPC is provided with no support and without any obligation on the
15*45085Smckusick  * part of Sun Microsystems, Inc. to assist in its use, correction,
16*45085Smckusick  * modification or enhancement.
17*45085Smckusick  *
18*45085Smckusick  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19*45085Smckusick  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20*45085Smckusick  * OR ANY PART THEREOF.
21*45085Smckusick  *
22*45085Smckusick  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23*45085Smckusick  * or profits or other special, indirect and consequential damages, even if
24*45085Smckusick  * Sun has been advised of the possibility of such damages.
25*45085Smckusick  *
26*45085Smckusick  * Sun Microsystems, Inc.
27*45085Smckusick  * 2550 Garcia Avenue
28*45085Smckusick  * Mountain View, California  94043
29*45085Smckusick  */
30*45085Smckusick #if !defined(lint) && defined(SCCSIDS)
31*45085Smckusick static char sccsid[] = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
32*45085Smckusick #endif
33*45085Smckusick 
34*45085Smckusick /*
35*45085Smckusick  * pmap_prot2.c
36*45085Smckusick  * Protocol for the local binder service, or pmap.
37*45085Smckusick  *
38*45085Smckusick  * Copyright (C) 1984, Sun Microsystems, Inc.
39*45085Smckusick  */
40*45085Smckusick 
41*45085Smckusick #include <rpc/types.h>
42*45085Smckusick #include <rpc/xdr.h>
43*45085Smckusick #include <rpc/pmap_prot.h>
44*45085Smckusick 
45*45085Smckusick 
46*45085Smckusick /*
47*45085Smckusick  * What is going on with linked lists? (!)
48*45085Smckusick  * First recall the link list declaration from pmap_prot.h:
49*45085Smckusick  *
50*45085Smckusick  * struct pmaplist {
51*45085Smckusick  *	struct pmap pml_map;
52*45085Smckusick  *	struct pmaplist *pml_map;
53*45085Smckusick  * };
54*45085Smckusick  *
55*45085Smckusick  * Compare that declaration with a corresponding xdr declaration that
56*45085Smckusick  * is (a) pointer-less, and (b) recursive:
57*45085Smckusick  *
58*45085Smckusick  * typedef union switch (bool_t) {
59*45085Smckusick  *
60*45085Smckusick  *	case TRUE: struct {
61*45085Smckusick  *		struct pmap;
62*45085Smckusick  * 		pmaplist_t foo;
63*45085Smckusick  *	};
64*45085Smckusick  *
65*45085Smckusick  *	case FALSE: struct {};
66*45085Smckusick  * } pmaplist_t;
67*45085Smckusick  *
68*45085Smckusick  * Notice that the xdr declaration has no nxt pointer while
69*45085Smckusick  * the C declaration has no bool_t variable.  The bool_t can be
70*45085Smckusick  * interpreted as ``more data follows me''; if FALSE then nothing
71*45085Smckusick  * follows this bool_t; if TRUE then the bool_t is followed by
72*45085Smckusick  * an actual struct pmap, and then (recursively) by the
73*45085Smckusick  * xdr union, pamplist_t.
74*45085Smckusick  *
75*45085Smckusick  * This could be implemented via the xdr_union primitive, though this
76*45085Smckusick  * would cause a one recursive call per element in the list.  Rather than do
77*45085Smckusick  * that we can ``unwind'' the recursion
78*45085Smckusick  * into a while loop and do the union arms in-place.
79*45085Smckusick  *
80*45085Smckusick  * The head of the list is what the C programmer wishes to past around
81*45085Smckusick  * the net, yet is the data that the pointer points to which is interesting;
82*45085Smckusick  * this sounds like a job for xdr_reference!
83*45085Smckusick  */
84*45085Smckusick bool_t
xdr_pmaplist(xdrs,rp)85*45085Smckusick xdr_pmaplist(xdrs, rp)
86*45085Smckusick 	register XDR *xdrs;
87*45085Smckusick 	register struct pmaplist **rp;
88*45085Smckusick {
89*45085Smckusick 	/*
90*45085Smckusick 	 * more_elements is pre-computed in case the direction is
91*45085Smckusick 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
92*45085Smckusick 	 * xdr_bool when the direction is XDR_DECODE.
93*45085Smckusick 	 */
94*45085Smckusick 	bool_t more_elements;
95*45085Smckusick 	register int freeing = (xdrs->x_op == XDR_FREE);
96*45085Smckusick 	register struct pmaplist **next;
97*45085Smckusick 
98*45085Smckusick 	while (TRUE) {
99*45085Smckusick 		more_elements = (bool_t)(*rp != NULL);
100*45085Smckusick 		if (! xdr_bool(xdrs, &more_elements))
101*45085Smckusick 			return (FALSE);
102*45085Smckusick 		if (! more_elements)
103*45085Smckusick 			return (TRUE);  /* we are done */
104*45085Smckusick 		/*
105*45085Smckusick 		 * the unfortunate side effect of non-recursion is that in
106*45085Smckusick 		 * the case of freeing we must remember the next object
107*45085Smckusick 		 * before we free the current object ...
108*45085Smckusick 		 */
109*45085Smckusick 		if (freeing)
110*45085Smckusick 			next = &((*rp)->pml_next);
111*45085Smckusick 		if (! xdr_reference(xdrs, (caddr_t *)rp,
112*45085Smckusick 		    (u_int)sizeof(struct pmaplist), xdr_pmap))
113*45085Smckusick 			return (FALSE);
114*45085Smckusick 		rp = (freeing) ? next : &((*rp)->pml_next);
115*45085Smckusick 	}
116*45085Smckusick }
117