make tiles array variable sizes. 32*32*2 is too many bytes to use

when the typical board at 15*15 requires less than 1/4 of that.
This commit is contained in:
Andy2 2011-11-18 18:59:59 -08:00
parent cc0105e14c
commit 21330af568
2 changed files with 25 additions and 13 deletions

View file

@ -1,6 +1,7 @@
/* -*- compile-command: "cd ../linux && make -j3 MEMDEBUG=TRUE"; -*- */ /* -*- compile-command: "cd ../linux && make -j3 MEMDEBUG=TRUE"; -*- */
/* /*
* Copyright 2000-2009 by Eric House (xwords@eehouse.org). All rights reserved. * Copyright 2000-2011 by Eric House (xwords@eehouse.org). All rights
* reserved.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -88,7 +89,9 @@ model_make( MPFORMAL DictionaryCtxt* dict,
const PlayerDicts* dicts, XW_UtilCtxt* util, XP_U16 nCols, const PlayerDicts* dicts, XW_UtilCtxt* util, XP_U16 nCols,
XP_U16 nRows ) XP_U16 nRows )
{ {
ModelCtxt* result = (ModelCtxt*)XP_MALLOC( mpool, sizeof( *result ) ); ModelCtxt* result;
XP_U16 size = sizeof(*result) + TILES_SIZE(result, nCols);
result = (ModelCtxt*)XP_MALLOC( mpool, size );
if ( result != NULL ) { if ( result != NULL ) {
XP_MEMSET( result, 0, sizeof(*result) ); XP_MEMSET( result, 0, sizeof(*result) );
MPASSIGN(result->vol.mpool, mpool); MPASSIGN(result->vol.mpool, mpool);
@ -247,7 +250,7 @@ model_init( ModelCtxt* model, XP_U16 nCols, XP_U16 nRows )
XP_ASSERT( model != NULL ); XP_ASSERT( model != NULL );
XP_MEMSET( model, 0, sizeof( *model ) ); XP_MEMSET( model, 0, sizeof( *model ) );
XP_MEMSET( &model->tiles, TILE_EMPTY_BIT, sizeof(model->tiles) ); XP_MEMSET( &model->tiles, TILE_EMPTY_BIT, TILES_SIZE(model, nCols) );
model->nCols = nCols; model->nCols = nCols;
model->nRows = nRows; model->nRows = nRows;
@ -689,17 +692,23 @@ model_getCellOwner( ModelCtxt* model, XP_U16 col, XP_U16 row )
static void static void
setModelTileRaw( ModelCtxt* model, XP_U16 col, XP_U16 row, CellTile tile ) setModelTileRaw( ModelCtxt* model, XP_U16 col, XP_U16 row, CellTile tile )
{ {
XP_ASSERT( col < MAX_COLS ); XP_ASSERT( col < model->nCols );
XP_ASSERT( row < MAX_ROWS ); XP_ASSERT( row < model->nRows );
model->tiles[col][row] = tile; model->tiles[(row*model->nCols) + col] = tile;
} /* model_setTile */ } /* model_setTile */
static CellTile static CellTile
getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row ) getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row )
{ {
XP_ASSERT( col < MAX_COLS ); CellTile tile;
XP_ASSERT( row < MAX_ROWS ); XP_U16 nCols = model->nCols;
return model->tiles[col][row]; XP_ASSERT( model->nRows == nCols );
if ( col < nCols && row < nCols ) {
tile = model->tiles[(row*nCols) + col];
} else {
tile = TILE_EMPTY_BIT;
}
return tile;
} /* getModelTileRaw */ } /* getModelTileRaw */
static void static void

View file

@ -1,6 +1,7 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */ /* -*- compile-command: "cd ../linux && make -j3 MEMDEBUG=TRUE"; -*- */
/* /*
* Copyright 2000 by Eric House (xwords@eehouse.org). All rights reserved. * Copyright 2000 - 2011 by Eric House (xwords@eehouse.org). All rights
* reserved.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -70,16 +71,18 @@ struct ModelCtxt {
ModelVolatiles vol; ModelVolatiles vol;
CellTile tiles[MAX_COLS][MAX_ROWS];
PlayerCtxt players[MAX_NUM_PLAYERS]; PlayerCtxt players[MAX_NUM_PLAYERS];
XP_U16 nPlayers; XP_U16 nPlayers;
XP_U16 nCols; XP_U16 nCols;
XP_U16 nRows; XP_U16 nRows;
XP_U16 nBonuses; XP_U16 nBonuses;
XWBonusType* bonuses; XWBonusType* bonuses;
CellTile tiles[];
}; };
#define TILES_SIZE(m,nc) ((nc) * (nc) * sizeof((m)->tiles[0]))
void invalidateScore( ModelCtxt* model, XP_S16 player ); void invalidateScore( ModelCtxt* model, XP_S16 player );
XP_Bool tilesInLine( ModelCtxt* model, XP_S16 turn, XP_Bool* isHorizontal ); XP_Bool tilesInLine( ModelCtxt* model, XP_S16 turn, XP_Bool* isHorizontal );
void normalizeMoves( ModelCtxt* model, XP_S16 turn, void normalizeMoves( ModelCtxt* model, XP_S16 turn,