From 7802cd067e247aed36ae20bb82ffc84abd7a27f6 Mon Sep 17 00:00:00 2001 From: Flower Black Date: Wed, 4 Dec 2024 11:16:36 +0800 Subject: [PATCH] Find order faster in KBuddyPagesAlloc when using GCC. --- Ubiquitous/XiZi_AIoT/softkernel/memory/buddy.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Ubiquitous/XiZi_AIoT/softkernel/memory/buddy.c b/Ubiquitous/XiZi_AIoT/softkernel/memory/buddy.c index 97817851a..ffebd5ea5 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/memory/buddy.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/memory/buddy.c @@ -77,8 +77,13 @@ static struct KPage* KBuddyPagesAlloc(struct KBuddy* pbuddy, int nPages) int i = 0, order = 0; // find order +#if defined(__GNUC__) + // see: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html + order = nPages ? (sizeof(int) * 8 - __builtin_clz(nPages) - 1) + !!(__builtin_popcount(nPages) != 1) : 0; +#else for (order = 0; (FREE_LIST_INDEX(order)) < nPages; order++) ; +#endif // find the free page list for (i = order; i < MAX_BUDDY_ORDER; i++) {