diff mbox series

lib: sbi_scratch: Optimize the alignment code for alloc size

Message ID 20230309094607.72318-1-wxjstz@126.com
State Superseded
Headers show
Series lib: sbi_scratch: Optimize the alignment code for alloc size | expand

Commit Message

Xiang W March 9, 2023, 9:46 a.m. UTC
Signed-off-by: Xiang W <wxjstz@126.com>
---
 lib/sbi/sbi_scratch.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Andreas Schwab March 9, 2023, 9:57 a.m. UTC | #1
On Mär 09 2023, Xiang W wrote:

> +	size = (size + __SIZEOF_POINTER__ - 1) & ~(__SIZEOF_POINTER__ - 1);

It is probably more robust to convert (__SIZEOF_POINTER__ - 1) to
unsigned long before inverting.  This currently depends on the fact that
~(__SIZEOF_POINTER__ - 1) is a signed int, and thus sign extended, to
work correctly.
diff mbox series

Patch

diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
index 87b34c6..ea62105 100644
--- a/lib/sbi/sbi_scratch.c
+++ b/lib/sbi/sbi_scratch.c
@@ -59,8 +59,7 @@  unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	if (size & (__SIZEOF_POINTER__ - 1))
-		size = (size & ~(__SIZEOF_POINTER__ - 1)) + __SIZEOF_POINTER__;
+	size = (size + __SIZEOF_POINTER__ - 1) & ~(__SIZEOF_POINTER__ - 1);
 
 	spin_lock(&extra_lock);