Giter VIP home page Giter VIP logo

Comments (6)

dwrensha avatar dwrensha commented on July 21, 2024

You'll need to provide your own implementation of the Allocator trait:

/// An object that allocates memory for a Cap'n Proto message as it is being built.
/// Users of capnproto-rust who wish to provide memory in non-standard ways should
/// implement this trait. Objects implementing this trait are intended to be wrapped
/// by `capnp::private::BuilderArena`, which handles calling the methods at the appropriate
/// times, including calling `deallocate_segment()` on drop.
///
/// # Safety
/// Implementions must ensure all of the following:
/// 1. The memory returned by `allocate_segment` is initialized to all zeroes.
/// 2. The memory returned by `allocate_segment` is valid until `deallocate_segment()`
/// is called on it.
/// 3. The allocated memory does not overlap with other allocated memory.
/// 4. The allocated memory is 8-byte aligned (or the "unaligned" feature is enabled
/// for the capnp crate).
pub unsafe trait Allocator {
/// Allocates zeroed memory for a new segment, returning a pointer to the start of the segment
/// and a u32 indicating the length of the segment in words. The allocated segment must be
/// at least `minimum_size` words long (`minimum_size * 8` bytes long). Allocator implementations
/// commonly allocate much more than the minimum, to reduce the total number of segments needed.
/// A reasonable strategy is to allocate the maximum of `minimum_size` and twice the size of the
/// previous segment.
fn allocate_segment(&mut self, minimum_size: u32) -> (*mut u8, u32);
/// Indicates that a segment, previously allocated via allocate_segment(), is no longer in use.
/// `word_size` is the length of the segment in words, as returned from `allocate_segment()`.
/// `words_used` is always less than or equal to `word_size`, and indicates how many
/// words (contiguous from the start of the segment) were possibly written with non-zero values.
///
/// # Safety
/// Callers must only call this method on a pointer that has previously been been returned
/// from `allocate_segment()`, and only once on each such segment. `word_size` must
/// equal the word size returned from `allocate_segment()`, and `words_used` must be at
/// most `word_size`.
unsafe fn deallocate_segment(&mut self, ptr: *mut u8, word_size: u32, words_used: u32);
}

One way to do it would be to have a wrapper for a single &mut[u8]. That would work simlarly to ScratchSpaceHeapAllocator, except without the HeapAllocator for when the segment fills up. (You would need to panic when the segment fills.)

from capnproto-rust.

zer0-droids avatar zer0-droids commented on July 21, 2024

Thank you @dwrensha for the quick reply! I will have a look at this.

from capnproto-rust.

dwrensha avatar dwrensha commented on July 21, 2024

I opened #441, which adds SingleSegmentAllocator.

from capnproto-rust.

zer0-droids avatar zer0-droids commented on July 21, 2024

Hi @dwrensha,

First, many thanks for the quick new release!

Unfortunately I still have issues to compile in my environment (no-std, no-alloc) something similar as the test case you added. The allocate_zeroed_vec() method returns a Vec which is not available, but it looks quite easy to fix, by passing to capnp::message::SingleSegmentAllocator::new() a reference to an aligned u8 slice.

The other problem I have seems harder to solve:

error[E0433]: failed to resolve: could not find Builderinmessage``

Indeed, the struct Builder is decorated with #[cfg(feature = "alloc")], so it is not available in my environment. We can't simply remove it since it depends on BuilderArenaImpl which uses a Vec.

from capnproto-rust.

dwrensha avatar dwrensha commented on July 21, 2024

Oops!

We need a no-alloc version of BuilderArenaImpl. The straightforward way to achieve that would be to hard code it to allow only one segment in the no-alloc case.

from capnproto-rust.

dwrensha avatar dwrensha commented on July 21, 2024

I opened #442.

from capnproto-rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.