From df483d29c20f080ad546bd382c42f72d489ac750 Mon Sep 17 00:00:00 2001 From: Stephen Fleischman Date: Mon, 26 Mar 2018 15:30:27 -0700 Subject: [PATCH 1/2] Replace `Heap` with `Global` to reflect changes in the latest nightly. Signed-off-by: stephenf --- pleco/src/lib.rs | 1 + pleco/src/tools/tt.rs | 13 ++++++------- pleco_engine/src/consts.rs | 14 +++++++------- pleco_engine/src/lib.rs | 1 + pleco_engine/src/tables/mod.rs | 10 +++++----- pleco_engine/src/threadpool/mod.rs | 16 ++++++++-------- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/pleco/src/lib.rs b/pleco/src/lib.rs index bf559b6..5de5c9e 100644 --- a/pleco/src/lib.rs +++ b/pleco/src/lib.rs @@ -78,6 +78,7 @@ #![feature(pointer_methods)] #![feature(cfg_target_feature, target_feature)] #![feature(stdsimd)] +#![feature(nonnull_cast)] #![allow(dead_code)] diff --git a/pleco/src/tools/tt.rs b/pleco/src/tools/tt.rs index 36c7cb2..7511f97 100644 --- a/pleco/src/tools/tt.rs +++ b/pleco/src/tools/tt.rs @@ -35,7 +35,7 @@ use std::ptr::NonNull; use std::mem; -use std::heap::{Alloc, Layout, Heap}; +use std::heap::{Alloc, Layout, Global}; use std::cmp::min; use std::cell::UnsafeCell; @@ -423,8 +423,7 @@ impl TranspositionTable { /// De-allocates the current heap. unsafe fn de_alloc(&self) { - Heap.dealloc((*self.clusters.get()).as_ptr() as *mut _, - Layout::array::(*self.cap.get()).unwrap()); + Global.dealloc((*self.clusters.get()).as_opaque(), Layout::array::(*self.cap.get()).unwrap()); } /// Returns the % of the hash table that is full. @@ -481,13 +480,13 @@ unsafe fn cluster_first_entry(cluster: *mut Cluster) -> *mut Entry { #[inline] fn alloc_room(size: usize) -> NonNull { unsafe { - let ptr = Heap.alloc_zeroed(Layout::array::(size).unwrap()); + let ptr = Global.alloc_zeroed(Layout::array::(size).unwrap()); let new_ptr = match ptr { - Ok(ptr) => ptr, - Err(err) => Heap.oom(err), + Ok(ptr) => ptr.cast(), + Err(_err) => Global.oom(), }; - NonNull::new(new_ptr as *mut Cluster).unwrap() + new_ptr } } diff --git a/pleco_engine/src/consts.rs b/pleco_engine/src/consts.rs index ffa51a6..cf0a256 100644 --- a/pleco_engine/src/consts.rs +++ b/pleco_engine/src/consts.rs @@ -1,5 +1,5 @@ //! Constant values and static structures. -use std::heap::{Alloc, Layout, Heap}; +use std::heap::{Alloc, Layout, Global}; use std::ptr::{NonNull, self}; use std::sync::atomic::AtomicBool; @@ -52,10 +52,10 @@ pub fn init_globals() { fn init_tt() { unsafe { let layout = Layout::new::(); - let result = Heap.alloc_zeroed(layout); + let result = Global.alloc_zeroed(layout); let new_ptr: *mut TranspositionTable = match result { - Ok(ptr) => ptr as *mut TranspositionTable, - Err(err) => Heap.oom(err), + Ok(ptr) => ptr.cast().as_ptr() as *mut TranspositionTable, + Err(_err) => Global.oom(), }; ptr::write(new_ptr, TranspositionTable::new(DEFAULT_TT_SIZE)); TT_TABLE = NonNull::new_unchecked(new_ptr); @@ -65,10 +65,10 @@ fn init_tt() { fn init_timer() { unsafe { let layout = Layout::new::(); - let result = Heap.alloc_zeroed(layout); + let result = Global.alloc_zeroed(layout); let new_ptr: *mut TimeManager = match result { - Ok(ptr) => ptr as *mut TimeManager, - Err(err) => Heap.oom(err), + Ok(ptr) => ptr.cast().as_ptr() as *mut TimeManager, + Err(_err) => Global.oom(), }; ptr::write(new_ptr, TimeManager::uninitialized()); TIMER = NonNull::new_unchecked(new_ptr); diff --git a/pleco_engine/src/lib.rs b/pleco_engine/src/lib.rs index d3d0d8d..2a87696 100644 --- a/pleco_engine/src/lib.rs +++ b/pleco_engine/src/lib.rs @@ -29,6 +29,7 @@ #![feature(fused)] #![feature(const_fn)] #![feature(box_into_raw_non_null)] +#![feature(nonnull_cast)] //#![crate_type = "staticlib"] diff --git a/pleco_engine/src/tables/mod.rs b/pleco_engine/src/tables/mod.rs index 4f1b0b3..900c464 100644 --- a/pleco_engine/src/tables/mod.rs +++ b/pleco_engine/src/tables/mod.rs @@ -7,7 +7,7 @@ pub mod capture_piece_history; pub mod butterfly; use std::ptr::NonNull; -use std::heap::{Alloc, Layout, Heap}; +use std::heap::{Alloc, Layout, Global}; use std::mem; use std::ptr; use std::ops::*; @@ -138,17 +138,17 @@ impl TableBase { // allocates space. unsafe fn alloc() -> NonNull { - let ptr = Heap.alloc_zeroed(Layout::array::(T::ENTRY_COUNT).unwrap()); + let ptr = Global.alloc_zeroed(Layout::array::(T::ENTRY_COUNT).unwrap()); let new_ptr = match ptr { - Ok(ptr) => ptr, - Err(err) => Heap.oom(err), + Ok(ptr) => ptr.cast().as_ptr(), + Err(_err) => Global.oom(), }; NonNull::new(new_ptr as *mut T).unwrap() } /// de-allocates the current table. unsafe fn de_alloc(&mut self) { - Heap.dealloc(self.table.as_ptr() as *mut _, + Global.dealloc(self.table.as_opaque(), Layout::array::(T::ENTRY_COUNT).unwrap()); } } diff --git a/pleco_engine/src/threadpool/mod.rs b/pleco_engine/src/threadpool/mod.rs index bd2adc2..68f76ad 100644 --- a/pleco_engine/src/threadpool/mod.rs +++ b/pleco_engine/src/threadpool/mod.rs @@ -1,6 +1,6 @@ //! Contains the ThreadPool and the individual Threads. -use std::heap::{Alloc, Layout, Heap}; +use std::heap::{Alloc, Layout, Global}; use std::sync::atomic::{AtomicBool,Ordering}; use std::thread::{JoinHandle,self}; use std::sync::{Once, ONCE_INIT}; @@ -39,10 +39,10 @@ pub fn init_threadpool() { .stack_size(THREAD_STACK_SIZE); let handle = scoped::builder_spawn_unsafe(builder, move || { let layout = Layout::new::(); - let result = Heap.alloc_zeroed(layout); + let result = Global.alloc_zeroed(layout); let new_ptr: *mut ThreadPool = match result { - Ok(ptr) => ptr as *mut ThreadPool, - Err(err) => Heap.oom(err), + Ok(ptr) => ptr.cast().as_ptr() as *mut ThreadPool, + Err(_err) => Global.oom(), }; ptr::write(new_ptr, ThreadPool::new()); THREADPOOL = NonNull::new_unchecked(new_ptr); @@ -138,10 +138,10 @@ impl ThreadPool { let layout = Layout::new::(); let cond = if len == 0 {self.main_cond.clone()} else {self.thread_cond.clone()}; unsafe { - let result = Heap.alloc_zeroed(layout); + let result = Global.alloc_zeroed(layout); let new_ptr: *mut Searcher = match result { - Ok(ptr) => ptr as *mut Searcher, - Err(err) => Heap.oom(err), + Ok(ptr) => ptr.cast().as_ptr() as *mut Searcher, + Err(_err) => Global.oom(), }; ptr::write(new_ptr, Searcher::new(len, cond)); self.threads.push(UnsafeCell::new(new_ptr)); @@ -217,7 +217,7 @@ impl ThreadPool { while let Some(unc) = self.threads.pop() { let th: *mut Searcher = *unc.get(); let layout = Layout::new::(); - Heap.dealloc(th as *mut _, layout); + Global.dealloc(NonNull::new_unchecked(th).as_opaque(), layout); } } From ce19f92d14ca901e5d2ba98c218abf8de32f3d74 Mon Sep 17 00:00:00 2001 From: Stephen Fleischman Date: Mon, 26 Mar 2018 15:30:27 -0700 Subject: [PATCH 2/2] Updated pleco to 0.4.1 and pleco_engine to 0.1.2, to reflect the recent nightly changes with Heap allocations Signed-off-by: stephenf --- README.md | 2 +- pleco/Cargo.toml | 2 +- pleco_engine/Cargo.toml | 4 ++-- pleco_engine/src/engine.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 52f9962..01cbd0f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ UCI (Universal Chess Interface) compatible engine. The overall goal for this project is to utilize the efficiency of Rust to create a Chess AI matching the speed of modern chess engines. For the engine, the majority of the code is a direct port of Stockfish's C++ code. See [their website](https://stockfishchess.org/) for more information about the engine. As such, the credit for all of the advanced algorithms used for searching, evaluation, -and many others, go directly to the maintainers and authors of Stockfish. This project is simply for speed comparisons +and many others, go directly to the maintainers and authors of Stockfish. This project is for speed comparisons between the two languages, as well as for educational purposes. - [Documentation](https://docs.rs/pleco), [crates.io](https://crates.io/crates/pleco) for library functionality diff --git a/pleco/Cargo.toml b/pleco/Cargo.toml index abd34de..1f855da 100644 --- a/pleco/Cargo.toml +++ b/pleco/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pleco" -version = "0.4.0" +version = "0.4.1" authors = ["Stephen Fleischman "] description = "A blazingly-fast chess library." homepage = "https://github.com/sfleischman105/Pleco" diff --git a/pleco_engine/Cargo.toml b/pleco_engine/Cargo.toml index 98c64fe..5f9dc6f 100644 --- a/pleco_engine/Cargo.toml +++ b/pleco_engine/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pleco_engine" -version = "0.1.1" # Reminder to change in ./engine.rs +version = "0.1.2" # Reminder to change in ./engine.rs authors = ["Stephen Fleischman "] description = "A blazingly-fast Chess AI." homepage = "https://github.com/sfleischman105/Pleco" @@ -67,7 +67,7 @@ path = "src/lib.rs" doctest = true [dependencies] -pleco = { path = "../pleco", version = "0.4.0" } +pleco = { path = "../pleco", version = "0.4.1" } clippy = {version = "0.0.191", optional = true} chrono = "0.4.1" rand = "0.4.2" diff --git a/pleco_engine/src/engine.rs b/pleco_engine/src/engine.rs index c127d7a..1290c4b 100644 --- a/pleco_engine/src/engine.rs +++ b/pleco_engine/src/engine.rs @@ -20,7 +20,7 @@ use num_cpus; pub static ID_NAME: &str = "Pleco"; pub static ID_AUTHORS: &str = "Stephen Fleischman"; -pub static VERSION: &str = "0.1.1"; +pub static VERSION: &str = "0.1.2"; #[derive(PartialEq)] enum SearchType {