From a24bd4696a8a43ffe0841b9df845e89da8d82ce0 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Tue, 12 Feb 2013 10:20:51 -0500 Subject: score: New routines to align addresses up or down. --- cpukit/score/inline/rtems/score/address.inl | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'cpukit/score/inline/rtems/score/address.inl') diff --git a/cpukit/score/inline/rtems/score/address.inl b/cpukit/score/inline/rtems/score/address.inl index 17bcd0a016..590a80f4a7 100644 --- a/cpukit/score/inline/rtems/score/address.inl +++ b/cpukit/score/inline/rtems/score/address.inl @@ -142,6 +142,50 @@ RTEMS_INLINE_ROUTINE bool _Addresses_Is_in_range ( return (address >= base && address <= limit); } +/** + * @brief Align address to nearest multiple of alignment, rounding up. + * + * This function returns the given address aligned to the given alignment. + * If the address already is aligned, or if alignment is 0, the address is + * returned as is. The returned address is greater than or equal to the + * given address. + * + * @param[in] address is the address to align. + * @param[in] alignment is the boundary for alignment and must be a power of 2 + * + * @return Returns the aligned address. + */ +RTEMS_INLINE_ROUTINE void *_Addresses_Align_up( + void *address, + size_t alignment +) +{ + uintptr_t mask = alignment - (uintptr_t)1; + return (void*)(((uintptr_t)address + mask) & ~mask); +} + +/** + * @brief Align address to nearest multiple of alignment, truncating. + * + * This function returns the given address aligned to the given alignment. + * If the address already is aligned, or if alignment is 0, the address is + * returned as is. The returned address is less than or equal to the + * given address. + * + * @param[in] address is the address to align. + * @param[in] alignment is the boundary for alignment and must be a power of 2. + * + * @return Returns the aligned address. + */ +RTEMS_INLINE_ROUTINE void *_Addresses_Align_down( + void *address, + size_t alignment +) +{ + uintptr_t mask = alignment - (uintptr_t)1; + return (void*)((uintptr_t)address & ~mask); +} + /** @} */ #endif -- cgit v1.2.3