summaryrefslogtreecommitdiff
path: root/include/utils
diff options
context:
space:
mode:
authorDavid Pursell <dpursell@google.com>2016-09-23 22:56:01 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-09-23 22:56:01 +0000
commit7bc0052ff105c016c929af2c874095c1f46fd32d (patch)
tree8812efb982587e300952c439f8a4051577f6e42b /include/utils
parent8e3427fb0452aef1c5ea26d087b883bd1e03b7fc (diff)
parent9f106bece820f61962d2b45fdb2653c4139fc8ab (diff)
downloadcore-7bc0052ff105c016c929af2c874095c1f46fd32d.tar.gz
Merge "Flattenable: switch from assignment to memcpy()." am: b2356a6993 am: bb276d8555
am: 9f106bece8 Change-Id: I43e4e5c6bb0aa1fd23a1d0d9776c9e2c8028527e
Diffstat (limited to 'include/utils')
-rw-r--r--include/utils/Flattenable.h11
1 files changed, 9 insertions, 2 deletions
diff --git a/include/utils/Flattenable.h b/include/utils/Flattenable.h
index 882a8b249..c37ac60c7 100644
--- a/include/utils/Flattenable.h
+++ b/include/utils/Flattenable.h
@@ -19,10 +19,13 @@
#include <stdint.h>
+#include <string.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/Debug.h>
+#include <type_traits>
+
namespace android {
@@ -60,14 +63,18 @@ public:
// write a POD structure
template<typename T>
static void write(void*& buffer, size_t& size, const T& value) {
- *static_cast<T*>(buffer) = value;
+ static_assert(std::is_trivially_copyable<T>::value,
+ "Cannot flatten a non-trivially-copyable type");
+ memcpy(buffer, &value, sizeof(T));
advance(buffer, size, sizeof(T));
}
// read a POD structure
template<typename T>
static void read(void const*& buffer, size_t& size, T& value) {
- value = *static_cast<T const*>(buffer);
+ static_assert(std::is_trivially_copyable<T>::value,
+ "Cannot unflatten a non-trivially-copyable type");
+ memcpy(&value, buffer, sizeof(T));
advance(buffer, size, sizeof(T));
}
};