diff --git a/src/alloc.c b/src/alloc.c new file mode 100644 index 0000000..aa2c82b --- /dev/null +++ b/src/alloc.c @@ -0,0 +1,51 @@ +/* alloc.c - various memory allocation helpers + * + * Copyright (C) 2024 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include + +#include + +#include "chessdefs.h" +#include "alloc.h" + +/* force BUG_ON, to get a program abort for failed malloc/free + */ +#pragma push_macro("BUG_ON") +#undef BUG_ON +#define BUG_ON 1 +#include + +void *safe_alloc(size_t size) +{ + void *mem = malloc(size); + bug_on(mem == NULL); + return mem; +} + +void *safe_alloc_page_aligned(size_t size) +{ + void *mem = malloc(size); + bug_on(mem == NULL); + return mem; +} + +void safe_free(void *ptr) +{ + bug_on(ptr == NULL); + free(ptr); +} + +/* restore BUG_ON + */ +#pragma pop_macro("BUG_ON") diff --git a/src/alloc.h b/src/alloc.h new file mode 100644 index 0000000..4054813 --- /dev/null +++ b/src/alloc.h @@ -0,0 +1,21 @@ +/* alloc.h - various memory allocation helpers + * + * Copyright (C) 2024 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include + +#include "chessdefs.h" + +void *safe_alloc(size_t size); +void *safe_alloc_page_aligned(size_t size); +void safe_free(void *ptr); diff --git a/src/bitboard.c b/src/bitboard.c index 35ae218..327b6bf 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -30,7 +30,7 @@ bitboard_t bb_line[64][64]; bitboard_t bb_knight[64], bb_king[64], bb_pawn_attacks[2][64]; /* vectors are clockwise from N */ -static int knight_vector[] = { +static int knight_vector[8] = { NORTH_EAST + NORTH, NORTH_EAST + EAST, SOUTH_EAST + EAST, SOUTH_EAST + SOUTH, SOUTH_WEST + SOUTH, SOUTH_WEST + WEST, diff --git a/src/fen.c b/src/fen.c index 789130d..ef4ae6b 100644 --- a/src/fen.c +++ b/src/fen.c @@ -21,7 +21,7 @@ #include #include "chessdefs.h" -#include "misc.h" +#include "alloc.h" #include "position.h" #include "fen.h" @@ -288,7 +288,7 @@ char *pos2fen(const pos_t *pos, char *fen) int cur = 0; if (!fen) - fen = safe_malloc(92); + fen = safe_alloc(92); /* 1) position */ diff --git a/src/hash.c b/src/hash.c index 4fc5ba6..3b0bdd2 100644 --- a/src/hash.c +++ b/src/hash.c @@ -18,7 +18,7 @@ #include #include "chessdefs.h" -#include "misc.h" +#include "alloc.h" #include "position.h" #include "piece.h" #include "hash.h" @@ -211,7 +211,7 @@ int tt_create(s32 sizemb) hash_tt.mask = -1ull >> (64 - nbits); - hash_tt.keys = safe_malloc(hash_tt.bytes); + hash_tt.keys = safe_alloc(hash_tt.bytes); //printf("bits=%2d size=%'15lu/%'6d Mb/%'14lu buckets ", // hash_tt.nbits, hash_tt.bytes, hash_tt.mb, hash_tt.nbuckets); diff --git a/src/misc.h b/src/misc.h index d3d6d5e..20ced35 100644 --- a/src/misc.h +++ b/src/misc.h @@ -14,34 +14,5 @@ #ifndef _UTIL_H #define _UTIL_H -#include -#include - -#include "chessdefs.h" - -#undef safe_malloc -#undef safe_free - -/* force BUG_ON, to get a program abort for failed malloc/free - */ -#pragma push_macro("BUG_ON") -#undef BUG_ON -#define BUG_ON -#include - -#define safe_malloc(size) ({ \ - void *_ret = malloc(size); \ - bug_on(_ret == NULL); \ - _ret; \ - }) - -#define safe_free(ptr) do { \ - bug_on(ptr == NULL); \ - free(ptr); \ - } while (0) - -/* restore BUG_ON - */ -#pragma pop_macro("BUG_ON") #endif /* UTIL_H */ diff --git a/src/position.c b/src/position.c index 2cc4c9c..4736b07 100644 --- a/src/position.c +++ b/src/position.c @@ -27,7 +27,7 @@ #include "hq.h" #include "fen.h" #include "piece.h" -#include "misc.h" +#include "alloc.h" #include "board.h" #include "attack.h" #include "hist.h" @@ -42,7 +42,7 @@ */ pos_t *pos_new(void) { - return safe_malloc(sizeof(pos_t)); + return safe_alloc(sizeof(pos_t)); } /** @@ -57,7 +57,7 @@ pos_t *pos_new(void) */ pos_t *pos_dup(const pos_t *pos) { - pos_t *newpos = safe_malloc(sizeof(pos_t)); + pos_t *newpos = safe_alloc(sizeof(pos_t)); *newpos = *pos; return newpos;