From 21330af5681c1409ae73f598a9636ad644f1ffc8 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Fri, 18 Nov 2011 18:59:59 -0800 Subject: [PATCH] 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. --- xwords4/common/model.c | 27 ++++++++++++++++++--------- xwords4/common/modelp.h | 11 +++++++---- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/xwords4/common/model.c b/xwords4/common/model.c index 4745f414d..f6fc9fb49 100644 --- a/xwords4/common/model.c +++ b/xwords4/common/model.c @@ -1,6 +1,7 @@ /* -*- 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 * 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, 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 ) { XP_MEMSET( result, 0, sizeof(*result) ); MPASSIGN(result->vol.mpool, mpool); @@ -247,7 +250,7 @@ model_init( ModelCtxt* model, XP_U16 nCols, XP_U16 nRows ) XP_ASSERT( model != NULL ); 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->nRows = nRows; @@ -689,17 +692,23 @@ model_getCellOwner( ModelCtxt* model, XP_U16 col, XP_U16 row ) static void setModelTileRaw( ModelCtxt* model, XP_U16 col, XP_U16 row, CellTile tile ) { - XP_ASSERT( col < MAX_COLS ); - XP_ASSERT( row < MAX_ROWS ); - model->tiles[col][row] = tile; + XP_ASSERT( col < model->nCols ); + XP_ASSERT( row < model->nRows ); + model->tiles[(row*model->nCols) + col] = tile; } /* model_setTile */ static CellTile getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row ) { - XP_ASSERT( col < MAX_COLS ); - XP_ASSERT( row < MAX_ROWS ); - return model->tiles[col][row]; + CellTile tile; + XP_U16 nCols = model->nCols; + XP_ASSERT( model->nRows == nCols ); + if ( col < nCols && row < nCols ) { + tile = model->tiles[(row*nCols) + col]; + } else { + tile = TILE_EMPTY_BIT; + } + return tile; } /* getModelTileRaw */ static void diff --git a/xwords4/common/modelp.h b/xwords4/common/modelp.h index 2d52f4c11..dfcc5afe8 100644 --- a/xwords4/common/modelp.h +++ b/xwords4/common/modelp.h @@ -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 * modify it under the terms of the GNU General Public License @@ -70,16 +71,18 @@ struct ModelCtxt { ModelVolatiles vol; - CellTile tiles[MAX_COLS][MAX_ROWS]; - PlayerCtxt players[MAX_NUM_PLAYERS]; XP_U16 nPlayers; XP_U16 nCols; XP_U16 nRows; XP_U16 nBonuses; XWBonusType* bonuses; + + CellTile tiles[]; }; +#define TILES_SIZE(m,nc) ((nc) * (nc) * sizeof((m)->tiles[0])) + void invalidateScore( ModelCtxt* model, XP_S16 player ); XP_Bool tilesInLine( ModelCtxt* model, XP_S16 turn, XP_Bool* isHorizontal ); void normalizeMoves( ModelCtxt* model, XP_S16 turn,