From e314daa383a3fa4110e6eb1918ad270d3dd06a37 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Mon, 28 Jun 2010 16:56:09 +0200 Subject: [PATCH] board_resize(), board_copy(): Factor out board_alloc() --- board.c | 157 ++++++++++++++++++++++------------------------------------------ 1 file changed, 54 insertions(+), 103 deletions(-) diff --git a/board.c b/board.c index 479a098..68aabc0 100644 --- a/board.c +++ b/board.c @@ -56,108 +56,11 @@ board_init(void) return b; } -struct board * -board_copy(struct board *b2, struct board *b1) +static size_t +board_alloc(struct board *board) { - memcpy(b2, b1, sizeof(struct board)); - - int bsize = board_size2(b2) * sizeof(*b2->b); - int gsize = board_size2(b2) * sizeof(*b2->g); - int fsize = board_size2(b2) * sizeof(*b2->f); - int nsize = board_size2(b2) * sizeof(*b2->n); - int psize = board_size2(b2) * sizeof(*b2->p); - int hsize = board_size2(b2) * 2 * sizeof(*b2->h); - int gisize = board_size2(b2) * sizeof(*b2->gi); -#ifdef WANT_BOARD_C - int csize = board_size2(b2) * sizeof(*b2->c); -#else - int csize = 0; -#endif -#ifdef BOARD_SPATHASH - int ssize = board_size2(b2) * sizeof(*b2->spathash); -#else - int ssize = 0; -#endif -#ifdef BOARD_PAT3 - int p3size = board_size2(b2) * sizeof(*b2->pat3); -#else - int p3size = 0; -#endif -#ifdef BOARD_TRAITS - int tsize = board_size2(b2) * sizeof(*b2->t); - int tqsize = board_size2(b2) * sizeof(*b2->t); -#else - int tsize = 0; - int tqsize = 0; -#endif -#ifdef BOARD_GAMMA - int pbsize = board_size2(b2) * sizeof(*b2->prob[0].items); - int rowpbsize = board_size(b2) * sizeof(*b2->prob[0].rowtotals); -#else - int pbsize = 0; - int rowpbsize = 0; -#endif - int cdsize = board_size2(b2) * sizeof(*b2->coord); - void *x = malloc2(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize); - memcpy(x, b1->b, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize); - b2->b = x; x += bsize; - b2->g = x; x += gsize; - b2->f = x; x += fsize; - b2->p = x; x += psize; - b2->n = x; x += nsize; - b2->h = x; x += hsize; - b2->gi = x; x += gisize; -#ifdef WANT_BOARD_C - b2->c = x; x += csize; -#endif -#ifdef BOARD_SPATHASH - b2->spathash = x; x += ssize; -#endif -#ifdef BOARD_PAT3 - b2->pat3 = x; x += p3size; -#endif -#ifdef BOARD_TRAITS - b2->t = x; x += tsize; - b2->tq = x; x += tqsize; -#endif -#ifdef BOARD_GAMMA - b2->prob[0].items = x; x += pbsize; - b2->prob[1].items = x; x += pbsize; - b2->prob[0].rowtotals = x; x += rowpbsize; - b2->prob[1].rowtotals = x; x += rowpbsize; -#endif - b2->coord = x; x += cdsize; - - return b2; -} - -void -board_done_noalloc(struct board *board) -{ - if (board->b) free(board->b); -} - -void -board_done(struct board *board) -{ - board_done_noalloc(board); - free(board); -} - -void -board_resize(struct board *board, int size) -{ -#ifdef BOARD_SIZE - assert(board_size(board) == size + 2); -#endif - board->size = size + 2 /* S_OFFBOARD margin */; - board->size2 = board_size(board) * board_size(board); - - board->bits2 = 1; - while ((1 << board->bits2) < board->size2) board->bits2++; - - if (board->b) - free(board->b); + /* We do not allocate the board structure itself but we allocate + * all the arrays with board contents. */ int bsize = board_size2(board) * sizeof(*board->b); int gsize = board_size2(board) * sizeof(*board->g); @@ -196,8 +99,11 @@ board_resize(struct board *board, int size) int rowpbsize = 0; #endif int cdsize = board_size2(board) * sizeof(*board->coord); - void *x = malloc2(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize); - memset(x, 0, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize); + + size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize; + void *x = malloc2(size); + + /* board->b must come first */ board->b = x; x += bsize; board->g = x; x += gsize; board->f = x; x += fsize; @@ -225,6 +131,51 @@ board_resize(struct board *board, int size) board->prob[1].rowtotals = x; x += rowpbsize; #endif board->coord = x; x += cdsize; + + return size; +} + +struct board * +board_copy(struct board *b2, struct board *b1) +{ + memcpy(b2, b1, sizeof(struct board)); + + size_t size = board_alloc(b2); + memcpy(b2->b, b1->b, size); + + return b2; +} + +void +board_done_noalloc(struct board *board) +{ + if (board->b) free(board->b); +} + +void +board_done(struct board *board) +{ + board_done_noalloc(board); + free(board); +} + +void +board_resize(struct board *board, int size) +{ +#ifdef BOARD_SIZE + assert(board_size(board) == size + 2); +#endif + board->size = size + 2 /* S_OFFBOARD margin */; + board->size2 = board_size(board) * board_size(board); + + board->bits2 = 1; + while ((1 << board->bits2) < board->size2) board->bits2++; + + if (board->b) + free(board->b); + + size_t asize = board_alloc(board); + memset(board->b, 0, asize); } void -- 2.11.4.GIT