Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions conformance-tests/tests/memory_leak_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use bladeink::story::Story;
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicI64, Ordering};

mod common;

static ALLOCATED: AtomicI64 = AtomicI64::new(0);

struct TrackingAllocator;

unsafe impl GlobalAlloc for TrackingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc(layout) };
if !ptr.is_null() {
ALLOCATED.fetch_add(layout.size() as i64, Ordering::Relaxed);
}
ptr
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) };
ALLOCATED.fetch_sub(layout.size() as i64, Ordering::Relaxed);
}

unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let new_ptr = unsafe { System.realloc(ptr, layout, new_size) };
if !new_ptr.is_null() {
ALLOCATED.fetch_sub(layout.size() as i64, Ordering::Relaxed);
ALLOCATED.fetch_add(new_size as i64, Ordering::Relaxed);
}
new_ptr
}
}

#[global_allocator]
static GLOBAL: TrackingAllocator = TrackingAllocator;

fn run_story_to_end(json_string: &str) {
let mut story = Story::new(json_string).expect("failed to create story");
while story.can_continue() || !story.get_current_choices().is_empty() {
while story.can_continue() {
story.cont().expect("cont failed");
}
if !story.get_current_choices().is_empty() {
story.choose_choice_index(0).expect("choose failed");
}
}
}

#[test]
fn the_intercept_on_loop_test() {
const THREADS: usize = 10;
const STORIES_PER_THREAD: usize = 100;

let json_string = common::get_json_string("inkfiles/TheIntercept.ink.json").unwrap();

let before = ALLOCATED.load(Ordering::SeqCst);

std::thread::scope(|s| {
let json = &json_string;
let handles: Vec<_> = (0..THREADS)
.map(|_| {
s.spawn(move || {
for _ in 0..STORIES_PER_THREAD {
run_story_to_end(json);
}
})
})
.collect();

for handle in handles {
handle.join().expect("thread panicked");
}
});

let after = ALLOCATED.load(Ordering::SeqCst);
let leaked = after - before;

assert_eq!(
leaked,
0,
"Memory leak detected: {} bytes still allocated after dropping {} stories",
leaked,
THREADS * STORIES_PER_THREAD
);
}
71 changes: 44 additions & 27 deletions lib/src/divert.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::{cell::RefCell, fmt, rc::Rc};
use std::{
cell::RefCell,
fmt,
rc::{Rc, Weak},
};

use crate::{
container::Container,
object::{Object, RTObject},
path::{Component, Path},
pointer::{self, Pointer},
pointer::Pointer,
push_pop::PushPopType,
};

pub struct Divert {
obj: Object,
target_pointer: RefCell<Pointer>,
target_cache: RefCell<Option<(Weak<Container>, i32)>>,
target_path: RefCell<Option<Path>>,
pub external_args: usize,
pub is_conditional: bool,
Expand All @@ -37,7 +41,7 @@ impl Divert {
stack_push_type,
is_external,
external_args,
target_pointer: RefCell::new(pointer::NULL.clone()),
target_cache: RefCell::new(None),
target_path: RefCell::new(Self::target_path_string(target_path)),
variable_divert_name: var_divert_name,
}
Expand Down Expand Up @@ -80,39 +84,52 @@ impl Divert {
}

pub fn get_target_pointer(self: &Rc<Self>) -> Pointer {
let target_pointer_null = self.target_pointer.borrow().is_null();
if target_pointer_null {
let target_obj =
Object::resolve_path(self.clone(), self.target_path.borrow().as_ref().unwrap())
.obj
.clone();

if self
.target_path
.borrow()
.as_ref()
.unwrap()
.get_last_component()
.unwrap()
.is_index()
{
self.target_pointer.borrow_mut().container = target_obj.get_object().get_parent();
self.target_pointer.borrow_mut().index = self
if let Some((weak, index)) = self.target_cache.borrow().as_ref() {
if let Some(container) = weak.upgrade() {
return Pointer {
container: Some(container),
index: *index,
};
}
}

let target_obj =
Object::resolve_path(self.clone(), self.target_path.borrow().as_ref().unwrap())
.obj
.clone();

let pointer = if self
.target_path
.borrow()
.as_ref()
.unwrap()
.get_last_component()
.unwrap()
.is_index()
{
Pointer {
container: target_obj.get_object().get_parent(),
index: self
.target_path
.borrow()
.as_ref()
.unwrap()
.get_last_component()
.unwrap()
.index
.unwrap() as i32;
} else {
let c = target_obj.into_any().downcast::<Container>();
self.target_pointer.replace(Pointer::start_of(c.unwrap()));
.unwrap() as i32,
}
} else {
let c = target_obj.into_any().downcast::<Container>().unwrap();
Pointer::start_of(c)
};

if let Some(container) = &pointer.container {
self.target_cache
.replace(Some((Rc::downgrade(container), pointer.index)));
}

self.target_pointer.borrow().clone()
pointer
}

pub fn get_target_path(self: &Rc<Self>) -> Option<Path> {
Expand Down