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