Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / xdr_reference.c
1 #ifndef lint
2 static char sfs_xdr_reference_id[] = "@(#)xdr_reference.c     2.1     97/10/23";
3 #endif
4 /* @(#)xdr_reference.c  2.1 88/07/29 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
53 #endif
54
55 /*
56  * xdr_reference.c, Generic XDR routines impelmentation.
57  *
58  * Copyright (C) 1987, Sun Microsystems, Inc.
59  *
60  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
61  * "pointers".  See xdr.h for more info on the interface to xdr.
62  */
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <string.h>
68 #include "rpc/types.h"
69 #include "rpc/xdr.h"
70
71 #define LASTUNSIGNED    ((uint_t)0-1)
72
73 /*
74  * XDR an indirect pointer
75  * xdr_reference is for recursively translating a structure that is
76  * referenced by a pointer inside the structure that is currently being
77  * translated.  pp references a pointer to storage. If *pp is null
78  * the  necessary storage is allocated.
79  * size is the sizeof the referneced structure.
80  * proc is the routine to handle the referenced structure.
81  */
82 bool_t
83 xdr_reference(
84         XDR *xdrs,
85         void **pp,              /* the pointer to work on */
86         uint_t size,            /* size of the object pointed to */
87         xdrproc_t proc)         /* xdr routine to handle the object */
88 {
89         void *loc = *pp;
90         bool_t stat;
91
92         if (loc == NULL)
93                 switch (xdrs->x_op) {
94                 case XDR_FREE:
95                         return (TRUE);
96
97                 case XDR_DECODE:
98                         *pp = loc = (void *) mem_alloc(size);
99                         if (loc == NULL) {
100                                 (void) fprintf(stderr,
101                                     "xdr_reference: out of memory\n");
102                                 return (FALSE);
103                         }
104                         memset(loc, '\0', (int)size);
105                         break;
106         }
107
108         stat = (*proc)(xdrs, loc, LASTUNSIGNED);
109
110         if (xdrs->x_op == XDR_FREE) {
111                 mem_free(loc, size);
112                 *pp = NULL;
113         }
114         return (stat);
115 }
116
117
118 /*
119  * xdr_pointer():
120  *
121  * XDR a pointer to a possibly recursive data structure. This
122  * differs with xdr_reference in that it can serialize/deserialiaze
123  * trees correctly.
124  *
125  *  What's sent is actually a union:
126  *
127  *  union object_pointer switch (boolean b) {
128  *  case TRUE: object_data data;
129  *  case FALSE: void nothing;
130  *  }
131  *
132  * > objpp: Pointer to the pointer to the object.
133  * > obj_size: size of the object.
134  * > xdr_obj: routine to XDR an object.
135  *
136  */
137 bool_t
138 xdr_pointer(
139         XDR *xdrs,
140         void **objpp,
141         uint_t obj_size,
142         xdrproc_t xdr_obj)
143 {
144
145         bool_t more_data;
146
147         more_data = (*objpp != NULL);
148         if (! xdr_bool(xdrs,&more_data)) {
149                 return (FALSE);
150         }
151         if (! more_data) {
152                 *objpp = NULL;
153                 return (TRUE);
154         }
155         return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
156 }