Make Flattenable not virtual

Fallout from the Flattenable change, update all its uses.

Additionnaly, fix/tighten size checks when (un)flatten()ing
things.

Removed the assumption by some flattenables (e.g.: Fence)
that the size passed to them would be exact (it can
and will be larger in some cases)

The code in Parcel is a bit complicated so that we don't
have to expose the full implementation (and also to
keep the code smallish).

Change-Id: I0bf1c8aca2a3128491b4f45510bc46667e566dde
diff --git a/libs/gui/Sensor.cpp b/libs/gui/Sensor.cpp
index c52a88f..d84c370 100644
--- a/libs/gui/Sensor.cpp
+++ b/libs/gui/Sensor.cpp
@@ -98,85 +98,85 @@
     return mVersion;
 }
 
-size_t Sensor::getSize() const
+size_t Sensor::getFlattenedSize() const
 {
-    return  sizeof(int32_t) + ((mName.length() + 3) & ~3) +
-            sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
+    size_t fixedSize =
             sizeof(int32_t) * 3 +
             sizeof(float) * 4 +
             sizeof(int32_t);
+
+    size_t variableSize =
+            sizeof(int32_t) + FlattenableUtils::align<4>(mName.length()) +
+            sizeof(int32_t) + FlattenableUtils::align<4>(mVendor.length());
+
+    return fixedSize + variableSize;
 }
 
-static inline
-size_t write(void* buffer, size_t offset, const String8& value) {
-    memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
-    return (value.length() + 3) & ~3;
-}
+status_t Sensor::flatten(void* buffer, size_t size) const {
+    if (size < getFlattenedSize()) {
+        return NO_MEMORY;
+    }
 
-static inline
-size_t write(void* buffer, size_t offset, float value) {
-    *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
-    return sizeof(float);
-}
+    FlattenableUtils::write(buffer, size, mName.length());
+    memcpy(static_cast<char*>(buffer), mName.string(), mName.length());
+    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mName.length()));
 
-static inline
-size_t write(void* buffer, size_t offset, int32_t value) {
-    *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
-    return sizeof(int32_t);
-}
+    FlattenableUtils::write(buffer, size, mVendor.length());
+    memcpy(static_cast<char*>(buffer), mVendor.string(), mVendor.length());
+    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mVendor.length()));
 
-status_t Sensor::flatten(void* buffer) const
-{
-    size_t offset = 0;
-    offset += write(buffer, offset, int32_t(mName.length()));
-    offset += write(buffer, offset, mName);
-    offset += write(buffer, offset, int32_t(mVendor.length()));
-    offset += write(buffer, offset, mVendor);
-    offset += write(buffer, offset, mVersion);
-    offset += write(buffer, offset, mHandle);
-    offset += write(buffer, offset, mType);
-    offset += write(buffer, offset, mMinValue);
-    offset += write(buffer, offset, mMaxValue);
-    offset += write(buffer, offset, mResolution);
-    offset += write(buffer, offset, mPower);
-    offset += write(buffer, offset, mMinDelay);
+    FlattenableUtils::write(buffer, size, mVersion);
+    FlattenableUtils::write(buffer, size, mHandle);
+    FlattenableUtils::write(buffer, size, mType);
+    FlattenableUtils::write(buffer, size, mMinValue);
+    FlattenableUtils::write(buffer, size, mMaxValue);
+    FlattenableUtils::write(buffer, size, mResolution);
+    FlattenableUtils::write(buffer, size, mPower);
+    FlattenableUtils::write(buffer, size, mMinDelay);
     return NO_ERROR;
 }
 
-static inline
-size_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
-    value->setTo(static_cast<char const*>(buffer) + offset, len);
-    return (len + 3) & ~3;
-}
+status_t Sensor::unflatten(void const* buffer, size_t size) {
+    size_t len;
 
-static inline
-size_t read(void const* buffer, size_t offset, float* value) {
-    *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
-    return sizeof(float);
-}
+    if (size < sizeof(size_t)) {
+        return NO_MEMORY;
+    }
+    FlattenableUtils::read(buffer, size, len);
+    if (size < len) {
+        return NO_MEMORY;
+    }
+    mName.setTo(static_cast<char const*>(buffer), len);
+    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
 
-static inline
-size_t read(void const* buffer, size_t offset, int32_t* value) {
-    *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
-    return sizeof(int32_t);
-}
 
-status_t Sensor::unflatten(void const* buffer, size_t size)
-{
-    int32_t len;
-    size_t offset = 0;
-    offset += read(buffer, offset, &len);
-    offset += read(buffer, offset, &mName, len);
-    offset += read(buffer, offset, &len);
-    offset += read(buffer, offset, &mVendor, len);
-    offset += read(buffer, offset, &mVersion);
-    offset += read(buffer, offset, &mHandle);
-    offset += read(buffer, offset, &mType);
-    offset += read(buffer, offset, &mMinValue);
-    offset += read(buffer, offset, &mMaxValue);
-    offset += read(buffer, offset, &mResolution);
-    offset += read(buffer, offset, &mPower);
-    offset += read(buffer, offset, &mMinDelay);
+    if (size < sizeof(size_t)) {
+        return NO_MEMORY;
+    }
+    FlattenableUtils::read(buffer, size, len);
+    if (size < len) {
+        return NO_MEMORY;
+    }
+    mVendor.setTo(static_cast<char const*>(buffer), len);
+    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
+
+    size_t fixedSize =
+            sizeof(int32_t) * 3 +
+            sizeof(float) * 4 +
+            sizeof(int32_t);
+
+    if (size < fixedSize) {
+        return NO_MEMORY;
+    }
+
+    FlattenableUtils::read(buffer, size, mVersion);
+    FlattenableUtils::read(buffer, size, mHandle);
+    FlattenableUtils::read(buffer, size, mType);
+    FlattenableUtils::read(buffer, size, mMinValue);
+    FlattenableUtils::read(buffer, size, mMaxValue);
+    FlattenableUtils::read(buffer, size, mResolution);
+    FlattenableUtils::read(buffer, size, mPower);
+    FlattenableUtils::read(buffer, size, mMinDelay);
     return NO_ERROR;
 }