Skip to content

Commit

Permalink
Add NoArc<T> to codebase.
Browse files Browse the repository at this point in the history
This adds a sound (but memory-leaky) abstraction around an unsafe
dereference, which lets us access our movetable without reference
counting or atomic operations or locks or anything! Yay!
  • Loading branch information
ethanbarry committed Nov 25, 2024
1 parent 69e9176 commit 9dd6cb5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::sync::LazyLock;

use gamemanager::GameManager;
use movetable::MoveTable;
use movetable::{noarc, MoveTable};

mod bitboard;
mod gamemanager;
Expand All @@ -13,6 +13,8 @@ mod ucimanager;
pub static MOVETABLE: LazyLock<MoveTable> = std::sync::LazyLock::new(MoveTable::default);

fn main() {
let movetable = noarc::NoArc::new(MoveTable::default());

let gm =
GameManager::from_fen_string("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8");
let mvlst = gm.legal_moves();
Expand Down
2 changes: 2 additions & 0 deletions src/movetable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::types::{Color, PieceType};
use dashmap::DashMap;

pub mod noarc;

/// A HashMap of [`(Color, PieceType, u64)`] indexing [`Vec<Vec<u64>>`] where
/// the index integer is a position on the board (must be a power of two) and
/// the list of lists is a list of rays---that is, each direction the object
Expand Down
24 changes: 24 additions & 0 deletions src/movetable/noarc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::ops::Deref;

#[derive(Clone)]
pub struct NoArc<T> {
ptr: *mut T,
}

impl<T> NoArc<T> {
pub fn new(val: T) -> Self {
Self {
ptr: Box::leak(Box::new(val)),
}
}
}

unsafe impl<T> Sync for NoArc<T> {}
unsafe impl<T> Send for NoArc<T> {}

impl<T> Deref for NoArc<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { self.ptr.as_ref().unwrap() }
}
}

0 comments on commit 9dd6cb5

Please sign in to comment.