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

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