summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Michael Hoisie <hoisie@google.com> 2024-03-12 18:15:14 +0000
committer Michael Hoisie <hoisie@google.com> 2024-09-28 05:49:44 +0000
commite28dd9f756fd7df72e39260e82a7569a3636d0af (patch)
tree33e1a587994d105c1ba9426844144143f95699b5
parent35bb4587b0f6435dfcdb4b7937506239bbd2c4f6 (diff)
Use MappedFile for mmap-related operations in CursorWindow
MappedFile from libbase can encapsulate all mmap and munmap operations. The advantage to MappedFile is that it is supported on all host platforms, including Windows. This change is a no-op in terms of functionality. Test: Roboelctric SQLiteDatabaseTest Bug: 317884162 Change-Id: I77b6b5481b64a930ecd2bd5ddf3692e54be60751
-rw-r--r--libs/androidfw/CursorWindow.cpp21
-rw-r--r--libs/androidfw/include/androidfw/CursorWindow.h4
2 files changed, 17 insertions, 8 deletions
diff --git a/libs/androidfw/CursorWindow.cpp b/libs/androidfw/CursorWindow.cpp
index 5e645cceea2d..cbb1e8f82838 100644
--- a/libs/androidfw/CursorWindow.cpp
+++ b/libs/androidfw/CursorWindow.cpp
@@ -18,11 +18,12 @@
#include <androidfw/CursorWindow.h>
-#include <sys/mman.h>
-
#include "android-base/logging.h"
+#include "android-base/mapped_file.h"
#include "cutils/ashmem.h"
+using android::base::MappedFile;
+
namespace android {
/**
@@ -39,7 +40,7 @@ CursorWindow::CursorWindow() {
CursorWindow::~CursorWindow() {
if (mAshmemFd != -1) {
- ::munmap(mData, mSize);
+ mMappedFile.reset();
::close(mAshmemFd);
} else {
free(mData);
@@ -75,6 +76,7 @@ fail_silent:
status_t CursorWindow::maybeInflate() {
int ashmemFd = 0;
void* newData = nullptr;
+ std::unique_ptr<MappedFile> mappedFile;
// Bail early when we can't expand any further
if (mReadOnly || mSize == mInflatedSize) {
@@ -95,11 +97,12 @@ status_t CursorWindow::maybeInflate() {
goto fail_silent;
}
- newData = ::mmap(nullptr, mInflatedSize, PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0);
- if (newData == MAP_FAILED) {
+ mappedFile = MappedFile::FromFd(ashmemFd, 0, mInflatedSize, PROT_READ | PROT_WRITE);
+ if (mappedFile == nullptr) {
PLOG(ERROR) << "Failed mmap";
goto fail_silent;
}
+ newData = mappedFile->data();
if (ashmem_set_prot_region(ashmemFd, PROT_READ) < 0) {
PLOG(ERROR) << "Failed ashmem_set_prot_region";
@@ -120,6 +123,7 @@ status_t CursorWindow::maybeInflate() {
mData = newData;
mSize = mInflatedSize;
mSlotsOffset = newSlotsOffset;
+ mMappedFile = std::move(mappedFile);
updateSlotsData();
}
@@ -130,7 +134,7 @@ status_t CursorWindow::maybeInflate() {
fail:
LOG(ERROR) << "Failed maybeInflate";
fail_silent:
- ::munmap(newData, mInflatedSize);
+ mappedFile.reset();
::close(ashmemFd);
return UNKNOWN_ERROR;
}
@@ -167,11 +171,12 @@ status_t CursorWindow::createFromParcel(Parcel* parcel, CursorWindow** outWindow
goto fail_silent;
}
- window->mData = ::mmap(nullptr, window->mSize, PROT_READ, MAP_SHARED, window->mAshmemFd, 0);
- if (window->mData == MAP_FAILED) {
+ window->mMappedFile = MappedFile::FromFd(window->mAshmemFd, 0, window->mSize, PROT_READ);
+ if (window->mMappedFile == nullptr) {
PLOG(ERROR) << "Failed mmap";
goto fail_silent;
}
+ window->mData = window->mMappedFile->data();
} else {
window->mAshmemFd = -1;
diff --git a/libs/androidfw/include/androidfw/CursorWindow.h b/libs/androidfw/include/androidfw/CursorWindow.h
index 9ec026a19c4c..c2eac12eb77d 100644
--- a/libs/androidfw/include/androidfw/CursorWindow.h
+++ b/libs/androidfw/include/androidfw/CursorWindow.h
@@ -26,6 +26,8 @@
#include "binder/Parcel.h"
#include "utils/String8.h"
+#include "android-base/mapped_file.h"
+
#define LOG_WINDOW(...)
namespace android {
@@ -149,6 +151,8 @@ private:
String8 mName;
int mAshmemFd = -1;
void* mData = nullptr;
+ std::unique_ptr<android::base::MappedFile> mMappedFile;
+
/**
* Pointer to the first FieldSlot, used to optimize the extremely
* hot code path of getFieldSlot().