Add static version of queue #1670

Open
opened 2024-03-17 03:43:18 -05:00 by daveythacher · 4 comments
daveythacher commented 2024-03-17 03:43:18 -05:00 (Migrated from github.com)

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?
github.com/raspberrypi/pico-sdk@6a7db34ff6/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;
}
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. ```C 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; } ```
peterharperuk commented 2024-03-18 05:27:45 -05:00 (Migrated from github.com)

Yes, that would be good

Yes, that would be good
lurch commented 2024-03-18 06:14:24 -05:00 (Migrated from github.com)

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) ?

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)` ?
daveythacher commented 2024-03-18 07:21:21 -05:00 (Migrated from github.com)

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.

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.
el-bert0 commented 2026-08-23 08:38:46 -05:00 (Migrated from github.com)

Great idea, I find it useful. I would propose to append _static instead of prepend static_ as all the functions of the queue.h starts with queue_

Great idea, I find it useful. I would propose to append `_static` instead of prepend `static_` as all the functions of the `queue.h` starts with `queue_`
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-raspberrypi/pico-sdk#1670
No description provided.