blob: 20b27c9eb94804632b3336d6f6127ebc1aada0c6 [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--;
118 LOGE("The row failed, so back out the new row accounting from allocRowSlot %d", mHeader->numRows);
119 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()) {
144 LOGE("need to grow: mSize = %d, size = %d, freeSpace() = %d, numRows = %d", mSize, size, freeSpace(), mHeader->numRows);
145 // Only grow the window if the first row doesn't fit
146 if (mHeader->numRows > 1) {
147LOGE("not growing since there are already %d row(s), max size %d", mHeader->numRows, mMaxSize);
148 return 0;
149 }
150
151 // Find a new size that will fit the allocation
152 int allocated = mSize - freeSpace();
153 int newSize = mSize + WINDOW_ALLOCATION_SIZE;
154 while (size > (newSize - allocated)) {
155 newSize += WINDOW_ALLOCATION_SIZE;
156 if (newSize > mMaxSize) {
157 LOGE("Attempting to grow window beyond max size (%d)", mMaxSize);
158 return 0;
159 }
160 }
161LOG_WINDOW("found size %d", newSize);
162 mSize = newSize;
163 }
164
165 uint32_t offset = mFreeOffset + padding;
166 mFreeOffset += size;
167 return offset;
168}
169
170row_slot_t * CursorWindow::getRowSlot(int row)
171{
172 LOG_WINDOW("enter getRowSlot current row num %d, this row %d", mHeader->numRows, row);
173 int chunkNum = row / ROW_SLOT_CHUNK_NUM_ROWS;
174 int chunkPos = row % ROW_SLOT_CHUNK_NUM_ROWS;
175 int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t);
176 uint8_t * rowChunk = mData + sizeof(window_header_t);
177 for (int i = 0; i < chunkNum; i++) {
178 rowChunk = offsetToPtr(*((uint32_t *)(mData + chunkPtrOffset)));
179 chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t));
180 }
181 return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t)));
182 LOG_WINDOW("exit getRowSlot current row num %d, this row %d", mHeader->numRows, row);
183}
184
185row_slot_t * CursorWindow::allocRowSlot()
186{
187 int chunkNum = mHeader->numRows / ROW_SLOT_CHUNK_NUM_ROWS;
188 int chunkPos = mHeader->numRows % ROW_SLOT_CHUNK_NUM_ROWS;
189 int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t);
190 uint8_t * rowChunk = mData + sizeof(window_header_t);
191LOG_WINDOW("Allocating row slot, mHeader->numRows is %d, chunkNum is %d, chunkPos is %d", mHeader->numRows, chunkNum, chunkPos);
192 for (int i = 0; i < chunkNum; i++) {
193 uint32_t nextChunkOffset = *((uint32_t *)(mData + chunkPtrOffset));
194LOG_WINDOW("nextChunkOffset is %d", nextChunkOffset);
195 if (nextChunkOffset == 0) {
196 // Allocate a new row chunk
197 nextChunkOffset = alloc(ROW_SLOT_CHUNK_SIZE, true);
198 if (nextChunkOffset == 0) {
199 return NULL;
200 }
201 rowChunk = offsetToPtr(nextChunkOffset);
202LOG_WINDOW("allocated new chunk at %d, rowChunk = %p", nextChunkOffset, rowChunk);
203 *((uint32_t *)(mData + chunkPtrOffset)) = rowChunk - mData;
204 // Mark the new chunk's next 'pointer' as null
205 *((uint32_t *)(rowChunk + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t))) = 0;
206 } else {
207LOG_WINDOW("follwing 'pointer' to next chunk, offset of next pointer is %d", chunkPtrOffset);
208 rowChunk = offsetToPtr(nextChunkOffset);
209 chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t));
210 }
211 }
212 mHeader->numRows++;
213
214 return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t)));
215}
216
217field_slot_t * CursorWindow::getFieldSlotWithCheck(int row, int column)
218{
219 if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) {
220 LOGE("Bad request for field slot %d,%d. numRows = %d, numColumns = %d", row, column, mHeader->numRows, mHeader->numColumns);
221 return NULL;
222 }
223 row_slot_t * rowSlot = getRowSlot(row);
224 if (!rowSlot) {
225 LOGE("Failed to find rowSlot for row %d", row);
226 return NULL;
227 }
228 if (rowSlot->offset == 0 || rowSlot->offset >= mSize) {
229 LOGE("Invalid rowSlot, offset = %d", rowSlot->offset);
230 return NULL;
231 }
232 int fieldDirOffset = rowSlot->offset;
233 return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column;
234}
235
236uint32_t CursorWindow::read_field_slot(int row, int column, field_slot_t * slotOut)
237{
238 if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) {
239 LOGE("Bad request for field slot %d,%d. numRows = %d, numColumns = %d", row, column, mHeader->numRows, mHeader->numColumns);
240 return -1;
241 }
242 row_slot_t * rowSlot = getRowSlot(row);
243 if (!rowSlot) {
244 LOGE("Failed to find rowSlot for row %d", row);
245 return -1;
246 }
247 if (rowSlot->offset == 0 || rowSlot->offset >= mSize) {
248 LOGE("Invalid rowSlot, offset = %d", rowSlot->offset);
249 return -1;
250 }
251LOG_WINDOW("Found field directory for %d,%d at rowSlot %d, offset %d", row, column, (uint8_t *)rowSlot - mData, rowSlot->offset);
252 field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(rowSlot->offset);
253LOG_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);
254
255 // Copy the data to the out param
256 slotOut->data.buffer.offset = fieldDir[column].data.buffer.offset;
257 slotOut->data.buffer.size = fieldDir[column].data.buffer.size;
258 slotOut->type = fieldDir[column].type;
259 return 0;
260}
261
262void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size)
263{
264 assert(offset + size <= mSize);
265 memcpy(mData + offset, data, size);
266}
267
268void CursorWindow::copyIn(uint32_t offset, int64_t data)
269{
270 assert(offset + sizeof(int64_t) <= mSize);
271 memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t));
272}
273
274void CursorWindow::copyIn(uint32_t offset, double data)
275{
276 assert(offset + sizeof(double) <= mSize);
277 memcpy(mData + offset, (uint8_t *)&data, sizeof(double));
278}
279
280void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size)
281{
282 assert(offset + size <= mSize);
283 memcpy(data, mData + offset, size);
284}
285
286int64_t CursorWindow::copyOutLong(uint32_t offset)
287{
288 int64_t value;
289 assert(offset + sizeof(int64_t) <= mSize);
290 memcpy(&value, mData + offset, sizeof(int64_t));
291 return value;
292}
293
294double CursorWindow::copyOutDouble(uint32_t offset)
295{
296 double value;
297 assert(offset + sizeof(double) <= mSize);
298 memcpy(&value, mData + offset, sizeof(double));
299 return value;
300}
301
302bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value)
303{
304 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
305 if (!fieldSlot) {
306 return false;
307 }
308
309#if WINDOW_STORAGE_INLINE_NUMERICS
310 fieldSlot->data.l = value;
311#else
312 int offset = alloc(sizeof(int64_t));
313 if (!offset) {
314 return false;
315 }
316
317 copyIn(offset, value);
318
319 fieldSlot->data.buffer.offset = offset;
320 fieldSlot->data.buffer.size = sizeof(int64_t);
321#endif
322 fieldSlot->type = FIELD_TYPE_INTEGER;
323 return true;
324}
325
326bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value)
327{
328 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
329 if (!fieldSlot) {
330 return false;
331 }
332
333#if WINDOW_STORAGE_INLINE_NUMERICS
334 fieldSlot->data.d = value;
335#else
336 int offset = alloc(sizeof(int64_t));
337 if (!offset) {
338 return false;
339 }
340
341 copyIn(offset, value);
342
343 fieldSlot->data.buffer.offset = offset;
344 fieldSlot->data.buffer.size = sizeof(double);
345#endif
346 fieldSlot->type = FIELD_TYPE_FLOAT;
347 return true;
348}
349
350bool CursorWindow::putNull(unsigned int row, unsigned int col)
351{
352 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
353 if (!fieldSlot) {
354 return false;
355 }
356
357 fieldSlot->type = FIELD_TYPE_NULL;
358 fieldSlot->data.buffer.offset = 0;
359 fieldSlot->data.buffer.size = 0;
360 return true;
361}
362
363bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOut)
364{
365 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
366 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) {
367 return false;
368 }
369
370#if WINDOW_STORAGE_INLINE_NUMERICS
371 *valueOut = fieldSlot->data.l;
372#else
373 *valueOut = copyOutLong(fieldSlot->data.buffer.offset);
374#endif
375 return true;
376}
377
378bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueOut)
379{
380 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
381 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_FLOAT) {
382 return false;
383 }
384
385#if WINDOW_STORAGE_INLINE_NUMERICS
386 *valueOut = fieldSlot->data.d;
387#else
388 *valueOut = copyOutDouble(fieldSlot->data.buffer.offset);
389#endif
390 return true;
391}
392
393bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut)
394{
395 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
396 if (!fieldSlot) {
397 return false;
398 }
399
400 if (fieldSlot->type != FIELD_TYPE_NULL) {
401 *valueOut = false;
402 } else {
403 *valueOut = true;
404 }
405 return true;
406}
407
408}; // namespace android