Skip to content

Commit 512a295

Browse files
committed
Add align_ptr_size_left() to utils_common.h
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 8c63afe commit 512a295

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/utils/utils_common.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef UMF_COMMON_H
1111
#define UMF_COMMON_H 1
1212

13+
#include <stdint.h>
1314
#include <stdio.h>
1415
#include <stdlib.h>
1516
#include <string.h>
@@ -77,6 +78,25 @@ static inline void *Zalloc(size_t s) {
7778
} \
7879
} while (0)
7980

81+
// align a pointer and a size
82+
static inline void align_ptr_size(void **ptr, size_t *size, size_t alignment) {
83+
uintptr_t p = (uintptr_t)*ptr;
84+
size_t s = *size;
85+
86+
// align pointer to 'alignment' bytes and adjust the size
87+
size_t rest = p & (alignment - 1);
88+
if (rest) {
89+
p += alignment - rest;
90+
s -= alignment - rest;
91+
}
92+
93+
ASSERT((p & (alignment - 1)) == 0);
94+
ASSERT((s & (alignment - 1)) == 0);
95+
96+
*ptr = (void *)p;
97+
*size = s;
98+
}
99+
80100
static inline size_t align_size(size_t size, size_t alignment) {
81101
// align size to 'alignment' bytes
82102
size_t rest = size & (alignment - 1);

0 commit comments

Comments
 (0)