-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
268 lines (220 loc) · 5.68 KB
/
memory.c
File metadata and controls
268 lines (220 loc) · 5.68 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* 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"
#include "pool.h"
#define SENGINE_POSITION_POOL_BLOCKSIZE 5000
#define SENGINE_BOARD_POOL_BLOCKSIZE 5000
#define SENGINE_HASHVALUE_POOL_BLOCKSIZE 16000
#define SENGINE_BOARDLIST_POOL_BLOCKSIZE 5000
#define SENGINE_IDBOARD_POOL_BLOCKSIZE 50
#define SENGINE_CSL_POOL_BLOCKSIZE 10
#define SENGINE_PIN_STATUS_BLOCKSIZE 10
extern bool opt_classify;
static pool pos_pool_ptr;
static pool hval_pool_ptr;
static pool blist_pool_ptr;
static pool board_pool_ptr;
static pool idb_pool_ptr;
static pool csl_pool_ptr;
static pool ps_pool_ptr;
void init_mem(void)
{
poolInitialize(&pos_pool_ptr, sizeof(POSITION), SENGINE_POSITION_POOL_BLOCKSIZE);
poolInitialize(&blist_pool_ptr, sizeof(BOARDLIST), SENGINE_BOARDLIST_POOL_BLOCKSIZE);
poolInitialize(&board_pool_ptr, sizeof(BOARD), SENGINE_BOARD_POOL_BLOCKSIZE);
if (opt_classify == true) {
poolInitialize(&idb_pool_ptr, sizeof(ID_BOARD), SENGINE_IDBOARD_POOL_BLOCKSIZE);
poolInitialize(&csl_pool_ptr, sizeof(CHECK_SQUARE_LIST), SENGINE_CSL_POOL_BLOCKSIZE);
poolInitialize(&ps_pool_ptr, sizeof(PIN_STATUS), SENGINE_PIN_STATUS_BLOCKSIZE);
}
return;
}
void close_mem(void)
{
poolFreePool(&pos_pool_ptr);
poolFreePool(&blist_pool_ptr);
poolFreePool(&board_pool_ptr);
if (opt_classify == true) {
poolFreePool(&idb_pool_ptr);
poolFreePool(&csl_pool_ptr);
poolFreePool(&ps_pool_ptr);
}
return;
}
void setup_mpool()
{
poolInitialize(&hval_pool_ptr, sizeof(HASHVALUE), SENGINE_HASHVALUE_POOL_BLOCKSIZE);
return;
}
void destroy_mpool()
{
poolFreePool(&hval_pool_ptr);
return;
}
PIN_STATUS* get_pin_status()
{
PIN_STATUS* ptr;
ptr = (PIN_STATUS*) poolMalloc(&ps_pool_ptr);
SENGINE_MEM_ASSERT(ptr);
utstring_new(ptr->w_before);
utstring_new(ptr->w_after);
utstring_new(ptr->b_before);
utstring_new(ptr->b_after);
return ptr;
}
void free_pin_status(PIN_STATUS* ptr)
{
utstring_free(ptr->w_before);
utstring_free(ptr->w_after);
utstring_free(ptr->b_before);
utstring_free(ptr->b_after);
poolFree(&ps_pool_ptr, ptr);
return;
}
CHECK_SQUARE_LIST* getCSL()
{
CHECK_SQUARE_LIST* pcsl;
pcsl = (CHECK_SQUARE_LIST*) poolMalloc(&csl_pool_ptr);
SENGINE_MEM_ASSERT(pcsl);
memset((void*) pcsl, '\0', sizeof(CHECK_SQUARE_LIST));
return pcsl;
}
void freeCSL(CHECK_SQUARE_LIST* inCSL)
{
poolFree(&csl_pool_ptr, inCSL);
}
ID_BOARD* getIdBoard()
{
ID_BOARD* pidb;
pidb = (ID_BOARD*) poolMalloc(&idb_pool_ptr);
SENGINE_MEM_ASSERT(pidb);
memset((void*) pidb, 'Z', sizeof(ID_BOARD));
pidb->white_ids[64] = '\0';
pidb->black_ids[64] = '\0';
return pidb;
}
ID_BOARD* cloneIdBoard(ID_BOARD* inIdBrd)
{
ID_BOARD* pidb;
pidb = (ID_BOARD*) poolMalloc(&idb_pool_ptr);
SENGINE_MEM_ASSERT(pidb);
memcpy((void*) pidb, (void*) inIdBrd, sizeof(ID_BOARD));
return pidb;
}
void freeIdBoard(ID_BOARD* inIdBrd)
{
poolFree(&idb_pool_ptr, inIdBrd);
return;
}
BOARD* getBoard(POSITION* ppos, unsigned char played, unsigned char move)
{
BOARD* rpbrd;
//rpbrd = calloc(1, sizeof(BOARD));
rpbrd = (BOARD*) poolMalloc(&board_pool_ptr);
SENGINE_MEM_ASSERT(rpbrd);
memset((void*) rpbrd, 0, sizeof(BOARD));
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 = (BOARD*) poolMalloc(&board_pool_ptr);
SENGINE_MEM_ASSERT(rpbrd);
memcpy((void*) rpbrd, (void*) inBrd, sizeof(BOARD));
rpbrd->next = NULL;
return rpbrd;
}
POSITION* getPosition(POSITION* ppos)
{
POSITION* rpos;
rpos = (POSITION*) poolMalloc(&pos_pool_ptr);
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 = (HASHVALUE*) poolMalloc(&hval_pool_ptr);
SENGINE_MEM_ASSERT(hv);
memset((void*) hv, 0, sizeof(HASHVALUE));
return hv;
}
void freeHashValue(HASHVALUE* ptr)
{
poolFree(&hval_pool_ptr, ptr);
return;
}
BOARDLIST* getBoardlist(unsigned char tplay, unsigned char move)
{
BOARDLIST* pbl;
pbl = (BOARDLIST*) poolMalloc(&blist_pool_ptr);
SENGINE_MEM_ASSERT(pbl);
memset((void*) pbl, 0, sizeof(BOARDLIST));
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);
}
poolFree(&board_pool_ptr, pbrd);
return;
}
void freePosition(POSITION* ppos)
{
assert(ppos != NULL);
poolFree(&pos_pool_ptr, 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);
}
poolFree(&blist_pool_ptr, pbl);
}
return;
}