From 840f6b79c73b7714843f38d179dbbd6bf45b822d Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Mon, 28 Mar 2022 21:30:58 +0100 Subject: 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 --- firmware/buflib.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'firmware/buflib.c') diff --git a/firmware/buflib.c b/firmware/buflib.c index 05e489ea14..4b557eb59d 100644 --- a/firmware/buflib.c +++ b/firmware/buflib.c @@ -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; } -- cgit v1.2.3