{"thread":{"id":"60652","subject":"[PATCH] mem-pool: simplify alignment calculation","startedAt":"2023-12-24T17:02:06Z","lastAt":"2023-12-24T17:02:06Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"486017","messageId":"3e15d11a-bd19-49ca-b674-9b50e0ba7fc2@web.de","threadId":"60652","inReplyTo":null,"subject":"[PATCH] mem-pool: simplify alignment calculation","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2023-12-24T17:02:04Z","receivedAt":"2023-12-24T17:02:06Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Use DIV_ROUND_UP in mem_pool_alloc() to round the allocation length to\nthe next multiple of GIT_MAX_ALIGNMENT instead of twiddling bits\nexplicitly.  This is shorter and clearer, to the point that we no longer\nneed the comment that explains what's being calculated.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\nLatest Clang emits the same x64 instructions for both versions; GCC only\nemits the supposedly optimal output for the patched version:\nhttps://godbolt.org/z/jPscnPqna\n\n mem-pool.c | 4 +---\n 1 file changed, 1 insertion(+), 3 deletions(-)\n\ndiff --git a/mem-pool.c b/mem-pool.c\nindex 2b25521e2d..2078c22b09 100644\n--- a/mem-pool.c\n+++ b/mem-pool.c\n@@ -89,9 +89,7 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)\n \tstruct mp_block *p = NULL;\n \tvoid *r;\n\n-\t/* round up to a 'GIT_MAX_ALIGNMENT' alignment */\n-\tif (len & (GIT_MAX_ALIGNMENT - 1))\n-\t\tlen += GIT_MAX_ALIGNMENT - (len & (GIT_MAX_ALIGNMENT - 1));\n+\tlen = DIV_ROUND_UP(len, GIT_MAX_ALIGNMENT) * GIT_MAX_ALIGNMENT;\n\n \tif (pool->mp_block &&\n \t    pool->mp_block->end - pool->mp_block->next_free >= len)\n--\n2.43.0\n"}]}