diff options
| author | 2009-08-17 13:33:27 -0700 | |
|---|---|---|
| committer | 2009-08-17 15:31:25 -0700 | |
| commit | 84a6d041e2c69d4d0b69269a0b59e725c9f119a3 (patch) | |
| tree | 6841d5b1d696e27f66c30ede34c73d30cf5cc5c3 | |
| parent | 7f87b14a5b569b5526725fcf2d942156948d064c (diff) | |
Support for marshalling pointers / intptr_t in Parcel.
Some refactoring to eliminate code duplication in Parcel implementation.
| -rw-r--r-- | include/binder/Parcel.h | 11 | ||||
| -rw-r--r-- | include/utils/Debug.h | 2 | ||||
| -rw-r--r-- | libs/binder/Parcel.cpp | 158 |
3 files changed, 76 insertions, 95 deletions
diff --git a/include/binder/Parcel.h b/include/binder/Parcel.h index 58c2d9ad76..ba6c7111f5 100644 --- a/include/binder/Parcel.h +++ b/include/binder/Parcel.h @@ -74,6 +74,7 @@ public: status_t writeInt64(int64_t val); status_t writeFloat(float val); status_t writeDouble(double val); + status_t writeIntPtr(intptr_t val); status_t writeCString(const char* str); status_t writeString8(const String8& str); status_t writeString16(const String16& str); @@ -109,6 +110,8 @@ public: status_t readFloat(float *pArg) const; double readDouble() const; status_t readDouble(double *pArg) const; + intptr_t readIntPtr() const; + status_t readIntPtr(intptr_t *pArg) const; const char* readCString() const; String8 readString8() const; @@ -163,6 +166,14 @@ private: void initState(); void scanForFds() const; + template<class T> + status_t readAligned(T *pArg) const; + + template<class T> T readAligned() const; + + template<class T> + status_t writeAligned(T val); + status_t mError; uint8_t* mData; size_t mDataSize; diff --git a/include/utils/Debug.h b/include/utils/Debug.h index 21d04bdcec..d9ed32d7df 100644 --- a/include/utils/Debug.h +++ b/include/utils/Debug.h @@ -29,6 +29,8 @@ template<> struct CompileTimeAssert<true> {}; #define COMPILE_TIME_ASSERT(_exp) \ template class CompileTimeAssert< (_exp) >; #endif +#define COMPILE_TIME_ASSERT_FUNCTION_SCOPE(_exp) \ + CompileTimeAssert<( _exp )>(); // --------------------------------------------------------------------------- diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index 785a3c5ddb..e397bce1cf 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -562,54 +562,27 @@ restart_write: status_t Parcel::writeInt32(int32_t val) { - if ((mDataPos+sizeof(val)) <= mDataCapacity) { -restart_write: - *reinterpret_cast<int32_t*>(mData+mDataPos) = val; - return finishWrite(sizeof(val)); - } - - status_t err = growData(sizeof(val)); - if (err == NO_ERROR) goto restart_write; - return err; + return writeAligned(val); } status_t Parcel::writeInt64(int64_t val) { - if ((mDataPos+sizeof(val)) <= mDataCapacity) { -restart_write: - *reinterpret_cast<int64_t*>(mData+mDataPos) = val; - return finishWrite(sizeof(val)); - } - - status_t err = growData(sizeof(val)); - if (err == NO_ERROR) goto restart_write; - return err; + return writeAligned(val); } status_t Parcel::writeFloat(float val) { - if ((mDataPos+sizeof(val)) <= mDataCapacity) { -restart_write: - *reinterpret_cast<float*>(mData+mDataPos) = val; - return finishWrite(sizeof(val)); - } - - status_t err = growData(sizeof(val)); - if (err == NO_ERROR) goto restart_write; - return err; + return writeAligned(val); } status_t Parcel::writeDouble(double val) { - if ((mDataPos+sizeof(val)) <= mDataCapacity) { -restart_write: - *reinterpret_cast<double*>(mData+mDataPos) = val; - return finishWrite(sizeof(val)); - } + return writeAligned(val); +} - status_t err = growData(sizeof(val)); - if (err == NO_ERROR) goto restart_write; - return err; +status_t Parcel::writeIntPtr(intptr_t val) +{ + return writeAligned(val); } status_t Parcel::writeCString(const char* str) @@ -768,103 +741,98 @@ const void* Parcel::readInplace(size_t len) const return NULL; } -status_t Parcel::readInt32(int32_t *pArg) const -{ - if ((mDataPos+sizeof(int32_t)) <= mDataSize) { +template<class T> +status_t Parcel::readAligned(T *pArg) const { + COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); + + if ((mDataPos+sizeof(T)) <= mDataSize) { const void* data = mData+mDataPos; - mDataPos += sizeof(int32_t); - *pArg = *reinterpret_cast<const int32_t*>(data); + mDataPos += sizeof(T); + *pArg = *reinterpret_cast<const T*>(data); return NO_ERROR; } else { return NOT_ENOUGH_DATA; } } +template<class T> +T Parcel::readAligned() const { + T result; + if (readAligned(&result) != NO_ERROR) { + result = 0; + } + + return result; +} + +template<class T> +status_t Parcel::writeAligned(T val) { + COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); + + if ((mDataPos+sizeof(val)) <= mDataCapacity) { +restart_write: + *reinterpret_cast<T*>(mData+mDataPos) = val; + return finishWrite(sizeof(val)); + } + + status_t err = growData(sizeof(val)); + if (err == NO_ERROR) goto restart_write; + return err; +} + +status_t Parcel::readInt32(int32_t *pArg) const +{ + return readAligned(pArg); +} + int32_t Parcel::readInt32() const { - if ((mDataPos+sizeof(int32_t)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(int32_t); - LOGV("readInt32 Setting data pos of %p to %d\n", this, mDataPos); - return *reinterpret_cast<const int32_t*>(data); - } - return 0; + return readAligned<int32_t>(); } status_t Parcel::readInt64(int64_t *pArg) const { - if ((mDataPos+sizeof(int64_t)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(int64_t); - *pArg = *reinterpret_cast<const int64_t*>(data); - LOGV("readInt64 Setting data pos of %p to %d\n", this, mDataPos); - return NO_ERROR; - } else { - return NOT_ENOUGH_DATA; - } + return readAligned(pArg); } int64_t Parcel::readInt64() const { - if ((mDataPos+sizeof(int64_t)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(int64_t); - LOGV("readInt64 Setting data pos of %p to %d\n", this, mDataPos); - return *reinterpret_cast<const int64_t*>(data); - } - return 0; + return readAligned<int64_t>(); } status_t Parcel::readFloat(float *pArg) const { - if ((mDataPos+sizeof(float)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(float); - LOGV("readFloat Setting data pos of %p to %d\n", this, mDataPos); - *pArg = *reinterpret_cast<const float*>(data); - return NO_ERROR; - } else { - return NOT_ENOUGH_DATA; - } + return readAligned(pArg); } float Parcel::readFloat() const { - if ((mDataPos+sizeof(float)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(float); - LOGV("readFloat Setting data pos of %p to %d\n", this, mDataPos); - return *reinterpret_cast<const float*>(data); - } - return 0; + return readAligned<float>(); } status_t Parcel::readDouble(double *pArg) const { - if ((mDataPos+sizeof(double)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(double); - LOGV("readDouble Setting data pos of %p to %d\n", this, mDataPos); - *pArg = *reinterpret_cast<const double*>(data); - return NO_ERROR; - } else { - return NOT_ENOUGH_DATA; - } + return readAligned(pArg); } double Parcel::readDouble() const { - if ((mDataPos+sizeof(double)) <= mDataSize) { - const void* data = mData+mDataPos; - mDataPos += sizeof(double); - LOGV("readDouble Setting data pos of %p to %d\n", this, mDataPos); - return *reinterpret_cast<const double*>(data); - } - return 0; + return readAligned<double>(); +} + +status_t Parcel::readIntPtr(intptr_t *pArg) const +{ + return readAligned(pArg); +} + + +intptr_t Parcel::readIntPtr() const +{ + return readAligned<intptr_t>(); } |