@@ -358,25 +358,38 @@ where
358
358
}
359
359
360
360
/// 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).
361
374
pub unsafe trait Allocator {
362
375
/// Allocates zeroed memory for a new segment, returning a pointer to the start of the segment
363
376
/// and a u32 indicating the length of the segment in words. The allocated segment must be
364
377
/// at least `minimum_size` words long (`minimum_size * 8` bytes long). Allocator implementations
365
378
/// commonly allocate much more than the minimum, to reduce the total number of segments needed.
366
379
/// A reasonable strategy is to allocate the maximum of `minimum_size` and twice the size of the
367
380
/// 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).
374
381
fn allocate_segment ( & mut self , minimum_size : u32 ) -> ( * mut u8 , u32 ) ;
375
382
376
383
/// Indicates that a segment, previously allocated via allocate_segment(), is no longer in use.
377
384
/// `word_size` is the length of the segment in words, as returned from `allocate_segment()`.
378
385
/// `words_used` is always less than or equal to `word_size`, and indicates how many
379
386
/// 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`.
380
393
fn deallocate_segment ( & mut self , ptr : * mut u8 , word_size : u32 , words_used : u32 ) ;
381
394
}
382
395
@@ -493,6 +506,8 @@ where
493
506
TypedBuilder :: new ( self )
494
507
}
495
508
509
+ /// Retrieves the underlying `Allocator`, deallocating all currently-allocated
510
+ /// segments.
496
511
pub fn into_allocator ( self ) -> A {
497
512
self . arena . into_allocator ( )
498
513
}
0 commit comments