The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2007 The Android Open Source Project |
| 3 | * |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | * |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | * |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "CursorWindow" |
| 19 | |
Mathias Agopian | 49d2b18 | 2012-02-27 18:11:20 -0800 | [diff] [blame] | 20 | #include <androidfw/CursorWindow.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 21 | #include <binder/Parcel.h> |
| 22 | #include <utils/Log.h> |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 23 | |
| 24 | #include <cutils/ashmem.h> |
| 25 | #include <sys/mman.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <assert.h> |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 33 | CursorWindow::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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 39 | CursorWindow::~CursorWindow() { |
| 40 | ::munmap(mData, mSize); |
| 41 | ::close(mAshmemFd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 44 | status_t CursorWindow::create(const String8& name, size_t size, CursorWindow** outCursorWindow) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 45 | String8 ashmemName("CursorWindow: "); |
| 46 | ashmemName.append(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 48 | status_t result; |
| 49 | int ashmemFd = ashmem_create_region(ashmemName.string(), size); |
| 50 | if (ashmemFd < 0) { |
| 51 | result = -errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } else { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 53 | result = ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE); |
| 54 | if (result >= 0) { |
| 55 | void* data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0); |
| 56 | if (data == MAP_FAILED) { |
| 57 | result = -errno; |
| 58 | } else { |
| 59 | result = ashmem_set_prot_region(ashmemFd, PROT_READ); |
| 60 | if (result >= 0) { |
| 61 | CursorWindow* window = new CursorWindow(name, ashmemFd, |
| 62 | data, size, false /*readOnly*/); |
| 63 | result = window->clear(); |
| 64 | if (!result) { |
| 65 | LOG_WINDOW("Created new CursorWindow: freeOffset=%d, " |
| 66 | "numRows=%d, numColumns=%d, mSize=%d, mData=%p", |
| 67 | window->mHeader->freeOffset, |
| 68 | window->mHeader->numRows, |
| 69 | window->mHeader->numColumns, |
| 70 | window->mSize, window->mData); |
| 71 | *outCursorWindow = window; |
| 72 | return OK; |
| 73 | } |
| 74 | delete window; |
| 75 | } |
| 76 | } |
| 77 | ::munmap(data, size); |
| 78 | } |
| 79 | ::close(ashmemFd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 81 | *outCursorWindow = NULL; |
| 82 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 85 | status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow) { |
| 86 | String8 name = parcel->readString8(); |
| 87 | |
| 88 | status_t result; |
| 89 | int ashmemFd = parcel->readFileDescriptor(); |
| 90 | if (ashmemFd == int(BAD_TYPE)) { |
| 91 | result = BAD_TYPE; |
| 92 | } else { |
| 93 | ssize_t size = ashmem_get_size_region(ashmemFd); |
| 94 | if (size < 0) { |
| 95 | result = UNKNOWN_ERROR; |
| 96 | } else { |
Nick Kralevich | e104df9 | 2019-01-14 14:03:36 -0800 | [diff] [blame^] | 97 | int dupAshmemFd = ::fcntl(ashmemFd, F_DUPFD_CLOEXEC, 0); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 98 | if (dupAshmemFd < 0) { |
| 99 | result = -errno; |
| 100 | } else { |
Fyodor Kupolov | 45e2e95 | 2017-02-13 18:35:12 -0800 | [diff] [blame] | 101 | // the size of the ashmem descriptor can be modified between ashmem_get_size_region |
| 102 | // call and mmap, so we'll check again immediately after memory is mapped |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 103 | void* data = ::mmap(NULL, size, PROT_READ, MAP_SHARED, dupAshmemFd, 0); |
| 104 | if (data == MAP_FAILED) { |
| 105 | result = -errno; |
Fyodor Kupolov | 45e2e95 | 2017-02-13 18:35:12 -0800 | [diff] [blame] | 106 | } else if (ashmem_get_size_region(dupAshmemFd) != size) { |
| 107 | ::munmap(data, size); |
| 108 | result = BAD_VALUE; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 109 | } else { |
| 110 | CursorWindow* window = new CursorWindow(name, dupAshmemFd, |
| 111 | data, size, true /*readOnly*/); |
| 112 | LOG_WINDOW("Created CursorWindow from parcel: freeOffset=%d, " |
| 113 | "numRows=%d, numColumns=%d, mSize=%d, mData=%p", |
| 114 | window->mHeader->freeOffset, |
| 115 | window->mHeader->numRows, |
| 116 | window->mHeader->numColumns, |
| 117 | window->mSize, window->mData); |
| 118 | *outCursorWindow = window; |
| 119 | return OK; |
| 120 | } |
| 121 | ::close(dupAshmemFd); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | *outCursorWindow = NULL; |
| 126 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 129 | status_t CursorWindow::writeToParcel(Parcel* parcel) { |
| 130 | status_t status = parcel->writeString8(mName); |
| 131 | if (!status) { |
| 132 | status = parcel->writeDupFileDescriptor(mAshmemFd); |
| 133 | } |
| 134 | return status; |
| 135 | } |
| 136 | |
| 137 | status_t CursorWindow::clear() { |
| 138 | if (mReadOnly) { |
| 139 | return INVALID_OPERATION; |
| 140 | } |
| 141 | |
| 142 | mHeader->freeOffset = sizeof(Header) + sizeof(RowSlotChunk); |
| 143 | mHeader->firstChunkOffset = sizeof(Header); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mHeader->numRows = 0; |
| 145 | mHeader->numColumns = 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 146 | |
| 147 | RowSlotChunk* firstChunk = static_cast<RowSlotChunk*>(offsetToPtr(mHeader->firstChunkOffset)); |
| 148 | firstChunk->nextChunkOffset = 0; |
| 149 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 152 | status_t CursorWindow::setNumColumns(uint32_t numColumns) { |
| 153 | if (mReadOnly) { |
| 154 | return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 156 | |
| 157 | uint32_t cur = mHeader->numColumns; |
| 158 | if ((cur > 0 || mHeader->numRows > 0) && cur != numColumns) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 159 | ALOGE("Trying to go from %d columns to %d", cur, numColumns); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 160 | return INVALID_OPERATION; |
| 161 | } |
| 162 | mHeader->numColumns = numColumns; |
| 163 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 166 | status_t CursorWindow::allocRow() { |
| 167 | if (mReadOnly) { |
| 168 | return INVALID_OPERATION; |
| 169 | } |
| 170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | // Fill in the row slot |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 172 | RowSlot* rowSlot = allocRowSlot(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | if (rowSlot == NULL) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 174 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Allocate the slots for the field directory |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 178 | size_t fieldDirSize = mHeader->numColumns * sizeof(FieldSlot); |
| 179 | uint32_t fieldDirOffset = alloc(fieldDirSize, true /*aligned*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | if (!fieldDirOffset) { |
| 181 | mHeader->numRows--; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 182 | LOG_WINDOW("The row failed, so back out the new row accounting " |
| 183 | "from allocRowSlot %d", mHeader->numRows); |
| 184 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 186 | FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(fieldDirOffset)); |
| 187 | memset(fieldDir, 0, fieldDirSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 189 | LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n", |
| 190 | mHeader->numRows - 1, offsetFromPtr(rowSlot), fieldDirSize, fieldDirOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | rowSlot->offset = fieldDirOffset; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 192 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 195 | status_t CursorWindow::freeLastRow() { |
| 196 | if (mReadOnly) { |
| 197 | return INVALID_OPERATION; |
| 198 | } |
| 199 | |
| 200 | if (mHeader->numRows > 0) { |
| 201 | mHeader->numRows--; |
| 202 | } |
| 203 | return OK; |
| 204 | } |
| 205 | |
| 206 | uint32_t CursorWindow::alloc(size_t size, bool aligned) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | uint32_t padding; |
| 208 | if (aligned) { |
| 209 | // 4 byte alignment |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 210 | padding = (~mHeader->freeOffset + 1) & 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } else { |
| 212 | padding = 0; |
| 213 | } |
| 214 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 215 | uint32_t offset = mHeader->freeOffset + padding; |
| 216 | uint32_t nextFreeOffset = offset + size; |
| 217 | if (nextFreeOffset > mSize) { |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 218 | ALOGW("Window is full: requested allocation %zu bytes, " |
| 219 | "free space %zu bytes, window size %zu bytes", |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 220 | size, freeSpace(), mSize); |
| 221 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 224 | mHeader->freeOffset = nextFreeOffset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | return offset; |
| 226 | } |
| 227 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 228 | CursorWindow::RowSlot* CursorWindow::getRowSlot(uint32_t row) { |
| 229 | uint32_t chunkPos = row; |
| 230 | RowSlotChunk* chunk = static_cast<RowSlotChunk*>( |
| 231 | offsetToPtr(mHeader->firstChunkOffset)); |
| 232 | while (chunkPos >= ROW_SLOT_CHUNK_NUM_ROWS) { |
| 233 | chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset)); |
| 234 | chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 236 | return &chunk->slots[chunkPos]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 239 | CursorWindow::RowSlot* CursorWindow::allocRowSlot() { |
| 240 | uint32_t chunkPos = mHeader->numRows; |
| 241 | RowSlotChunk* chunk = static_cast<RowSlotChunk*>( |
| 242 | offsetToPtr(mHeader->firstChunkOffset)); |
| 243 | while (chunkPos > ROW_SLOT_CHUNK_NUM_ROWS) { |
| 244 | chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset)); |
| 245 | chunkPos -= ROW_SLOT_CHUNK_NUM_ROWS; |
| 246 | } |
| 247 | if (chunkPos == ROW_SLOT_CHUNK_NUM_ROWS) { |
| 248 | if (!chunk->nextChunkOffset) { |
| 249 | chunk->nextChunkOffset = alloc(sizeof(RowSlotChunk), true /*aligned*/); |
| 250 | if (!chunk->nextChunkOffset) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | return NULL; |
| 252 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 254 | chunk = static_cast<RowSlotChunk*>(offsetToPtr(chunk->nextChunkOffset)); |
| 255 | chunk->nextChunkOffset = 0; |
| 256 | chunkPos = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 258 | mHeader->numRows += 1; |
| 259 | return &chunk->slots[chunkPos]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 262 | CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) { |
| 263 | if (row >= mHeader->numRows || column >= mHeader->numColumns) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 264 | ALOGE("Failed to read row %d, column %d from a CursorWindow which " |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 265 | "has %d rows, %d columns.", |
| 266 | row, column, mHeader->numRows, mHeader->numColumns); |
| 267 | return NULL; |
| 268 | } |
| 269 | RowSlot* rowSlot = getRowSlot(row); |
| 270 | if (!rowSlot) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 271 | ALOGE("Failed to find rowSlot for row %d.", row); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
| 274 | FieldSlot* fieldDir = static_cast<FieldSlot*>(offsetToPtr(rowSlot->offset)); |
| 275 | return &fieldDir[column]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 278 | status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) { |
| 279 | return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 282 | status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value, |
| 283 | size_t sizeIncludingNull) { |
| 284 | return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 287 | status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column, |
| 288 | const void* value, size_t size, int32_t type) { |
| 289 | if (mReadOnly) { |
| 290 | return INVALID_OPERATION; |
| 291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 293 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 295 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 298 | uint32_t offset = alloc(size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | if (!offset) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 300 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 303 | memcpy(offsetToPtr(offset), value, size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 305 | fieldSlot->type = type; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | fieldSlot->data.buffer.offset = offset; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 307 | fieldSlot->data.buffer.size = size; |
| 308 | return OK; |
| 309 | } |
| 310 | |
| 311 | status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) { |
| 312 | if (mReadOnly) { |
| 313 | return INVALID_OPERATION; |
| 314 | } |
| 315 | |
| 316 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
| 317 | if (!fieldSlot) { |
| 318 | return BAD_VALUE; |
| 319 | } |
| 320 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | fieldSlot->type = FIELD_TYPE_INTEGER; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 322 | fieldSlot->data.l = value; |
| 323 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 326 | status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) { |
| 327 | if (mReadOnly) { |
| 328 | return INVALID_OPERATION; |
| 329 | } |
| 330 | |
| 331 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 333 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | } |
| 335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | fieldSlot->type = FIELD_TYPE_FLOAT; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 337 | fieldSlot->data.d = value; |
| 338 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 341 | status_t CursorWindow::putNull(uint32_t row, uint32_t column) { |
| 342 | if (mReadOnly) { |
| 343 | return INVALID_OPERATION; |
| 344 | } |
| 345 | |
| 346 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 348 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | fieldSlot->type = FIELD_TYPE_NULL; |
| 352 | fieldSlot->data.buffer.offset = 0; |
| 353 | fieldSlot->data.buffer.size = 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 354 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | }; // namespace android |