blob: b02374f1ed782f839feba5a636833674e2cc2f25 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size)
240{
241 assert(offset + size <= mSize);
242 memcpy(mData + offset, data, size);
243}
244
245void CursorWindow::copyIn(uint32_t offset, int64_t data)
246{
247 assert(offset + sizeof(int64_t) <= mSize);
248 memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t));
249}
250
251void CursorWindow::copyIn(uint32_t offset, double data)
252{
253 assert(offset + sizeof(double) <= mSize);
254 memcpy(mData + offset, (uint8_t *)&data, sizeof(double));
255}
256
257void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size)
258{
259 assert(offset + size <= mSize);
260 memcpy(data, mData + offset, size);
261}
262
263int64_t CursorWindow::copyOutLong(uint32_t offset)
264{
265 int64_t value;
266 assert(offset + sizeof(int64_t) <= mSize);
267 memcpy(&value, mData + offset, sizeof(int64_t));
268 return value;
269}
270
271double CursorWindow::copyOutDouble(uint32_t offset)
272{
273 double value;
274 assert(offset + sizeof(double) <= mSize);
275 memcpy(&value, mData + offset, sizeof(double));
276 return value;
277}
278
279bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value)
280{
281 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
282 if (!fieldSlot) {
283 return false;
284 }
285
286#if WINDOW_STORAGE_INLINE_NUMERICS
287 fieldSlot->data.l = value;
288#else
289 int offset = alloc(sizeof(int64_t));
290 if (!offset) {
291 return false;
292 }
293
294 copyIn(offset, value);
295
296 fieldSlot->data.buffer.offset = offset;
297 fieldSlot->data.buffer.size = sizeof(int64_t);
298#endif
299 fieldSlot->type = FIELD_TYPE_INTEGER;
300 return true;
301}
302
303bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value)
304{
305 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
306 if (!fieldSlot) {
307 return false;
308 }
309
310#if WINDOW_STORAGE_INLINE_NUMERICS
311 fieldSlot->data.d = value;
312#else
313 int offset = alloc(sizeof(int64_t));
314 if (!offset) {
315 return false;
316 }
317
318 copyIn(offset, value);
319
320 fieldSlot->data.buffer.offset = offset;
321 fieldSlot->data.buffer.size = sizeof(double);
322#endif
323 fieldSlot->type = FIELD_TYPE_FLOAT;
324 return true;
325}
326
327bool CursorWindow::putNull(unsigned int row, unsigned int col)
328{
329 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
330 if (!fieldSlot) {
331 return false;
332 }
333
334 fieldSlot->type = FIELD_TYPE_NULL;
335 fieldSlot->data.buffer.offset = 0;
336 fieldSlot->data.buffer.size = 0;
337 return true;
338}
339
340bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOut)
341{
342 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
343 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) {
344 return false;
345 }
Jeff Brown3bc6bbc2011-10-06 13:11:04 -0700346
347 *valueOut = getFieldSlotValueLong(fieldSlot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return true;
349}
350
351bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueOut)
352{
353 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
354 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_FLOAT) {
355 return false;
356 }
357
Jeff Brown3bc6bbc2011-10-06 13:11:04 -0700358 *valueOut = getFieldSlotValueDouble(fieldSlot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 return true;
360}
361
362bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut)
363{
364 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
365 if (!fieldSlot) {
366 return false;
367 }
368
369 if (fieldSlot->type != FIELD_TYPE_NULL) {
370 *valueOut = false;
371 } else {
372 *valueOut = true;
373 }
374 return true;
375}
376
377}; // namespace android