buflib: update first_free_handle in handle_alloc

Since we're scanning the handle table for the first free slot,
we know none of the scanned slots are free. Use that knowledge
to update first_free_handle and avoid rescanning filled slots
again when the next handle is allocated.

Change-Id: I457372f66c231168cfffa7e905d1e9fb80002f5f
This commit is contained in:
Aidan MacDonald 2022-03-28 21:30:58 +01:00
parent f622bcfe4f
commit 840f6b79c7

View file

@ -196,8 +196,17 @@ union buflib_data* handle_alloc(struct buflib_context *ctx)
if (handle >= ctx->alloc_end)
ctx->last_handle--;
else
{
/* We know the table is full, so update first_free_handle */
ctx->first_free_handle = ctx->last_handle - 1;
return NULL;
}
}
/* We know there are no free handles between the old first_free_handle
* and the found handle, therefore we can update first_free_handle */
ctx->first_free_handle = handle - 1;
handle->val = -1;
return handle;
}