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 | |
Michael Hoisie | a8c4d1a | 2024-10-01 22:54:13 +0000 | [diff] [blame] | 142 | #ifdef __linux__ |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 143 | status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outWindow) { |
| 144 | *outWindow = nullptr; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 145 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 146 | CursorWindow* window = new CursorWindow(); |
| 147 | if (!window) goto fail; |
| 148 | |
| 149 | if (parcel->readString8(&window->mName)) goto fail; |
| 150 | if (parcel->readUint32(&window->mNumRows)) goto fail; |
| 151 | if (parcel->readUint32(&window->mNumColumns)) goto fail; |
| 152 | if (parcel->readUint32(&window->mSize)) goto fail; |
| 153 | |
| 154 | if ((window->mNumRows * window->mNumColumns * kSlotSizeBytes) > window->mSize) { |
| 155 | LOG(ERROR) << "Unexpected size " << window->mSize << " for " << window->mNumRows |
| 156 | << " rows and " << window->mNumColumns << " columns"; |
| 157 | goto fail_silent; |
| 158 | } |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 159 | |
| 160 | bool isAshmem; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 161 | if (parcel->readBool(&isAshmem)) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 162 | if (isAshmem) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 163 | window->mAshmemFd = parcel->readFileDescriptor(); |
| 164 | if (window->mAshmemFd < 0) { |
| 165 | LOG(ERROR) << "Failed readFileDescriptor"; |
| 166 | goto fail_silent; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 167 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 168 | |
| 169 | window->mAshmemFd = ::fcntl(window->mAshmemFd, F_DUPFD_CLOEXEC, 0); |
| 170 | if (window->mAshmemFd < 0) { |
| 171 | PLOG(ERROR) << "Failed F_DUPFD_CLOEXEC"; |
| 172 | goto fail_silent; |
| 173 | } |
| 174 | |
Michael Hoisie | e28dd9f | 2024-03-12 18:15:14 +0000 | [diff] [blame] | 175 | window->mMappedFile = MappedFile::FromFd(window->mAshmemFd, 0, window->mSize, PROT_READ); |
| 176 | if (window->mMappedFile == nullptr) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 177 | PLOG(ERROR) << "Failed mmap"; |
| 178 | goto fail_silent; |
| 179 | } |
Michael Hoisie | e28dd9f | 2024-03-12 18:15:14 +0000 | [diff] [blame] | 180 | window->mData = window->mMappedFile->data(); |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 181 | } else { |
| 182 | window->mAshmemFd = -1; |
| 183 | |
| 184 | if (window->mSize > kInlineSize) { |
| 185 | LOG(ERROR) << "Unexpected size " << window->mSize << " for inline window"; |
| 186 | goto fail_silent; |
| 187 | } |
| 188 | |
| 189 | window->mData = malloc(window->mSize); |
| 190 | if (!window->mData) goto fail; |
| 191 | |
| 192 | if (parcel->read(window->mData, window->mSize)) goto fail; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 193 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 195 | // We just came from a remote source, so we're read-only |
| 196 | // and we can't inflate ourselves |
| 197 | window->mInflatedSize = window->mSize; |
| 198 | window->mReadOnly = true; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 199 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 200 | window->updateSlotsData(); |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 201 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 202 | LOG(DEBUG) << "Created from parcel: " << window->toString(); |
| 203 | *outWindow = window; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 204 | return OK; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 205 | |
| 206 | fail: |
| 207 | LOG(ERROR) << "Failed createFromParcel"; |
| 208 | fail_silent: |
| 209 | delete window; |
| 210 | return UNKNOWN_ERROR; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 211 | } |
| 212 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 213 | status_t CursorWindow::writeToParcel(Parcel* parcel) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 214 | LOG(DEBUG) << "Writing to parcel: " << this->toString(); |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 215 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 216 | if (parcel->writeString8(mName)) goto fail; |
| 217 | if (parcel->writeUint32(mNumRows)) goto fail; |
| 218 | if (parcel->writeUint32(mNumColumns)) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 219 | if (mAshmemFd != -1) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 220 | if (parcel->writeUint32(mSize)) goto fail; |
| 221 | if (parcel->writeBool(true)) goto fail; |
| 222 | if (parcel->writeDupFileDescriptor(mAshmemFd)) goto fail; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 223 | } else { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 224 | // 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] | 225 | // we can compact ourselves on the wire. |
| 226 | size_t slotsSize = sizeOfSlots(); |
| 227 | size_t compactedSize = sizeInUse(); |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 228 | if (parcel->writeUint32(compactedSize)) goto fail; |
| 229 | if (parcel->writeBool(false)) goto fail; |
| 230 | void* dest = parcel->writeInplace(compactedSize); |
| 231 | if (!dest) goto fail; |
| 232 | memcpy(static_cast<uint8_t*>(dest), |
| 233 | static_cast<uint8_t*>(mData), mAllocOffset); |
| 234 | memcpy(static_cast<uint8_t*>(dest) + compactedSize - slotsSize, |
| 235 | static_cast<uint8_t*>(mData) + mSlotsOffset, slotsSize); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 236 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 237 | return OK; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 238 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 239 | fail: |
| 240 | LOG(ERROR) << "Failed writeToParcel"; |
| 241 | fail_silent: |
| 242 | return UNKNOWN_ERROR; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 243 | } |
Michael Hoisie | a8c4d1a | 2024-10-01 22:54:13 +0000 | [diff] [blame] | 244 | #endif |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 245 | |
| 246 | status_t CursorWindow::clear() { |
| 247 | if (mReadOnly) { |
| 248 | return INVALID_OPERATION; |
| 249 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 250 | mAllocOffset = 0; |
| 251 | mSlotsOffset = mSize; |
| 252 | mNumRows = 0; |
| 253 | mNumColumns = 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 254 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 257 | void CursorWindow::updateSlotsData() { |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame] | 258 | mSlotsStart = static_cast<uint8_t*>(mData) + mSize - kSlotSizeBytes; |
| 259 | mSlotsEnd = static_cast<uint8_t*>(mData) + mSlotsOffset; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void* CursorWindow::offsetToPtr(uint32_t offset, uint32_t bufferSize = 0) { |
| 263 | if (offset > mSize) { |
| 264 | LOG(ERROR) << "Offset " << offset |
| 265 | << " out of bounds, max value " << mSize; |
| 266 | return nullptr; |
| 267 | } |
| 268 | if (offset + bufferSize > mSize) { |
| 269 | LOG(ERROR) << "End offset " << (offset + bufferSize) |
| 270 | << " out of bounds, max value " << mSize; |
| 271 | return nullptr; |
| 272 | } |
| 273 | return static_cast<uint8_t*>(mData) + offset; |
| 274 | } |
| 275 | |
| 276 | uint32_t CursorWindow::offsetFromPtr(void* ptr) { |
| 277 | return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData); |
| 278 | } |
| 279 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 280 | status_t CursorWindow::setNumColumns(uint32_t numColumns) { |
| 281 | if (mReadOnly) { |
| 282 | return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 284 | uint32_t cur = mNumColumns; |
| 285 | if ((cur > 0 || mNumRows > 0) && cur != numColumns) { |
| 286 | LOG(ERROR) << "Trying to go from " << cur << " columns to " << numColumns; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 287 | return INVALID_OPERATION; |
| 288 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 289 | mNumColumns = numColumns; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 290 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 293 | status_t CursorWindow::allocRow() { |
| 294 | if (mReadOnly) { |
| 295 | return INVALID_OPERATION; |
| 296 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 297 | size_t size = mNumColumns * kSlotSizeBytes; |
Jeff Sharkey | b2bbdaa | 2020-10-20 13:19:04 -0600 | [diff] [blame] | 298 | int32_t newOffset = mSlotsOffset - size; |
| 299 | if (newOffset < (int32_t) mAllocOffset) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 300 | maybeInflate(); |
| 301 | newOffset = mSlotsOffset - size; |
Jeff Sharkey | b2bbdaa | 2020-10-20 13:19:04 -0600 | [diff] [blame] | 302 | if (newOffset < (int32_t) mAllocOffset) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 303 | return NO_MEMORY; |
| 304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 306 | memset(offsetToPtr(newOffset), 0, size); |
| 307 | mSlotsOffset = newOffset; |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame] | 308 | updateSlotsData(); |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 309 | mNumRows++; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 310 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 313 | status_t CursorWindow::freeLastRow() { |
| 314 | if (mReadOnly) { |
| 315 | return INVALID_OPERATION; |
| 316 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 317 | size_t size = mNumColumns * kSlotSizeBytes; |
Jeff Sharkey | b2bbdaa | 2020-10-20 13:19:04 -0600 | [diff] [blame] | 318 | size_t newOffset = mSlotsOffset + size; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 319 | if (newOffset > mSize) { |
| 320 | return NO_MEMORY; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 321 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 322 | mSlotsOffset = newOffset; |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame] | 323 | updateSlotsData(); |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 324 | mNumRows--; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 325 | return OK; |
| 326 | } |
| 327 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 328 | status_t CursorWindow::alloc(size_t size, uint32_t* outOffset) { |
| 329 | if (mReadOnly) { |
| 330 | return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 332 | size_t alignedSize = (size + 3) & ~3; |
Jeff Sharkey | b2bbdaa | 2020-10-20 13:19:04 -0600 | [diff] [blame] | 333 | size_t newOffset = mAllocOffset + alignedSize; |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 334 | if (newOffset > mSlotsOffset) { |
| 335 | maybeInflate(); |
| 336 | newOffset = mAllocOffset + alignedSize; |
| 337 | if (newOffset > mSlotsOffset) { |
| 338 | return NO_MEMORY; |
Jeff Sharkey | 539fdff | 2020-09-23 21:43:23 -0600 | [diff] [blame] | 339 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 341 | *outOffset = mAllocOffset; |
| 342 | mAllocOffset = newOffset; |
| 343 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 346 | CursorWindow::FieldSlot* CursorWindow::getFieldSlot(uint32_t row, uint32_t column) { |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 347 | // This is carefully tuned to use as few cycles as |
| 348 | // possible, since this is an extremely hot code path; |
| 349 | // see CursorWindow_bench.cpp for more details |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame] | 350 | void *result = static_cast<uint8_t*>(mSlotsStart) |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 351 | - (((row * mNumColumns) + column) << kSlotShift); |
Jeff Sharkey | b2bbdaa | 2020-10-20 13:19:04 -0600 | [diff] [blame] | 352 | if (result < mSlotsEnd || result > mSlotsStart || column >= mNumColumns) { |
Jeff Sharkey | 256da5a | 2020-10-13 09:40:52 -0600 | [diff] [blame] | 353 | LOG(ERROR) << "Failed to read row " << row << ", column " << column |
| 354 | << " from a window with " << mNumRows << " rows, " << mNumColumns << " columns"; |
| 355 | return nullptr; |
| 356 | } else { |
| 357 | return static_cast<FieldSlot*>(result); |
| 358 | } |
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::putBlob(uint32_t row, uint32_t column, const void* value, size_t size) { |
| 362 | return putBlobOrString(row, column, value, size, FIELD_TYPE_BLOB); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 365 | status_t CursorWindow::putString(uint32_t row, uint32_t column, const char* value, |
| 366 | size_t sizeIncludingNull) { |
| 367 | return putBlobOrString(row, column, value, sizeIncludingNull, FIELD_TYPE_STRING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 370 | status_t CursorWindow::putBlobOrString(uint32_t row, uint32_t column, |
| 371 | const void* value, size_t size, int32_t type) { |
| 372 | if (mReadOnly) { |
| 373 | return INVALID_OPERATION; |
| 374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 376 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 378 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 381 | uint32_t offset; |
| 382 | if (alloc(size, &offset)) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 383 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 386 | memcpy(offsetToPtr(offset), value, size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | |
Jeff Sharkey | ae2d88a | 2020-09-26 18:57:32 -0600 | [diff] [blame] | 388 | fieldSlot = getFieldSlot(row, column); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 389 | fieldSlot->type = type; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | fieldSlot->data.buffer.offset = offset; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 391 | fieldSlot->data.buffer.size = size; |
| 392 | return OK; |
| 393 | } |
| 394 | |
| 395 | status_t CursorWindow::putLong(uint32_t row, uint32_t column, int64_t value) { |
| 396 | if (mReadOnly) { |
| 397 | return INVALID_OPERATION; |
| 398 | } |
| 399 | |
| 400 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
| 401 | if (!fieldSlot) { |
| 402 | return BAD_VALUE; |
| 403 | } |
| 404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | fieldSlot->type = FIELD_TYPE_INTEGER; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 406 | fieldSlot->data.l = value; |
| 407 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 410 | status_t CursorWindow::putDouble(uint32_t row, uint32_t column, double value) { |
| 411 | if (mReadOnly) { |
| 412 | return INVALID_OPERATION; |
| 413 | } |
| 414 | |
| 415 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 417 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | fieldSlot->type = FIELD_TYPE_FLOAT; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 421 | fieldSlot->data.d = value; |
| 422 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 425 | status_t CursorWindow::putNull(uint32_t row, uint32_t column) { |
| 426 | if (mReadOnly) { |
| 427 | return INVALID_OPERATION; |
| 428 | } |
| 429 | |
| 430 | FieldSlot* fieldSlot = getFieldSlot(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | if (!fieldSlot) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 432 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | fieldSlot->type = FIELD_TYPE_NULL; |
| 436 | fieldSlot->data.buffer.offset = 0; |
| 437 | fieldSlot->data.buffer.size = 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 438 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | }; // namespace android |