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