From bcaa9465e99051bef52eeb2124b91dacaf2d7ee3 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Mon, 28 Mar 2022 21:23:39 +0100 Subject: buflib: optimize find_block_before Exiting the loop implies next_block == block, so remove that check. The check ret < block is false only if block is the first block, which can be checked before the loop, saving a few cycles in that case. Change-Id: Id493b5259a23a35a70b09dfe4bc4eacaf420760c --- firmware/buflib.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'firmware') diff --git a/firmware/buflib.c b/firmware/buflib.c index c6ec011653..4551fd8dca 100644 --- a/firmware/buflib.c +++ b/firmware/buflib.c @@ -664,23 +664,22 @@ find_block_before(struct buflib_context *ctx, union buflib_data* block, union buflib_data *ret = ctx->buf_start, *next_block = ret; + /* no previous block */ + if (next_block == block) + return NULL; + /* find the block that's before the current one */ - while (next_block < block) + while (next_block != block) { ret = next_block; next_block += abs(ret->val); } - /* If next_block == block, the above loop didn't go anywhere. If it did, - * and the block before this one is empty, that is the wanted one - */ - if (next_block == block && ret < block) - { - if (is_free && ret->val >= 0) /* NULL if found block isn't free */ - return NULL; - return ret; - } - return NULL; + /* don't return it if the found block isn't free */ + if (is_free && ret->val >= 0) + return NULL; + + return ret; } /* Free the buffer associated with handle_num. */ -- cgit v1.2.3