1 unstable release
| 0.1.0-alpha.1 | Dec 27, 2025 |
|---|
#948 in Memory management
215KB
4.5K
SLoC
memkit-bevy
Bevy ECS integration for the memkit ecosystem.
Version: 0.1.0-alpha.1
Overview
memkit-bevy provides seamless integration between memkit and the Bevy game engine. It automatically manages frame lifecycle and exposes the allocator as a Bevy resource.
Features
- Automatic Frame Lifecycle — Frame begin/end tied to Bevy's
First/Lastschedules - Resource Integration —
MkAllocatorResas a BevyResource - System Sets —
MkSystemSet::FrameBeginandMkSystemSet::FrameEndfor ordering - Zero Configuration — Just add the plugin and go
- Optional Prelude — Enable
bevy_preludefeature for full Bevy re-exports
Quick Start
use bevy::prelude::*;
use memkit_bevy::{MkPlugin, MkAllocatorRes};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(MkPlugin::default())
.add_systems(Update, my_system)
.run();
}
fn my_system(alloc: Res<MkAllocatorRes>) {
// Frame automatically managed by plugin
// Allocations are valid until end of frame
if let Some(data) = alloc.frame_box(MyData::new()) {
// ... use data ...
}
}
#[derive(Default)]
struct MyData {
value: f32,
}
Custom Configuration
use memkit::MkConfig;
use memkit_bevy::MkPlugin;
// Option 1: Custom config
let config = MkConfig {
frame_arena_size: 32 * 1024 * 1024, // 32 MB
..Default::default()
};
App::new()
.add_plugins(MkPlugin::with_config(config))
.run();
// Option 2: Just set arena size
App::new()
.add_plugins(MkPlugin::with_arena_size(64 * 1024 * 1024)) // 64 MB
.run();
System Ordering
memkit-bevy provides system sets for proper ordering:
use memkit_bevy::{MkPlugin, MkSystemSet};
App::new()
.add_plugins(MkPlugin::default())
// Run after frame begins
.add_systems(First, my_init_system.after(MkSystemSet::FrameBegin))
// Run before frame ends
.add_systems(Last, my_cleanup_system.before(MkSystemSet::FrameEnd))
.run();
Allocation Methods
fn my_system(alloc: Res<MkAllocatorRes>) {
// Raw pointer allocation (you must initialize)
let ptr: *mut MyStruct = alloc.frame_alloc::<MyStruct>();
// Boxed allocation (initialized)
let boxed: Option<MkFrameBox<MyStruct>> = alloc.frame_box(MyStruct::default());
// Slice allocation
let slice: Option<MkFrameSlice<f32>> = alloc.frame_slice::<f32>(1024);
// Check if frame is active
if alloc.is_frame_active() {
// Safe to allocate
}
}
Feature Flags
| Feature | Description |
|---|---|
bevy_prelude |
Re-exports bevy::prelude::* for convenience |
Types
| Type | Description |
|---|---|
MkPlugin |
Bevy plugin for memkit integration |
MkAllocatorRes |
Bevy resource wrapping MkAllocator |
MkSystemSet |
System sets for frame lifecycle ordering |
Compatibility
| memkit-bevy | Bevy |
|---|---|
| 0.12.x | 0.17.x |
How It Works
- Frame Begin (
Firstschedule): CallsMkAllocator::begin_frame() - Your Systems (
Update, etc.): UseRes<MkAllocatorRes>for frame allocations - Frame End (
Lastschedule): CallsMkAllocator::end_frame(), resetting the arena
All frame allocations are automatically freed when the frame ends — no manual cleanup needed.
License
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE.md file for details.
Dependencies
~12–27MB
~361K SLoC