Skip to content

Commit f0fdc8c

Browse files
committed
Add align_ptr() to utils_common.h
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent a17b61e commit f0fdc8c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/utils/utils_common.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ static inline size_t align_size(size_t size, size_t alignment) {
9696
}
9797
}
9898

99+
static inline void *align_ptr(void *ptr, size_t alignment) {
100+
// align pointer to 'alignment' bytes
101+
uintptr_t uptr = (uintptr_t)ptr;
102+
uintptr_t rest = uptr & (alignment - 1);
103+
if (rest) {
104+
return (void *)(uptr - rest + alignment);
105+
} else {
106+
return ptr;
107+
}
108+
}
109+
99110
#ifdef __cplusplus
100111
}
101112
#endif

0 commit comments

Comments
 (0)