Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add n_free_blocks() option #17

Merged
merged 10 commits into from
Mar 1, 2022
Merged

Add n_free_blocks() option #17

merged 10 commits into from
Mar 1, 2022

Conversation

vbkaisetsu
Copy link
Member

@vbkaisetsu vbkaisetsu commented Mar 1, 2022

This branch removes the following constants:

  • INIT_CAPACITY: states is initialized by with_capacity() with this value, but it is often reallocated for a large automaton. On the other hand, this capacity is too large for small devices.
  • FREE_BLOCKS and FREE_STATES: Some small devices do not have enough memory, so I added an option to change these parameters.

@vbkaisetsu vbkaisetsu marked this pull request as ready for review March 1, 2022 08:18
@vbkaisetsu vbkaisetsu enabled auto-merge (squash) March 1, 2022 10:13
Copy link
Member

@kampersanda kampersanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your update.

As I commented, making FREE_BLOCKS smaller does not necessarily mean that the memory usage will be smaller.
However, a large extras should not be allocated in stack memory, and I agree with the modification.

src/builder.rs Outdated Show resolved Hide resolved
src/builder.rs Outdated Show resolved Hide resolved
@vbkaisetsu
Copy link
Member Author

I am using a tiny machine that has just a few amount of the memory.

In this machine, num_free_blocks=16 is too large to construct an automaton, so I added a new description.

This is an example. In this environment, num_free_blocks must be smaller than or equal to 11.

#![feature(alloc_error_handler)]
#![no_main]
#![no_std]

use panic_halt as _;

use core::alloc::Layout;

use alloc_cortex_m::CortexMHeap;
use cortex_m_rt::entry;
use cortex_m_semihosting::{hprintln, debug};

#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();

const HEAP_SIZE: usize = 40 * 1024; // 40 KiB

#[entry]
fn main() -> ! {
    unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }

    let patterns = &["this", "is", "a", "test"];

    let pma = daachorse::DoubleArrayAhoCorasickBuilder::new().num_free_blocks(16).build(patterns);

    if pma.is_ok() {
        hprintln!("OK!").unwrap();
    } else {
        hprintln!("ERROR!").unwrap();
    }

    debug::exit(debug::EXIT_SUCCESS);

    loop {}
}

#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
    hprintln!("<<< allocation error >>>").unwrap();
    loop {}
}
$ qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel target/thumbv7m-none-eabi/release/daachorse-lm3s6965evb
Timer with period zero, disabling
<<< allocation error >>>

@vbkaisetsu vbkaisetsu merged commit 158e51b into main Mar 1, 2022
@vbkaisetsu vbkaisetsu deleted the n-free-blocks branch March 2, 2022 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants