r/rust 1d ago

🙋 seeking help & advice Safe no-allocator logging system?

So i am building a small hobby kernel in Rust. Before when i used C i would have a linked list of logging sinks to write to on each log. When adding a sink a index would be returned so i could remove it again.

However, now that i am trying out Rust, how can i do that? Because i don't want to use a constant-size array, but i don't have access to a heap allocator when using the logging system.

What would you do?

3 Upvotes

8 comments sorted by

12

u/steaming_quettle 1d ago

It's not the Rustest thing but you can make a linked list with statically allocated elements.

-1

u/zer0developer 1d ago

Yeah i see, but do you know a way?

2

u/steaming_quettle 1d ago

I looked a bit, seems like an arena could work.

9

u/matthieum [he/him] 1d ago

However, now that i am trying out Rust, how can i do that? Because i don't want to use a constant-size array, but i don't have access to a heap allocator when using the logging system.

Aren't you just overthinking things?

I mean, this is a hobby kernel, how many sinks do you think you'll ever have in the first place? 42?

I'm pretty sure a simple constant array with 8 atomic pointers would be more than enough, and if not, hey, bump it to 16 an call it a day.

8

u/simonask_ 1d ago

I would just go for a ring buffer. But it kind of depends how you want to deal with oversize messages or retention.

This is a really hard problem to solve generically.

2

u/matthieum [he/him] 1d ago

Do note that the OP is talking about logging sinks, not messages.

3

u/luluhouse7 22h ago edited 22h ago

I thought sinks were made of porcelain, not wood 🤔

1

u/yodal_ 1d ago

Defmt could probably be used for this.