blob: 047a4c84457a08cce418ee20db9fdd2dd350f93a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "CursorWindow"
19
20#include <utils/Log.h>
Mathias Agopian49d2b182012-02-27 18:11:20 -080021#include <androidfw/CursorWindow.h>
Jeff Brown0cde89f2011-10-10 14:50:10 -070022
23#include <cutils/ashmem.h>
24#include <sys/mman.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26#include <assert.h>
27#include <string.h>
28#include <stdlib.h>
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030namespace android {
31
Jeff Brown0cde89f2011-10-10 14:50:10 -070032CursorWindow::CursorWindow(const String8& name, int ashmemFd,
33 void* data, size_t size, bool readOnly) :
34 mName(name), mAshmemFd(ashmemFd), mData(data), mSize(size), mReadOnly(readOnly) {
35 mHeader = static_cast<Header*>(mData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036}
37
Jeff Brown0cde89f2011-10-10 14:50:10 -070038CursorWindow::~CursorWindow() {
39 ::munmap(mData, mSize);
40 ::close(mAshmemFd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041}
42
Jeff Brown5e5d6d82011-10-12 15:41:34 -070043status_t CursorWindow::create(const String8& name, size_t size, CursorWindow** outCursorWindow) {
Jeff Brown0cde89f2011-10-10 14:50:10 -070044 String8 ashmemName("CursorWindow: ");
45 ashmemName.append(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Jeff Brown0cde89f2011-10-10 14:50:10 -070047 status_t result;
48 int ashmemFd = ashmem_create_region(ashmemName.string(), size);
49 if (ashmemFd < 0) {
50 result = -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 } else {
Jeff Brown0cde89f2011-10-10 14:50:10 -070052 result = ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE);
53 if (result >= 0) {
54 void* data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0);
55 if (data == MAP_FAILED) {
56 result = -errno;
57 } else {
58 result = ashmem_set_prot_region(ashmemFd, PROT_READ);
59 if (result >= 0) {
60 CursorWindow* window = new CursorWindow(name, ashmemFd,
61 data, size, false /*readOnly*/);
62 result = window->clear();
63 if (!result) {
64 LOG_WINDOW("Created new CursorWindow: freeOffset=%d, "
65 "numRows=%d, numColumns=%d, mSize=%d, mData=%p",
66 window->mHeader->freeOffset,
67 window->mHeader->numRows,
68 window->mHeader->numColumns,
69 window->mSize, window->mData);
70 *outCursorWindow = window;
71 return OK;
72 }
73 delete window;
74 }
75 }
76 ::munmap(data, size);
77 }
78 ::close(ashmemFd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
Jeff Brown0cde89f2011-10-10 14:50:10 -070080 *outCursorWindow = NULL;
81 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082}
83
Jeff Brown0cde89f2011-10-10 14:50:10 -070084status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow) {
85 String8 name = parcel->readString8();
86
87 status_t result;
88 int ashmemFd = parcel->readFileDescriptor();
89 if (ashmemFd == int(BAD_TYPE)) {
90 result = BAD_TYPE;
91 } else {
92 ssize_t size = ashmem_get_size_region(ashmemFd);
93 if (size < 0) {
94 result = UNKNOWN_ERROR;
95 } else {
96 int dupAshmemFd = ::dup(ashmemFd);
97 if (dupAshmemFd < 0) {
98 result = -errno;
99 } else {
100 void* data = ::mmap(NULL, size, PROT_READ, MAP_SHARED, dupAshmemFd, 0);
101 if (data == MAP_FAILED) {
102 result = -errno;
103 } else {
104 CursorWindow* window = new CursorWindow(name, dupAshmemFd,
105 data, size, true /*readOnly*/);
106 LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, "
107 "numRows=%d, numColumns=%d, mSize=%d, mData=%p",
108 window->mHeader->freeOffset,
109 window->mHeader->numRows,
110 window->mHeader->numColumns,
111 window->mSize, window->mData);
112 *outCursorWindow = window;
113 return OK;
114 }
115 ::close(dupAshmemFd);
116 }
117 }
118 }
119 *outCursorWindow = NULL;
120 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121}
122
Jeff Brown0cde89f2011-10-10 14:50:10 -0700123status_t CursorWindow::writeToParcel(Parcel* parcel) {
124 status_t status = parcel->writeString8(mName);
125 if (!status) {
126 status = parcel->writeDupFileDescriptor(mAshmemFd);
127 }
128 return status;
129}
130
131status_t CursorWindow::clear() {
132 if (mReadOnly) {
133 return INVALID_OPERATION;
134 }
135
136 mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk);
137 mHeader->firstChunkOffset = sizeof(Header);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 mHeader->numRows = 0;
139 mHeader->numColumns = 0;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700140
141 RowSlotChunk* firstChunk = static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
142 firstChunk->nextChunkOffset = 0;
143 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144}
145
Jeff Brown0cde89f2011-10-10 14:50:10 -0700146status_t CursorWindow::setNumColumns(uint32_t numColumns) {
147 if (mReadOnly) {
148 return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700150
151 uint32_t cur = mHeader->numColumns;
152 if ((cur > 0 || mHeader->numRows > 0) && cur != numColumns) {
Steve Block3762c312012-01-06 19:20:56 +0000153 ALOGE("Trying to go from %d columns to %d", cur, numColumns);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700154 return INVALID_OPERATION;
155 }
156 mHeader->numColumns = numColumns;
157 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158}
159
Jeff Brown0cde89f2011-10-10 14:50:10 -0700160status_t CursorWindow::allocRow() {
161 if (mReadOnly) {
162 return INVALID_OPERATION;
163 }
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 // Fill in the row slot
Jeff Brown0cde89f2011-10-10 14:50:10 -0700166 RowSlot* rowSlot = allocRowSlot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 if (rowSlot == NULL) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700168 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170
171 // Allocate the slots for the field directory
Jeff Brown0cde89f2011-10-10 14:50:10 -0700172 size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot);
173 uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 if (!fieldDirOffset) {
175 mHeader->numRows--;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700176 LOG_WINDOW("The row failed, so back out the new row accounting "
177 "from allocRowSlot %d", mHeader->numRows);
178 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700180 FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(fieldDirOffset));
181 memset(fieldDir, 0, fieldDirSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Jeff Brown0cde89f2011-10-10 14:50:10 -0700183 LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n",
184 mHeader->numRows - 1, offsetFromPtr(rowSlot), fieldDirSize, fieldDirOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 rowSlot->offset = fieldDirOffset;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700186 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187}
188
Jeff Brown0cde89f2011-10-10 14:50:10 -0700189status_t CursorWindow::freeLastRow() {
190 if (mReadOnly) {
191 return INVALID_OPERATION;
192 }
193
194 if (mHeader->numRows > 0) {
195 mHeader->numRows--;
196 }
197 return OK;
198}
199
200uint32_t CursorWindow::alloc(size_t size, bool aligned) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 uint32_t padding;
202 if (aligned) {
203 // 4 byte alignment
Jeff Brown0cde89f2011-10-10 14:50:10 -0700204 padding = (~mHeader->freeOffset + 1) & 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 } else {
206 padding = 0;
207 }
208
Jeff Brown0cde89f2011-10-10 14:50:10 -0700209 uint32_t offset = mHeader->freeOffset + padding;
210 uint32_t nextFreeOffset = offset + size;
211 if (nextFreeOffset > mSize) {
Steve Block8564c8d2012-01-05 23:22:43 +0000212 ALOGW("Window is full: requested allocation %d bytes, "
Jeff Brown0cde89f2011-10-10 14:50:10 -0700213 "free space %d bytes, window size %d bytes",
214 size, freeSpace(), mSize);
215 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
217
Jeff Brown0cde89f2011-10-10 14:50:10 -0700218 mHeader->freeOffset = nextFreeOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 return offset;
220}
221
Jeff Brown0cde89f2011-10-10 14:50:10 -0700222CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) {
223 uint32_t chunkPos = row;
224 RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
225 offsetToPtr(mHeader->firstChunkOffset));
226 while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) {
227 chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
228 chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700230 return &chunk->slots[chunkPos];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231}
232
Jeff Brown0cde89f2011-10-10 14:50:10 -0700233CursorWindow::RowSlot* CursorWindow::allocRowSlot() {
234 uint32_t chunkPos = mHeader->numRows;
235 RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
236 offsetToPtr(mHeader->firstChunkOffset));
237 while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) {
238 chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
239 chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
240 }
241 if (chunkPos == ROW_SLOT_CHUNK_NUM_ROWS) {
242 if (!chunk->nextChunkOffset) {
243 chunk->nextChunkOffset = alloc(sizeof(RowSlotChunk), true /*aligned*/);
244 if (!chunk->nextChunkOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 return NULL;
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700248 chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
249 chunk->nextChunkOffset = 0;
250 chunkPos = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700252 mHeader->numRows += 1;
253 return &chunk->slots[chunkPos];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254}
255
Jeff Brown0cde89f2011-10-10 14:50:10 -0700256CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) {
257 if (row >= mHeader->numRows || column >= mHeader->numColumns) {
Steve Block3762c312012-01-06 19:20:56 +0000258 ALOGE("Failed to read row %d, column %d from a CursorWindow which "
Jeff Brown0cde89f2011-10-10 14:50:10 -0700259 "has %d rows, %d columns.",
260 row, column, mHeader->numRows, mHeader->numColumns);
261 return NULL;
262 }
263 RowSlot* rowSlot = getRowSlot(row);
264 if (!rowSlot) {
Steve Block3762c312012-01-06 19:20:56 +0000265 ALOGE("Failed to find rowSlot for row %d.", row);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700266 return NULL;
267 }
268 FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(rowSlot->offset));
269 return &fieldDir[column];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270}
271
Jeff Brown0cde89f2011-10-10 14:50:10 -0700272status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) {
273 return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274}
275
Jeff Brown0cde89f2011-10-10 14:50:10 -0700276status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value,
277 size_t sizeIncludingNull) {
278 return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279}
280
Jeff Brown0cde89f2011-10-10 14:50:10 -0700281status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column,
282 const void* value, size_t size, int32_t type) {
283 if (mReadOnly) {
284 return INVALID_OPERATION;
285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
Jeff Brown0cde89f2011-10-10 14:50:10 -0700287 FieldSlot* fieldSlot = getFieldSlot(row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 if (!fieldSlot) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700289 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
291
Jeff Brown0cde89f2011-10-10 14:50:10 -0700292 uint32_t offset = alloc(size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 if (!offset) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700294 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
Jeff Brown0cde89f2011-10-10 14:50:10 -0700297 memcpy(offsetToPtr(offset), value, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Jeff Brown0cde89f2011-10-10 14:50:10 -0700299 fieldSlot->type = type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 fieldSlot->data.buffer.offset = offset;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700301 fieldSlot->data.buffer.size = size;
302 return OK;
303}
304
305status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) {
306 if (mReadOnly) {
307 return INVALID_OPERATION;
308 }
309
310 FieldSlot* fieldSlot = getFieldSlot(row, column);
311 if (!fieldSlot) {
312 return BAD_VALUE;
313 }
314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 fieldSlot->type = FIELD_TYPE_INTEGER;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700316 fieldSlot->data.l = value;
317 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318}
319
Jeff Brown0cde89f2011-10-10 14:50:10 -0700320status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) {
321 if (mReadOnly) {
322 return INVALID_OPERATION;
323 }
324
325 FieldSlot* fieldSlot = getFieldSlot(row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 if (!fieldSlot) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700327 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 }
329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 fieldSlot->type = FIELD_TYPE_FLOAT;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700331 fieldSlot->data.d = value;
332 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333}
334
Jeff Brown0cde89f2011-10-10 14:50:10 -0700335status_t CursorWindow::putNull(uint32_t row, uint32_t column) {
336 if (mReadOnly) {
337 return INVALID_OPERATION;
338 }
339
340 FieldSlot* fieldSlot = getFieldSlot(row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 if (!fieldSlot) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700342 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344
345 fieldSlot->type = FIELD_TYPE_NULL;
346 fieldSlot->data.buffer.offset = 0;
347 fieldSlot->data.buffer.size = 0;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700348 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349}
350
351}; // namespace android