pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

Add static version of queue

Open daveythacher opened this issue 3 months ago • 3 comments

I was looking into the queue API and I noticed that it works off the heap. It should be possible to implement this as static? https://github.com/raspberrypi/pico-sdk/blob/6a7db34ff63345a7badec79ebea3aaef1712f374/src/common/pico_util/queue.c#L11-L18

It is a little more risky, but for people that do not want to use heap they are probably stuck with that.

void static_queue_init_with_spinlock(queue_t *q, uint element_size, uint element_count, uint spinlock_num, uint8_t *storage) {
    lock_init(&q->core, spinlock_num);
    q->data = storage;
    q->element_count = (uint16_t)element_count;
    q->element_size = (uint16_t)element_size;
    q->wptr = 0;
    q->rptr = 0;
}

daveythacher avatar Mar 17 '24 08:03 daveythacher

Yes, that would be good

peterharperuk avatar Mar 18 '24 10:03 peterharperuk

In your proposed new function, it might also be worth passing in a storage_size parameter, and then asserting that storage_size >= ((element_count + 1) * element_size) ?

lurch avatar Mar 18 '24 11:03 lurch

Forgot about the assert. I had considered it but did not see the point in changing the return signature. (We can be mislead.)

Note there is no nullptr check.

daveythacher avatar Mar 18 '24 12:03 daveythacher