-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
133 lines (114 loc) · 2.43 KB
/
memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* sengine.c
* (c) 2017-2018, Brian Stephenson
* brian@bstephen.me.uk
*
* A program to test orthodox chess problems of the types:
*
* directmates
* selfmates
* relfexmates
* helpmates
*
* Input is taken from the program options and output is xml on stdout.
*
* This is the module that contains all memory management
*/
#include "sengine.h"
void init_mem(void)
{
return;
}
void close_mem(void)
{
return;
}
BOARD* getBoard(POSITION* ppos, unsigned char played, unsigned char move)
{
BOARD* rpbrd;
rpbrd = calloc(1, sizeof(BOARD));
SENGINE_MEM_ASSERT(rpbrd);
rpbrd->pos = getPosition(ppos);
rpbrd->tag = '*';
rpbrd->side = played;
rpbrd->ply = move;
rpbrd->qualifier[0] = '\0';
rpbrd->use_count = 1;
return rpbrd;
}
BOARD* cloneBoard(BOARD* inBrd)
{
BOARD* rpbrd;
rpbrd = malloc(sizeof(BOARD));
SENGINE_MEM_ASSERT(rpbrd);
memcpy((void*) rpbrd, (void*) inBrd, sizeof(BOARD));
rpbrd->next = NULL;
return rpbrd;
}
POSITION* getPosition(POSITION* ppos)
{
POSITION* rpos;
rpos = (POSITION*) malloc(sizeof(POSITION));
SENGINE_MEM_ASSERT(rpos);
memcpy(rpos, ppos, sizeof(POSITION));
return rpos;
}
KILLERHASHVALUE* getKillerHashValue(void)
{
KILLERHASHVALUE* khv = calloc(1, sizeof(HASHVALUE));
SENGINE_MEM_ASSERT(khv);
return khv;
}
HASHVALUE* getHashValue(void)
{
HASHVALUE* hv = calloc(1, sizeof(HASHVALUE));
SENGINE_MEM_ASSERT(hv);
return hv;
}
BOARDLIST* getBoardlist(unsigned char tplay, unsigned char move)
{
BOARDLIST* pbl;
pbl = calloc(1, sizeof(BOARDLIST));
SENGINE_MEM_ASSERT(pbl);
pbl->toPlay = tplay;
pbl->moveNumber = move;
pbl->use_count = 1;
pbl->vektor = (BOARD*) NULL;
return pbl;
}
void freeBoard(BOARD* pbrd)
{
assert(pbrd != NULL);
if (pbrd->pos != NULL) {
freePosition(pbrd->pos);
}
if (pbrd->nextply != NULL) {
freeBoardlist(pbrd->nextply);
}
if (pbrd->threat != NULL) {
freeBoardlist(pbrd->threat);
}
free(pbrd);
return;
}
void freePosition(POSITION* ppos)
{
assert(ppos != NULL);
free(ppos);
return;
}
void freeBoardlist(BOARDLIST* pbl)
{
assert(pbl != NULL);
pbl->use_count--;
if (pbl->use_count == 0) {
BOARD* b;
BOARD* tmp;
LL_FOREACH_SAFE(pbl->vektor, b, tmp) {
LL_DELETE(pbl->vektor, b);
freeBoard(b);
}
free(pbl);
}
return;
}