buflib: Add pinned get/put data functions

These are more efficient than separate pin/unpin calls because
pin count increment and decrement can be done cheaply when the
data pointer is known.

Secondly, pinned access can be made safe against preemption by
hardware interrupts or other CPU cores; buflib_get_data() can't.
This makes it more useful under different threading models and
for SMP targets; both of which are not particularly relevant to
Rockbox now, but might be in the future.

Change-Id: I09284251b83bbbc59ef88a494c8fda26a7f7ef26
This commit is contained in:
Aidan MacDonald 2023-01-14 18:20:59 +00:00
parent 92565e9246
commit 800bc000a0
5 changed files with 109 additions and 35 deletions

View file

@ -168,6 +168,18 @@ unsigned buflib_pin_count(struct buflib_context *ctx, int handle)
return h->pin_count;
}
void _buflib_malloc_put_data_pinned(struct buflib_context *ctx, void *data)
{
for (int i = 0; i < ctx->num_allocs; ++i)
{
if (ctx->allocs[i].user == data)
{
ctx->allocs[i].pin_count--;
break;
}
}
}
int buflib_free(struct buflib_context *ctx, int handle)
{
if (handle <= 0)