From bd7d9f8551eebbdc6f5113632051c4bb638ff0f5 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Fri, 12 Nov 2021 18:33:39 +0100 Subject: [PATCH] add memory pool for positions --- Makefile | 2 +- TODO.md | 10 ++++++++-- make.deps | 4 ++-- src/eval.c | 3 ++- src/fen.c | 3 ++- src/move.c | 5 +++-- src/move.h | 3 ++- src/piece.c | 3 ++- src/position.c | 21 +++++++++++++++------ src/position.h | 5 ++++- 10 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 176ef4a..2991294 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ CFLAGS += -Wmissing-declarations ##################################### DEBUG flags CFLAGS += -DDEBUG # global -#CFLAGS += -DDEBUG_POOL # memory pools management +CFLAGS += -DDEBUG_POOL # memory pools management CFLAGS += -DDEBUG_FEN # FEN decoding CFLAGS += -DDEBUG_MOVE # move generation CFLAGS += -DDEBUG_EVAL # eval functions diff --git a/TODO.md b/TODO.md index 4b9ed88..246dd2c 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,13 @@ ### NEXT STEPS -### IDEAS -- `move_gen()`: `doit` is maybe redundant with test on current turn. +### IMPORTANT +- memory: plan for total memory release (pool, etc...) to please valgrind. + - not so easy: need to keep track of allocated blocks, **and** to understand that no object is in use. +- move.c, piece.c: function to remove an element from list. +- position.c: duplicate fully a position (including board & piece list) + - should move list be duplicated ? ### MISC +- `move_gen()`: `doit` is maybe redundant with test on current turn. +- should board be included in position ? diff --git a/make.deps b/make.deps index 7cd9b0b..5fed552 100644 --- a/make.deps +++ b/make.deps @@ -5,9 +5,9 @@ ./obj/fen.o:: src/fen.c src/debug.h src/chessdefs.h src/bits.h src/position.h \ src/board.h src/piece.h src/list.h src/pool.h src/fen.h ./obj/move.o:: src/move.c src/chessdefs.h src/bits.h src/debug.h src/piece.h \ - src/board.h src/position.h src/list.h src/pool.h src/move.h + src/board.h src/position.h src/pool.h src/list.h src/move.h ./obj/piece.o:: src/piece.c src/chessdefs.h src/bits.h src/debug.h src/piece.h \ - src/board.h src/position.h src/list.h src/pool.h + src/board.h src/position.h src/pool.h src/list.h ./obj/pool.o:: src/pool.c src/list.h src/pool.h src/debug.h ./obj/position.o:: src/position.c src/chessdefs.h src/bits.h src/debug.h \ src/position.h src/board.h src/piece.h src/list.h src/pool.h src/fen.h diff --git a/src/eval.c b/src/eval.c index 93bb65e..60f25cd 100644 --- a/src/eval.c +++ b/src/eval.c @@ -86,7 +86,8 @@ int main(int ac, char**av) debug_init(2); piece_pool_init(); moves_pool_init(); - pos = pos_create(); + pos_pool_init(); + pos = pos_get(); if (ac == 1) { pos_startpos(pos); diff --git a/src/fen.c b/src/fen.c index 3f814b7..3f730a9 100644 --- a/src/fen.c +++ b/src/fen.c @@ -167,7 +167,8 @@ int main(int ac, char**av) debug_init(5); piece_pool_init(); - pos = pos_create(); + pos_pool_init(); + pos = pos_get(); if (ac == 1) { pos_startpos(pos); } else { diff --git a/src/move.c b/src/move.c index b26e554..3593237 100644 --- a/src/move.c +++ b/src/move.c @@ -48,7 +48,7 @@ static struct can_castle { pool_t *moves_pool_init() { if (!moves_pool) - moves_pool = pool_init("moves", 128, sizeof(piece_list_t)); + moves_pool = pool_init("moves", 128, sizeof(move_t)); return moves_pool; } @@ -481,7 +481,8 @@ int main(int ac, char**av) debug_init(1); piece_pool_init(); moves_pool_init(); - pos = pos_create(); + pos_pool_init(); + pos = pos_get(); if (ac == 1) { pos_startpos(pos); diff --git a/src/move.h b/src/move.h index 7302cce..0062193 100644 --- a/src/move.h +++ b/src/move.h @@ -47,7 +47,8 @@ typedef struct move_s { piece_t taken; /* removed piece */ piece_t promotion; /* promoted piece */ move_flags_t flags; - struct list_head list; + struct list_head list; /* next move */ + struct pos_t *pos; /* resulting position */ } move_t; pool_t *moves_pool_init(); diff --git a/src/piece.c b/src/piece.c index ce62524..31682ba 100644 --- a/src/piece.c +++ b/src/piece.c @@ -89,7 +89,8 @@ int main(int ac, char**av) pos_t *pos; debug_init(5); - pos = pos_create(); + pos_pool_init(); + pos = pos_get(); piece_pool_init(); if (ac == 1) { diff --git a/src/position.c b/src/position.c index ec4e037..58db82b 100644 --- a/src/position.c +++ b/src/position.c @@ -22,6 +22,8 @@ #include "fen.h" #include "piece.h" +static pool_t *pos_pool; + #define BYTE_PRINT "%c%c%c%c%c%c%c%c" #define BYTE2BIN(b) ((b) & 0x01 ? '1' : '0'), \ ((b) & 0x02 ? '1' : '0'), \ @@ -112,13 +114,13 @@ void pos_print(pos_t *pos) popcount64(pos->controlled[BLACK])); printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE], pos->mobility[BLACK]); - printf("Bitbords occupied :\n"); + printf("Bitboards occupied :\n"); bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]); - printf("Bitbords controlled :\n"); + printf("Bitboards controlled :\n"); bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]); } -pos_t *pos_init(pos_t *pos) +pos_t *pos_clear(pos_t *pos) { int file, rank; board_t *board = pos->board; @@ -161,15 +163,15 @@ pos_t *pos_startpos(pos_t *pos) return fen2pos(pos, startfen); } -pos_t *pos_create() +pos_t *pos_get() { - pos_t *pos = malloc(sizeof(pos_t)); + pos_t *pos = pool_get(pos_pool); if (pos) { //printf("sizeof(board)=%lu\n", sizeof (board_t)); pos->board = malloc(sizeof (board_t)*BOARDSIZE); //printf("board mem: %p-%p\n", pos->board, pos->board+sizeof (board_t)); if (pos->board) - pos_init(pos); + pos_clear(pos); else { free(pos); pos = NULL; @@ -177,3 +179,10 @@ pos_t *pos_create() } return pos; } + +pool_t *pos_pool_init() +{ + if (!pos_pool) + pos_pool = pool_init("positions", 128, sizeof(pos_t)); + return pos_pool; +} diff --git a/src/position.h b/src/position.h index fd04249..2b12fa7 100644 --- a/src/position.h +++ b/src/position.h @@ -17,6 +17,7 @@ #include #include "chessdefs.h" #include "board.h" +#include "pool.h" #include "list.h" typedef struct pos_s { @@ -40,8 +41,10 @@ void bitboard_print(bitboard_t bb); void bitboard_print2(bitboard_t bb1, bitboard_t bb2); void pos_pieces_print(pos_t *pos); void pos_print(pos_t *pos); -pos_t *pos_init(pos_t *pos); +pos_t *pos_clear(pos_t *pos); pos_t *pos_startpos(pos_t *pos); pos_t *pos_create(); +pool_t *pos_pool_init(); +pos_t *pos_get(); #endif