add stream_copyBits, and use it to hash the move stack -- commented

out via conditional compile as it still doesn't work in all cases.
This commit is contained in:
Eric House 2012-05-22 19:24:54 -07:00
parent d1869a233c
commit 60e1d50f43
6 changed files with 58 additions and 33 deletions

View file

@ -21,6 +21,7 @@
#ifdef ENABLE_LOGGING
#include "dbgutil.h"
#include "strutils.h"
#define CASESTR(s) case s: return #s
@ -76,17 +77,17 @@ BoardObjectType_2str( BoardObjectType obj )
#ifdef DEBUG
void
dbg_logstream( XWStreamCtxt* stream, const char* func, int line )
dbg_logstream( const XWStreamCtxt* stream, const char* func, int line )
{
if ( !!stream ) {
XWStreamPos pos = stream_getPos( stream, POS_READ );
XP_U16 len = stream_getSize( stream );
XP_UCHAR buf[len+1];
stream_getBytes( stream, buf, len );
buf[len] = '\0';
XP_LOGF( "stream %p at pos %lx from line %d of func %s: \"%s\"", stream,
pos, line, func, buf );
(void)stream_setPos( stream, POS_READ, pos );
XP_U16 len = 0;
XWStreamPos end = stream_getPos( stream, POS_WRITE );
stream_copyBits( stream, 0, end, NULL, &len );
XP_U8 buf[len];
stream_copyBits( stream, 0, end, buf, &len );
char comment[128];
XP_SNPRINTF( comment, VSIZE(comment), "%s line %d", func, line );
LOG_HEX( buf, len, comment );
} else {
XP_LOGF( "stream from line %d of func %s is null",
line, func );

View file

@ -27,7 +27,7 @@ const char* DrawFocusState_2str( DrawFocusState dfs );
const char* BoardObjectType_2str( BoardObjectType dfs );
# ifdef DEBUG
void dbg_logstream( XWStreamCtxt* stream, const char* func, int line );
void dbg_logstream( const XWStreamCtxt* stream, const char* func, int line );
# define XP_LOGSTREAM( s ) \
dbg_logstream( s, __func__, __LINE__ )
# else

View file

@ -171,6 +171,28 @@ mem_stream_getBits( XWStreamCtxt* p_sctx, XP_U16 nBits )
return result;
} /* stream_getBits */
#if defined HASH_STREAM || defined DEBUG
static void
mem_stream_copyBits( const XWStreamCtxt* p_sctx, XWStreamPos XP_UNUSED(startPos),
XWStreamPos endPos, XP_U8* buf, XP_U16* lenp )
{
MemStreamCtxt* stream = (MemStreamCtxt*)p_sctx;
XP_U16 len = BYTE_PART(endPos);
XP_Bool hasBits = 0 != BIT_PART(endPos);
if ( hasBits ) {
++len;
}
if ( !!buf && len <= *lenp ) {
XP_ASSERT( len <= stream->nBytesAllocated );
XP_MEMCPY( buf, stream->buf, len );
if ( hasBits ) {
buf[len-1] &= 1 << BIT_PART(endPos);
}
}
*lenp = len;
}
#endif
static void
mem_stream_putBytes( XWStreamCtxt* p_sctx, const void* whence,
XP_U16 count )
@ -441,6 +463,9 @@ make_vtable( MemStreamCtxt* stream )
SET_VTABLE_ENTRY( vtable, stream_getU16, mem );
SET_VTABLE_ENTRY( vtable, stream_getU32, mem );
SET_VTABLE_ENTRY( vtable, stream_getBits, mem );
#if defined HASH_STREAM || defined DEBUG
SET_VTABLE_ENTRY( vtable, stream_copyBits, mem );
#endif
SET_VTABLE_ENTRY( vtable, stream_putU8, mem );
SET_VTABLE_ENTRY( vtable, stream_putBytes, mem );

View file

@ -1,6 +1,6 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/* -*- compile-command: "cd ../linux && make -j3 MEMDEBUG=TRUE"; -*- */
/*
* Copyright 2001, 2006 by Eric House (xwords@eehouse.org). All rights
* Copyright 2001, 2006-2012 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
@ -25,6 +25,7 @@
#include "movestak.h"
#include "memstream.h"
#include "strutils.h"
#include "dbgutil.h"
/* HASH_STREAM: It should be possible to hash the move stack by simply hashing
the stream from the beginning to the top of the undo stack (excluding
@ -33,7 +34,6 @@
have a XWStreamPos that corresponds to the undo-top and so I can't figure
out the length. Hashing that includes the redo part of the stack doesn't
work once there's been undo activity. (Not sure why...) */
// #define HASH_STREAM
#ifdef CPLUS
extern "C" {
@ -67,20 +67,6 @@ stack_init( StackCtxt* stack )
} /* stack_init */
#ifdef STREAM_VERS_BIGBOARD
#ifdef HASH_STREAM
static XP_U16
figureStackSize( const StackCtxt* stack )
{
XWStreamCtxt* data = stack->data;
XWStreamPos oldReadPos = stream_setPos( data, POS_READ, START_OF_STREAM );
XWStreamPos oldWritePos = stream_setPos( data, POS_WRITE, stack->cachedPos );
XP_U16 len = stream_getSize( data );
(void)stream_setPos( data, POS_READ, oldReadPos );
(void)stream_setPos( data, POS_WRITE, oldWritePos );
return len;
}
#endif
static XP_U32
augmentHash( XP_U32 hash, const XP_U8* ptr, XP_U16 len )
{
@ -138,10 +124,12 @@ stack_getHash( StackCtxt* stack )
{
XP_U32 hash;
#ifdef HASH_STREAM
XP_U16 len = figureStackSize( stack );
const XP_U8* ptr = stream_getPtr( stack->data );
LOG_HEX( ptr, len, __func__ );
hash = augmentHash( 0L, ptr, len );
XP_U16 len = 0;
stream_copyBits( stack->data, 0, stack->top, NULL, &len );
XP_U8 buf[len];
stream_copyBits( stack->data, 0, stack->top, buf, &len );
LOG_HEX( buf, len, __func__ );
hash = augmentHash( 0L, buf, len );
#else
XP_U16 nn, nEntries = stack->nEntries;
hash = 0L;
@ -312,7 +300,7 @@ pushEntry( StackCtxt* stack, const StackEntry* entry )
++stack->nEntries;
stack->highWaterMark = stack->nEntries;
stack->top = stream_setPos( stream, POS_WRITE, oldLoc );
// XP_LOGF( "after %s size now %d", __func__, figureStackSize( stack ) );
XP_LOGSTREAM( stack->data );
} /* pushEntry */
static void
@ -485,7 +473,7 @@ stack_popEntry( StackCtxt* stack, StackEntry* entry )
setCacheReadyFor( stack, nn ); /* set cachedPos by side-effect */
stack->top = stack->cachedPos;
}
//XP_LOGF( "after %s size now %d", __func__, figureStackSize( stack ) );
XP_LOGSTREAM( stack->data );
return found;
} /* stack_popEntry */

View file

@ -48,6 +48,10 @@ typedef struct StreamCtxVTable {
XP_U16 (*m_stream_getU16)( XWStreamCtxt* dctx );
XP_U32 (*m_stream_getU32)( XWStreamCtxt* dctx );
XP_U32 (*m_stream_getBits)( XWStreamCtxt* dctx, XP_U16 nBits );
#if defined HASH_STREAM || defined DEBUG
void (*m_stream_copyBits)( const XWStreamCtxt* dctx, XWStreamPos startPos,
XWStreamPos endPos, XP_U8* buf, XP_U16* len );
#endif
void (*m_stream_putU8)( XWStreamCtxt* dctx, XP_U8 byt );
void (*m_stream_putBytes)( XWStreamCtxt* dctx, const void* whence,
@ -109,6 +113,11 @@ struct XWStreamCtxt {
#define stream_getBits(sc, n) \
(sc)->vtable->m_stream_getBits((sc), (n))
#if defined HASH_STREAM || defined DEBUG
#define stream_copyBits(sc, s, e, b, l) \
(sc)->vtable->m_stream_copyBits((sc), (s), (e), (b), (l))
#endif
#define stream_putU8(sc, b) \
(sc)->vtable->m_stream_putU8((sc), (b))

View file

@ -94,6 +94,8 @@ DEFINES += -DTEXT_MODEL
DEFINES += -DXWFEATURE_WALKDICT
DEFINES += -DXWFEATURE_WALKDICT_FILTER
DEFINES += -DXWFEATURE_DICTSANITY
# DEFINES += -DHASH_STREAM
# MAX_ROWS controls STREAM_VERS_BIGBOARD and with it move hashing
DEFINES += -DMAX_ROWS=32