add memory pool for positions

This commit is contained in:
2021-11-12 18:33:39 +01:00
parent 4442901b58
commit bd7d9f8551
10 changed files with 41 additions and 18 deletions

View File

@@ -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

10
TODO.md
View File

@@ -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 ?

View File

@@ -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

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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);

View File

@@ -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();

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -17,6 +17,7 @@
#include <stdint.h>
#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