Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
noinst_HEADERS = amy.h bitboard.h blunder.h bookup.h commands.h dbase.h eco.h \
evaluation.h evaluation_config.h filter.h hashtable.h heap.h \
init.h inline.h learn.h magic.h mates.h movedata.h next.h \
pgn.h probe.h random.h recog.h search.h search_io.h \
state_machine.h swap.h test_blunder.h test_dbase.h \
test_yaml.h time_ctl.h tree.h types.h utils.h yaml.h
pgn.h probe.h random.h recog.h safe_malloc.h search.h \
search_io.h state_machine.h swap.h test_blunder.h \
test_dbase.h test_yaml.h time_ctl.h tree.h types.h utils.h \
yaml.h

.PHONY: format
format:
Expand Down
2 changes: 1 addition & 1 deletion include/blunder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#ifndef BLUNDER_H
#define BLUNDER_H

#include "types.h"
#include "dbase.h"
#include "types.h"

move_t get_best_move_from_comment(char *, struct Position *, char *);
void BlunderCheck(char *);
Expand Down
65 changes: 65 additions & 0 deletions include/safe_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*

Amy - a chess playing program

Copyright (c) 2002-2026, Thorsten Greiner
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef SAFE_MALLOC_H
#define SAFE_MALLOC_H

#include <stdio.h>
#include <stdlib.h>

static void *safe_malloc(size_t size) {
void *ptr = malloc(size);
if (ptr == NULL) {
perror(NULL);
exit(1);
}
return ptr;
}

static void *safe_calloc(size_t count, size_t size) {
void *ptr = calloc(count, size);
if (ptr == NULL) {
perror(NULL);
exit(1);
}
return ptr;
}

static void *safe_realloc(void *old_ptr, size_t size) {
void *ptr = realloc(old_ptr, size);
if (ptr == NULL) {
perror(NULL);
exit(1);
}
return ptr;
}

#endif
5 changes: 2 additions & 3 deletions src/blunder.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

#include "dbase.h"
#include "pgn.h"
#include "safe_malloc.h"
#include "search.h"
#include "types.h"
#include "utils.h"
#include "yaml.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -71,8 +71,7 @@ move_t get_best_move_from_comment(char *comment, struct Position *p,

size_t len = strlen(ptr);

char *buffer = malloc(len + 1);
abort_if_allocation_failed(buffer);
char *buffer = safe_malloc(len + 1);

strncpy(buffer, ptr, len + 1);

Expand Down
3 changes: 2 additions & 1 deletion src/bookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "eco.h"
#include "pgn.h"
#include "random.h"
#include "safe_malloc.h"
#include "tree.h"
#include "utils.h"

Expand Down Expand Up @@ -91,7 +92,7 @@ static tree_node_t *PutBookEntry(tree_node_t *database, hash_t hk, int result,
}

if (entry == NULL) {
entry = calloc(1, sizeof(struct BookEntry));
entry = safe_calloc(1, sizeof(struct BookEntry));
}

if (result == 1) {
Expand Down
27 changes: 6 additions & 21 deletions src/dbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "magic.h"
#include "mates.h"
#include "recog.h"
#include "safe_malloc.h"
#include "swap.h"
#include "types.h"
#include "utils.h"
Expand Down Expand Up @@ -2164,7 +2165,7 @@ static void ReadEPD(struct Position *p, char *x) {
* due to the use of strtok, sorry :-)
*/

line = malloc(strlen(x) + 1);
line = safe_malloc(strlen(x) + 1);
strcpy(line, x);
x = line;

Expand Down Expand Up @@ -2558,17 +2559,9 @@ bool IsPassed(const struct Position *p, int sq, int side) {
*/

struct Position *CreatePositionFromEPD(char *epd) {
struct Position *p = calloc(1, sizeof(struct Position));
if (!p) {
Print(0, "Cannot allocate Position.\n");
exit(1);
}
struct Position *p = safe_calloc(1, sizeof(struct Position));
p->gameLogSize = INITIAL_GAME_LOG_SIZE;
p->gameLog = calloc(p->gameLogSize, sizeof(struct GameLog));
if (!p->gameLog) {
Print(0, "Cannot allocate GameLog.\n");
exit(1);
}
p->gameLog = safe_calloc(p->gameLogSize, sizeof(struct GameLog));
p->actLog = p->gameLog;
ReadEPD(p, epd);
p->actLog->gl_IrrevCount = 0;
Expand All @@ -2594,19 +2587,11 @@ struct Position *InitialPosition(void) {
}

struct Position *ClonePosition(struct Position *src) {
struct Position *p = calloc(1, sizeof(struct Position));
if (!p) {
Print(0, "Cannot allocated Position.\n");
exit(1);
}
struct Position *p = safe_calloc(1, sizeof(struct Position));
memcpy(p, src, sizeof(struct Position));

p->gameLogSize = src->gameLogSize;
p->gameLog = calloc(p->gameLogSize, sizeof(struct GameLog));
if (!p->gameLog) {
Print(0, "Cannot allocate GameLog.\n");
exit(1);
}
p->gameLog = safe_calloc(p->gameLogSize, sizeof(struct GameLog));
memcpy(p->gameLog, src->gameLog, sizeof(struct GameLog) * p->gameLogSize);

p->actLog = p->gameLog + (src->actLog - src->gameLog);
Expand Down
14 changes: 5 additions & 9 deletions src/evaluation_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "dbase.h"
#include "evaluation.h"
#include "safe_malloc.h"
#include "search.h"
#include "utils.h"
#include "yaml.h"
Expand Down Expand Up @@ -265,8 +266,7 @@ static void set_piece_square_table(struct Node *node, char *name,

static void set_array(struct Node *node, char *name, int16_t *target_array,
unsigned int count) {
int *destination = malloc(sizeof(int) * count);
abort_if_allocation_failed(destination);
int *destination = safe_malloc(sizeof(int) * count);

struct IntArrayLookupResult array_result =
get_as_int_array(node, name, destination, count);
Expand All @@ -292,7 +292,6 @@ static void configure_name(struct Node *node) {

if (result.result_code == OK) {
ConfigurationName = result.result;
abort_if_allocation_failed(ConfigurationName);
Print(0, "Using configuration name: %s\n", ConfigurationName);
}
}
Expand Down Expand Up @@ -403,8 +402,7 @@ static char *read_file(char *file_name) {
size_t buf_size = page_size;
size_t total_bytes_read = 0;

char *buffer = malloc(buf_size);
abort_if_allocation_failed(buffer);
char *buffer = safe_malloc(buf_size);

char *ptr = buffer;

Expand All @@ -419,8 +417,7 @@ static char *read_file(char *file_name) {

if ((total_bytes_read + page_size) >= buf_size) {
buf_size *= 2;
buffer = realloc(buffer, buf_size);
abort_if_allocation_failed(buffer);
buffer = safe_realloc(buffer, buf_size);
ptr = buffer + total_bytes_read;
}
}
Expand All @@ -429,8 +426,7 @@ static char *read_file(char *file_name) {

if ((total_bytes_read + 1) >= buf_size) {
buf_size += 1;
buffer = realloc(buffer, buf_size);
abort_if_allocation_failed(buffer);
buffer = safe_realloc(buffer, buf_size);
}
*ptr = '\0';

Expand Down
7 changes: 4 additions & 3 deletions src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include "hashtable.h"
#include "random.h"
#include "safe_malloc.h"
#include "search.h"
#include "utils.h"

Expand Down Expand Up @@ -540,7 +541,7 @@ void AllocateHT(void) {
HT_Size = 1 << HT_Bits;
HT_Mask = HT_Size - 1;

TranspositionTable = calloc(HT_Size, sizeof(struct HTEntry));
TranspositionTable = safe_calloc(HT_Size, sizeof(struct HTEntry));

/* Thread-local hash table - only calculate sizes and bits here...*/
L_HT_Size = 1 << L_HT_Bits;
Expand All @@ -549,12 +550,12 @@ void AllocateHT(void) {
PT_Size = 1 << PT_Bits;
PT_Mask = PT_Size - 1;

PawnTable = calloc(PT_Size, sizeof(struct PTEntry));
PawnTable = safe_calloc(PT_Size, sizeof(struct PTEntry));

ST_Size = 1 << ST_Bits;
ST_Mask = ST_Size - 1;

ScoreTable = calloc(ST_Size, sizeof(struct STEntry));
ScoreTable = safe_calloc(ST_Size, sizeof(struct STEntry));

Print(0, "Hashtable sizes: %d k, %d k, %d k (%d, %d, %d bits)\n",
((1 << HT_Bits) * sizeof(struct HTEntry)) / 1024,
Expand Down
21 changes: 5 additions & 16 deletions src/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,21 @@
*/

#include "heap.h"
#include "safe_malloc.h"

static const int DATA_SIZE = 1024;
static const int SECTION_SIZE = 32;

heap_t allocate_heap(void) {
heap_t heap = (heap_t)malloc(sizeof(struct heap));
if (heap == NULL) {
perror("Cannot allocate heap:");
exit(1);
}

move_t *data = (move_t *)malloc(DATA_SIZE * sizeof(move_t));
if (data == NULL) {
perror("Cannot allocate heap:");
exit(1);
}
heap_t heap = (heap_t)safe_malloc(sizeof(struct heap));

move_t *data = (move_t *)safe_malloc(DATA_SIZE * sizeof(move_t));

heap->data = data;
heap->capacity = DATA_SIZE;

heap_section_t sections =
(heap_section_t)malloc(SECTION_SIZE * sizeof(struct heap_section));
if (sections == NULL) {
perror("Cannot allocate heap:");
exit(1);
}
(heap_section_t)safe_malloc(SECTION_SIZE * sizeof(struct heap_section));

heap->sections_start = sections;
heap->sections_end = sections + SECTION_SIZE;
Expand Down
27 changes: 5 additions & 22 deletions src/next.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "heap.h"
#include "init.h"
#include "inline.h"
#include "safe_malloc.h"
#include "search.h"
#include "swap.h"
#include "utils.h"
Expand All @@ -47,26 +48,12 @@
#endif

struct SearchData *CreateSearchData(struct Position *p) {
struct SearchData *sd = calloc(1, sizeof(struct SearchData));
if (!sd) {
Print(0, "Cannot allocate SearchData.\n");
exit(1);
}
struct SearchData *sd = safe_calloc(1, sizeof(struct SearchData));

sd->position = p;

sd->statusTable = calloc(MAX_TREE_SIZE, sizeof(struct SearchStatus));
if (!sd->statusTable) {
Print(0, "Cannot allocate SearchStatus.\n");
exit(1);
}
sd->statusTable = safe_calloc(MAX_TREE_SIZE, sizeof(struct SearchStatus));
sd->current = sd->statusTable;

sd->killerTable = calloc(MAX_TREE_SIZE, sizeof(struct KillerEntry));
if (!sd->killerTable) {
Print(0, "Cannot allocate KillerEntry.\n");
exit(1);
}
sd->killerTable = safe_calloc(MAX_TREE_SIZE, sizeof(struct KillerEntry));
sd->killer = sd->killerTable;

sd->heap = allocate_heap();
Expand All @@ -75,11 +62,7 @@ struct SearchData *CreateSearchData(struct Position *p) {
sd->data_heap_size = 0;

#if MP
sd->localHashTable = calloc(sizeof(struct HTEntry), L_HT_Size);
if (!sd->localHashTable) {
Print(0, "Cannot allocate thread-local hashtable.\n");
exit(1);
}
sd->localHashTable = safe_calloc(sizeof(struct HTEntry), L_HT_Size);
sd->deferred_heap = allocate_heap();
#endif

Expand Down
3 changes: 2 additions & 1 deletion src/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "config.h"
#include "dbase.h"
#include "safe_malloc.h"
#include "search.h"
#include "utils.h"
#include <stdlib.h>
Expand Down Expand Up @@ -68,7 +69,7 @@ void InitEGTB(char *tbpath) {
TB_CRC_CHECK = 0;
EGTBMenCount = IInitializeTb(tbpath);
if (EGTBMenCount != 0) {
void *egtb_cache = malloc(EGTB_CACHE_SIZE);
void *egtb_cache = safe_malloc(EGTB_CACHE_SIZE);
Print(0, "Found %d-men endgame table bases.\n", EGTBMenCount);
FTbSetCacheSize(egtb_cache, EGTB_CACHE_SIZE);
}
Expand Down
Loading