这是最终的解决方案。请注意,这是一个 C 版本。
使用静态可重新分配缓冲区。下面以我们为例。
xdr_copy.h
#define XDR_COPY( T, d, s ) xdr_copy_(( xdrproc_t )xdr_##T, ( char* )d, ( const char* )s, sizeof( T ))
extern bool_t xdr_copy( xdrproc_t proc, char* d, const char* s ) ;
extern bool_t xdr_copy_( xdrproc_t proc, char* d, const char* s, const unsigned size ) ;
xdr_copy.c
... /* removing all #includes for clarity */
#define XDR_BUFFER_SIZE ( 100 * 1024 )
#define XDR_BUFFER_DELTA ( 10 * 1024 )
static char* xdr_buffer = NULL ;
static unsigned xdr_buffer_size = 0 ;
static char* xdr_buffer_realloc( const unsigned delta )
{
char* rv = realloc( xdr_buffer, xdr_buffer_size + delta ) ;
if ( rv )
{
xdr_buffer_size += delta ;
xdr_buffer = rv ;
}
return rv ;
}
static char* get_xdr_buffer()
{
if ( !xdr_buffer )
xdr_buffer = xdr_buffer_realloc( XDR_BUFFER_SIZE ) ;
return xdr_buffer ;
}
bool_t xdr_copy( xdrproc_t proc, char* d, const char* s )
{
XDR x ;
char* buffer = get_xdr_buffer() ;
while ( buffer )
{
xdrmem_create( &x, buffer, xdr_buffer_size, XDR_ENCODE ) ;
if (( *proc )( &x, ( caddr_t* )s ))
{
xdr_destroy( &x ) ;
xdrmem_create( &x, buffer, xdr_buffer_size, XDR_DECODE ) ;
( *proc )( &x, ( caddr_t* )d ) ;
break ;
}
else
{
buffer = xdr_buffer_realloc( XDR_BUFFER_DELTA ) ;
xdr_destroy( &x ) ;
}
}
if ( buffer )
{
xdr_destroy( &x ) ;
return 1 ;
}
else
return 0 ;
}
bool_t xdr_copy_( xdrproc_t proc, char* d, const char* s, const unsigned size )
{
memset( d, 0, size ) ;
return xdr_copy( proc, d, s ) ;
}
例子
MyRPCArgs copy ;
if ( !XDR_COPY( MyRPCArgs, ©, source_ptr ))
... /* report memory allocation issue */