blob: 47bbd04e1f911be56d6c5fbf2fb2b9e7ee67f581 [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>
Mike Lockwood2807df82010-05-27 17:04:23 -040021#include <binder/CursorWindow.h>
Mathias Agopiand1f74d02010-01-29 16:54:04 -080022#include <binder/MemoryHeapBase.h>
23#include <binder/MemoryBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <assert.h>
26#include <string.h>
27#include <stdlib.h>
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029namespace android {
30
31CursorWindow::CursorWindow(size_t maxSize) :
32 mMaxSize(maxSize)
33{
34}
35
Mathias Agopiand1f74d02010-01-29 16:54:04 -080036bool CursorWindow::setMemory(const sp<IMemory>& memory)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037{
38 mMemory = memory;
39 mData = (uint8_t *) memory->pointer();
40 if (mData == NULL) {
41 return false;
42 }
43 mHeader = (window_header_t *) mData;
44
45 // Make the window read-only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 ssize_t size = memory->size();
47 mSize = size;
48 mMaxSize = size;
49 mFreeOffset = size;
50LOG_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);
51 return true;
52}
53
54bool CursorWindow::initBuffer(bool localOnly)
55{
56 //TODO Use a non-memory dealer mmap region for localOnly
57
Mathias Agopiand1f74d02010-01-29 16:54:04 -080058 sp<MemoryHeapBase> heap;
59 heap = new MemoryHeapBase(mMaxSize, 0, "CursorWindow");
60 if (heap != NULL) {
61 mMemory = new MemoryBase(heap, 0, mMaxSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 if (mMemory != NULL) {
63 mData = (uint8_t *) mMemory->pointer();
64 if (mData) {
65 mHeader = (window_header_t *) mData;
66 mSize = mMaxSize;
67
68 // Put the window into a clean state
69 clear();
70 LOG_WINDOW("Created CursorWindow with new MemoryDealer: mFreeOffset = %d, mSize = %d, mMaxSize = %d, mData = %p", mFreeOffset, mSize, mMaxSize, mData);
71 return true;
72 }
73 }
Mathias Agopiand1f74d02010-01-29 16:54:04 -080074 LOGE("CursorWindow heap allocation failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 return false;
76 } else {
Mathias Agopiand1f74d02010-01-29 16:54:04 -080077 LOGE("failed to create the CursorWindow heap");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 return false;
79 }
80}
81
82CursorWindow::~CursorWindow()
83{
84 // Everything that matters is a smart pointer
85}
86
87void CursorWindow::clear()
88{
89 mHeader->numRows = 0;
90 mHeader->numColumns = 0;
91 mFreeOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE;
92 // Mark the first chunk's next 'pointer' as null
93 *((uint32_t *)(mData + mFreeOffset - sizeof(uint32_t))) = 0;
94}
95
96int32_t CursorWindow::freeSpace()
97{
98 int32_t freeSpace = mSize - mFreeOffset;
99 if (freeSpace < 0) {
100 freeSpace = 0;
101 }
102 return freeSpace;
103}
104
105field_slot_t * CursorWindow::allocRow()
106{
107 // Fill in the row slot
108 row_slot_t * rowSlot = allocRowSlot();
109 if (rowSlot == NULL) {
110 return NULL;
111 }
112
113 // Allocate the slots for the field directory
114 size_t fieldDirSize = mHeader->numColumns * sizeof(field_slot_t);
115 uint32_t fieldDirOffset = alloc(fieldDirSize);
116 if (!fieldDirOffset) {
117 mHeader->numRows--;
Vasu Norie6544e42010-10-26 16:44:44 -0700118 LOG_WINDOW("The row failed, so back out the new row accounting from allocRowSlot %d", mHeader->numRows);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 return NULL;
120 }
121 field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(fieldDirOffset);
122 memset(fieldDir, 0x0, fieldDirSize);
123
124LOG_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);
125 rowSlot->offset = fieldDirOffset;
126
127 return fieldDir;
128}
129
130uint32_t CursorWindow::alloc(size_t requestedSize, bool aligned)
131{
132 int32_t size;
133 uint32_t padding;
134 if (aligned) {
135 // 4 byte alignment
136 padding = 4 - (mFreeOffset & 0x3);
137 } else {
138 padding = 0;
139 }
140
141 size = requestedSize + padding;
142
143 if (size > freeSpace()) {
Vasu Norib7e2c782010-10-05 16:50:51 -0700144 LOGV("need to grow: mSize = %d, size = %d, freeSpace() = %d, numRows = %d", mSize, size,
145 freeSpace(), mHeader->numRows);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 // Only grow the window if the first row doesn't fit
147 if (mHeader->numRows > 1) {
Vasu Norib7e2c782010-10-05 16:50:51 -0700148 LOGV("not growing since there are already %d row(s), max size %d", mHeader->numRows,
149 mMaxSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 return 0;
151 }
152
153 // Find a new size that will fit the allocation
154 int allocated = mSize - freeSpace();
155 int newSize = mSize + WINDOW_ALLOCATION_SIZE;
156 while (size > (newSize - allocated)) {
157 newSize += WINDOW_ALLOCATION_SIZE;
158 if (newSize > mMaxSize) {
159 LOGE("Attempting to grow window beyond max size (%d)", mMaxSize);
160 return 0;
161 }
162 }
163LOG_WINDOW("found size %d", newSize);
164 mSize = newSize;
165 }
166
167 uint32_t offset = mFreeOffset + padding;
168 mFreeOffset += size;
169 return offset;
170}
171
172row_slot_t * CursorWindow::getRowSlot(int row)
173{
174 LOG_WINDOW("enter getRowSlot current row num %d, this row %d", mHeader->numRows, row);
175 int chunkNum = row / ROW_SLOT_CHUNK_NUM_ROWS;
176 int chunkPos = row % ROW_SLOT_CHUNK_NUM_ROWS;
177 int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t);
178 uint8_t * rowChunk = mData + sizeof(window_header_t);
179 for (int i = 0; i < chunkNum; i++) {
180 rowChunk = offsetToPtr(*((uint32_t *)(mData + chunkPtrOffset)));
181 chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t));
182 }
183 return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t)));
184 LOG_WINDOW("exit getRowSlot current row num %d, this row %d", mHeader->numRows, row);
185}
186
187row_slot_t * CursorWindow::allocRowSlot()
188{
189 int chunkNum = mHeader->numRows / ROW_SLOT_CHUNK_NUM_ROWS;
190 int chunkPos = mHeader->numRows % ROW_SLOT_CHUNK_NUM_ROWS;
191 int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t);
192 uint8_t * rowChunk = mData + sizeof(window_header_t);
193LOG_WINDOW("Allocating row slot, mHeader->numRows is %d, chunkNum is %d, chunkPos is %d", mHeader->numRows, chunkNum, chunkPos);
194 for (int i = 0; i < chunkNum; i++) {
195 uint32_t nextChunkOffset = *((uint32_t *)(mData + chunkPtrOffset));
196LOG_WINDOW("nextChunkOffset is %d", nextChunkOffset);
197 if (nextChunkOffset == 0) {
198 // Allocate a new row chunk
199 nextChunkOffset = alloc(ROW_SLOT_CHUNK_SIZE, true);
200 if (nextChunkOffset == 0) {
201 return NULL;
202 }
203 rowChunk = offsetToPtr(nextChunkOffset);
204LOG_WINDOW("allocated new chunk at %d, rowChunk = %p", nextChunkOffset, rowChunk);
205 *((uint32_t *)(mData + chunkPtrOffset)) = rowChunk - mData;
206 // Mark the new chunk's next 'pointer' as null
207 *((uint32_t *)(rowChunk + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t))) = 0;
208 } else {
209LOG_WINDOW("follwing 'pointer' to next chunk, offset of next pointer is %d", chunkPtrOffset);
210 rowChunk = offsetToPtr(nextChunkOffset);
211 chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t));
212 }
213 }
214 mHeader->numRows++;
215
216 return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t)));
217}
218
219field_slot_t * CursorWindow::getFieldSlotWithCheck(int row, int column)
220{
221 if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) {
Vasu Norib37f8a82010-11-29 11:22:22 -0800222 LOGE("Failed to read row# %d, column# from a CursorWindow which has %d rows, %d columns.",
223 row, column, mHeader->numRows, mHeader->numColumns);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 return NULL;
225 }
226 row_slot_t * rowSlot = getRowSlot(row);
227 if (!rowSlot) {
228 LOGE("Failed to find rowSlot for row %d", row);
229 return NULL;
230 }
231 if (rowSlot->offset == 0 || rowSlot->offset >= mSize) {
232 LOGE("Invalid rowSlot, offset = %d", rowSlot->offset);
233 return NULL;
234 }
235 int fieldDirOffset = rowSlot->offset;
236 return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column;
237}
238
239uint32_t CursorWindow::read_field_slot(int row, int column, field_slot_t * slotOut)
240{
241 if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) {
Vasu Norib37f8a82010-11-29 11:22:22 -0800242 LOGE("Can't read row# %d, col# %d from CursorWindow. Make sure your Cursor is initialized correctly.",
243 row, column);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 return -1;
245 }
246 row_slot_t * rowSlot = getRowSlot(row);
247 if (!rowSlot) {
248 LOGE("Failed to find rowSlot for row %d", row);
249 return -1;
250 }
251 if (rowSlot->offset == 0 || rowSlot->offset >= mSize) {
252 LOGE("Invalid rowSlot, offset = %d", rowSlot->offset);
253 return -1;
254 }
255LOG_WINDOW("Found field directory for %d,%d at rowSlot %d, offset %d", row, column, (uint8_t *)rowSlot - mData, rowSlot->offset);
256 field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(rowSlot->offset);
257LOG_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);
258
259 // Copy the data to the out param
260 slotOut->data.buffer.offset = fieldDir[column].data.buffer.offset;
261 slotOut->data.buffer.size = fieldDir[column].data.buffer.size;
262 slotOut->type = fieldDir[column].type;
263 return 0;
264}
265
266void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size)
267{
268 assert(offset + size <= mSize);
269 memcpy(mData + offset, data, size);
270}
271
272void CursorWindow::copyIn(uint32_t offset, int64_t data)
273{
274 assert(offset + sizeof(int64_t) <= mSize);
275 memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t));
276}
277
278void CursorWindow::copyIn(uint32_t offset, double data)
279{
280 assert(offset + sizeof(double) <= mSize);
281 memcpy(mData + offset, (uint8_t *)&data, sizeof(double));
282}
283
284void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size)
285{
286 assert(offset + size <= mSize);
287 memcpy(data, mData + offset, size);
288}
289
290int64_t CursorWindow::copyOutLong(uint32_t offset)
291{
292 int64_t value;
293 assert(offset + sizeof(int64_t) <= mSize);
294 memcpy(&value, mData + offset, sizeof(int64_t));
295 return value;
296}
297
298double CursorWindow::copyOutDouble(uint32_t offset)
299{
300 double value;
301 assert(offset + sizeof(double) <= mSize);
302 memcpy(&value, mData + offset, sizeof(double));
303 return value;
304}
305
306bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value)
307{
308 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
309 if (!fieldSlot) {
310 return false;
311 }
312
313#if WINDOW_STORAGE_INLINE_NUMERICS
314 fieldSlot->data.l = value;
315#else
316 int offset = alloc(sizeof(int64_t));
317 if (!offset) {
318 return false;
319 }
320
321 copyIn(offset, value);
322
323 fieldSlot->data.buffer.offset = offset;
324 fieldSlot->data.buffer.size = sizeof(int64_t);
325#endif
326 fieldSlot->type = FIELD_TYPE_INTEGER;
327 return true;
328}
329
330bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value)
331{
332 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
333 if (!fieldSlot) {
334 return false;
335 }
336
337#if WINDOW_STORAGE_INLINE_NUMERICS
338 fieldSlot->data.d = value;
339#else
340 int offset = alloc(sizeof(int64_t));
341 if (!offset) {
342 return false;
343 }
344
345 copyIn(offset, value);
346
347 fieldSlot->data.buffer.offset = offset;
348 fieldSlot->data.buffer.size = sizeof(double);
349#endif
350 fieldSlot->type = FIELD_TYPE_FLOAT;
351 return true;
352}
353
354bool CursorWindow::putNull(unsigned int row, unsigned int col)
355{
356 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
357 if (!fieldSlot) {
358 return false;
359 }
360
361 fieldSlot->type = FIELD_TYPE_NULL;
362 fieldSlot->data.buffer.offset = 0;
363 fieldSlot->data.buffer.size = 0;
364 return true;
365}
366
367bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOut)
368{
369 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
370 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) {
371 return false;
372 }
373
374#if WINDOW_STORAGE_INLINE_NUMERICS
375 *valueOut = fieldSlot->data.l;
376#else
377 *valueOut = copyOutLong(fieldSlot->data.buffer.offset);
378#endif
379 return true;
380}
381
382bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueOut)
383{
384 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
385 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_FLOAT) {
386 return false;
387 }
388
389#if WINDOW_STORAGE_INLINE_NUMERICS
390 *valueOut = fieldSlot->data.d;
391#else
392 *valueOut = copyOutDouble(fieldSlot->data.buffer.offset);
393#endif
394 return true;
395}
396
397bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut)
398{
399 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
400 if (!fieldSlot) {
401 return false;
402 }
403
404 if (fieldSlot->type != FIELD_TYPE_NULL) {
405 *valueOut = false;
406 } else {
407 *valueOut = true;
408 }
409 return true;
410}
411
412}; // namespace android