我有一个来自电线的RPC结构T_Struct。我想复制它,但我不想编写一个单独的函数来处理其成员的所有结构、分配和数组(特别是我必须对其他结构的音调做同样的事情)。
既然我已经有了一种解码、编码和免费的方法,那么有这样的东西有意义吗?
void copy_T_Struct( T_Struct* destination, T_Struct* source )
{
XDR xdr ;
/* Is there a way I can know the size of the buffer for the struct? */
char buffer[ 10240 ] ;
xdrmem_create( &xdr, buffer, sizeof( buffer ), XDR_ENCODE ) ;
( *xdr_T_Struct )( &xdr, source ) ; /* serialize to buffer */
xdr.x_op = XDR_DECODE ;
memset( destination, 0, sizeof( *destination )) ; /* without it I see segfault */
( *xdr_T_Struct )( &xdr, destination ) ; /* serialize back to T_Struct */
xdr_destroy( &xdr ) ;
}我知道到最后,我也可以打电话给xdr_free((xdrproc_t)xdr_T_Struct, (char *)destination ) ;
发布于 2014-11-20 03:53:43
这是最后的解决办法。注意,这是一个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 */https://stackoverflow.com/questions/27000237
复制相似问题