summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Kalesh Singh <kaleshsingh@google.com> 2023-08-10 13:46:09 -0700
committer Kalesh Singh <kaleshsingh@google.com> 2023-08-11 01:29:55 -0700
commitc1d4f195bc63c7c591a540948f79d6156ab1c792 (patch)
treec60ec3aa2f7bdf9c94d7048f00e34d6eeac403dc
parentf5a3b8a848ef1c3f72d2fa960437ca9b66ead654 (diff)
protoutil: EncodedBuffer: Remove usage of PAGE_SIZE 4096
bionic hard codes the PAGE_SIZE macro as 4096. This is going away as Android begins to support larger page sizes. Remove the usage of this assumption from protoutil source; use instead getpagesize() which provides the real pagesize. Test: atest -c libprotoutil_test Bug: 295228590 Change-Id: I6c3212600256d61a71430dfbbf7b0c9944fe4859 Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
-rw-r--r--libs/protoutil/src/EncodedBuffer.cpp6
-rw-r--r--libs/protoutil/tests/EncodedBuffer_test.cpp9
2 files changed, 10 insertions, 5 deletions
diff --git a/libs/protoutil/src/EncodedBuffer.cpp b/libs/protoutil/src/EncodedBuffer.cpp
index 96b54c63a836..afb54a6c49b6 100644
--- a/libs/protoutil/src/EncodedBuffer.cpp
+++ b/libs/protoutil/src/EncodedBuffer.cpp
@@ -17,6 +17,7 @@
#include <stdlib.h>
#include <sys/mman.h>
+#include <unistd.h>
#include <android/util/EncodedBuffer.h>
#include <android/util/protobuf.h>
@@ -25,7 +26,8 @@
namespace android {
namespace util {
-const size_t BUFFER_SIZE = 8 * 1024; // 8 KB
+constexpr size_t BUFFER_SIZE = 8 * 1024; // 8 KB
+const size_t kPageSize = getpagesize();
EncodedBuffer::Pointer::Pointer() : Pointer(BUFFER_SIZE)
{
@@ -92,7 +94,7 @@ EncodedBuffer::EncodedBuffer(size_t chunkSize)
{
// Align chunkSize to memory page size
chunkSize = chunkSize == 0 ? BUFFER_SIZE : chunkSize;
- mChunkSize = (chunkSize / PAGE_SIZE + ((chunkSize % PAGE_SIZE == 0) ? 0 : 1)) * PAGE_SIZE;
+ mChunkSize = (chunkSize + (kPageSize - 1)) & ~(kPageSize - 1);
mWp = Pointer(mChunkSize);
mEp = Pointer(mChunkSize);
}
diff --git a/libs/protoutil/tests/EncodedBuffer_test.cpp b/libs/protoutil/tests/EncodedBuffer_test.cpp
index 8a7becf68d69..a09558544c26 100644
--- a/libs/protoutil/tests/EncodedBuffer_test.cpp
+++ b/libs/protoutil/tests/EncodedBuffer_test.cpp
@@ -15,13 +15,16 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <unistd.h>
+
using namespace android::util;
using android::sp;
constexpr size_t __TEST_CHUNK_SIZE = 16UL;
-constexpr size_t TEST_CHUNK_SIZE = (__TEST_CHUNK_SIZE + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
-constexpr size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2;
-constexpr size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE;
+const size_t kPageSize = getpagesize();
+const size_t TEST_CHUNK_SIZE = (__TEST_CHUNK_SIZE + (kPageSize - 1)) & ~(kPageSize - 1);
+const size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2;
+const size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE;
static void expectPointer(EncodedBuffer::Pointer* p, size_t pos) {
EXPECT_EQ(p->pos(), pos);