summaryrefslogtreecommitdiffstats
path: root/libs/binder/CursorWindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/binder/CursorWindow.cpp')
-rw-r--r--libs/binder/CursorWindow.cpp498
1 files changed, 237 insertions, 261 deletions
diff --git a/libs/binder/CursorWindow.cpp b/libs/binder/CursorWindow.cpp
index b02374f..1b85a71 100644
--- a/libs/binder/CursorWindow.cpp
+++ b/libs/binder/CursorWindow.cpp
@@ -19,8 +19,9 @@
#include <utils/Log.h>
#include <binder/CursorWindow.h>
-#include <binder/MemoryHeapBase.h>
-#include <binder/MemoryBase.h>
+
+#include <cutils/ashmem.h>
+#include <sys/mman.h>
#include <assert.h>
#include <string.h>
@@ -28,350 +29,325 @@
namespace android {
-CursorWindow::CursorWindow(size_t maxSize) :
- mMaxSize(maxSize)
-{
+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<Header*>(mData);
}
-bool CursorWindow::setMemory(const sp<IMemory>& 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;
+CursorWindow::~CursorWindow() {
+ ::munmap(mData, mSize);
+ ::close(mAshmemFd);
}
-bool CursorWindow::initBuffer(bool localOnly)
-{
- //TODO Use a non-memory dealer mmap region for localOnly
-
- sp<MemoryHeapBase> 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;
+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)");
+
+ 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;
+ }
}
- }
- LOGE("CursorWindow heap allocation failed");
- return false;
+ ::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;
} else {
- LOGE("failed to create the CursorWindow heap");
- return false;
+ 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);
+ }
+ }
}
+ *outCursorWindow = NULL;
+ return result;
}
-CursorWindow::~CursorWindow()
-{
- // Everything that matters is a smart pointer
+status_t CursorWindow::writeToParcel(Parcel* parcel) {
+ status_t status = parcel->writeString8(mName);
+ if (!status) {
+ status = parcel->writeDupFileDescriptor(mAshmemFd);
+ }
+ return status;
}
-void CursorWindow::clear()
-{
+status_t CursorWindow::clear() {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
+ }
+
+ mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk);
+ mHeader->firstChunkOffset = sizeof(Header);
mHeader->numRows = 0;
mHeader->numColumns = 0;
- 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;
+
+ RowSlotChunk* firstChunk = static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
+ firstChunk->nextChunkOffset = 0;
+ return OK;
}
-int32_t CursorWindow::freeSpace()
-{
- int32_t freeSpace = mSize - mFreeOffset;
- if (freeSpace < 0) {
- freeSpace = 0;
+status_t CursorWindow::setNumColumns(uint32_t numColumns) {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
+ }
+
+ 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;
}
- return freeSpace;
+ mHeader->numColumns = numColumns;
+ return OK;
}
-field_slot_t * CursorWindow::allocRow()
-{
+status_t CursorWindow::allocRow() {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
+ }
+
// Fill in the row slot
- row_slot_t * rowSlot = allocRowSlot();
+ RowSlot* rowSlot = allocRowSlot();
if (rowSlot == NULL) {
- return NULL;
+ return NO_MEMORY;
}
// Allocate the slots for the field directory
- size_t fieldDirSize = mHeader->numColumns * sizeof(field_slot_t);
- uint32_t fieldDirOffset = alloc(fieldDirSize);
+ size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot);
+ uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/);
if (!fieldDirOffset) {
mHeader->numRows--;
- LOG_WINDOW("The row failed, so back out the new row accounting from allocRowSlot %d", mHeader->numRows);
- return NULL;
+ LOG_WINDOW("The row failed, so back out the new row accounting "
+ "from allocRowSlot %d", mHeader->numRows);
+ return NO_MEMORY;
}
- field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(fieldDirOffset);
- memset(fieldDir, 0x0, fieldDirSize);
+ FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(fieldDirOffset));
+ memset(fieldDir, 0, fieldDirSize);
-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);
+ 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);
rowSlot->offset = fieldDirOffset;
+ return OK;
+}
+
+status_t CursorWindow::freeLastRow() {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
+ }
- return fieldDir;
+ if (mHeader->numRows > 0) {
+ mHeader->numRows--;
+ }
+ return OK;
}
-uint32_t CursorWindow::alloc(size_t requestedSize, bool aligned)
-{
- int32_t size;
+uint32_t CursorWindow::alloc(size_t size, bool aligned) {
uint32_t padding;
if (aligned) {
// 4 byte alignment
- padding = 4 - (mFreeOffset & 0x3);
+ padding = (~mHeader->freeOffset + 1) & 3;
} else {
padding = 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;
+ 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;
}
- uint32_t offset = mFreeOffset + padding;
- mFreeOffset += size;
+ mHeader->freeOffset = nextFreeOffset;
return offset;
}
-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));
+CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) {
+ uint32_t chunkPos = row;
+ RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
+ offsetToPtr(mHeader->firstChunkOffset));
+ while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) {
+ chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
+ chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
}
- return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t)));
- LOG_WINDOW("exit getRowSlot current row num %d, this row %d", mHeader->numRows, row);
+ return &chunk->slots[chunkPos];
}
-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) {
+CursorWindow::RowSlot* CursorWindow::allocRowSlot() {
+ uint32_t chunkPos = mHeader->numRows;
+ RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
+ offsetToPtr(mHeader->firstChunkOffset));
+ while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) {
+ chunk = static_cast<RowSlotChunk*>(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) {
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<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
+ chunk->nextChunkOffset = 0;
+ chunkPos = 0;
}
- mHeader->numRows++;
-
- return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t)));
-}
-
-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;
+ mHeader->numRows += 1;
+ return &chunk->slots[chunkPos];
}
-void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size)
-{
- assert(offset + size <= mSize);
- memcpy(mData + offset, data, size);
-}
-
-void CursorWindow::copyIn(uint32_t offset, int64_t data)
-{
- assert(offset + sizeof(int64_t) <= mSize);
- memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t));
-}
-
-void CursorWindow::copyIn(uint32_t offset, double data)
-{
- assert(offset + sizeof(double) <= mSize);
- memcpy(mData + offset, (uint8_t *)&data, sizeof(double));
+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<FieldSlot*>(offsetToPtr(rowSlot->offset));
+ return &fieldDir[column];
}
-void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size)
-{
- assert(offset + size <= mSize);
- memcpy(data, mData + offset, size);
+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);
}
-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;
+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);
}
-double CursorWindow::copyOutDouble(uint32_t offset)
-{
- double value;
- assert(offset + sizeof(double) <= mSize);
- memcpy(&value, mData + offset, sizeof(double));
- return value;
-}
+status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column,
+ const void* value, size_t size, int32_t type) {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
+ }
-bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value)
-{
- field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
+ FieldSlot* fieldSlot = getFieldSlot(row, column);
if (!fieldSlot) {
- return false;
+ return BAD_VALUE;
}
-#if WINDOW_STORAGE_INLINE_NUMERICS
- fieldSlot->data.l = value;
-#else
- int offset = alloc(sizeof(int64_t));
+ uint32_t offset = alloc(size);
if (!offset) {
- return false;
+ return NO_MEMORY;
}
- copyIn(offset, value);
+ memcpy(offsetToPtr(offset), value, size);
+ fieldSlot->type = type;
fieldSlot->data.buffer.offset = offset;
- fieldSlot->data.buffer.size = sizeof(int64_t);
-#endif
- fieldSlot->type = FIELD_TYPE_INTEGER;
- return true;
+ fieldSlot->data.buffer.size = size;
+ return OK;
}
-bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value)
-{
- field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
- if (!fieldSlot) {
- return false;
+status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
}
-#if WINDOW_STORAGE_INLINE_NUMERICS
- fieldSlot->data.d = value;
-#else
- int offset = alloc(sizeof(int64_t));
- if (!offset) {
- return false;
+ FieldSlot* fieldSlot = getFieldSlot(row, column);
+ if (!fieldSlot) {
+ return BAD_VALUE;
}
- copyIn(offset, value);
-
- fieldSlot->data.buffer.offset = offset;
- fieldSlot->data.buffer.size = sizeof(double);
-#endif
- fieldSlot->type = FIELD_TYPE_FLOAT;
- return true;
+ fieldSlot->type = FIELD_TYPE_INTEGER;
+ fieldSlot->data.l = value;
+ return OK;
}
-bool CursorWindow::putNull(unsigned int row, unsigned int col)
-{
- field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
- if (!fieldSlot) {
- return false;
+status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
}
- fieldSlot->type = FIELD_TYPE_NULL;
- fieldSlot->data.buffer.offset = 0;
- fieldSlot->data.buffer.size = 0;
- 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;
+ FieldSlot* fieldSlot = getFieldSlot(row, column);
+ if (!fieldSlot) {
+ return BAD_VALUE;
}
- *valueOut = getFieldSlotValueLong(fieldSlot);
- return true;
+ fieldSlot->type = FIELD_TYPE_FLOAT;
+ fieldSlot->data.d = value;
+ return OK;
}
-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;
+status_t CursorWindow::putNull(uint32_t row, uint32_t column) {
+ if (mReadOnly) {
+ return INVALID_OPERATION;
}
- *valueOut = getFieldSlotValueDouble(fieldSlot);
- return true;
-}
-
-bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut)
-{
- field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
+ FieldSlot* fieldSlot = getFieldSlot(row, column);
if (!fieldSlot) {
- return false;
- }
-
- if (fieldSlot->type != FIELD_TYPE_NULL) {
- *valueOut = false;
- } else {
- *valueOut = true;
+ return BAD_VALUE;
}
- return true;
+
+ fieldSlot->type = FIELD_TYPE_NULL;
+ fieldSlot->data.buffer.offset = 0;
+ fieldSlot->data.buffer.size = 0;
+ return OK;
}
}; // namespace android