Skip to content

Commit edfd847

Browse files
committed
fundamental: Move alignment logic to memory-util-fundamental.h
Aligning is closely related to memory management, so let's move these macros and functions to memory-util-fundamental.h. This will allow us to move assertion related logic out of macro-fundamental.h as well in a later commit.
1 parent 5a124e8 commit edfd847

File tree

2 files changed

+76
-76
lines changed

2 files changed

+76
-76
lines changed

src/fundamental/macro-fundamental.h

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -438,82 +438,6 @@
438438
(typeof(memory)) NULL; \
439439
})
440440

441-
static inline size_t ALIGN_TO(size_t l, size_t ali) {
442-
assert(ISPOWEROF2(ali));
443-
444-
if (l > SIZE_MAX - (ali - 1))
445-
return SIZE_MAX; /* indicate overflow */
446-
447-
return ((l + (ali - 1)) & ~(ali - 1));
448-
}
449-
450-
static inline uint64_t ALIGN_TO_U64(uint64_t l, uint64_t ali) {
451-
assert(ISPOWEROF2(ali));
452-
453-
if (l > UINT64_MAX - (ali - 1))
454-
return UINT64_MAX; /* indicate overflow */
455-
456-
return ((l + (ali - 1)) & ~(ali - 1));
457-
}
458-
459-
static inline size_t ALIGN_DOWN(size_t l, size_t ali) {
460-
assert(ISPOWEROF2(ali));
461-
462-
return l & ~(ali - 1);
463-
}
464-
465-
static inline uint64_t ALIGN_DOWN_U64(uint64_t l, uint64_t ali) {
466-
assert(ISPOWEROF2(ali));
467-
468-
return l & ~(ali - 1);
469-
}
470-
471-
static inline size_t ALIGN_OFFSET(size_t l, size_t ali) {
472-
assert(ISPOWEROF2(ali));
473-
474-
return l & (ali - 1);
475-
}
476-
477-
static inline uint64_t ALIGN_OFFSET_U64(uint64_t l, uint64_t ali) {
478-
assert(ISPOWEROF2(ali));
479-
480-
return l & (ali - 1);
481-
}
482-
483-
#define ALIGN2(l) ALIGN_TO(l, 2)
484-
#define ALIGN4(l) ALIGN_TO(l, 4)
485-
#define ALIGN8(l) ALIGN_TO(l, 8)
486-
#define ALIGN2_PTR(p) ((void*) ALIGN2((uintptr_t) p))
487-
#define ALIGN4_PTR(p) ((void*) ALIGN4((uintptr_t) p))
488-
#define ALIGN8_PTR(p) ((void*) ALIGN8((uintptr_t) p))
489-
#define ALIGN(l) ALIGN_TO(l, sizeof(void*))
490-
#define ALIGN_PTR(p) ((void*) ALIGN((uintptr_t) (p)))
491-
492-
/* Checks if the specified pointer is aligned as appropriate for the specific type */
493-
#define IS_ALIGNED16(p) (((uintptr_t) p) % alignof(uint16_t) == 0)
494-
#define IS_ALIGNED32(p) (((uintptr_t) p) % alignof(uint32_t) == 0)
495-
#define IS_ALIGNED64(p) (((uintptr_t) p) % alignof(uint64_t) == 0)
496-
497-
/* Same as ALIGN_TO but callable in constant contexts. */
498-
#define CONST_ALIGN_TO(l, ali) \
499-
__builtin_choose_expr( \
500-
__builtin_constant_p(l) && \
501-
__builtin_constant_p(ali) && \
502-
CONST_ISPOWEROF2(ali) && \
503-
(l <= SIZE_MAX - (ali - 1)), /* overflow? */ \
504-
((l) + (ali) - 1) & ~((ali) - 1), \
505-
VOID_0)
506-
507-
/* Similar to ((t *) (void *) (p)) to cast a pointer. The macro asserts that the pointer has a suitable
508-
* alignment for type "t". This exists for places where otherwise "-Wcast-align=strict" would issue a
509-
* warning or if you want to assert that the cast gives a pointer of suitable alignment. */
510-
#define CAST_ALIGN_PTR(t, p) \
511-
({ \
512-
const void *_p = (p); \
513-
assert(((uintptr_t) _p) % alignof(t) == 0); \
514-
(t *) _p; \
515-
})
516-
517441
#define UPDATE_FLAG(orig, flag, b) \
518442
((b) ? ((orig) | (flag)) : ((orig) & ~(flag)))
519443
#define SET_FLAG(v, flag, b) \

src/fundamental/memory-util-fundamental.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,79 @@ static inline void array_cleanup(const ArrayCleanup *c) {
142142
*p = (empty); \
143143
} \
144144
}
145+
146+
static inline size_t ALIGN_TO(size_t l, size_t ali) {
147+
assert(ISPOWEROF2(ali));
148+
149+
if (l > SIZE_MAX - (ali - 1))
150+
return SIZE_MAX; /* indicate overflow */
151+
152+
return ((l + (ali - 1)) & ~(ali - 1));
153+
}
154+
155+
static inline uint64_t ALIGN_TO_U64(uint64_t l, uint64_t ali) {
156+
assert(ISPOWEROF2(ali));
157+
158+
if (l > UINT64_MAX - (ali - 1))
159+
return UINT64_MAX; /* indicate overflow */
160+
161+
return ((l + (ali - 1)) & ~(ali - 1));
162+
}
163+
164+
static inline size_t ALIGN_DOWN(size_t l, size_t ali) {
165+
assert(ISPOWEROF2(ali));
166+
167+
return l & ~(ali - 1);
168+
}
169+
170+
static inline uint64_t ALIGN_DOWN_U64(uint64_t l, uint64_t ali) {
171+
assert(ISPOWEROF2(ali));
172+
173+
return l & ~(ali - 1);
174+
}
175+
176+
static inline size_t ALIGN_OFFSET(size_t l, size_t ali) {
177+
assert(ISPOWEROF2(ali));
178+
179+
return l & (ali - 1);
180+
}
181+
182+
static inline uint64_t ALIGN_OFFSET_U64(uint64_t l, uint64_t ali) {
183+
assert(ISPOWEROF2(ali));
184+
185+
return l & (ali - 1);
186+
}
187+
188+
#define ALIGN2(l) ALIGN_TO(l, 2)
189+
#define ALIGN4(l) ALIGN_TO(l, 4)
190+
#define ALIGN8(l) ALIGN_TO(l, 8)
191+
#define ALIGN2_PTR(p) ((void*) ALIGN2((uintptr_t) p))
192+
#define ALIGN4_PTR(p) ((void*) ALIGN4((uintptr_t) p))
193+
#define ALIGN8_PTR(p) ((void*) ALIGN8((uintptr_t) p))
194+
#define ALIGN(l) ALIGN_TO(l, sizeof(void*))
195+
#define ALIGN_PTR(p) ((void*) ALIGN((uintptr_t) (p)))
196+
197+
/* Checks if the specified pointer is aligned as appropriate for the specific type */
198+
#define IS_ALIGNED16(p) (((uintptr_t) p) % alignof(uint16_t) == 0)
199+
#define IS_ALIGNED32(p) (((uintptr_t) p) % alignof(uint32_t) == 0)
200+
#define IS_ALIGNED64(p) (((uintptr_t) p) % alignof(uint64_t) == 0)
201+
202+
/* Same as ALIGN_TO but callable in constant contexts. */
203+
#define CONST_ALIGN_TO(l, ali) \
204+
__builtin_choose_expr( \
205+
__builtin_constant_p(l) && \
206+
__builtin_constant_p(ali) && \
207+
CONST_ISPOWEROF2(ali) && \
208+
(l <= SIZE_MAX - (ali - 1)), /* overflow? */ \
209+
((l) + (ali) - 1) & ~((ali) - 1), \
210+
VOID_0)
211+
212+
/* Similar to ((t *) (void *) (p)) to cast a pointer. The macro asserts that the pointer has a suitable
213+
* alignment for type "t". This exists for places where otherwise "-Wcast-align=strict" would issue a
214+
* warning or if you want to assert that the cast gives a pointer of suitable alignment. */
215+
#define CAST_ALIGN_PTR(t, p) \
216+
({ \
217+
const void *_p = (p); \
218+
assert(((uintptr_t) _p) % alignof(t) == 0); \
219+
(t *) _p; \
220+
})

0 commit comments

Comments
 (0)