diff options
Diffstat (limited to 'src/pal/src/include/pal/utils.h')
-rw-r--r-- | src/pal/src/include/pal/utils.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/pal/src/include/pal/utils.h b/src/pal/src/include/pal/utils.h index 3ddad4ae2f..f381d957ab 100644 --- a/src/pal/src/include/pal/utils.h +++ b/src/pal/src/include/pal/utils.h @@ -20,10 +20,66 @@ Abstract: #ifndef _PAL_UTILS_H_ #define _PAL_UTILS_H_ +#include <stdint.h> + #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Alignment helpers (copied for PAL use from stdmacros.h) + +inline size_t ALIGN_UP(size_t val, size_t alignment) +{ + // alignment must be a power of 2 for this implementation to work (need modulo otherwise) + _ASSERTE(0 == (alignment & (alignment - 1))); + size_t result = (val + (alignment - 1)) & ~(alignment - 1); + _ASSERTE(result >= val); // check for overflow + return result; +} + +inline void* ALIGN_UP(void* val, size_t alignment) +{ + return (void*)ALIGN_UP((size_t)val, alignment); +} + +inline uint8_t* ALIGN_UP(uint8_t* val, size_t alignment) +{ + return (uint8_t*)ALIGN_UP((size_t)val, alignment); +} + +inline size_t ALIGN_DOWN(size_t val, size_t alignment) +{ + // alignment must be a power of 2 for this implementation to work (need modulo otherwise) + _ASSERTE(0 == (alignment & (alignment - 1))); + size_t result = val & ~(alignment - 1); + return result; +} + +inline void* ALIGN_DOWN(void* val, size_t alignment) +{ + return (void*)ALIGN_DOWN((size_t)val, alignment); +} + +inline uint8_t* ALIGN_DOWN(uint8_t* val, size_t alignment) +{ + return (uint8_t*)ALIGN_DOWN((size_t)val, alignment); +} + +inline BOOL IS_ALIGNED(size_t val, size_t alignment) +{ + // alignment must be a power of 2 for this implementation to work (need modulo otherwise) + _ASSERTE(0 == (alignment & (alignment - 1))); + return 0 == (val & (alignment - 1)); +} + +inline BOOL IS_ALIGNED(const void* val, size_t alignment) +{ + return IS_ALIGNED((size_t)val, alignment); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + #ifdef __cplusplus extern "C" { |