DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with Remote Procedure Calls (RPC)

An example of performing memory allocation with XDR

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.


NOTE: This is true for most XDR routines. The indirection is necessary because these routines often allocate memory.

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);

To have XDR to do the allocation, this routine must be rewritten 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 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, if finalp->string were NULL, then it would not be freed. The same is true for finalp->simplep.

To summarize:

When building simple programs like those given as examples in this section, a programmer does not have to worry about the three modes.
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005