百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rust-alloc-no-stdlib

> 编程语言
开源

一个与通用分配器的接口,这样一个不包含 stdlib 的 no_std Rust 库可以分配内存,无论是否链接了 stdlib。

179 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个与通用分配器的接口,这样一个不包含 stdlib 的 no_std Rust 库可以分配内存,无论是否链接了 stdlib。

Framework for allocating memory in #![no_std] modules.

Requirements

  • Rust 1.6

Documentation

Currently there is no standard way to allocate memory from within a module that is no_std. This provides a mechanism to describe a memory allocation that can be satisfied entirely on the stack, by unsafely linking to calloc, or by unsafely referencing a mutable global variable. This library currently will leak memory if free_cell isn't specifically invoked on memory.

However, if linked by a library that actually can depend on the stdlib then that library can simply pass in a few allocators and use the standard Box allocation and will free automatically.

This library should also make it possible to entirely jail a rust application that needs dynamic allocations by preallocating a maximum limit of data upfront using calloc and using seccomp to disallow future syscalls.

Usage

There are 3 modes for allocating memory, each with advantages and disadvantages

On the stack

This is possible without the stdlib at all However, this eats into the natural ulimit on the stack depth and generally limits the program to only a few megs of dynamically allocated data

Example:

…

On the heap, but uninitialized

This does allocate data every time it is requested, but it does not allocate the memory, so naturally it is unsafe. The caller must initialize the memory properly

rust
let mut halloc = unsafe{HeapAllocUninitialized::::new()};
{ // heap test
    let mut x = halloc.alloc_cell(100000);
    x[0] = 4;
    let mut y = halloc.alloc_cell(110000);
    y[0] = 5;
    let mut z = halloc.alloc_cell(120000);
    z[0] = 6;
    assert_eq!(y[0], 5);
    halloc.free_cell(y);
    assert_eq!(x[0], 4);
    assert_eq!(x[9], 0);
    assert_eq!(z[0], 6);
    ...
}

On the heap in a single pool allocation

This does a single big allocation on the heap, after which no further usage of the stdlib will happen. This can be useful for a jailed application that wishes to restrict syscalls at this point

…

What changed in 3.0.0

Forbid unsafe and moving of the Calloc malloc into the unsafe bin so we can forbid unsafe using the compiler.

Note: this is a breaking change for downstreams that referenced CallocBackingStore/AllocatorC without the "unsafe" feature (e.g. brotli's "seccomp" feature, which should now also enable alloc-no-stdlib/unsafe).

Contributors

  • Daniel Reiter Horn

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言