diff options
Diffstat (limited to 'include/lib/ArduinoJson/Memory/Alignment.hpp')
-rw-r--r-- | include/lib/ArduinoJson/Memory/Alignment.hpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/include/lib/ArduinoJson/Memory/Alignment.hpp b/include/lib/ArduinoJson/Memory/Alignment.hpp new file mode 100644 index 0000000..bf16798 --- /dev/null +++ b/include/lib/ArduinoJson/Memory/Alignment.hpp @@ -0,0 +1,60 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Namespace.hpp> + +#include <stddef.h> // size_t + +namespace ARDUINOJSON_NAMESPACE { + +#if ARDUINOJSON_ENABLE_ALIGNMENT + +inline bool isAligned(size_t value) { + const size_t mask = sizeof(void *) - 1; + size_t addr = value; + return (addr & mask) == 0; +} + +inline size_t addPadding(size_t bytes) { + const size_t mask = sizeof(void *) - 1; + return (bytes + mask) & ~mask; +} + +template <size_t bytes> +struct AddPadding { + static const size_t mask = sizeof(void *) - 1; + static const size_t value = (bytes + mask) & ~mask; +}; + +#else + +inline bool isAligned(size_t) { + return true; +} + +inline size_t addPadding(size_t bytes) { + return bytes; +} + +template <size_t bytes> +struct AddPadding { + static const size_t value = bytes; +}; + +#endif + +template <typename T> +inline bool isAligned(T *ptr) { + return isAligned(reinterpret_cast<size_t>(ptr)); +} + +template <typename T> +inline T *addPadding(T *p) { + size_t address = addPadding(reinterpret_cast<size_t>(p)); + return reinterpret_cast<T *>(address); +} + +} // namespace ARDUINOJSON_NAMESPACE |