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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #define LOG_TAG "CursorWindow" |
| 18 | |
Mathias Agopian | 49d2b18 | 2012-02-27 18:11:20 -0800 | [diff] [blame] | 19 | #include <androidfw/CursorWindow.h> |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 20 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 23 | #include "android-base/logging.h" |
| 24 | #include "cutils/ashmem.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 28 | /** |
| 29 | * By default windows are lightweight inline allocations of this size; |
| 30 | * they're only inflated to ashmem regions when more space is needed. |
| 31 | */ |
| 32 | static constexpr const size_t kInlineSize = 16384; |
| 33 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 34 | static constexpr const size_t kSlotShift = 4; |
| 35 | static constexpr const size_t kSlotSizeBytes = 1 << kSlotShift; |
| 36 | |
| 37 | CursorWindow::CursorWindow() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 40 | CursorWindow::~CursorWindow() { |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 41 | if (mAshmemFd != -1) { |
| 42 | ::munmap(mData, mSize); |
| 43 | ::close(mAshmemFd); |
| 44 | } else { |
| 45 | free(mData); |
| 46 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 49 | status_t CursorWindow::create(const String8 &name, size_t inflatedSize, CursorWindow **outWindow) { |
| 50 | *outWindow = nullptr; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 51 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 52 | CursorWindow* window = new CursorWindow(); |
| 53 | if (!window) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 54 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 55 | window->mName = name; |
| 56 | window->mSize = std::min(kInlineSize, inflatedSize); |
| 57 | window->mInflatedSize = inflatedSize; |
| 58 | window->mData = malloc(window->mSize); |
| 59 | if (!window->mData) goto fail; |
| 60 | window->mReadOnly = false; |
| 61 | |
| 62 | window->clear(); |
| 63 | window->updateSlotsData(); |
| 64 | |
| 65 | LOG(DEBUG) << "Created: " << window->toString(); |
| 66 | *outWindow = window; |
| 67 | return OK; |
| 68 | |
| 69 | fail: |
| 70 | LOG(ERROR) << "Failed create"; |
| 71 | fail_silent: |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 72 | delete window; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 73 | return UNKNOWN_ERROR; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 74 | } |
| 75 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 76 | status_t CursorWindow::maybeInflate() { |
| 77 | int ashmemFd = 0; |
| 78 | void* newData = nullptr; |
| 79 | |
| 80 | // Bail early when we can't expand any further |
| 81 | if (mReadOnly || mSize == mInflatedSize) { |
| 82 | return INVALID_OPERATION; |
| 83 | } |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 84 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 85 | String8 ashmemName("CursorWindow: "); |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 86 | ashmemName.append(mName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 88 | ashmemFd = ashmem_create_region(ashmemName.string(), mInflatedSize); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 89 | if (ashmemFd < 0) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 90 | PLOG(ERROR) << "Failed ashmem_create_region"; |
| 91 | goto fail_silent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 93 | |
| 94 | if (ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE) < 0) { |
| 95 | PLOG(ERROR) << "Failed ashmem_set_prot_region"; |
| 96 | goto fail_silent; |
| 97 | } |
| 98 | |
| 99 | newData = ::mmap(nullptr, mInflatedSize, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0); |
| 100 | if (newData == MAP_FAILED) { |
| 101 | PLOG(ERROR) << "Failed mmap"; |
| 102 | goto fail_silent; |
| 103 | } |
| 104 | |
| 105 | if (ashmem_set_prot_region(ashmemFd, PROT_READ) < 0) { |
| 106 | PLOG(ERROR) << "Failed ashmem_set_prot_region"; |
| 107 | goto fail_silent; |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | // Migrate existing contents into new ashmem region |
| 112 | uint32_t slotsSize = mSize - mSlotsOffset; |
| 113 | uint32_t newSlotsOffset = mInflatedSize - slotsSize; |
| 114 | memcpy(static_cast<uint8_t*>(newData), |
| 115 | static_cast<uint8_t*>(mData), mAllocOffset); |
| 116 | memcpy(static_cast<uint8_t*>(newData) + newSlotsOffset, |
| 117 | static_cast<uint8_t*>(mData) + mSlotsOffset, slotsSize); |
| 118 | |
| 119 | free(mData); |
| 120 | mAshmemFd = ashmemFd; |
| 121 | mData = newData; |
| 122 | mSize = mInflatedSize; |
| 123 | mSlotsOffset = newSlotsOffset; |
| 124 | |
| 125 | updateSlotsData(); |
| 126 | } |
| 127 | |
| 128 | LOG(DEBUG) << "Inflated: " << this->toString(); |
| 129 | return OK; |
| 130 | |
| 131 | fail: |
| 132 | LOG(ERROR) << "Failed maybeInflate"; |
| 133 | fail_silent: |
| 134 | ::munmap(newData, mInflatedSize); |
| 135 | ::close(ashmemFd); |
| 136 | return UNKNOWN_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 139 | status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outWindow) { |
| 140 | *outWindow = nullptr; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 141 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 142 | CursorWindow* window = new CursorWindow(); |
| 143 | if (!window) goto fail; |
| 144 | |
| 145 | if (parcel->readString8(&window->mName)) goto fail; |
| 146 | if (parcel->readUint32(&window->mNumRows)) goto fail; |
| 147 | if (parcel->readUint32(&window->mNumColumns)) goto fail; |
| 148 | if (parcel->readUint32(&window->mSize)) goto fail; |
| 149 | |
| 150 | if ((window->mNumRows * window->mNumColumns * kSlotSizeBytes) > window->mSize) { |
| 151 | LOG(ERROR) << "Unexpected size " << window->mSize << " for " << window->mNumRows |
| 152 | << " rows and " << window->mNumColumns << " columns"; |
| 153 | goto fail_silent; |
| 154 | } |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 155 | |
| 156 | bool isAshmem; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 157 | if (parcel->readBool(&isAshmem)) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 158 | if (isAshmem) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 159 | window->mAshmemFd = parcel->readFileDescriptor(); |
| 160 | if (window->mAshmemFd < 0) { |
| 161 | LOG(ERROR) << "Failed readFileDescriptor"; |
| 162 | goto fail_silent; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 163 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 164 | |
| 165 | window->mAshmemFd = ::fcntl(window->mAshmemFd, F_DUPFD_CLOEXEC, 0); |
| 166 | if (window->mAshmemFd < 0) { |
| 167 | PLOG(ERROR) << "Failed F_DUPFD_CLOEXEC"; |
| 168 | goto fail_silent; |
| 169 | } |
| 170 | |
| 171 | window->mData = ::mmap(nullptr, window->mSize, PROT_READ, MAP_SHARED, window->mAshmemFd, 0); |
| 172 | if (window->mData == MAP_FAILED) { |
| 173 | PLOG(ERROR) << "Failed mmap"; |
| 174 | goto fail_silent; |
| 175 | } |
| 176 | } else { |
| 177 | window->mAshmemFd = -1; |
| 178 | |
| 179 | if (window->mSize > kInlineSize) { |
| 180 | LOG(ERROR) << "Unexpected size " << window->mSize << " for inline window"; |
| 181 | goto fail_silent; |
| 182 | } |
| 183 | |
| 184 | window->mData = malloc(window->mSize); |
| 185 | if (!window->mData) goto fail; |
| 186 | |
| 187 | if (parcel->read(window->mData, window->mSize)) goto fail; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 190 | // We just came from a remote source, so we're read-only |
| 191 | // and we can't inflate ourselves |
| 192 | window->mInflatedSize = window->mSize; |
| 193 | window->mReadOnly = true; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 194 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 195 | window->updateSlotsData(); |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 196 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 197 | LOG(DEBUG) << "Created from parcel: " << window->toString(); |
| 198 | *outWindow = window; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 199 | return OK; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 200 | |
| 201 | fail: |
| 202 | LOG(ERROR) << "Failed createFromParcel"; |
| 203 | fail_silent: |
| 204 | delete window; |
| 205 | return UNKNOWN_ERROR; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 206 | } |
| 207 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 208 | status_t CursorWindow::writeToParcel(Parcel* parcel) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 209 | LOG(DEBUG) << "Writing to parcel: " << this->toString(); |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 210 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 211 | if (parcel->writeString8(mName)) goto fail; |
| 212 | if (parcel->writeUint32(mNumRows)) goto fail; |
| 213 | if (parcel->writeUint32(mNumColumns)) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 214 | if (mAshmemFd != -1) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 215 | if (parcel->writeUint32(mSize)) goto fail; |
| 216 | if (parcel->writeBool(true)) goto fail; |
| 217 | if (parcel->writeDupFileDescriptor(mAshmemFd)) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 218 | } else { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 219 | // Since we know we're going to be read-only on the remote side, |
| 220 | // we can compact ourselves on the wire, with just enough padding |
| 221 | // to ensure our slots stay aligned |
| 222 | size_t slotsSize = mSize - mSlotsOffset; |
| 223 | size_t compactedSize = mAllocOffset + slotsSize; |
| 224 | compactedSize = (compactedSize + 3) & ~3; |
| 225 | if (parcel->writeUint32(compactedSize)) goto fail; |
| 226 | if (parcel->writeBool(false)) goto fail; |
| 227 | void* dest = parcel->writeInplace(compactedSize); |
| 228 | if (!dest) goto fail; |
| 229 | memcpy(static_cast<uint8_t*>(dest), |
| 230 | static_cast<uint8_t*>(mData), mAllocOffset); |
| 231 | memcpy(static_cast<uint8_t*>(dest) + compactedSize - slotsSize, |
| 232 | static_cast<uint8_t*>(mData) + mSlotsOffset, slotsSize); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 233 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 234 | return OK; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 235 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 236 | fail: |
| 237 | LOG(ERROR) << "Failed writeToParcel"; |
| 238 | fail_silent: |
| 239 | return UNKNOWN_ERROR; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | status_t CursorWindow::clear() { |
| 243 | if (mReadOnly) { |
| 244 | return INVALID_OPERATION; |
| 245 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 246 | mAllocOffset = 0; |
| 247 | mSlotsOffset = mSize; |
| 248 | mNumRows = 0; |
| 249 | mNumColumns = 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 250 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 253 | void CursorWindow::updateSlotsData() { |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame^] | 254 | mSlotsStart = static_cast<uint8_t*>(mData) + mSize - kSlotSizeBytes; |
| 255 | mSlotsEnd = static_cast<uint8_t*>(mData) + mSlotsOffset; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void* CursorWindow::offsetToPtr(uint32_t offset, uint32_t bufferSize = 0) { |
| 259 | if (offset > mSize) { |
| 260 | LOG(ERROR) << "Offset " << offset |
| 261 | << " out of bounds, max value " << mSize; |
| 262 | return nullptr; |
| 263 | } |
| 264 | if (offset + bufferSize > mSize) { |
| 265 | LOG(ERROR) << "End offset " << (offset + bufferSize) |
| 266 | << " out of bounds, max value " << mSize; |
| 267 | return nullptr; |
| 268 | } |
| 269 | return static_cast<uint8_t*>(mData) + offset; |
| 270 | } |
| 271 | |
| 272 | uint32_t CursorWindow::offsetFromPtr(void* ptr) { |
| 273 | return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData); |
| 274 | } |
| 275 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 276 | status_t CursorWindow::setNumColumns(uint32_t numColumns) { |
| 277 | if (mReadOnly) { |
| 278 | return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 280 | uint32_t cur = mNumColumns; |
| 281 | if ((cur > 0 || mNumRows > 0) && cur != numColumns) { |
| 282 | LOG(ERROR) << "Trying to go from " << cur << " columns to " << numColumns; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 283 | return INVALID_OPERATION; |
| 284 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 285 | mNumColumns = numColumns; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 286 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 289 | status_t CursorWindow::allocRow() { |
| 290 | if (mReadOnly) { |
| 291 | return INVALID_OPERATION; |
| 292 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 293 | size_t size = mNumColumns * kSlotSizeBytes; |
| 294 | off_t newOffset = mSlotsOffset - size; |
| 295 | if (newOffset < mAllocOffset) { |
| 296 | maybeInflate(); |
| 297 | newOffset = mSlotsOffset - size; |
| 298 | if (newOffset < mAllocOffset) { |
| 299 | return NO_MEMORY; |
| 300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 302 | memset(offsetToPtr(newOffset), 0, size); |
| 303 | mSlotsOffset = newOffset; |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame^] | 304 | updateSlotsData(); |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 305 | mNumRows++; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 306 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 309 | status_t CursorWindow::freeLastRow() { |
| 310 | if (mReadOnly) { |
| 311 | return INVALID_OPERATION; |
| 312 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 313 | size_t size = mNumColumns * kSlotSizeBytes; |
| 314 | off_t newOffset = mSlotsOffset + size; |
| 315 | if (newOffset > mSize) { |
| 316 | return NO_MEMORY; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 317 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 318 | mSlotsOffset = newOffset; |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame^] | 319 | updateSlotsData(); |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 320 | mNumRows--; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 321 | return OK; |
| 322 | } |
| 323 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 324 | status_t CursorWindow::alloc(size_t size, uint32_t* outOffset) { |
| 325 | if (mReadOnly) { |
| 326 | return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 328 | size_t alignedSize = (size + 3) & ~3; |
| 329 | off_t newOffset = mAllocOffset + alignedSize; |
| 330 | if (newOffset > mSlotsOffset) { |
| 331 | maybeInflate(); |
| 332 | newOffset = mAllocOffset + alignedSize; |
| 333 | if (newOffset > mSlotsOffset) { |
| 334 | return NO_MEMORY; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 335 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 337 | *outOffset = mAllocOffset; |
| 338 | mAllocOffset = newOffset; |
| 339 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 342 | CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 343 | // This is carefully tuned to use as few cycles as |
| 344 | // possible, since this is an extremely hot code path; |
| 345 | // see CursorWindow_bench.cpp for more details |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame^] | 346 | void *result = static_cast<uint8_t*>(mSlotsStart) |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 347 | - (((row * mNumColumns) + column) << kSlotShift); |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame^] | 348 | if (result < mSlotsEnd || column >= mNumColumns) { |
| 349 | LOG(ERROR) << "Failed to read row " << row << ", column " << column |
| 350 | << " from a window with " << mNumRows << " rows, " << mNumColumns << " columns"; |
| 351 | return nullptr; |
| 352 | } else { |
| 353 | return static_cast<FieldSlot*>(result); |
| 354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 357 | status_t CursorWindow::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) { |
| 358 | return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 361 | status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value, |
| 362 | size_t sizeIncludingNull) { |
| 363 | return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 366 | status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column, |
| 367 | const void* value, size_t size, int32_t type) { |
| 368 | if (mReadOnly) { |
| 369 | return INVALID_OPERATION; |
| 370 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 372 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 374 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 377 | uint32_t offset; |
| 378 | if (alloc(size, &offset)) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 379 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 382 | memcpy(offsetToPtr(offset), value, size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 384 | fieldSlot = getFieldSlot(row, column); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 385 | fieldSlot->type = type; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | fieldSlot->data.buffer.offset = offset; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 387 | fieldSlot->data.buffer.size = size; |
| 388 | return OK; |
| 389 | } |
| 390 | |
| 391 | status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) { |
| 392 | if (mReadOnly) { |
| 393 | return INVALID_OPERATION; |
| 394 | } |
| 395 | |
| 396 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
| 397 | if (!fieldSlot) { |
| 398 | return BAD_VALUE; |
| 399 | } |
| 400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | fieldSlot->type = FIELD_TYPE_INTEGER; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 402 | fieldSlot->data.l = value; |
| 403 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 406 | status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) { |
| 407 | if (mReadOnly) { |
| 408 | return INVALID_OPERATION; |
| 409 | } |
| 410 | |
| 411 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 413 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | fieldSlot->type = FIELD_TYPE_FLOAT; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 417 | fieldSlot->data.d = value; |
| 418 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 421 | status_t CursorWindow::putNull(uint32_t row, uint32_t column) { |
| 422 | if (mReadOnly) { |
| 423 | return INVALID_OPERATION; |
| 424 | } |
| 425 | |
| 426 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 428 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | fieldSlot->type = FIELD_TYPE_NULL; |
| 432 | fieldSlot->data.buffer.offset = 0; |
| 433 | fieldSlot->data.buffer.size = 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 434 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | }; // namespace android |