add stream_getPtr() and use to remove a couple of allocs, including

one per message sent.
This commit is contained in:
Andy2 2011-09-18 16:54:36 -07:00
parent b579bb0bca
commit 63893b8a0d
4 changed files with 20 additions and 16 deletions

View file

@ -2020,7 +2020,6 @@ send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
XP_U16 len = 0;
CommsAddrRec addr;
XWStreamCtxt* tmpStream;
XP_U8* buf;
comms_getAddr( comms, &addr );
tmpStream = mem_stream_make( MPPARM(comms->mpool)
@ -2101,22 +2100,17 @@ send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
}
len = stream_getSize( tmpStream );
buf = XP_MALLOC( comms->mpool, len );
if ( buf != NULL ) {
stream_getBytes( tmpStream, buf, len );
}
stream_destroy( tmpStream );
if ( buf != NULL ) {
if ( 0 < len ) {
XP_U16 result;
XP_LOGF( "%s: passing %d bytes to sendproc", __func__, len );
result = (*comms->procs.send)( buf, len, &addr,
comms->procs.closure );
result = (*comms->procs.send)( stream_getPtr(tmpStream), len,
&addr, comms->procs.closure );
success = result == len;
if ( success ) {
setHeartbeatTimer( comms );
}
}
XP_FREE( comms->mpool, buf );
stream_destroy( tmpStream );
}
return success;
} /* send_via_relay */

View file

@ -301,7 +301,7 @@ mem_stream_copyFromStream( XWStreamCtxt* p_sctx, XWStreamCtxt* src,
if ( nBytes < len ) {
len = nBytes;
}
stream_getBytes( src, buf, len );
stream_getBytes( src, buf, len );// fix to use stream_getPtr()?
stream_putBytes( p_sctx, buf, len );
nBytes -= len;
}
@ -338,6 +338,13 @@ mem_stream_getSize( const XWStreamCtxt* p_sctx )
return size;
} /* mem_stream_getSize */
static const XP_U8*
mem_stream_getPtr( const XWStreamCtxt* p_sctx )
{
MemStreamCtxt* stream = (MemStreamCtxt*)p_sctx;
return stream->buf;
} /* mem_stream_getPtr */
static XP_PlayerAddr
mem_stream_getAddress( XWStreamCtxt* p_sctx )
{
@ -451,6 +458,7 @@ make_vtable( MemStreamCtxt* stream )
SET_VTABLE_ENTRY( vtable, stream_close, mem );
SET_VTABLE_ENTRY( vtable, stream_getSize, mem );
SET_VTABLE_ENTRY( vtable, stream_getPtr, mem );
SET_VTABLE_ENTRY( vtable, stream_getAddress, mem );
SET_VTABLE_ENTRY( vtable, stream_setAddress, mem );

View file

@ -841,14 +841,11 @@ showPrevScore( ServerCtxt* server )
if ( !!server->nv.prevMoveStream ) {
XWStreamCtxt* prevStream = server->nv.prevMoveStream;
XP_U16 len = stream_getSize( prevStream );
XP_UCHAR* buf = XP_MALLOC( server->mpool, len );
stream_getBytes( prevStream, buf, len );
stream_destroy( prevStream );
server->nv.prevMoveStream = NULL;
stream_putBytes( stream, buf, len );
XP_FREE( server->mpool, buf );
stream_putBytes( stream, stream_getPtr( prevStream ), len );
stream_destroy( prevStream );
}
(void)util_userQuery( util, QUERY_ROBOT_MOVE, stream );

View file

@ -69,6 +69,8 @@ typedef struct StreamCtxVTable {
void (*m_stream_close)( XWStreamCtxt* dctx );
XP_U16 (*m_stream_getSize)( const XWStreamCtxt* dctx );
const XP_U8* (*m_stream_getPtr)( const XWStreamCtxt* dctx );
/* void (*m_stream_makeReturnAddr)( XWStreamCtxt* dctx, XP_PlayerAddr* addr, */
/* XP_U16* addrLen ); */
@ -143,6 +145,9 @@ struct XWStreamCtxt {
#define stream_getSize(sc) \
(sc)->vtable->m_stream_getSize((sc))
#define stream_getPtr(sc) \
(sc)->vtable->m_stream_getPtr((sc))
#define stream_makeReturnAddr(sc,addr,len) \
(sc)->vtable->m_stream_makeReturnAddr((sc),(addr),(len))