From 1f313b942bb509d4a17a2f78b6d077ccbe7eb93f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 13 Dec 2011 01:54:59 +0100 Subject: [PATCH] * include/grub/types.h (grub_properly_aligned_t): New type. (GRUB_PROPERLY_ALIGNED_ARRAY): New macro. (grub_get_unaligned16): Add explicit casts. (grub_get_unaligned32): Likewise. (grub_get_unaligned64): Likewise. (grub_set_unaligned16): New function. (grub_set_unaligned32): Likewise. --- ChangeLog | 10 +++++++ include/grub/types.h | 66 ++++++++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index fd7add031..c64fe07b7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2011-12-13 Vladimir Serbinenko + + * include/grub/types.h (grub_properly_aligned_t): New type. + (GRUB_PROPERLY_ALIGNED_ARRAY): New macro. + (grub_get_unaligned16): Add explicit casts. + (grub_get_unaligned32): Likewise. + (grub_get_unaligned64): Likewise. + (grub_set_unaligned16): New function. + (grub_set_unaligned32): Likewise. + 2011-12-13 Vladimir Serbinenko * grub-core/normal/datetime.c (grub_weekday_names): Make const. diff --git a/include/grub/types.h b/include/grub/types.h index f057dd3d3..e61a9a5c6 100644 --- a/include/grub/types.h +++ b/include/grub/types.h @@ -140,6 +140,10 @@ typedef grub_int32_t grub_ssize_t; #define PTR_TO_UINT32(x) ((grub_uint32_t)(grub_uint64_t)(x)) #endif +typedef grub_uint64_t grub_properly_aligned_t; + +#define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[(size + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)] + /* The type for representing a file offset. */ typedef grub_uint64_t grub_off_t; @@ -222,31 +226,57 @@ static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t x) # define grub_cpu_to_le32_compile_time(x) ((grub_uint32_t) (x)) #endif /* ! WORDS_BIGENDIAN */ -static inline grub_uint16_t grub_get_unaligned16(void *ptr) +static inline grub_uint16_t grub_get_unaligned16 (const void *ptr) { - struct - { - grub_uint16_t d; - } __attribute__((packed)) *dd = ptr; - return dd->d; + struct grub_unaligned_uint16_t + { + grub_uint16_t d; + } __attribute__ ((packed)); + const struct grub_unaligned_uint16_t *dd + = (const struct grub_unaligned_uint16_t *) ptr; + return dd->d; } -static inline grub_uint32_t grub_get_unaligned32(void *ptr) +static inline void grub_set_unaligned16 (void *ptr, grub_uint16_t val) { - struct - { - grub_uint32_t d; - } __attribute__((packed)) *dd = ptr; - return dd->d; + struct grub_unaligned_uint16_t + { + grub_uint16_t d; + } __attribute__ ((packed)); + struct grub_unaligned_uint16_t *dd = (struct grub_unaligned_uint16_t *) ptr; + dd->d = val; } -static inline grub_uint64_t grub_get_unaligned64(void *ptr) +static inline grub_uint32_t grub_get_unaligned32 (const void *ptr) { - struct - { - grub_uint64_t d; - } __attribute__((packed)) *dd = ptr; - return dd->d; + struct grub_unaligned_uint32_t + { + grub_uint32_t d; + } __attribute__ ((packed)); + const struct grub_unaligned_uint32_t *dd + = (const struct grub_unaligned_uint32_t *) ptr; + return dd->d; +} + +static inline void grub_set_unaligned32 (void *ptr, grub_uint32_t val) +{ + struct grub_unaligned_uint32_t + { + grub_uint32_t d; + } __attribute__ ((packed)); + struct grub_unaligned_uint32_t *dd = (struct grub_unaligned_uint32_t *) ptr; + dd->d = val; +} + +static inline grub_uint64_t grub_get_unaligned64 (const void *ptr) +{ + struct grub_unaligned_uint64_t + { + grub_uint64_t d; + } __attribute__ ((packed)); + const struct grub_unaligned_uint64_t *dd + = (const struct grub_unaligned_uint64_t *)ptr; + return dd->d; } #endif /* ! GRUB_TYPES_HEADER */