Skip to content

The stack will never overflow if you have enough physical memory

Notifications You must be signed in to change notification settings

lassade/virtual_stack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Virtual Stack

The stack will never overflow if you have enough physical memory!

Concept

Uses some assembly trickery to change the RSP (stack register) to point to a custom heap allocated memory address, and when it is nearly full allocate a new slab of data on the heap to be used as an stack.

Debug and release modes have different stack requirements, been the debug mode much more memory hungry. This lib will use 4 times more memory on debug mode in an attempt to make it work, but you may need to increase the amount of memory manually.

Only x86_64 and requires rust nightly

Although this crate only has a x86_64 implementation the same principles can be used for other architectures.

Where's how to use it

use virtual_stack::*;
// Recursive function
struct Fib;

impl Recursive<usize, usize> for Fib {
    // You may also need to change the constants `SIZE`, `LEFT` and `COPY`
    // to better control the stack properties

    fn call(n: usize, s: Option<StackFrame>) -> usize {
        if (n == 0) || (n == 1) {
            n
        } else {
            // Recursive call, stack never overflows, but you can run out memory!
            Fib::recursive_call(n - 1, s) + Fib::recursive_call(n - 2, s)
        }
    }
}

fn main() {
    // Frist call always uses the program stack
    Fib::call(10, None);
}

About

The stack will never overflow if you have enough physical memory

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages