minimax

minimax.git
git clone git://git.lenczewski.org/minimax.git
Log | Files | Refs

commit 682bf4587b62811e55af563bd35570956c9953cd
parent 8f973f3838bff33f539d02acae5b4ed7c05e5113
Author: MikoĊ‚aj Lenczewski <mblenczewski@gmail.com>
Date:   Sun, 12 Jan 2025 00:36:14 +0000

Port to windows

Diffstat:
Mminimax.c | 40+++++++++++++++++++++++++---------------
1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/minimax.c b/minimax.c @@ -16,7 +16,6 @@ # define _DEFAULT_SOURCE 1 #endif -#include <alloca.h> #include <assert.h> #include <stdalign.h> #include <stddef.h> @@ -27,7 +26,14 @@ #include <math.h> -#include <sys/mman.h> +#if defined(__linux__) +# include <alloca.h> +# include <sys/mman.h> +# define alloca_impl alloca +#elif defined(_WIN32) +# include <malloc.h> +# define alloca_impl _alloca +#endif /* configuration * =========================================================================== @@ -415,7 +421,7 @@ create_tree(struct board *board, struct move move, enum player player, if (!available_moves) goto end; - struct move *moves = alloca(available_moves * sizeof *moves); + struct move *moves = alloca_impl(available_moves * sizeof *moves); get_available_moves(board, moves); for (size_t i = 0; i < available_moves; i++) { @@ -508,7 +514,7 @@ minimax_get_best_move(struct minimax *self, struct board *board, enum player pla max_search_depth, nodecount(available_moves, max_search_depth), max_nodes); #endif - struct move moves[available_moves]; + struct move *moves = alloca_impl(available_moves * sizeof *moves); get_available_moves(board, moves); self->possible_moves.head = NULL; @@ -565,16 +571,12 @@ minimax_get_best_move(struct minimax *self, struct board *board, enum player pla static size_t get_input_coord(void) { - char *line = NULL; - size_t len; + char buf[64]; while (true) { - ssize_t res = getline(&line, &len, stdin); - if (res < 0) - exit(EXIT_FAILURE); + char *line = fgets(buf, sizeof buf, stdin); size_t coord; if (sscanf(line, "%zu", &coord)) { - free(line); return coord; } else { printf("invalid coordinate. try again.\n"); @@ -587,17 +589,25 @@ main(void) { // NOTE: we use an arena to avoid expensive system allocators, and so // that we can easily free all nodes without iterating the tree + struct arena arena; + +#if defined(__linux__) int prot = PROT_READ|PROT_WRITE; int flags = MAP_PRIVATE|MAP_ANONYMOUS; - struct arena arena = { - .ptr = mmap(NULL, ARENA_SIZE, prot, flags, -1, 0), - .cap = ARENA_SIZE, - .len = 0, - }; + arena.ptr = mmap(NULL, ARENA_SIZE, prot, flags, -1, 0); + arena.cap = ARENA_SIZE; + arena.len = 0; assert(arena.ptr != MAP_FAILED); madvise(arena.ptr, arena.cap, MADV_HUGEPAGE); +#elif defined(_WIN32) + arena.ptr = malloc(ARENA_SIZE); + arena.cap = ARENA_SIZE; + arean.len = 0; + + assert(arena.ptr); +#endif size_t max_nodes = arena.cap / sizeof(struct minimax_node); fprintf(stderr, "Arena cap: %zu bytes, node size: %zu bytes, max node count: %zu\n",