|
|
XDR routines not only perform input and output, they also perform memory allocation.
The second parameter of xdr_array is a pointer to an array, rather than the array itself.
If it is NULL, then xdr_array allocates space for the array and returns a pointer to it, putting the size of the array in the third argument. As an example, consider the following XDR routine xdr_chararr1, which deals with a fixed array of bytes with length SIZE:
xdr_chararr1(xdrsp, chararr) XDR *xdrsp; char chararr[]; { char *p; int len;If space has already been allocated in chararr, it can be called from a server like this:p = chararr; len = SIZE; return (xdr_bytes(xdrsp, &p, &len, SIZE)); }
char chararr[SIZE];To have XDR to do the allocation, this routine must be rewritten in the following way:svc_getargs(transp, xdr_chararr1, chararr);
xdr_chararr2(xdrsp, chararrp) XDR *xdrsp; char **chararrp; { int len;Then the RPC call might look like this:len = SIZE; return (xdr_bytes(xdrsp, charrarrp, &len, SIZE)); }
char *arrptr;Note that, after being used, the character array should normally be freed with svc_freeargs. svc_freeargs will not attempt to free any memory if the variable indicating it is NULL. For example, in the routine xdr_finalexample, given earlier, ifarrptr = NULL; svc_getargs(transp, xdr_chararr2, &arrptr); /* * Use the result here */ svc_freeargs(transp, xdr_chararr2, &arrptr);
finalp->string
were
NULL,
then it would not be freed.
The same is true for
finalp->simplep
.
To summarize: