bdwgc-alloc 0.7.0

impl GlobalAlloc and Allocator for bdwgc
# bdwgc-rust

[![GitHub Action](https://img.shields.io/github/actions/workflow/status/bdwgc/bdwgc-rust/test.yaml?branch=main&style=flat-square)](https://github.com/bdwgc/bdwgc-rust/actions)
[![Crate](https://img.shields.io/crates/v/bdwgc-alloc.svg?style=flat-square)](https://crates.io/crates/bdwgc-alloc)
[![License](https://img.shields.io/github/license/bdwgc/bdwgc-rust.svg?style=flat-square)](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE)

[`GlobalAlloc`](https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html) and [`Allocator`](https://doc.rust-lang.org/std/alloc/trait.Allocator.html) implementations for [`bdwgc`][bdwgc], the conservative garbage collector.

This crate is for use cases in which developers need to integrate [`bdwgc`][bdwgc] into their programs written in Rust (e.g. writing a runtime library in Rust for their own programming language whose GC is done by [`bdwgc`][bdwgc].)

The raw bindings to the C API of [`bdwgc`][bdwgc] that this crate is built on are available as the `bdwgc-alloc-sys` crate.

## Usage

```rust
use bdwgc_alloc::Allocator;
use std::thread::spawn;

#[global_allocator]
static GLOBAL_ALLOCATOR: Allocator = unsafe { Allocator::new() };

fn main() {
    unsafe { Allocator::initialize() }

    // Unreachable objects are collected automatically.
    Box::leak(Box::new([0u8; 256]));

    // Threads other than a main thread must be registered to a collector before allocation.
    spawn(|| {
        let _registration = unsafe { Allocator::register_current_thread() }.unwrap();

        Box::leak(Box::new([0u8; 256]));
    })
    .join()
    .unwrap();
}
```

To allocate only some objects by the collector, pass the allocator to collections instead of making it the global one. Note that the collector does not scan memory of other allocators, so never keep the only pointers to such objects there.

```rust
use bdwgc_alloc::Allocator;

fn main() {
    unsafe { Allocator::initialize() }

    // The vector is kept only on a stack of a main thread.
    let mut numbers = Vec::new_in(unsafe { Allocator::new() });

    numbers.push(42);
}
```

By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature:

```sh
cargo build --no-default-features --features cmake
```

## License

[MIT](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE)

[bdwgc]: https://github.com/bdwgc/bdwgc