move from printf to logX() - unfinished.

This commit is contained in:
2021-11-03 12:39:23 +01:00
parent 45c01f59f7
commit 0e117bc2f9
9 changed files with 178 additions and 93 deletions

View File

@@ -1,12 +1,28 @@
# Makefile - GNU make only.
#
# Copyright (C) 2021 Bruno Raoult ("br")
# Licensed under the GNU General Public License v3.0 or later.
# Some rights reserved. See COPYING.
# * You should have received a copy of the GNU General Public License along with this
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
# #
BINDIR=./bin BINDIR := ./bin
SRCDIR=./src SRCDIR := ./src
OBJDIR := ./obj
DEPS := make.deps
SRC=$(wildcard $(SRCDIR)/*.c) SRC=$(wildcard $(SRCDIR)/*.c)
INC=$(wildcard $(SRCDIR)/*.h) INC=$(wildcard $(SRCDIR)/*.h)
SRC_S=$(notdir $(SRC))
BIN=fen pool piece move CC=gcc
.SECONDEXPANSION:
OBJ=$(addprefix $(OBJDIR)/,$(SRC_S:.c=.o))
BIN=fen pool piece move debug
CFLAGS += -std=gnu99 CFLAGS += -std=gnu99
CFLAGS += -g CFLAGS += -g
@@ -17,28 +33,55 @@ CFLAGS += -Wextra
#CFLAGS += -Werror #CFLAGS += -Werror
CFLAGS += -Wmissing-declarations CFLAGS += -Wmissing-declarations
all: clean $(BIN) ##################################### DEBUG flags
CFLAGS += -DDEBUG # global
CFLAGS += -DDEBUG_POOL # memory pools management
CFLAGS += -DDEBUG_FEN # FEN decoding
#CFLAGS += -DDEBUG_MAINSLEEP # sleep 1 sec within main loop (SIGINTR test)
#CFLAGS += -DDEBUG_EVAL2 # eval 2
#CFLAGS += -DDEBUG_EVAL3 # eval 3
#CFLAGS += -DDEBUG_MEM # malloc
all: $(OBJ) $(BIN)
@echo CFLAGS used: $(CFLAGS)
$(DEPS): $(SRC) $(INC)
@echo generating dependancies.
@$(CC) -MM $(SRC) > $@
@sed -i "s|\(.*\.o\):|${OBJDIR}/\0:|" $@
include $(DEPS)
.PHONY: clean .PHONY: clean
clean: clean:
rm -rf *.o core $(BIN) rm -rf $(OBJ) core $(BIN) $(DEPS)
fen: CFLAGS+=-DFENBIN #$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.c
fen: $(SRC) # @mkdir -p $(@D)
echo SRC=$(SRC) # $(CC) -c $(CFLAGS) -o $@ $<
$(CC) $(CFLAGS) $? -o $@ $(OBJDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(@D)
@echo compiling $@.
@$(CC) -c $(CFLAGS) -o $@ $<
pool: CFLAGS+=-DPOOLBIN #fen: CFLAGS+=-DBIN_$$@
pool: $(SRC) $(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
echo SRC=$(SRC) @echo compiling $@.
$(CC) $(CFLAGS) $? -o $@ @$(CC) -DBIN_$@ $(CFLAGS) $^ -o $@
piece: CFLAGS+=-DPIECEBIN #pool: CFLAGS+=-DPOOLBIN
piece: $(SRC) #pool: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
echo SRC=$(SRC) # $(CC) $(CFLAGS) $^ -o $@
$(CC) $(CFLAGS) $? -o $@
move: CFLAGS+=-DMOVEBIN # piece: CFLAGS+=-DPIECEBIN
move: $(SRC) # piece: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
echo SRC=$(SRC) # $(CC) $(CFLAGS) $^ -o $@
$(CC) $(CFLAGS) $? -o $@
# move: CFLAGS+=-DMOVEBIN
# move: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# $(CC) $(CFLAGS) $^ -o $@
# debug: CFLAGS+=-DDEBUGBIN
# debug: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# $(CC) $(CFLAGS) $^ -o $@

10
make.deps Normal file
View File

@@ -0,0 +1,10 @@
./obj/debug.o:: src/debug.c src/debug.h
./obj/fen.o:: src/fen.c src/debug.h src/chessdefs.h src/position.h src/board.h \
src/list.h src/fen.h src/piece.h src/pool.h
./obj/move.o:: src/move.c src/chessdefs.h src/piece.h src/board.h src/list.h \
src/position.h src/pool.h src/move.h
./obj/piece.o:: src/piece.c src/chessdefs.h src/piece.h src/board.h src/list.h \
src/position.h src/pool.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/position.h src/board.h \
src/list.h src/fen.h src/piece.h src/pool.h

View File

@@ -21,19 +21,27 @@
#define MILLISEC 1000000 /* milli sec in sec */ #define MILLISEC 1000000 /* milli sec in sec */
static int64_t timer_start; /* in nanosecond */ static int64_t timer_start; /* in nanosecond */
static uint32_t debug_level=0;
int clock_gettime(clockid_t clockid, struct timespec *tp); void debug_level_set(uint32_t level)
{
debug_level = level;;
void debug_init() log(0, "debug level set to %u\n", level);
}
void debug_init(uint32_t level)
{ {
struct timespec timer; struct timespec timer;
debug_level_set(level);
if (!clock_gettime(CLOCK_MONOTONIC, &timer)) { if (!clock_gettime(CLOCK_MONOTONIC, &timer)) {
timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec; timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec;
} }
else { else {
timer_start = 0; timer_start = 0;
} }
log("timer started.\n"); log(0, "timer started.\n");
} }
inline static int64_t timer_elapsed() inline static int64_t timer_elapsed()
@@ -51,9 +59,12 @@ inline static int64_t timer_elapsed()
* @src : source filename (or NULL) * @src : source filename (or NULL)
* @line : line number * @line : line number
*/ */
void debug(bool timestamp, uint32_t indent, const char *src, void debug(uint32_t level, bool timestamp, uint32_t indent, const char *src,
uint32_t line, const char *fmt, ...) uint32_t line, const char *fmt, ...)
{ {
if (level > debug_level)
return;
va_list ap; va_list ap;
if (indent) if (indent)
@@ -68,28 +79,30 @@ void debug(bool timestamp, uint32_t indent, const char *src,
if (src) { if (src) {
if (line) if (line)
printf("[%s:%u] ", src, line); printf("[%s:%u] ", src, line);
else { else
printf("[%s] ", src); printf("[%s] ", src);
}
} }
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintf(fmt, ap);
va_end(ap); va_end(ap);
} }
#ifdef DEBUGBIN #ifdef BIN_debug
#include <unistd.h> #include <unistd.h>
int main() int main()
{ {
int foo=1; int foo=1;
debug_init(); debug_init(5);
log("log=%d\n", foo++); log(0, "log0=%d\n", foo++);
log_i(1, "log_i=%d\n", foo++); log(1, "log1=%d\n", foo++);
log_i(2, "log_i=%d\n", foo++); log(2, "log2=%d\n", foo++);
log_it(3, "log_it=%d\n", foo++); log_i(2, "log_i 2=%d\n", foo++);
log_f("log_f=%d\n", foo++); log_i(5, "log_i 5=%d\n", foo++);
log_i(6, "log_i 6=%d\n", foo++);
log_it(4, "log_it 4=%d\n", foo++);
log_f(1, "log_f 5=%d\n", foo++);
} }
#endif #endif

View File

@@ -17,38 +17,50 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#ifdef DEBUG void debug_init(uint32_t level);
void debug_level_set(uint32_t level);
void debug_init(void); void debug_devel_set(uint32_t level);
void debug(uint32_t level, bool timestamp, uint32_t indent,
void debug(bool timestamp, uint32_t indent,
const char *src, uint32_t line, const char *, ...); const char *src, uint32_t line, const char *, ...);
/* format: func name, no indent, no timestamp #ifdef DEBUG
/* format: only printf
*/
#define log(level, fmt, args...) \
debug((level), false, 0, NULL, 0, fmt, ##args)
/* format: func name, no line number, indent, no timestamp
* foo:15 val=2 * foo:15 val=2
*/ */
#define log(fmt, args...) debug(false, 0, __func__, __LINE__, fmt, ##args) #define log_f(level, fmt, args...) \
debug((level), false, 0, __func__, 0, fmt, ##args)
/* format : func name, indent, no timestamp /* format : func name, indent, no timestamp
* foo:15 val=2 * foo:15 val=2
*/ */
#define log_i(i, fmt, args...) debug(false, (i), __func__, __LINE__, fmt, args) #define log_i(level, fmt, args...) \
debug((level), false, (level), __func__, __LINE__, fmt, args)
/* format : func name, indent, timestamp /* format : func name, indent, timestamp
* []foo:15 val=2 * []foo:15 val=2
*/ */
#define log_it(i, fmt, args...) debug(true, (i), __func__, __LINE__, fmt, args) #define log_it(level, fmt, args...) \
debug((level), true, (level), __func__, __LINE__, fmt, args)
/* format: file name, no indent, no timestamp /* format: file name, no indent, no timestamp
* foo:15 val=2 * foo:15 val=2
*
* #define log_f(level, fmt, args...) \
* debug((level), false, 0, __FILE__, __LINE__, fmt, args)
*/ */
#define log_f(fmt, args...) debug(false, 0, __FILE__, __LINE__, fmt, args)
#else #else
#define log(...) #define log(level, fmt, args...)
#define f #define log_i(...)
#define log_it(...)
#define log_f(...)
#endif /* DEBUG */ #endif /* DEBUG */
#endif /* DEBUG_H */ #endif /* DEBUG_H */

View File

@@ -17,6 +17,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "debug.h"
#include "chessdefs.h" #include "chessdefs.h"
#include "position.h" #include "position.h"
#include "board.h" #include "board.h"
@@ -77,14 +78,13 @@ pos_t *fen2pos(pos_t *pos, char *fen)
piece = KING; piece = KING;
//goto set_square; //goto set_square;
set_square: set_square:
/*printf("f=%d r=%d *p=%c piece=%c color=%d\n", # ifdef DEBUG_FEN
log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n",
file, rank, *p, cp, color); file, rank, *p, cp, color);
*/ # endif
piece |= color; piece |= color;
//board[SQ88(file, rank)]->piece = piece;
board[SQ88(file, rank)].piece = piece; board[SQ88(file, rank)].piece = piece;
piece_add(pos, piece, SQUARE(file, rank)); piece_add(pos, piece, SQUARE(file, rank));
//board[SQ88(file, rank)]->piece |= isupper(*p)? WHITE: BLACK;
file++; file++;
break; break;
case '/': case '/':
@@ -98,12 +98,14 @@ pos_t *fen2pos(pos_t *pos, char *fen)
} }
} }
} }
/*for (rank = 7; rank >= 0; --rank) { # ifdef DEBUG_FEN
for (rank = 7; rank >= 0; --rank) {
for (file = 0; file < 8; ++file) { for (file = 0; file < 8; ++file) {
printf("%2x ", board[SQ88(file, rank)]->piece); log(5, "%02x ", board[SQ88(file, rank)].piece);
} }
putchar('\n'); log(5, "\n");
}*/ }
# endif
/* 2) next move color /* 2) next move color
*/ */
@@ -150,16 +152,17 @@ pos_t *fen2pos(pos_t *pos, char *fen)
* 6) current move number * 6) current move number
*/ */
SKIP_BLANK(p); SKIP_BLANK(p);
//printf("pos=%d\n", (int)(p-fen)); log_i(5, "pos=%d\n", (int)(p-fen));
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove); sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
return pos; return pos;
} }
#ifdef FENBIN #ifdef BIN_fen
int main(int ac, char**av) int main(int ac, char**av)
{ {
pos_t *pos; pos_t *pos;
debug_init(3);
piece_pool_init(); piece_pool_init();
pos = pos_create(); pos = pos_create();
if (ac == 1) { if (ac == 1) {

View File

@@ -244,8 +244,6 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece)
int pseudo_moves_castle(pos_t *pos) int pseudo_moves_castle(pos_t *pos)
{ {
//piece_t piece = PIECE(ppiece->piece);
//square_t square = ppiece->square, new;
unsigned char color = pos->turn; unsigned char color = pos->turn;
board_t *board = pos->board; board_t *board = pos->board;
unsigned char rank1, castle_K, castle_Q; unsigned char rank1, castle_K, castle_Q;
@@ -302,8 +300,8 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece)
move_t *move; move_t *move;
int count = 0; int count = 0;
//printf("square=%#04x piece=%#04x color = %s\n", square, piece, color==WHITE? "white": "black"); /*printf("square=%#04x piece=%#04x color = %s\n", square, piece, color==WHITE? "white": "black");
/*printf("%s: pos:%p piece:%d [%s] at %#04x[%c%c]\n", __func__, pos, piece, printf("%s: pos:%p piece:%d [%s] at %#04x[%c%c]\n", __func__, pos, piece,
P_NAME(piece), P_NAME(piece),
square, square,
FILE2C(GET_F(square)), FILE2C(GET_F(square)),
@@ -373,17 +371,18 @@ int moves_get(pos_t *pos)
}; };
*/ */
#ifdef MOVEBIN #ifdef BIN_move
#include "fen.h" #include "fen.h"
#include "debug.h"
int main(int ac, char**av) int main(int ac, char**av)
{ {
pos_t *pos; pos_t *pos;
debug_init(1);
pos = pos_create(); pos = pos_create();
piece_pool_init(); piece_pool_init();
moves_pool_init(); moves_pool_init();
if (ac == 1) { if (ac == 1) {
pos_startpos(pos); pos_startpos(pos);
} else { } else {

View File

@@ -11,8 +11,8 @@
* *
*/ */
#ifndef ROOK_H #ifndef MOVE_H
#define ROOK_H #define MOVE_H
#include "chessdefs.h" #include "chessdefs.h"
#include "position.h" #include "position.h"
@@ -54,4 +54,4 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *piece);
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece); int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece);
int moves_get(pos_t *pos); int moves_get(pos_t *pos);
#endif #endif /* MODE_H */

View File

@@ -95,13 +95,11 @@ piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
return new; return new;
} }
#ifdef PIECEBIN #ifdef BIN_piece
#include "fen.h" #include "fen.h"
int main(int ac, char**av) int main(int ac, char**av)
{ {
//size_t i;
pos_t *pos; pos_t *pos;
//piece_list_t *plist;
pos = pos_create(); pos = pos_create();
piece_pool_init(); piece_pool_init();

View File

@@ -22,16 +22,16 @@
#include <stdlib.h> #include <stdlib.h>
#include "list.h" #include "list.h"
#include "pool.h" #include "pool.h"
#include "debug.h"
void pool_stats(pool_t *pool) void pool_stats(pool_t *pool)
{ {
if (pool) { if (pool) {
printf("[%s] pool [%p]: ", pool->name, (void *)pool); # ifdef DEBUG_POOL
printf("avail:%u ", pool->available); log_f(1, "[%s] pool [%p]: avail:%u alloc:%u grow:%u eltsize:%lu\n",
printf("alloc:%u ", pool->allocated); pool->name, (void *)pool, pool->available, pool->allocated,
printf("grow:%u ", pool->growsize); pool->growsize, pool->eltsize);
printf("eltsize:%lu ", pool->eltsize); # endif
printf("\n");
} }
} }
@@ -39,8 +39,10 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
{ {
pool_t *pool; pool_t *pool;
printf("%s: name=[%s] growsize=%u eltsize=%lu\n", # ifdef DEBUG_POOL
__func__, name, growsize, eltsize); log_f(1, "name=[%s] growsize=%u eltsize=%lu\n",
name, growsize, eltsize);
# endif
/* we need at least this space in struct */ /* we need at least this space in struct */
if (eltsize < sizeof (struct list_head)) if (eltsize < sizeof (struct list_head))
return NULL; return NULL;
@@ -57,15 +59,14 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
static uint32_t _pool_add(pool_t *pool, struct list_head *elt) static uint32_t _pool_add(pool_t *pool, struct list_head *elt)
{ {
/* # ifdef DEBUG_POOL
printf("%s: pool=%p &head=%p elt=%p off1=%lu off2=%lu\n", log_f(10, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n",
__func__,
(void *)pool, (void *)pool,
(void *)&pool->head, (void *)&pool->head,
(void *)elt, (void *)elt,
(void *)&pool->head-(void *)pool, (void *)&pool->head-(void *)pool,
offsetof(pool_t, head)); offsetof(pool_t, head));
*/ # endif
list_add(elt, &pool->head); list_add(elt, &pool->head);
return ++pool->available; return ++pool->available;
@@ -81,7 +82,6 @@ static struct list_head *_pool_get(pool_t *pool)
struct list_head *res = pool->head.next; struct list_head *res = pool->head.next;
pool->available--; pool->available--;
list_del(res); list_del(res);
// printf("%s: res=%p\n", __func__, (void *)res);
return res; return res;
} }
@@ -93,28 +93,34 @@ void *pool_get(pool_t *pool)
void *alloc = malloc(pool->eltsize * pool->growsize); void *alloc = malloc(pool->eltsize * pool->growsize);
void *cur; void *cur;
uint32_t i; uint32_t i;
printf("+++ %s [%s]: growing pool from %u to %u elements.\n", __func__, # ifdef DEBUG_POOL
log_f(2, "[%s]: growing pool from %u to %u elements.\n",
pool->name, pool->name,
pool->allocated, pool->allocated,
pool->allocated + pool->growsize); pool->allocated + pool->growsize);
# endif
if (!alloc) if (!alloc)
return NULL; return NULL;
//printf(" (old=%u)\n", pool->allocated); # ifdef DEBUG_POOL
log_f(5, " (old=%u)\n", pool->allocated);
# endif
pool->allocated += pool->growsize; pool->allocated += pool->growsize;
//printf(" (new=%u)\n", pool->allocated); # ifdef DEBUG_POOL
log_f(5, " (new=%u)\n", pool->allocated);
# endif
for (i = 0; i < pool->growsize; ++i) { for (i = 0; i < pool->growsize; ++i) {
cur = alloc + i * pool->eltsize; cur = alloc + i * pool->eltsize;
//printf("%s: alloc=%p cur=%p\n", __func__, alloc, cur); # ifdef DEBUG_POOL
log_f(5, "alloc=%p cur=%p\n", alloc, cur);
# endif
_pool_add(pool, (struct list_head *)cur); _pool_add(pool, (struct list_head *)cur);
} }
pool_stats(pool); pool_stats(pool);
} }
//printf("%s: returning %p pointer\n", __func__, res);
return _pool_get(pool); return _pool_get(pool);
} }
#ifdef POOLBIN #ifdef BIN_pool
struct d { struct d {
uint16_t data1; uint16_t data1;
char c; char c;
@@ -132,7 +138,8 @@ int main(int ac, char**av)
char ccur='z'; char ccur='z';
struct d *elt; struct d *elt;
printf("%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt), debug_init(3);
log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
sizeof(*elt), offsetof(struct d, list)); sizeof(*elt), offsetof(struct d, list));
if ((pool = pool_init("dummy", 3, sizeof(*elt)))) { if ((pool = pool_init("dummy", 3, sizeof(*elt)))) {
@@ -140,7 +147,7 @@ int main(int ac, char**av)
for (int cur=1; cur<ac; ++cur) { for (int cur=1; cur<ac; ++cur) {
total = atoi(av[cur]); total = atoi(av[cur]);
if (action == 0) { /* add elt to list */ if (action == 0) { /* add elt to list */
printf("adding %d elements\n", total); log_f(2, "adding %d elements\n", total);
for (int i = 0; i < total; ++i) { for (int i = 0; i < total; ++i) {
elt = pool_get(pool); elt = pool_get(pool);
elt->data1 = icur++; elt->data1 = icur++;
@@ -150,7 +157,7 @@ int main(int ac, char**av)
pool_stats(pool); pool_stats(pool);
action = 1; action = 1;
} else { /* remove one elt from list */ } else { /* remove one elt from list */
printf("deleting %d elements\n", total); log_f(2, "deleting %d elements\n", total);
for (int i = 0; i < total; ++i) { for (int i = 0; i < total; ++i) {
if (!list_empty(&head)) { if (!list_empty(&head)) {
elt = list_last_entry(&head, struct d, list); elt = list_last_entry(&head, struct d, list);