From 6c9245aff4a2f47e4effc91c2ba484932abfef39 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Wed, 19 Nov 2014 18:30:15 +0000 Subject: [PATCH] Align a variable to a compile time value --- aligned_variable.h | 195 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 aligned_variable.h diff --git a/aligned_variable.h b/aligned_variable.h new file mode 100644 index 00000000..4328441b --- /dev/null +++ b/aligned_variable.h @@ -0,0 +1,195 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. + +Copyright(c) 2014 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef __ETL_ALIGNED_VARIABLE__ +#define __ETL_ALIGNED_VARIABLE__ + +///\defgroup aligned_variable aligned_variable +/// Creates a variable of the specified type at the specified alignment. +/// \ingroup utilities + +namespace etl +{ + template + struct aligned_variable; + +#if defined(COMPILER_MICROSOFT) + /// Align 1 + template + struct aligned_variable + { + __declspec(align(1)) T value; + }; + + /// Align 2 + template + struct aligned_variable + { + __declspec(align(2)) T value; + }; + + /// Align 4 + template + struct aligned_variable + { + __declspec(align(4)) T value; + }; + + /// Align 8 + template + struct aligned_variable + { + __declspec(align(8)) T value; + }; + + /// Align 16 + template + struct aligned_variable + { + __declspec(align(16)) T value; + }; +#endif + +#if defined(COMPILER_GCC) + /// Align 1 + template + struct aligned_variable + { + T value __attribute__((aligned(1))); + }; + + /// Align 2 + template + struct aligned_variable + { + T value __attribute__((aligned(2))); + }; + + /// Align 4 + template + struct aligned_variable + { + T value __attribute__((aligned(4))); + }; + + /// Align 8 + template + struct aligned_variable + { + T value __attribute__((aligned(8))); + }; + + /// Align 16 + template + struct aligned_variable + { + T value __attribute__((aligned(16))); + }; +#endif + +#if defined(COMPILER_KEIL_ARM) + /// Align 1 + template + struct aligned_variable + { + __align(1) T value; + }; + + /// Align 2 + template + struct aligned_variable + { + __align(2) T value; + }; + + /// Align 4 + template + struct aligned_variable + { + __align(4) T value; +}; + + /// Align 8 + template + struct aligned_variable + { + __align(8) T value; + }; + + /// Align 16 + template + struct aligned_variable + { + __align(16) T value; + }; +#endif + +#if defined (COMPILER_IAR_ARM) + /// Align 1 + template + struct aligned_variable + { +#pragma data_alignment=1 + T value; + }; + + /// Align 2 + template + struct aligned_variable + { +#pragma data_alignment=2 + T value; + }; + + /// Align 4 + template + struct aligned_variable + { +#pragma data_alignment=4 + T value; + }; + + /// Align 8 + template + struct aligned_variable + { +#pragma data_alignment=8 + T value; + }; + + /// Align 16 + template + struct aligned_variable + { +#pragma data_alignment=16 + T value; + }; +#endif +} + +#endif \ No newline at end of file