Skip to content

Commit 6be1b83

Browse files
committed
add some Allocator-related documentation
1 parent f9665a0 commit 6be1b83

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

capnp/src/message.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,25 +358,38 @@ where
358358
}
359359

360360
/// An object that allocates memory for a Cap'n Proto message as it is being built.
361+
/// Users of capnproto-rust who wish to provide memory in non-standard ways should
362+
/// implement this trait. Objects implementing this trait are intended to be wrapped
363+
/// by `capnp::private::BuilderArena`, which handles calling the methods at the appropriate
364+
/// times, including calling `deallocate_segment()` on drop.
365+
///
366+
/// # Safety
367+
/// Implementions must ensure all of the following:
368+
/// 1. The memory returned by `allocate_segment` is initialized to all zeroes.
369+
/// 2. The memory returned by `allocate_segment` is valid until `deallocate_segment()`
370+
/// is called on it.
371+
/// 3. The allocated memory does not overlap with other allocated memory.
372+
/// 4. The allocated memory is 8-byte aligned (or the "unaligned" feature is enabled
373+
/// for the capnp crate).
361374
pub unsafe trait Allocator {
362375
/// Allocates zeroed memory for a new segment, returning a pointer to the start of the segment
363376
/// and a u32 indicating the length of the segment in words. The allocated segment must be
364377
/// at least `minimum_size` words long (`minimum_size * 8` bytes long). Allocator implementations
365378
/// commonly allocate much more than the minimum, to reduce the total number of segments needed.
366379
/// A reasonable strategy is to allocate the maximum of `minimum_size` and twice the size of the
367380
/// previous segment.
368-
///
369-
/// UNSAFETY ALERT: Implementors must ensure all of the following:
370-
/// 1. the returned memory is initialized to all zeroes,
371-
/// 2. the returned memory is valid until deallocate_segment() is called on it,
372-
/// 3. the memory doesn't overlap with other allocated memory,
373-
/// 4. the memory is 8-byte aligned (or the "unaligned" feature is enabled for the capnp crate).
374381
fn allocate_segment(&mut self, minimum_size: u32) -> (*mut u8, u32);
375382

376383
/// Indicates that a segment, previously allocated via allocate_segment(), is no longer in use.
377384
/// `word_size` is the length of the segment in words, as returned from `allocate_segment()`.
378385
/// `words_used` is always less than or equal to `word_size`, and indicates how many
379386
/// words (contiguous from the start of the segment) were possibly written with non-zero values.
387+
///
388+
/// # Safety
389+
/// Callers must only call this method on a pointer that has previously been been returned
390+
/// from `allocate_segment()`, and only once on each such segment. `word_size` must
391+
/// equal the word size returned from `allocate_segment()`, and `words_used` must be at
392+
/// most `word_size`.
380393
fn deallocate_segment(&mut self, ptr: *mut u8, word_size: u32, words_used: u32);
381394
}
382395

@@ -493,6 +506,8 @@ where
493506
TypedBuilder::new(self)
494507
}
495508

509+
/// Retrieves the underlying `Allocator`, deallocating all currently-allocated
510+
/// segments.
496511
pub fn into_allocator(self) -> A {
497512
self.arena.into_allocator()
498513
}

capnp/src/private/arena.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ where
251251
self.len() == 0
252252
}
253253

254+
/// Retrieves the underlying `Allocator`, deallocating all currently-allocated
255+
/// segments.
254256
pub fn into_allocator(self) -> A {
255257
let mut inner = self.inner.into_inner();
256258
inner.deallocate_all();

0 commit comments

Comments
 (0)