blob: bdd4dd6505cdcd88ee860c7516a6ae03f435d3d0 [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()) {
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) {
222 LOGE("Bad request for field slot %d,%d. numRows = %d, numColumns = %d", row, column, mHeader->numRows, mHeader->numColumns);
223 return NULL;
224 }
225 row_slot_t * rowSlot = getRowSlot(row);
226 if (!rowSlot) {
227 LOGE("Failed to find rowSlot for row %d", row);
228 return NULL;
229 }
230 if (rowSlot->offset == 0 || rowSlot->offset >= mSize) {
231 LOGE("Invalid rowSlot, offset = %d", rowSlot->offset);
232 return NULL;
233 }
234 int fieldDirOffset = rowSlot->offset;
235 return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column;
236}
237
238uint32_t CursorWindow::read_field_slot(int row, int column, field_slot_t * slotOut)
239{
240 if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) {
241 LOGE("Bad request for field slot %d,%d. numRows = %d, numColumns = %d", row, column, mHeader->numRows, mHeader->numColumns);
242 return -1;
243 }
244 row_slot_t * rowSlot = getRowSlot(row);
245 if (!rowSlot) {
246 LOGE("Failed to find rowSlot for row %d", row);
247 return -1;
248 }
249 if (rowSlot->offset == 0 || rowSlot->offset >= mSize) {
250 LOGE("Invalid rowSlot, offset = %d", rowSlot->offset);
251 return -1;
252 }
253LOG_WINDOW("Found field directory for %d,%d at rowSlot %d, offset %d", row, column, (uint8_t *)rowSlot - mData, rowSlot->offset);
254 field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(rowSlot->offset);
255LOG_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);
256
257 // Copy the data to the out param
258 slotOut->data.buffer.offset = fieldDir[column].data.buffer.offset;
259 slotOut->data.buffer.size = fieldDir[column].data.buffer.size;
260 slotOut->type = fieldDir[column].type;
261 return 0;
262}
263
264void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size)
265{
266 assert(offset + size <= mSize);
267 memcpy(mData + offset, data, size);
268}
269
270void CursorWindow::copyIn(uint32_t offset, int64_t data)
271{
272 assert(offset + sizeof(int64_t) <= mSize);
273 memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t));
274}
275
276void CursorWindow::copyIn(uint32_t offset, double data)
277{
278 assert(offset + sizeof(double) <= mSize);
279 memcpy(mData + offset, (uint8_t *)&data, sizeof(double));
280}
281
282void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size)
283{
284 assert(offset + size <= mSize);
285 memcpy(data, mData + offset, size);
286}
287
288int64_t CursorWindow::copyOutLong(uint32_t offset)
289{
290 int64_t value;
291 assert(offset + sizeof(int64_t) <= mSize);
292 memcpy(&value, mData + offset, sizeof(int64_t));
293 return value;
294}
295
296double CursorWindow::copyOutDouble(uint32_t offset)
297{
298 double value;
299 assert(offset + sizeof(double) <= mSize);
300 memcpy(&value, mData + offset, sizeof(double));
301 return value;
302}
303
304bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value)
305{
306 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
307 if (!fieldSlot) {
308 return false;
309 }
310
311#if WINDOW_STORAGE_INLINE_NUMERICS
312 fieldSlot->data.l = value;
313#else
314 int offset = alloc(sizeof(int64_t));
315 if (!offset) {
316 return false;
317 }
318
319 copyIn(offset, value);
320
321 fieldSlot->data.buffer.offset = offset;
322 fieldSlot->data.buffer.size = sizeof(int64_t);
323#endif
324 fieldSlot->type = FIELD_TYPE_INTEGER;
325 return true;
326}
327
328bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value)
329{
330 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
331 if (!fieldSlot) {
332 return false;
333 }
334
335#if WINDOW_STORAGE_INLINE_NUMERICS
336 fieldSlot->data.d = value;
337#else
338 int offset = alloc(sizeof(int64_t));
339 if (!offset) {
340 return false;
341 }
342
343 copyIn(offset, value);
344
345 fieldSlot->data.buffer.offset = offset;
346 fieldSlot->data.buffer.size = sizeof(double);
347#endif
348 fieldSlot->type = FIELD_TYPE_FLOAT;
349 return true;
350}
351
352bool CursorWindow::putNull(unsigned int row, unsigned int col)
353{
354 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
355 if (!fieldSlot) {
356 return false;
357 }
358
359 fieldSlot->type = FIELD_TYPE_NULL;
360 fieldSlot->data.buffer.offset = 0;
361 fieldSlot->data.buffer.size = 0;
362 return true;
363}
364
365bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOut)
366{
367 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
368 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) {
369 return false;
370 }
371
372#if WINDOW_STORAGE_INLINE_NUMERICS
373 *valueOut = fieldSlot->data.l;
374#else
375 *valueOut = copyOutLong(fieldSlot->data.buffer.offset);
376#endif
377 return true;
378}
379
380bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueOut)
381{
382 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
383 if (!fieldSlot || fieldSlot->type != FIELD_TYPE_FLOAT) {
384 return false;
385 }
386
387#if WINDOW_STORAGE_INLINE_NUMERICS
388 *valueOut = fieldSlot->data.d;
389#else
390 *valueOut = copyOutDouble(fieldSlot->data.buffer.offset);
391#endif
392 return true;
393}
394
395bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut)
396{
397 field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col);
398 if (!fieldSlot) {
399 return false;
400 }
401
402 if (fieldSlot->type != FIELD_TYPE_NULL) {
403 *valueOut = false;
404 } else {
405 *valueOut = true;
406 }
407 return true;
408}
409
410}; // namespace android