jamesmunns / bbqueue

A SPSC, lockless, no_std, thread safe, queue, based on BipBuffers
Apache License 2.0
417 stars 51 forks source link

Implement storing data in a pointer location #70

Closed mattico closed 1 year ago

mattico commented 4 years ago

cc #67

This allows the BBuffer to use a pointed-to memory location rather than an owned array. Some use-cases would be shared-memory IPC, MMIO, or using a specific memory region on embedded. In my case, I want to use a 64KiB block SRAM for a DMA buffer on an STM32H7. It's not enough to just place the BBuffer there because I want the state variables to live in ITCM for speed, and I want to be able to use the whole buffer for data. There isn't an option for a typenum that's 2^16 - sizeof(BBuffer) so I can't even construct one of the right size currently.

I implemented this similar to other embedded libs I've seen by adding a BBStorage trait which is implemented by either a PtrSorage or an ArrayStorage. It worked out okay, but the ergonomics of the ArrayStorage suffer due to const fns being unable to return generic types. Perhaps someone can think of a solution.

mattico commented 4 years ago

Here's an example of using this to implement a DMA transfer on STM32: https://github.com/mattico/stm32h7xx-hal/blob/df7cfd347a87ad37f9d6b7dcbd69f2a9369fbd07/examples/dma_uart.rs

timvisee commented 4 years ago

I did attempt to use this on shared memory between processes, but failed.

I think the problem is that only the data buffer (buf) is placed at the pointed-to memory location. The other fields such as write and read are on the stack. So this doesn't actually allow sharing the bip buffer (with state) as a whole, but just the data buffer part. To use this for communication between two processes it would require allocating the rest of the fields on the pointed-to memory block as well. Is this correct (my knowledge is lacking here)?

Edit: yes, it seems you noted that here:

I'm trying to do a similar thing, making the buffer use a specific SRAM at a specific address ... but I don't want to use any of the SRAM memory for the bookkeeping variables.

mattico commented 4 years ago

Right, that's a feature for me, wish it was more helpful for you. I suppose what you need is placement-new or a custom Box that allocates in shared memory. You should be able to just move the BBBuffer into your shared memory just fine but you'll probably have to use a raw pointer.