fitzgen / bumpalo

A fast bump allocation arena for Rust
https://docs.rs/bumpalo
Apache License 2.0
1.42k stars 112 forks source link

Add method to return currently allocated bytes size? #57

Closed novacrazy closed 4 years ago

novacrazy commented 4 years ago

I'm working with an application that prints diagnostic messages after running an operation to show how much memory was used during that operation, then proceeds on with the next operation. However, because the next operation relies on memory allocated in the previous operation, there are still active immutable borrows from the Bump allocator at the point where diagnostic information should be logged. Therefore, I cannot simply use arena.iter_allocated_chunks().map(|chunk| chunk.len()).sum() to return the allocated size, because that requires a mutable borrow.

Would you be willing to add a method for querying such information from an immutable Bump instance? Something like Bump::allocated_bytes(&self) -> usize

fitzgen commented 4 years ago

This seems reasonable to me. Would you like to make a pull request?

novacrazy commented 4 years ago

I'm not familiar with the codebase and reasoning for things enough to be confident I could write a safe implementation in the time I have. However, if you would be willing outline what it would take to make a safe implementation of such a function, I can give it a shot.

fitzgen commented 4 years ago

The dealloc_chunk_list helper function shows how you can iterate over each chunk. Implementing this new method should be basically the same as that, but instead of deallocating each chunk, summing each chunk's size (example of getting a chunk's size here, this could be factored out to a helper method on ChunkFooter).

Is that enough to get you started? Anything I can clear up?