Specifications

Page 12 Remote Procedure Call Programming Guide
The relevant routine is svc_getargs() which takes an SVCXPRT handle, the XDR routine, and a pointer to
where the input is to be placed as arguments.
3.2. Memory Allocation with XDR
XDR routines not only do input and output, they also do memory allocation. This is why 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;
p = chararr;
len = SIZE;
return (xdr_bytes(xdrsp, &p, &len, SIZE));
}
If space has already been allocated in chararr, it can be called from a server like this:
char chararr[SIZE];
svc_getargs(transp, xdr_chararr1, chararr);
If you want XDR to do the allocation, you would have to rewrite this routine in the following way:
xdr_chararr2(xdrsp, chararrp)
XDR *xdrsp;
char **chararrp;
{
int len;
len = SIZE;
return (xdr_bytes(xdrsp, charrarrp, &len, SIZE));
}
Then the RPC call might look like this:
char *arrptr;
arrptr = NULL;
svc_getargs(transp, xdr_chararr2, &arrptr);
/*
* Use the result here
*/
svc_freeargs(transp, xdr_chararr2, &arrptr);
Note that, after being used, the character array can 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 the routine
xdr_finalexample(), given earlier, if finalp->string was NULL, then it would not be freed. The same is true
for finalp->simplep.
To summarize, each XDR routine is responsible for serializing, deserializing, and freeing memory. When
an XDR routine is called from callrpc() the serializing part is used. When called from svc_getargs() the
deserializer is used. And when called from svc_freeargs() the memory deallocator is used. When building