Memory allocation: moved to alloc.[ch]

This commit is contained in:
2024-07-10 07:49:44 +02:00
parent 3100504fa2
commit 5401e83db8
7 changed files with 80 additions and 37 deletions

51
src/alloc.c Normal file
View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <brlib.h>
#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 <bug.h>
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")

21
src/alloc.h Normal file
View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "chessdefs.h"
void *safe_alloc(size_t size);
void *safe_alloc_page_aligned(size_t size);
void safe_free(void *ptr);

View File

@@ -30,7 +30,7 @@ bitboard_t bb_line[64][64];
bitboard_t bb_knight[64], bb_king[64], bb_pawn_attacks[2][64]; bitboard_t bb_knight[64], bb_king[64], bb_pawn_attacks[2][64];
/* vectors are clockwise from N */ /* vectors are clockwise from N */
static int knight_vector[] = { static int knight_vector[8] = {
NORTH_EAST + NORTH, NORTH_EAST + EAST, NORTH_EAST + NORTH, NORTH_EAST + EAST,
SOUTH_EAST + EAST, SOUTH_EAST + SOUTH, SOUTH_EAST + EAST, SOUTH_EAST + SOUTH,
SOUTH_WEST + SOUTH, SOUTH_WEST + WEST, SOUTH_WEST + SOUTH, SOUTH_WEST + WEST,

View File

@@ -21,7 +21,7 @@
#include <bug.h> #include <bug.h>
#include "chessdefs.h" #include "chessdefs.h"
#include "misc.h" #include "alloc.h"
#include "position.h" #include "position.h"
#include "fen.h" #include "fen.h"
@@ -288,7 +288,7 @@ char *pos2fen(const pos_t *pos, char *fen)
int cur = 0; int cur = 0;
if (!fen) if (!fen)
fen = safe_malloc(92); fen = safe_alloc(92);
/* 1) position /* 1) position
*/ */

View File

@@ -18,7 +18,7 @@
#include <bitops.h> #include <bitops.h>
#include "chessdefs.h" #include "chessdefs.h"
#include "misc.h" #include "alloc.h"
#include "position.h" #include "position.h"
#include "piece.h" #include "piece.h"
#include "hash.h" #include "hash.h"
@@ -211,7 +211,7 @@ int tt_create(s32 sizemb)
hash_tt.mask = -1ull >> (64 - nbits); 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 ", //printf("bits=%2d size=%'15lu/%'6d Mb/%'14lu buckets ",
// hash_tt.nbits, hash_tt.bytes, hash_tt.mb, hash_tt.nbuckets); // hash_tt.nbits, hash_tt.bytes, hash_tt.mb, hash_tt.nbuckets);

View File

@@ -14,34 +14,5 @@
#ifndef _UTIL_H #ifndef _UTIL_H
#define _UTIL_H #define _UTIL_H
#include <stdio.h>
#include <stdlib.h>
#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 <bug.h>
#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 */ #endif /* UTIL_H */

View File

@@ -27,7 +27,7 @@
#include "hq.h" #include "hq.h"
#include "fen.h" #include "fen.h"
#include "piece.h" #include "piece.h"
#include "misc.h" #include "alloc.h"
#include "board.h" #include "board.h"
#include "attack.h" #include "attack.h"
#include "hist.h" #include "hist.h"
@@ -42,7 +42,7 @@
*/ */
pos_t *pos_new(void) 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 *pos_dup(const pos_t *pos)
{ {
pos_t *newpos = safe_malloc(sizeof(pos_t)); pos_t *newpos = safe_alloc(sizeof(pos_t));
*newpos = *pos; *newpos = *pos;
return newpos; return newpos;