blob: e1067fcd4d3dbe78364c33a3eae1702d4378ec1e [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 *
Mark Salyzyn00adb862014-03-19 11:00:06 -07004 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 *
Mark Salyzyn00adb862014-03-19 11:00:06 -07008 * http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 *
Mark Salyzyn00adb862014-03-19 11:00:06 -070010 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "CursorWindow"
19
Mathias Agopian49d2b182012-02-27 18:11:20 -080020#include <androidfw/CursorWindow.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070021#include <binder/Parcel.h>
22#include <utils/Log.h>
Jeff Brown0cde89f2011-10-10 14:50:10 -070023
24#include <cutils/ashmem.h>
25#include <sys/mman.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27#include <assert.h>
28#include <string.h>
29#include <stdlib.h>
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031namespace android {
32
Jeff Brown0cde89f2011-10-10 14:50:10 -070033CursorWindow::CursorWindow(const String8& name, int ashmemFd,
34 void* data, size_t size, bool readOnly) :
35 mName(name), mAshmemFd(ashmemFd), mData(data), mSize(size), mReadOnly(readOnly) {
36 mHeader = static_cast<Header*>(mData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037}
38
Jeff Brown0cde89f2011-10-10 14:50:10 -070039CursorWindow::~CursorWindow() {
40 ::munmap(mData, mSize);
41 ::close(mAshmemFd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042}
43
Jeff Brown5e5d6d82011-10-12 15:41:34 -070044status_t CursorWindow::create(const String8& name, size_t size, CursorWindow** outCursorWindow) {
Jeff Brown0cde89f2011-10-10 14:50:10 -070045 String8 ashmemName("CursorWindow: ");
46 ashmemName.append(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Jeff Brown0cde89f2011-10-10 14:50:10 -070048 status_t result;
49 int ashmemFd = ashmem_create_region(ashmemName.string(), size);
50 if (ashmemFd < 0) {
51 result = -errno;
Makoto Onuki2276cea2019-04-02 14:05:05 -070052 ALOGE("CursorWindow: ashmem_create_region() failed: errno=%d.", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 } else {
Jeff Brown0cde89f2011-10-10 14:50:10 -070054 result = ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE);
Makoto Onuki2276cea2019-04-02 14:05:05 -070055 if (result < 0) {
56 ALOGE("CursorWindow: ashmem_set_prot_region() failed: errno=%d",errno);
57 } else {
Jeff Brown0cde89f2011-10-10 14:50:10 -070058 void* data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0);
59 if (data == MAP_FAILED) {
60 result = -errno;
Makoto Onuki2276cea2019-04-02 14:05:05 -070061 ALOGE("CursorWindow: mmap() failed: errno=%d.", errno);
Jeff Brown0cde89f2011-10-10 14:50:10 -070062 } else {
63 result = ashmem_set_prot_region(ashmemFd, PROT_READ);
Makoto Onuki2276cea2019-04-02 14:05:05 -070064 if (result < 0) {
65 ALOGE("CursorWindow: ashmem_set_prot_region() failed: errno=%d.", errno);
66 } else {
Jeff Brown0cde89f2011-10-10 14:50:10 -070067 CursorWindow* window = new CursorWindow(name, ashmemFd,
68 data, size, false /*readOnly*/);
69 result = window->clear();
70 if (!result) {
71 LOG_WINDOW("Created new CursorWindow: freeOffset=%d, "
72 "numRows=%d, numColumns=%d, mSize=%d, mData=%p",
73 window->mHeader->freeOffset,
74 window->mHeader->numRows,
75 window->mHeader->numColumns,
76 window->mSize, window->mData);
77 *outCursorWindow = window;
78 return OK;
79 }
80 delete window;
81 }
82 }
83 ::munmap(data, size);
84 }
85 ::close(ashmemFd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 }
Jeff Brown0cde89f2011-10-10 14:50:10 -070087 *outCursorWindow = NULL;
88 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089}
90
Jeff Brown0cde89f2011-10-10 14:50:10 -070091status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow) {
92 String8 name = parcel->readString8();
93
94 status_t result;
Makoto Onuki2276cea2019-04-02 14:05:05 -070095 int actualSize;
Jeff Brown0cde89f2011-10-10 14:50:10 -070096 int ashmemFd = parcel->readFileDescriptor();
97 if (ashmemFd == int(BAD_TYPE)) {
98 result = BAD_TYPE;
Makoto Onuki2276cea2019-04-02 14:05:05 -070099 ALOGE("CursorWindow: readFileDescriptor() failed");
Jeff Brown0cde89f2011-10-10 14:50:10 -0700100 } else {
101 ssize_t size = ashmem_get_size_region(ashmemFd);
102 if (size < 0) {
103 result = UNKNOWN_ERROR;
Makoto Onuki2276cea2019-04-02 14:05:05 -0700104 ALOGE("CursorWindow: ashmem_get_size_region() failed: errno=%d.", errno);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700105 } else {
Nick Kraleviche104df92019-01-14 14:03:36 -0800106 int dupAshmemFd = ::fcntl(ashmemFd, F_DUPFD_CLOEXEC, 0);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700107 if (dupAshmemFd < 0) {
108 result = -errno;
Makoto Onuki2276cea2019-04-02 14:05:05 -0700109 ALOGE("CursorWindow: fcntl() failed: errno=%d.", errno);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700110 } else {
Fyodor Kupolov45e2e952017-02-13 18:35:12 -0800111 // the size of the ashmem descriptor can be modified between ashmem_get_size_region
112 // call and mmap, so we'll check again immediately after memory is mapped
Jeff Brown0cde89f2011-10-10 14:50:10 -0700113 void* data = ::mmap(NULL, size, PROT_READ, MAP_SHARED, dupAshmemFd, 0);
114 if (data == MAP_FAILED) {
115 result = -errno;
Makoto Onuki2276cea2019-04-02 14:05:05 -0700116 ALOGE("CursorWindow: mmap() failed: errno=%d.", errno);
117 } else if ((actualSize = ashmem_get_size_region(dupAshmemFd)) != size) {
Fyodor Kupolov45e2e952017-02-13 18:35:12 -0800118 ::munmap(data, size);
119 result = BAD_VALUE;
Makoto Onuki2276cea2019-04-02 14:05:05 -0700120 ALOGE("CursorWindow: ashmem_get_size_region() returned %d, expected %d"
121 " errno=%d",
122 actualSize, (int) size, errno);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700123 } else {
124 CursorWindow* window = new CursorWindow(name, dupAshmemFd,
125 data, size, true /*readOnly*/);
126 LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, "
127 "numRows=%d, numColumns=%d, mSize=%d, mData=%p",
128 window->mHeader->freeOffset,
129 window->mHeader->numRows,
130 window->mHeader->numColumns,
131 window->mSize, window->mData);
132 *outCursorWindow = window;
133 return OK;
134 }
135 ::close(dupAshmemFd);
136 }
137 }
138 }
139 *outCursorWindow = NULL;
140 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141}
142
Jeff Brown0cde89f2011-10-10 14:50:10 -0700143status_t CursorWindow::writeToParcel(Parcel* parcel) {
144 status_t status = parcel->writeString8(mName);
145 if (!status) {
146 status = parcel->writeDupFileDescriptor(mAshmemFd);
147 }
148 return status;
149}
150
151status_t CursorWindow::clear() {
152 if (mReadOnly) {
153 return INVALID_OPERATION;
154 }
155
156 mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk);
157 mHeader->firstChunkOffset = sizeof(Header);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 mHeader->numRows = 0;
159 mHeader->numColumns = 0;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700160
161 RowSlotChunk* firstChunk = static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset));
162 firstChunk->nextChunkOffset = 0;
163 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164}
165
Jeff Brown0cde89f2011-10-10 14:50:10 -0700166status_t CursorWindow::setNumColumns(uint32_t numColumns) {
167 if (mReadOnly) {
168 return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700170
171 uint32_t cur = mHeader->numColumns;
172 if ((cur > 0 || mHeader->numRows > 0) && cur != numColumns) {
Steve Block3762c312012-01-06 19:20:56 +0000173 ALOGE("Trying to go from %d columns to %d", cur, numColumns);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700174 return INVALID_OPERATION;
175 }
176 mHeader->numColumns = numColumns;
177 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178}
179
Jeff Brown0cde89f2011-10-10 14:50:10 -0700180status_t CursorWindow::allocRow() {
181 if (mReadOnly) {
182 return INVALID_OPERATION;
183 }
184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 // Fill in the row slot
Jeff Brown0cde89f2011-10-10 14:50:10 -0700186 RowSlot* rowSlot = allocRowSlot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 if (rowSlot == NULL) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700188 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 }
190
191 // Allocate the slots for the field directory
Jeff Brown0cde89f2011-10-10 14:50:10 -0700192 size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot);
193 uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 if (!fieldDirOffset) {
195 mHeader->numRows--;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700196 LOG_WINDOW("The row failed, so back out the new row accounting "
197 "from allocRowSlot %d", mHeader->numRows);
198 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700200 FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(fieldDirOffset));
201 memset(fieldDir, 0, fieldDirSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
Jeff Brown0cde89f2011-10-10 14:50:10 -0700203 LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n",
204 mHeader->numRows - 1, offsetFromPtr(rowSlot), fieldDirSize, fieldDirOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 rowSlot->offset = fieldDirOffset;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700206 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207}
208
Jeff Brown0cde89f2011-10-10 14:50:10 -0700209status_t CursorWindow::freeLastRow() {
210 if (mReadOnly) {
211 return INVALID_OPERATION;
212 }
213
214 if (mHeader->numRows > 0) {
215 mHeader->numRows--;
216 }
217 return OK;
218}
219
220uint32_t CursorWindow::alloc(size_t size, bool aligned) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 uint32_t padding;
222 if (aligned) {
223 // 4 byte alignment
Jeff Brown0cde89f2011-10-10 14:50:10 -0700224 padding = (~mHeader->freeOffset + 1) & 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 } else {
226 padding = 0;
227 }
228
Jeff Brown0cde89f2011-10-10 14:50:10 -0700229 uint32_t offset = mHeader->freeOffset + padding;
230 uint32_t nextFreeOffset = offset + size;
231 if (nextFreeOffset > mSize) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000232 ALOGW("Window is full: requested allocation %zu bytes, "
233 "free space %zu bytes, window size %zu bytes",
Jeff Brown0cde89f2011-10-10 14:50:10 -0700234 size, freeSpace(), mSize);
235 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237
Jeff Brown0cde89f2011-10-10 14:50:10 -0700238 mHeader->freeOffset = nextFreeOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 return offset;
240}
241
Jeff Brown0cde89f2011-10-10 14:50:10 -0700242CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) {
243 uint32_t chunkPos = row;
244 RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
245 offsetToPtr(mHeader->firstChunkOffset));
246 while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) {
247 chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
248 chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700250 return &chunk->slots[chunkPos];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251}
252
Jeff Brown0cde89f2011-10-10 14:50:10 -0700253CursorWindow::RowSlot* CursorWindow::allocRowSlot() {
254 uint32_t chunkPos = mHeader->numRows;
255 RowSlotChunk* chunk = static_cast<RowSlotChunk*>(
256 offsetToPtr(mHeader->firstChunkOffset));
257 while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) {
258 chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
259 chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS;
260 }
261 if (chunkPos == ROW_SLOT_CHUNK_NUM_ROWS) {
262 if (!chunk->nextChunkOffset) {
263 chunk->nextChunkOffset = alloc(sizeof(RowSlotChunk), true /*aligned*/);
264 if (!chunk->nextChunkOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 return NULL;
266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700268 chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset));
269 chunk->nextChunkOffset = 0;
270 chunkPos = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
Jeff Brown0cde89f2011-10-10 14:50:10 -0700272 mHeader->numRows += 1;
273 return &chunk->slots[chunkPos];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274}
275
Jeff Brown0cde89f2011-10-10 14:50:10 -0700276CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) {
277 if (row >= mHeader->numRows || column >= mHeader->numColumns) {
Steve Block3762c312012-01-06 19:20:56 +0000278 ALOGE("Failed to read row %d, column %d from a CursorWindow which "
Jeff Brown0cde89f2011-10-10 14:50:10 -0700279 "has %d rows, %d columns.",
280 row, column, mHeader->numRows, mHeader->numColumns);
281 return NULL;
282 }
283 RowSlot* rowSlot = getRowSlot(row);
284 if (!rowSlot) {
Steve Block3762c312012-01-06 19:20:56 +0000285 ALOGE("Failed to find rowSlot for row %d.", row);
Jeff Brown0cde89f2011-10-10 14:50:10 -0700286 return NULL;
287 }
288 FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(rowSlot->offset));
289 return &fieldDir[column];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290}
291
Jeff Brown0cde89f2011-10-10 14:50:10 -0700292status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) {
293 return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294}
295
Jeff Brown0cde89f2011-10-10 14:50:10 -0700296status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value,
297 size_t sizeIncludingNull) {
298 return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299}
300
Jeff Brown0cde89f2011-10-10 14:50:10 -0700301status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column,
302 const void* value, size_t size, int32_t type) {
303 if (mReadOnly) {
304 return INVALID_OPERATION;
305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
Jeff Brown0cde89f2011-10-10 14:50:10 -0700307 FieldSlot* fieldSlot = getFieldSlot(row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 if (!fieldSlot) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700309 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311
Jeff Brown0cde89f2011-10-10 14:50:10 -0700312 uint32_t offset = alloc(size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 if (!offset) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700314 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316
Jeff Brown0cde89f2011-10-10 14:50:10 -0700317 memcpy(offsetToPtr(offset), value, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Jeff Brown0cde89f2011-10-10 14:50:10 -0700319 fieldSlot->type = type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 fieldSlot->data.buffer.offset = offset;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700321 fieldSlot->data.buffer.size = size;
322 return OK;
323}
324
325status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) {
326 if (mReadOnly) {
327 return INVALID_OPERATION;
328 }
329
330 FieldSlot* fieldSlot = getFieldSlot(row, column);
331 if (!fieldSlot) {
332 return BAD_VALUE;
333 }
334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 fieldSlot->type = FIELD_TYPE_INTEGER;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700336 fieldSlot->data.l = value;
337 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338}
339
Jeff Brown0cde89f2011-10-10 14:50:10 -0700340status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) {
341 if (mReadOnly) {
342 return INVALID_OPERATION;
343 }
344
345 FieldSlot* fieldSlot = getFieldSlot(row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 if (!fieldSlot) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700347 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 fieldSlot->type = FIELD_TYPE_FLOAT;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700351 fieldSlot->data.d = value;
352 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353}
354
Jeff Brown0cde89f2011-10-10 14:50:10 -0700355status_t CursorWindow::putNull(uint32_t row, uint32_t column) {
356 if (mReadOnly) {
357 return INVALID_OPERATION;
358 }
359
360 FieldSlot* fieldSlot = getFieldSlot(row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 if (!fieldSlot) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700362 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364
365 fieldSlot->type = FIELD_TYPE_NULL;
366 fieldSlot->data.buffer.offset = 0;
367 fieldSlot->data.buffer.size = 0;
Jeff Brown0cde89f2011-10-10 14:50:10 -0700368 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369}
370
371}; // namespace android