From 0ef47a6a150ea0bf41cdebd2b34bb1d72696ef8c Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Fri, 21 Oct 2011 15:18:28 -0700 Subject: [PATCH 1/4] mDirtyRegion is single threaded, but could be accessed from a hwc thread We now have mInvalidateRegion which holds the region to invalidate, it can be set from any thread as long as mInvalidateLock is held. We use fine-grained locking here because mInvalidateRegion can be set from anywhere, in particular frmo HWC callbacks. Bug: 5466774 Change-Id: Iafca20aa3f5b25a87755e65bde7b769aa8f997bc --- services/surfaceflinger/SurfaceFlinger.cpp | 18 ++++++++++++++++-- services/surfaceflinger/SurfaceFlinger.h | 7 +++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index 4b2866cb6..ba8f6308b 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -788,6 +788,8 @@ void SurfaceFlinger::handlePageFlip() } unlockPageFlip(currentLayers); + + mDirtyRegion.orSelf(getAndClearInvalidateRegion()); mDirtyRegion.andSelf(screenRegion); } @@ -1798,12 +1800,24 @@ status_t SurfaceFlinger::onTransact( } void SurfaceFlinger::repaintEverything() { - Mutex::Autolock _l(mStateLock); const DisplayHardware& hw(graphicPlane(0).displayHardware()); - mDirtyRegion.set(hw.bounds()); + const Rect bounds(hw.getBounds()); + setInvalidateRegion(Region(bounds)); signalEvent(); } +void SurfaceFlinger::setInvalidateRegion(const Region& reg) { + Mutex::Autolock _l(mInvalidateLock); + mInvalidateRegion = reg; +} + +Region SurfaceFlinger::getAndClearInvalidateRegion() { + Mutex::Autolock _l(mInvalidateLock); + Region reg(mInvalidateRegion); + mInvalidateRegion.clear(); + return reg; +} + // --------------------------------------------------------------------------- status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy, diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index 3c8f4e5cc..3284fdba9 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -308,6 +308,9 @@ private: void composeSurfaces(const Region& dirty); + void setInvalidateRegion(const Region& reg); + Region getAndClearInvalidateRegion(); + ssize_t addClientLayer(const sp& client, const sp& lbc); status_t addLayer_l(const sp& layer); @@ -367,6 +370,10 @@ private: bool mLayersRemoved; DefaultKeyedVector< wp, wp > mLayerMap; + // access must be protected by mInvalidateLock + mutable Mutex mInvalidateLock; + Region mInvalidateRegion; + // constant members (no synchronization needed for access) sp mServerHeap; surface_flinger_cblk_t* mServerCblk; From 1c46edd8c6a2fb58e2bdf841695c7fffa61ca923 Mon Sep 17 00:00:00 2001 From: The Android Automerger Date: Thu, 27 Oct 2011 17:39:53 -0700 Subject: [PATCH 2/4] Revert "Use ashmem for CursorWindows." This reverts commit 0cde89f5f025b7826be009ebb9673b970e180e32. --- include/binder/CursorWindow.h | 267 ++++++++++-------- libs/binder/CursorWindow.cpp | 514 ++++++++++++++++++---------------- libs/binder/Parcel.cpp | 4 +- 3 files changed, 419 insertions(+), 366 deletions(-) diff --git a/include/binder/CursorWindow.h b/include/binder/CursorWindow.h index 5d490ed98..d227244df 100644 --- a/include/binder/CursorWindow.h +++ b/include/binder/CursorWindow.h @@ -21,8 +21,18 @@ #include #include -#include -#include +#include +#include + +#define DEFAULT_WINDOW_SIZE 4096 +#define WINDOW_ALLOCATION_SIZE 4096 + +#define ROW_SLOT_CHUNK_NUM_ROWS 16 + +// Row slots are allocated in chunks of ROW_SLOT_CHUNK_NUM_ROWS, +// with an offset after the rows that points to the next chunk +#define ROW_SLOT_CHUNK_SIZE ((ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t)) + sizeof(uint32_t)) + #if LOG_NDEBUG @@ -36,157 +46,176 @@ #endif + +// When defined to true strings are stored as UTF8, otherwise they're UTF16 +#define WINDOW_STORAGE_UTF8 1 + +// When defined to true numberic values are stored inline in the field_slot_t, otherwise they're allocated in the window +#define WINDOW_STORAGE_INLINE_NUMERICS 1 + namespace android { +typedef struct +{ + uint32_t numRows; + uint32_t numColumns; +} window_header_t; + +typedef struct +{ + uint32_t offset; +} row_slot_t; + +typedef struct +{ + uint8_t type; + union { + double d; + int64_t l; + struct { + uint32_t offset; + uint32_t size; + } buffer; + } data; +} __attribute__((packed)) field_slot_t; + +#define FIELD_TYPE_NULL 0 +#define FIELD_TYPE_INTEGER 1 +#define FIELD_TYPE_FLOAT 2 +#define FIELD_TYPE_STRING 3 +#define FIELD_TYPE_BLOB 4 + /** * This class stores a set of rows from a database in a buffer. The begining of the - * window has first chunk of RowSlots, which are offsets to the row directory, followed by - * an offset to the next chunk in a linked-list of additional chunk of RowSlots in case + * window has first chunk of row_slot_ts, which are offsets to the row directory, followed by + * an offset to the next chunk in a linked-list of additional chunk of row_slot_ts in case * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a - * FieldSlot per column, which has the size, offset, and type of the data for that field. + * field_slot_t per column, which has the size, offset, and type of the data for that field. * Note that the data types come from sqlite3.h. - * - * Strings are stored in UTF-8. */ -class CursorWindow { - CursorWindow(const String8& name, int ashmemFd, - void* data, size_t size, bool readOnly); - +class CursorWindow +{ public: - /* Field types. */ - enum { - FIELD_TYPE_NULL = 0, - FIELD_TYPE_INTEGER = 1, - FIELD_TYPE_FLOAT = 2, - FIELD_TYPE_STRING = 3, - FIELD_TYPE_BLOB = 4, - }; + CursorWindow(size_t maxSize); + CursorWindow(){} + bool setMemory(const sp&); + ~CursorWindow(); - /* Opaque type that describes a field slot. */ - struct FieldSlot { - private: - int32_t type; - union { - double d; - int64_t l; - struct { - uint32_t offset; - uint32_t size; - } buffer; - } data; + bool initBuffer(bool localOnly); + sp getMemory() {return mMemory;} - friend class CursorWindow; - } __attribute((packed)); + size_t size() {return mSize;} + uint8_t * data() {return mData;} + uint32_t getNumRows() {return mHeader->numRows;} + uint32_t getNumColumns() {return mHeader->numColumns;} + void freeLastRow() { + if (mHeader->numRows > 0) { + mHeader->numRows--; + } + } + bool setNumColumns(uint32_t numColumns) + { + uint32_t cur = mHeader->numColumns; + if (cur > 0 && cur != numColumns) { + LOGE("Trying to go from %d columns to %d", cur, numColumns); + return false; + } + mHeader->numColumns = numColumns; + return true; + } - ~CursorWindow(); + int32_t freeSpace(); - static status_t create(const String8& name, size_t size, bool localOnly, - CursorWindow** outCursorWindow); - static status_t createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow); + void clear(); - status_t writeToParcel(Parcel* parcel); + /** + * Allocate a row slot and its directory. The returned + * pointer points to the begining of the row's directory + * or NULL if there wasn't room. The directory is + * initialied with NULL entries for each field. + */ + field_slot_t * allocRow(); - inline String8 name() { return mName; } - inline size_t size() { return mSize; } - inline size_t freeSpace() { return mSize - mHeader->freeOffset; } - inline uint32_t getNumRows() { return mHeader->numRows; } - inline uint32_t getNumColumns() { return mHeader->numColumns; } + /** + * Allocate a portion of the window. Returns the offset + * of the allocation, or 0 if there isn't enough space. + * If aligned is true, the allocation gets 4 byte alignment. + */ + uint32_t alloc(size_t size, bool aligned = false); - status_t clear(); - status_t setNumColumns(uint32_t numColumns); + /** + * Copy data into the window at the given offset. + */ + void copyIn(uint32_t offset, uint8_t const * data, size_t size); + void copyIn(uint32_t offset, int64_t data); + void copyIn(uint32_t offset, double data); - /** - * Allocate a row slot and its directory. - * The row is initialized will null entries for each field. - */ - status_t allocRow(); - status_t freeLastRow(); + void copyOut(uint32_t offset, uint8_t * data, size_t size); + int64_t copyOutLong(uint32_t offset); + double copyOutDouble(uint32_t offset); - status_t putBlob(uint32_t row, uint32_t column, const void* value, size_t size); - status_t putString(uint32_t row, uint32_t column, const char* value, size_t sizeIncludingNull); - status_t putLong(uint32_t row, uint32_t column, int64_t value); - status_t putDouble(uint32_t row, uint32_t column, double value); - status_t putNull(uint32_t row, uint32_t column); + bool putLong(unsigned int row, unsigned int col, int64_t value); + bool putDouble(unsigned int row, unsigned int col, double value); + bool putNull(unsigned int row, unsigned int col); - /** - * Gets the field slot at the specified row and column. - * Returns null if the requested row or column is not in the window. - */ - FieldSlot* getFieldSlot(uint32_t row, uint32_t column); + bool getLong(unsigned int row, unsigned int col, int64_t * valueOut); + bool getDouble(unsigned int row, unsigned int col, double * valueOut); + bool getNull(unsigned int row, unsigned int col, bool * valueOut); - inline int32_t getFieldSlotType(FieldSlot* fieldSlot) { - return fieldSlot->type; - } + uint8_t * offsetToPtr(uint32_t offset) {return mData + offset;} - inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) { + row_slot_t * allocRowSlot(); + + row_slot_t * getRowSlot(int row); + + /** + * return NULL if Failed to find rowSlot or + * Invalid rowSlot + */ + field_slot_t * getFieldSlotWithCheck(int row, int column); + field_slot_t * getFieldSlot(int row, int column) + { + int fieldDirOffset = getRowSlot(row)->offset; + return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column; + } + + int64_t getFieldSlotValueLong(field_slot_t* fieldSlot) { +#if WINDOW_STORAGE_INLINE_NUMERICS return fieldSlot->data.l; +#else + return copyOutLong(fieldSlot->data.buffer.offset); +#endif } - inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) { + double getFieldSlotValueDouble(field_slot_t* fieldSlot) { +#if WINDOW_STORAGE_INLINE_NUMERICS return fieldSlot->data.d; +#else + return copyOutDouble(fieldSlot->data.buffer.offset); +#endif } - inline const char* getFieldSlotValueString(FieldSlot* fieldSlot, - size_t* outSizeIncludingNull) { - *outSizeIncludingNull = fieldSlot->data.buffer.size; - return static_cast(offsetToPtr(fieldSlot->data.buffer.offset)); +#if WINDOW_STORAGE_UTF8 + char* getFieldSlotValueString(field_slot_t* fieldSlot) { + return reinterpret_cast(offsetToPtr(fieldSlot->data.buffer.offset)); } - - inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot, size_t* outSize) { - *outSize = fieldSlot->data.buffer.size; - return offsetToPtr(fieldSlot->data.buffer.offset); +#else + char16_t* getFieldSlotValueString(field_slot_t* fieldSlot) { + return reinterpret_cast(offsetToPtr(fieldSlot->data.buffer.offset)); } +#endif private: - static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100; - - struct Header { - // Offset of the lowest unused byte in the window. - uint32_t freeOffset; - - // Offset of the first row slot chunk. - uint32_t firstChunkOffset; - - uint32_t numRows; - uint32_t numColumns; - }; - - struct RowSlot { - uint32_t offset; - }; - - struct RowSlotChunk { - RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS]; - uint32_t nextChunkOffset; - }; - - String8 mName; - int mAshmemFd; - void* mData; + uint8_t * mData; size_t mSize; - bool mReadOnly; - Header* mHeader; - - inline void* offsetToPtr(uint32_t offset) { - return static_cast(mData) + offset; - } - - inline uint32_t offsetFromPtr(void* ptr) { - return static_cast(ptr) - static_cast(mData); - } + size_t mMaxSize; + window_header_t * mHeader; + sp mMemory; /** - * Allocate a portion of the window. Returns the offset - * of the allocation, or 0 if there isn't enough space. - * If aligned is true, the allocation gets 4 byte alignment. + * Offset of the lowest unused data byte in the array. */ - uint32_t alloc(size_t size, bool aligned = false); - - RowSlot* getRowSlot(uint32_t row); - RowSlot* allocRowSlot(); - - status_t putBlobOrString(uint32_t row, uint32_t column, - const void* value, size_t size, int32_t type); + uint32_t mFreeOffset; }; }; // namespace android diff --git a/libs/binder/CursorWindow.cpp b/libs/binder/CursorWindow.cpp index 1b85a71ca..b02374f1e 100644 --- a/libs/binder/CursorWindow.cpp +++ b/libs/binder/CursorWindow.cpp @@ -19,9 +19,8 @@ #include #include - -#include -#include +#include +#include #include #include @@ -29,325 +28,350 @@ namespace android { -CursorWindow::CursorWindow(const String8& name, int ashmemFd, - void* data, size_t size, bool readOnly) : - mName(name), mAshmemFd(ashmemFd), mData(data), mSize(size), mReadOnly(readOnly) { - mHeader = static_cast(mData); +CursorWindow::CursorWindow(size_t maxSize) : + mMaxSize(maxSize) +{ } -CursorWindow::~CursorWindow() { - ::munmap(mData, mSize); - ::close(mAshmemFd); +bool CursorWindow::setMemory(const sp& memory) +{ + mMemory = memory; + mData = (uint8_t *) memory->pointer(); + if (mData == NULL) { + return false; + } + mHeader = (window_header_t *) mData; + + // Make the window read-only + ssize_t size = memory->size(); + mSize = size; + mMaxSize = size; + mFreeOffset = size; +LOG_WINDOW("Created CursorWindow from existing IMemory: mFreeOffset = %d, numRows = %d, numColumns = %d, mSize = %d, mMaxSize = %d, mData = %p", mFreeOffset, mHeader->numRows, mHeader->numColumns, mSize, mMaxSize, mData); + return true; } -status_t CursorWindow::create(const String8& name, size_t size, bool localOnly, - CursorWindow** outCursorWindow) { - String8 ashmemName("CursorWindow: "); - ashmemName.append(name); - ashmemName.append(localOnly ? " (local)" : " (remote)"); +bool CursorWindow::initBuffer(bool localOnly) +{ + //TODO Use a non-memory dealer mmap region for localOnly - status_t result; - int ashmemFd = ashmem_create_region(ashmemName.string(), size); - if (ashmemFd < 0) { - result = -errno; - } else { - result = ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE); - if (result >= 0) { - void* data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0); - if (data == MAP_FAILED) { - result = -errno; - } else { - result = ashmem_set_prot_region(ashmemFd, PROT_READ); - if (result >= 0) { - CursorWindow* window = new CursorWindow(name, ashmemFd, - data, size, false /*readOnly*/); - result = window->clear(); - if (!result) { - LOG_WINDOW("Created new CursorWindow: freeOffset=%d, " - "numRows=%d, numColumns=%d, mSize=%d, mData=%p", - window->mHeader->freeOffset, - window->mHeader->numRows, - window->mHeader->numColumns, - window->mSize, window->mData); - *outCursorWindow = window; - return OK; - } - delete window; - } + sp heap; + heap = new MemoryHeapBase(mMaxSize, 0, "CursorWindow"); + if (heap != NULL) { + mMemory = new MemoryBase(heap, 0, mMaxSize); + if (mMemory != NULL) { + mData = (uint8_t *) mMemory->pointer(); + if (mData) { + mHeader = (window_header_t *) mData; + mSize = mMaxSize; + + // Put the window into a clean state + clear(); + LOG_WINDOW("Created CursorWindow with new MemoryDealer: mFreeOffset = %d, mSize = %d, mMaxSize = %d, mData = %p", mFreeOffset, mSize, mMaxSize, mData); + return true; } - ::munmap(data, size); - } - ::close(ashmemFd); - } - *outCursorWindow = NULL; - return result; -} - -status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow) { - String8 name = parcel->readString8(); - - status_t result; - int ashmemFd = parcel->readFileDescriptor(); - if (ashmemFd == int(BAD_TYPE)) { - result = BAD_TYPE; + } + LOGE("CursorWindow heap allocation failed"); + return false; } else { - ssize_t size = ashmem_get_size_region(ashmemFd); - if (size < 0) { - result = UNKNOWN_ERROR; - } else { - int dupAshmemFd = ::dup(ashmemFd); - if (dupAshmemFd < 0) { - result = -errno; - } else { - void* data = ::mmap(NULL, size, PROT_READ, MAP_SHARED, dupAshmemFd, 0); - if (data == MAP_FAILED) { - result = -errno; - } else { - CursorWindow* window = new CursorWindow(name, dupAshmemFd, - data, size, true /*readOnly*/); - LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, " - "numRows=%d, numColumns=%d, mSize=%d, mData=%p", - window->mHeader->freeOffset, - window->mHeader->numRows, - window->mHeader->numColumns, - window->mSize, window->mData); - *outCursorWindow = window; - return OK; - } - ::close(dupAshmemFd); - } - } + LOGE("failed to create the CursorWindow heap"); + return false; } - *outCursorWindow = NULL; - return result; } -status_t CursorWindow::writeToParcel(Parcel* parcel) { - status_t status = parcel->writeString8(mName); - if (!status) { - status = parcel->writeDupFileDescriptor(mAshmemFd); - } - return status; +CursorWindow::~CursorWindow() +{ + // Everything that matters is a smart pointer } -status_t CursorWindow::clear() { - if (mReadOnly) { - return INVALID_OPERATION; - } - - mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk); - mHeader->firstChunkOffset = sizeof(Header); +void CursorWindow::clear() +{ mHeader->numRows = 0; mHeader->numColumns = 0; - - RowSlotChunk* firstChunk = static_cast(offsetToPtr(mHeader->firstChunkOffset)); - firstChunk->nextChunkOffset = 0; - return OK; + mFreeOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE; + // Mark the first chunk's next 'pointer' as null + *((uint32_t *)(mData + mFreeOffset - sizeof(uint32_t))) = 0; } -status_t CursorWindow::setNumColumns(uint32_t numColumns) { - if (mReadOnly) { - return INVALID_OPERATION; +int32_t CursorWindow::freeSpace() +{ + int32_t freeSpace = mSize - mFreeOffset; + if (freeSpace < 0) { + freeSpace = 0; } - - uint32_t cur = mHeader->numColumns; - if ((cur > 0 || mHeader->numRows > 0) && cur != numColumns) { - LOGE("Trying to go from %d columns to %d", cur, numColumns); - return INVALID_OPERATION; - } - mHeader->numColumns = numColumns; - return OK; + return freeSpace; } -status_t CursorWindow::allocRow() { - if (mReadOnly) { - return INVALID_OPERATION; - } - +field_slot_t * CursorWindow::allocRow() +{ // Fill in the row slot - RowSlot* rowSlot = allocRowSlot(); + row_slot_t * rowSlot = allocRowSlot(); if (rowSlot == NULL) { - return NO_MEMORY; + return NULL; } // Allocate the slots for the field directory - size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot); - uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/); + size_t fieldDirSize = mHeader->numColumns * sizeof(field_slot_t); + uint32_t fieldDirOffset = alloc(fieldDirSize); if (!fieldDirOffset) { mHeader->numRows--; - LOG_WINDOW("The row failed, so back out the new row accounting " - "from allocRowSlot %d", mHeader->numRows); - return NO_MEMORY; + LOG_WINDOW("The row failed, so back out the new row accounting from allocRowSlot %d", mHeader->numRows); + return NULL; } - FieldSlot* fieldDir = static_cast(offsetToPtr(fieldDirOffset)); - memset(fieldDir, 0, fieldDirSize); + field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(fieldDirOffset); + memset(fieldDir, 0x0, fieldDirSize); - LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n", - mHeader->numRows - 1, offsetFromPtr(rowSlot), fieldDirSize, fieldDirOffset); +LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n", (mHeader->numRows - 1), ((uint8_t *)rowSlot) - mData, fieldDirSize, fieldDirOffset); rowSlot->offset = fieldDirOffset; - return OK; + + return fieldDir; } -status_t CursorWindow::freeLastRow() { - if (mReadOnly) { - return INVALID_OPERATION; - } - - if (mHeader->numRows > 0) { - mHeader->numRows--; - } - return OK; -} - -uint32_t CursorWindow::alloc(size_t size, bool aligned) { +uint32_t CursorWindow::alloc(size_t requestedSize, bool aligned) +{ + int32_t size; uint32_t padding; if (aligned) { // 4 byte alignment - padding = (~mHeader->freeOffset + 1) & 3; + padding = 4 - (mFreeOffset & 0x3); } else { padding = 0; } - uint32_t offset = mHeader->freeOffset + padding; - uint32_t nextFreeOffset = offset + size; - if (nextFreeOffset > mSize) { - LOGE("Window is full: requested allocation %d bytes, " - "free space %d bytes, window size %d bytes", - size, freeSpace(), mSize); - return 0; + size = requestedSize + padding; + + if (size > freeSpace()) { + LOGV("need to grow: mSize = %d, size = %d, freeSpace() = %d, numRows = %d", mSize, size, + freeSpace(), mHeader->numRows); + // Only grow the window if the first row doesn't fit + if (mHeader->numRows > 1) { + LOGV("not growing since there are already %d row(s), max size %d", mHeader->numRows, + mMaxSize); + return 0; + } + + // Find a new size that will fit the allocation + int allocated = mSize - freeSpace(); + int newSize = mSize + WINDOW_ALLOCATION_SIZE; + while (size > (newSize - allocated)) { + newSize += WINDOW_ALLOCATION_SIZE; + if (newSize > mMaxSize) { + LOGE("Attempting to grow window beyond max size (%d)", mMaxSize); + return 0; + } + } +LOG_WINDOW("found size %d", newSize); + mSize = newSize; } - mHeader->freeOffset = nextFreeOffset; + uint32_t offset = mFreeOffset + padding; + mFreeOffset += size; return offset; } -CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) { - uint32_t chunkPos = row; - RowSlotChunk* chunk = static_cast( - offsetToPtr(mHeader->firstChunkOffset)); - while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) { - chunk = static_cast(offsetToPtr(chunk->nextChunkOffset)); - chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS; +row_slot_t * CursorWindow::getRowSlot(int row) +{ + LOG_WINDOW("enter getRowSlot current row num %d, this row %d", mHeader->numRows, row); + int chunkNum = row / ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPos = row % ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t); + uint8_t * rowChunk = mData + sizeof(window_header_t); + for (int i = 0; i < chunkNum; i++) { + rowChunk = offsetToPtr(*((uint32_t *)(mData + chunkPtrOffset))); + chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t)); } - return &chunk->slots[chunkPos]; + return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t))); + LOG_WINDOW("exit getRowSlot current row num %d, this row %d", mHeader->numRows, row); } -CursorWindow::RowSlot* CursorWindow::allocRowSlot() { - uint32_t chunkPos = mHeader->numRows; - RowSlotChunk* chunk = static_cast( - offsetToPtr(mHeader->firstChunkOffset)); - while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) { - chunk = static_cast(offsetToPtr(chunk->nextChunkOffset)); - chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS; - } - if (chunkPos == ROW_SLOT_CHUNK_NUM_ROWS) { - if (!chunk->nextChunkOffset) { - chunk->nextChunkOffset = alloc(sizeof(RowSlotChunk), true /*aligned*/); - if (!chunk->nextChunkOffset) { +row_slot_t * CursorWindow::allocRowSlot() +{ + int chunkNum = mHeader->numRows / ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPos = mHeader->numRows % ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t); + uint8_t * rowChunk = mData + sizeof(window_header_t); +LOG_WINDOW("Allocating row slot, mHeader->numRows is %d, chunkNum is %d, chunkPos is %d", mHeader->numRows, chunkNum, chunkPos); + for (int i = 0; i < chunkNum; i++) { + uint32_t nextChunkOffset = *((uint32_t *)(mData + chunkPtrOffset)); +LOG_WINDOW("nextChunkOffset is %d", nextChunkOffset); + if (nextChunkOffset == 0) { + // Allocate a new row chunk + nextChunkOffset = alloc(ROW_SLOT_CHUNK_SIZE, true); + if (nextChunkOffset == 0) { return NULL; } + rowChunk = offsetToPtr(nextChunkOffset); +LOG_WINDOW("allocated new chunk at %d, rowChunk = %p", nextChunkOffset, rowChunk); + *((uint32_t *)(mData + chunkPtrOffset)) = rowChunk - mData; + // Mark the new chunk's next 'pointer' as null + *((uint32_t *)(rowChunk + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t))) = 0; + } else { +LOG_WINDOW("follwing 'pointer' to next chunk, offset of next pointer is %d", chunkPtrOffset); + rowChunk = offsetToPtr(nextChunkOffset); + chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t)); } - chunk = static_cast(offsetToPtr(chunk->nextChunkOffset)); - chunk->nextChunkOffset = 0; - chunkPos = 0; } - mHeader->numRows += 1; - return &chunk->slots[chunkPos]; + mHeader->numRows++; + + return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t))); } -CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) { - if (row >= mHeader->numRows || column >= mHeader->numColumns) { - LOGE("Failed to read row %d, column %d from a CursorWindow which " - "has %d rows, %d columns.", - row, column, mHeader->numRows, mHeader->numColumns); - return NULL; - } - RowSlot* rowSlot = getRowSlot(row); - if (!rowSlot) { - LOGE("Failed to find rowSlot for row %d.", row); - return NULL; - } - FieldSlot* fieldDir = static_cast(offsetToPtr(rowSlot->offset)); - return &fieldDir[column]; +field_slot_t * CursorWindow::getFieldSlotWithCheck(int row, int column) +{ + if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) { + LOGE("Failed to read row# %d, column# from a CursorWindow which has %d rows, %d columns.", + row, column, mHeader->numRows, mHeader->numColumns); + return NULL; + } + row_slot_t * rowSlot = getRowSlot(row); + if (!rowSlot) { + LOGE("Failed to find rowSlot for row %d", row); + return NULL; + } + if (rowSlot->offset == 0 || rowSlot->offset >= mSize) { + LOGE("Invalid rowSlot, offset = %d", rowSlot->offset); + return NULL; + } + int fieldDirOffset = rowSlot->offset; + return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column; } -status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) { - return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB); +void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size) +{ + assert(offset + size <= mSize); + memcpy(mData + offset, data, size); } -status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value, - size_t sizeIncludingNull) { - return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING); +void CursorWindow::copyIn(uint32_t offset, int64_t data) +{ + assert(offset + sizeof(int64_t) <= mSize); + memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t)); } -status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column, - const void* value, size_t size, int32_t type) { - if (mReadOnly) { - return INVALID_OPERATION; - } +void CursorWindow::copyIn(uint32_t offset, double data) +{ + assert(offset + sizeof(double) <= mSize); + memcpy(mData + offset, (uint8_t *)&data, sizeof(double)); +} - FieldSlot* fieldSlot = getFieldSlot(row, column); +void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size) +{ + assert(offset + size <= mSize); + memcpy(data, mData + offset, size); +} + +int64_t CursorWindow::copyOutLong(uint32_t offset) +{ + int64_t value; + assert(offset + sizeof(int64_t) <= mSize); + memcpy(&value, mData + offset, sizeof(int64_t)); + return value; +} + +double CursorWindow::copyOutDouble(uint32_t offset) +{ + double value; + assert(offset + sizeof(double) <= mSize); + memcpy(&value, mData + offset, sizeof(double)); + return value; +} + +bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); if (!fieldSlot) { - return BAD_VALUE; + return false; } - uint32_t offset = alloc(size); - if (!offset) { - return NO_MEMORY; - } - - memcpy(offsetToPtr(offset), value, size); - - fieldSlot->type = type; - fieldSlot->data.buffer.offset = offset; - fieldSlot->data.buffer.size = size; - return OK; -} - -status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) { - if (mReadOnly) { - return INVALID_OPERATION; - } - - FieldSlot* fieldSlot = getFieldSlot(row, column); - if (!fieldSlot) { - return BAD_VALUE; - } - - fieldSlot->type = FIELD_TYPE_INTEGER; +#if WINDOW_STORAGE_INLINE_NUMERICS fieldSlot->data.l = value; - return OK; +#else + int offset = alloc(sizeof(int64_t)); + if (!offset) { + return false; + } + + copyIn(offset, value); + + fieldSlot->data.buffer.offset = offset; + fieldSlot->data.buffer.size = sizeof(int64_t); +#endif + fieldSlot->type = FIELD_TYPE_INTEGER; + return true; } -status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) { - if (mReadOnly) { - return INVALID_OPERATION; - } - - FieldSlot* fieldSlot = getFieldSlot(row, column); +bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); if (!fieldSlot) { - return BAD_VALUE; + return false; } - fieldSlot->type = FIELD_TYPE_FLOAT; +#if WINDOW_STORAGE_INLINE_NUMERICS fieldSlot->data.d = value; - return OK; -} - -status_t CursorWindow::putNull(uint32_t row, uint32_t column) { - if (mReadOnly) { - return INVALID_OPERATION; +#else + int offset = alloc(sizeof(int64_t)); + if (!offset) { + return false; } - FieldSlot* fieldSlot = getFieldSlot(row, column); + copyIn(offset, value); + + fieldSlot->data.buffer.offset = offset; + fieldSlot->data.buffer.size = sizeof(double); +#endif + fieldSlot->type = FIELD_TYPE_FLOAT; + return true; +} + +bool CursorWindow::putNull(unsigned int row, unsigned int col) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); if (!fieldSlot) { - return BAD_VALUE; + return false; } fieldSlot->type = FIELD_TYPE_NULL; fieldSlot->data.buffer.offset = 0; fieldSlot->data.buffer.size = 0; - return OK; + return true; +} + +bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOut) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) { + return false; + } + + *valueOut = getFieldSlotValueLong(fieldSlot); + return true; +} + +bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueOut) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot || fieldSlot->type != FIELD_TYPE_FLOAT) { + return false; + } + + *valueOut = getFieldSlotValueDouble(fieldSlot); + return true; +} + +bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot) { + return false; + } + + if (fieldSlot->type != FIELD_TYPE_NULL) { + *valueOut = false; + } else { + *valueOut = true; + } + return true; } }; // namespace android diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index c7180cee0..608877eb7 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -752,7 +752,7 @@ status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob) int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); if (result < 0) { - status = result; + status = -result; } else { void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { @@ -760,7 +760,7 @@ status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob) } else { result = ashmem_set_prot_region(fd, PROT_READ); if (result < 0) { - status = result; + status = -result; } else { status = writeInt32(1); if (!status) { From 10a6f64fac3a5489cef6ccc593fd42c12c31a4bf Mon Sep 17 00:00:00 2001 From: The Android Automerger Date: Thu, 27 Oct 2011 17:41:26 -0700 Subject: [PATCH 3/4] Revert "Fix regression in CursorWindow.copyStingToBuffer." This reverts commit d0ff68da6a606602235fb8749473999e3d1bde53. --- include/utils/Unicode.h | 7 ------- libs/utils/Unicode.cpp | 13 ++++++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/utils/Unicode.h b/include/utils/Unicode.h index 927353377..6afb291f4 100644 --- a/include/utils/Unicode.h +++ b/include/utils/Unicode.h @@ -149,13 +149,6 @@ void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst); */ ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen); -/** - * Convert UTF-8 to UTF-16 including surrogate pairs. - * Returns a pointer to the end of the string (where a null terminator might go - * if you wanted to add one). - */ -char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* src, size_t srcLen, char16_t* dst); - /** * Convert UTF-8 to UTF-16 including surrogate pairs. The destination buffer * must be large enough to hold the result as measured by utf8_to_utf16_length diff --git a/libs/utils/Unicode.cpp b/libs/utils/Unicode.cpp index 41cbf035e..78c61b4fc 100644 --- a/libs/utils/Unicode.cpp +++ b/libs/utils/Unicode.cpp @@ -542,7 +542,11 @@ ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len) return u16measuredLen; } -char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, char16_t* u16str) +/** + * Convert a UTF-8 string to UTF-16. The destination UTF-16 buffer must have + * space for NULL at the end. + */ +void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) { const uint8_t* const u8end = u8str + u8len; const uint8_t* u8cur = u8str; @@ -565,12 +569,7 @@ char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, c u8cur += u8len; } - return u16cur; -} - -void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) { - char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str); - *end = 0; + *u16cur = 0; } } From 7c7b114bdda2cbd6019b9fc8d2df95be15244235 Mon Sep 17 00:00:00 2001 From: The Android Automerger Date: Thu, 27 Oct 2011 17:42:17 -0700 Subject: [PATCH 4/4] Revert "Clean up CursorWindow code." This reverts commit 3bc6bbc92cd2095f42039b5aadd0a14d0e5d9230. --- include/binder/CursorWindow.h | 28 ++---------------------- libs/binder/CursorWindow.cpp | 41 ++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/include/binder/CursorWindow.h b/include/binder/CursorWindow.h index d227244df..f0b2909dc 100644 --- a/include/binder/CursorWindow.h +++ b/include/binder/CursorWindow.h @@ -143,6 +143,8 @@ public: */ uint32_t alloc(size_t size, bool aligned = false); + uint32_t read_field_slot(int row, int column, field_slot_t * slot); + /** * Copy data into the window at the given offset. */ @@ -179,32 +181,6 @@ public: return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column; } - int64_t getFieldSlotValueLong(field_slot_t* fieldSlot) { -#if WINDOW_STORAGE_INLINE_NUMERICS - return fieldSlot->data.l; -#else - return copyOutLong(fieldSlot->data.buffer.offset); -#endif - } - - double getFieldSlotValueDouble(field_slot_t* fieldSlot) { -#if WINDOW_STORAGE_INLINE_NUMERICS - return fieldSlot->data.d; -#else - return copyOutDouble(fieldSlot->data.buffer.offset); -#endif - } - -#if WINDOW_STORAGE_UTF8 - char* getFieldSlotValueString(field_slot_t* fieldSlot) { - return reinterpret_cast(offsetToPtr(fieldSlot->data.buffer.offset)); - } -#else - char16_t* getFieldSlotValueString(field_slot_t* fieldSlot) { - return reinterpret_cast(offsetToPtr(fieldSlot->data.buffer.offset)); - } -#endif - private: uint8_t * mData; size_t mSize; diff --git a/libs/binder/CursorWindow.cpp b/libs/binder/CursorWindow.cpp index b02374f1e..47bbd04e1 100644 --- a/libs/binder/CursorWindow.cpp +++ b/libs/binder/CursorWindow.cpp @@ -236,6 +236,33 @@ field_slot_t * CursorWindow::getFieldSlotWithCheck(int row, int column) return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column; } +uint32_t CursorWindow::read_field_slot(int row, int column, field_slot_t * slotOut) +{ + if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) { + LOGE("Can't read row# %d, col# %d from CursorWindow. Make sure your Cursor is initialized correctly.", + row, column); + return -1; + } + row_slot_t * rowSlot = getRowSlot(row); + if (!rowSlot) { + LOGE("Failed to find rowSlot for row %d", row); + return -1; + } + if (rowSlot->offset == 0 || rowSlot->offset >= mSize) { + LOGE("Invalid rowSlot, offset = %d", rowSlot->offset); + return -1; + } +LOG_WINDOW("Found field directory for %d,%d at rowSlot %d, offset %d", row, column, (uint8_t *)rowSlot - mData, rowSlot->offset); + field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(rowSlot->offset); +LOG_WINDOW("Read field_slot_t %d,%d: offset = %d, size = %d, type = %d", row, column, fieldDir[column].data.buffer.offset, fieldDir[column].data.buffer.size, fieldDir[column].type); + + // Copy the data to the out param + slotOut->data.buffer.offset = fieldDir[column].data.buffer.offset; + slotOut->data.buffer.size = fieldDir[column].data.buffer.size; + slotOut->type = fieldDir[column].type; + return 0; +} + void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size) { assert(offset + size <= mSize); @@ -343,8 +370,12 @@ bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOu if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) { return false; } - - *valueOut = getFieldSlotValueLong(fieldSlot); + +#if WINDOW_STORAGE_INLINE_NUMERICS + *valueOut = fieldSlot->data.l; +#else + *valueOut = copyOutLong(fieldSlot->data.buffer.offset); +#endif return true; } @@ -355,7 +386,11 @@ bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueO return false; } - *valueOut = getFieldSlotValueDouble(fieldSlot); +#if WINDOW_STORAGE_INLINE_NUMERICS + *valueOut = fieldSlot->data.d; +#else + *valueOut = copyOutDouble(fieldSlot->data.buffer.offset); +#endif return true; }