Skip to content

Commit f0e3164

Browse files
committed
ref: convert thordb to safe code, by grouping all globals into a single thor type
1 parent c885770 commit f0e3164

File tree

11 files changed

+355
-295
lines changed

11 files changed

+355
-295
lines changed

crates/enddev/src/enddev.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use libc_wrapper::{_IO_FILE, stdout, fprintf, fputs, printf, feof, fopen, sscanf
1515
use legacy_zebra::src::zebra::LibcTimeSource;
1616
use std::io::Write;
1717
use libc::isalnum;
18+
use legacy_zebra::src::thordb::LegacyThor;
1819

1920
pub type C2RustUnnamed = u32;
2021
pub const _ISalnum: C2RustUnnamed = 8;
@@ -134,6 +135,7 @@ unsafe fn main_0(mut argc: i32, mut argv: *mut *mut i8)
134135
last_was_pass = 0;
135136
restart = 1;
136137
in_branch = 0;
138+
let mut thor = LegacyThor::new();
137139
loop {
138140
if restart != 0 {
139141
if in_branch != 0 {
@@ -196,7 +198,7 @@ unsafe fn main_0(mut argc: i32, mut argv: *mut *mut i8)
196198
0, 0,
197199
8, 60,
198200
60, 1,
199-
&mut ev_info, g_state) as i32
201+
&mut ev_info, g_state, &mut thor) as i32
200202
} else {
201203
move_0 = game_moves[g_state.moves.disks_played as usize];
202204
if g_state.moves.disks_played >= first_allowed_dev &&
@@ -221,7 +223,7 @@ unsafe fn main_0(mut argc: i32, mut argv: *mut *mut i8)
221223
let evaluated_list = extended_compute_move::<LibcFatalError>(side_to_move, 0,
222224
0, 8,
223225
60,
224-
60, g_state.config.echo, g_state);
226+
60, g_state.config.echo, g_state, &mut thor);
225227
assert_eq!(evaluated_list.get_evaluated_count(), g_state.moves.move_count[g_state.moves.disks_played as usize]);
226228
best_score = -(12345678);
227229
i_0 = 0;

crates/engine/src/game.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn extended_compute_move<L: ComputeMoveLogger, Out: ComputeMoveOutput, FE: F
164164
mut mid: i32,
165165
mut exact: i32,
166166
mut wld: i32, mut echo: i32, g_state: &mut FullState,
167-
update_cb: fn(&EvaluatedList), mut should_stop: StopFn, thor: &Thor)
167+
update_cb: fn(&EvaluatedList), mut should_stop: StopFn, thor: &mut Thor)
168168
-> EvaluatedList {
169169
let mut list = EvaluatedList::new();
170170
let mut i: i32 = 0;
@@ -882,7 +882,7 @@ pub fn generic_compute_move<L: ComputeMoveLogger, Out: ComputeMoveOutput, FE: Fr
882882
logger: &mut Option<L>,
883883
display_pv:i32,
884884
echo:i32, g_state: &mut FullState,
885-
thor: &Thor
885+
thor: &mut Thor
886886
)
887887
-> i8 {
888888
let mut book_eval_info = EvaluationType::new();
@@ -1302,7 +1302,7 @@ pub fn compute_move<L: ComputeMoveLogger, Out: ComputeMoveOutput, FE: FrontEnd,
13021302
search_forced: i32,
13031303
eval_info: &mut EvaluationType, display_pv:i32, echo:i32,
13041304
g_state: &mut FullState,
1305-
thor: &Thor
1305+
thor: &mut Thor
13061306
)
13071307
-> i8 {
13081308
return generic_compute_move::<L, Out, FE, Thor>(

crates/engine/src/thordb.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use crate::src::myrandom::MyRandom;
2+
use crate::src::timer::Timer;
23

34
pub trait ThorDatabase {
45
fn get_thor_game_move(&self, index: i32, move_number: i32) -> i32;
5-
fn database_search(&self, in_board: &[i32], side_to_move: i32);
6+
fn database_search(&mut self, in_board: &[i32], side_to_move: i32);
67
fn get_match_count(&self) -> i32;
78
fn get_black_win_count(&self) -> i32;
89
fn get_draw_count(&self) -> i32;
910
fn get_white_win_count(&self) -> i32;
1011
fn get_black_median_score(&self) -> i32;
1112
fn get_black_average_score(&self) -> f64;
12-
fn choose_thor_opening_move(&self, in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32;
13+
fn choose_thor_opening_move(&mut self, in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32;
14+
fn load_thor_files(&mut self, g_timer: &mut Timer);
1315
}

crates/engine/src/zebra.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ pub trait ZebraFrontend {
164164
fn report_engine_override();
165165
fn before_get_move();
166166
fn report_book_randomness(slack_: f64);
167-
fn load_thor_files(g_timer: &mut Timer);
168167
fn print_move_alternatives(side_to_move: i32, board_state: &mut BoardState, g_book: &mut Book);
169168
}
170169

@@ -212,7 +211,7 @@ pub fn next_state<
212211
ComputeMoveOut: ComputeMoveOutput,
213212
FE: FrontEnd,
214213
Thor: ThorDatabase
215-
>(play_state: &mut PlayGame<Source>, move_attempt: Option<MoveAttempt>, thor: &Thor) -> PlayGameState {
214+
>(play_state: &mut PlayGame<Source>, move_attempt: Option<MoveAttempt>, thor: &mut Thor) -> PlayGameState {
216215
play_state.state = match play_state.state {
217216
PlayGameState::Initial => {
218217
/* Decode the predefined move sequence */
@@ -244,7 +243,7 @@ pub fn next_state<
244243
reset_book_search(&mut play_state.g_state.g_book);
245244
set_deviation_value(play_state.g_state.config.low_thresh, play_state.g_state.config.high_thresh, play_state.g_state.config.dev_bonus, &mut play_state.g_state.g_book);
246245
if play_state.g_state.config.use_thor {
247-
ZF::load_thor_files(&mut play_state.g_state.timer);
246+
Thor::load_thor_files(thor, &mut play_state.g_state.timer);
248247
}
249248
let white_is_player = play_state.g_state.config.skill[0] == 0;
250249
let black_is_player = play_state.g_state.config.skill[2] == 0;

crates/legacy-zebra/src/game.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn ponder_move<
136136
_book: i32,
137137
mid: i32,
138138
exact: i32,
139-
wld: i32, display_pv: i32, mut echo:i32, g_state: &mut FullState, thor: &Thor) {
139+
wld: i32, display_pv: i32, mut echo:i32, g_state: &mut FullState, thor: &mut Thor) {
140140
type Rep = LibcFatalError;
141141

142142
let mut eval_info =EvaluationType::new();
@@ -302,10 +302,12 @@ pub fn get_pv(destin: &mut [i8], g_state: &mut FullState) -> i32 {
302302
};
303303
}
304304
pub fn extended_compute_move<FE: FrontEnd>(
305-
side_to_move: i32, book_only: i32, book: i32, mid: i32, exact: i32, wld: i32, echo: i32, g_state: &mut FullState)
305+
side_to_move: i32, book_only: i32, book: i32, mid: i32, exact: i32, wld: i32, echo: i32, g_state: &mut FullState,
306+
thor: &mut LegacyThor
307+
)
306308
-> EvaluatedList {
307309
engine::src::game::extended_compute_move::<LogFileHandler, LibcZebraOutput, FE, LegacyThor, _>(
308-
side_to_move, book_only, book, mid, exact, wld, echo, g_state, |_| (), || false, &LegacyThor
310+
side_to_move, book_only, book, mid, exact, wld, echo, g_state, |_| (), || false, thor
309311
)
310312
}
311313
/*
@@ -318,7 +320,7 @@ pub fn perform_extended_solve(side_to_move: i32,
318320
actual_move: i8,
319321
book: i32,
320322
exact_solve:
321-
i32, g_state: &mut FullState) {
323+
i32, g_state: &mut FullState, thor: &mut LegacyThor) {
322324
let mut i: i32 = 0;
323325
let mut mid: i32 = 0;
324326
let mut wld: i32 = 0;
@@ -365,14 +367,14 @@ pub fn perform_extended_solve(side_to_move: i32,
365367
0, book, mid - 1,
366368
exact - 1, wld - 1,
367369
1,
368-
&mut evaluated_list[0].eval, g_state);
370+
&mut evaluated_list[0].eval, g_state, thor);
369371
if evaluated_list[0].eval.type_0 as u32 == PASS_EVAL as i32 as u32 {
370372
/* Don't allow pass */
371373
legacy_compute_move(side_to_move, 0, 0,
372374
0, 0, book,
373375
mid - 1, exact - 1,
374376
wld - 1, 1,
375-
&mut evaluated_list[0].eval, g_state);
377+
&mut evaluated_list[0].eval, g_state, thor);
376378
if evaluated_list[0].eval.type_0 as u32 == PASS_EVAL as i32 as u32 {
377379
/* Game has ended */
378380
disc_diff = disc_count(side_to_move, &(g_state.board).board) -
@@ -422,7 +424,7 @@ pub fn perform_extended_solve(side_to_move: i32,
422424
if it isn't ACTUAL_MOVE */
423425
best_move = legacy_compute_move(side_to_move, 0, 0,
424426
0, 0, book, mid, exact,
425-
wld, 1, &mut evaluated_list[1].eval, g_state);
427+
wld, 1, &mut evaluated_list[1].eval, g_state, thor);
426428
if force_return == 0 && best_move != actual_move {
427429
/* Move list will contain best move first and then the actual move */
428430
game_evaluated_count = 2;
@@ -467,7 +469,9 @@ pub fn legacy_compute_move(side_to_move: i32,
467469
exact: i32,
468470
wld: i32,
469471
search_forced: i32,
470-
eval_info: &mut EvaluationType, g_state: &mut FullState)
472+
eval_info: &mut EvaluationType, g_state: &mut FullState,
473+
thor: &mut LegacyThor
474+
)
471475
-> i8 {
472476
return generic_compute_move::<LogFileHandler, LibcZebraOutput, LibcFatalError, LegacyThor>(side_to_move, update_all, my_time,
473477
my_incr, timed_depth,
@@ -477,7 +481,7 @@ pub fn legacy_compute_move(side_to_move: i32,
477481
&mut LogFileHandler::create_log_file_if_needed(),
478482
g_state.config.display_pv,
479483
g_state.config.echo,
480-
g_state, &LegacyThor);
484+
g_state, thor);
481485
}
482486

483487
pub struct LibcZebraOutput;

0 commit comments

Comments
 (0)