summaryrefslogtreecommitdiff
path: root/libshims/utils
diff options
context:
space:
mode:
Diffstat (limited to 'libshims/utils')
-rw-r--r--libshims/utils/Looper.cpp83
-rw-r--r--libshims/utils/VectorImpl.cpp79
2 files changed, 69 insertions, 93 deletions
diff --git a/libshims/utils/Looper.cpp b/libshims/utils/Looper.cpp
index 6c57b2e..2d696eb 100644
--- a/libshims/utils/Looper.cpp
+++ b/libshims/utils/Looper.cpp
@@ -29,7 +29,7 @@ WeakMessageHandler::~WeakMessageHandler() {
void WeakMessageHandler::handleMessage(const Message& message) {
sp<MessageHandler> handler = mHandler.promote();
- if (handler != NULL) {
+ if (handler != nullptr) {
handler->handleMessage(message);
}
}
@@ -51,43 +51,38 @@ int SimpleLooperCallback::handleEvent(int fd, int events, void* data) {
// --- Looper ---
-// Hint for number of file descriptors to be associated with the epoll instance.
-static const int EPOLL_SIZE_HINT = 8;
-
// Maximum number of file descriptors for which to retrieve poll events each iteration.
static const int EPOLL_MAX_EVENTS = 16;
static pthread_once_t gTLSOnce = PTHREAD_ONCE_INIT;
static pthread_key_t gTLSKey = 0;
-Looper::Looper(bool allowNonCallbacks) :
- mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
- mPolling(false), mEpollFd(-1), mEpollRebuildRequired(false),
- mNextRequestSeq(0), mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
- mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
- LOG_ALWAYS_FATAL_IF(mWakeEventFd < 0, "Could not make wake event fd: %s",
- strerror(errno));
+Looper::Looper(bool allowNonCallbacks)
+ : mAllowNonCallbacks(allowNonCallbacks),
+ mSendingMessage(false),
+ mPolling(false),
+ mEpollRebuildRequired(false),
+ mNextRequestSeq(0),
+ mResponseIndex(0),
+ mNextMessageUptime(LLONG_MAX) {
+ mWakeEventFd.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
+ LOG_ALWAYS_FATAL_IF(mWakeEventFd.get() < 0, "Could not make wake event fd: %s", strerror(errno));
AutoMutex _l(mLock);
rebuildEpollLocked();
}
Looper::~Looper() {
- close(mWakeEventFd);
- mWakeEventFd = -1;
- if (mEpollFd >= 0) {
- close(mEpollFd);
- }
}
void Looper::initTLSKey() {
- int result = pthread_key_create(& gTLSKey, threadDestructor);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not allocate TLS key.");
+ int error = pthread_key_create(&gTLSKey, threadDestructor);
+ LOG_ALWAYS_FATAL_IF(error != 0, "Could not allocate TLS key: %s", strerror(error));
}
void Looper::threadDestructor(void *st) {
Looper* const self = static_cast<Looper*>(st);
- if (self != NULL) {
+ if (self != nullptr) {
self->decStrong((void*)threadDestructor);
}
}
@@ -95,13 +90,13 @@ void Looper::threadDestructor(void *st) {
void Looper::setForThread(const sp<Looper>& looper) {
sp<Looper> old = getForThread(); // also has side-effect of initializing TLS
- if (looper != NULL) {
+ if (looper != nullptr) {
looper->incStrong((void*)threadDestructor);
}
pthread_setspecific(gTLSKey, looper.get());
- if (old != NULL) {
+ if (old != nullptr) {
old->decStrong((void*)threadDestructor);
}
}
@@ -116,7 +111,7 @@ sp<Looper> Looper::getForThread() {
sp<Looper> Looper::prepare(int opts) {
bool allowNonCallbacks = opts & PREPARE_ALLOW_NON_CALLBACKS;
sp<Looper> looper = Looper::getForThread();
- if (looper == NULL) {
+ if (looper == nullptr) {
looper = new Looper(allowNonCallbacks);
Looper::setForThread(looper);
}
@@ -137,18 +132,18 @@ void Looper::rebuildEpollLocked() {
#if DEBUG_CALLBACKS
ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this);
#endif
- close(mEpollFd);
+ mEpollFd.reset();
}
// Allocate the new epoll instance and register the wake pipe.
- mEpollFd = epoll_create(EPOLL_SIZE_HINT);
+ mEpollFd.reset(epoll_create1(EPOLL_CLOEXEC));
LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
struct epoll_event eventItem;
memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
eventItem.events = EPOLLIN;
- eventItem.data.fd = mWakeEventFd;
- int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
+ eventItem.data.fd = mWakeEventFd.get();
+ int result = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mWakeEventFd.get(), &eventItem);
LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance: %s",
strerror(errno));
@@ -157,7 +152,7 @@ void Looper::rebuildEpollLocked() {
struct epoll_event eventItem;
request.initEventItem(&eventItem);
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, request.fd, & eventItem);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, request.fd, &eventItem);
if (epollResult < 0) {
ALOGE("Error adding epoll events for fd %d while rebuilding epoll set: %s",
request.fd, strerror(errno));
@@ -190,9 +185,9 @@ int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outDa
"fd=%d, events=0x%x, data=%p",
this, ident, fd, events, data);
#endif
- if (outFd != NULL) *outFd = fd;
- if (outEvents != NULL) *outEvents = events;
- if (outData != NULL) *outData = data;
+ if (outFd != nullptr) *outFd = fd;
+ if (outEvents != nullptr) *outEvents = events;
+ if (outData != nullptr) *outData = data;
return ident;
}
}
@@ -201,9 +196,9 @@ int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outDa
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - returning result %d", this, result);
#endif
- if (outFd != NULL) *outFd = 0;
- if (outEvents != NULL) *outEvents = 0;
- if (outData != NULL) *outData = NULL;
+ if (outFd != nullptr) *outFd = 0;
+ if (outEvents != nullptr) *outEvents = 0;
+ if (outData != nullptr) *outData = nullptr;
return result;
}
@@ -239,7 +234,7 @@ int Looper::pollInner(int timeoutMillis) {
mPolling = true;
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
- int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
+ int eventCount = epoll_wait(mEpollFd.get(), eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
// No longer idling.
mPolling = false;
@@ -281,7 +276,7 @@ int Looper::pollInner(int timeoutMillis) {
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;
uint32_t epollEvents = eventItems[i].events;
- if (fd == mWakeEventFd) {
+ if (fd == mWakeEventFd.get()) {
if (epollEvents & EPOLLIN) {
awoken();
} else {
@@ -401,11 +396,11 @@ void Looper::wake() {
#endif
uint64_t inc = 1;
- ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd, &inc, sizeof(uint64_t)));
+ ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd.get(), &inc, sizeof(uint64_t)));
if (nWrite != sizeof(uint64_t)) {
if (errno != EAGAIN) {
- LOG_ALWAYS_FATAL("Could not write wake signal to fd %d: %s",
- mWakeEventFd, strerror(errno));
+ LOG_ALWAYS_FATAL("Could not write wake signal to fd %d (returned %zd): %s",
+ mWakeEventFd.get(), nWrite, strerror(errno));
}
}
}
@@ -416,7 +411,7 @@ void Looper::awoken() {
#endif
uint64_t counter;
- TEMP_FAILURE_RETRY(read(mWakeEventFd, &counter, sizeof(uint64_t)));
+ TEMP_FAILURE_RETRY(read(mWakeEventFd.get(), &counter, sizeof(uint64_t)));
}
void Looper::pushResponse(int events, const Request& request) {
@@ -427,7 +422,7 @@ void Looper::pushResponse(int events, const Request& request) {
}
int Looper::addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data) {
- return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : NULL, data);
+ return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : nullptr, data);
}
int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, void* data) {
@@ -467,14 +462,14 @@ int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callb
ssize_t requestIndex = mRequests.indexOfKey(fd);
if (requestIndex < 0) {
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, fd, &eventItem);
if (epollResult < 0) {
ALOGE("Error adding epoll events for fd %d: %s", fd, strerror(errno));
return -1;
}
mRequests.add(fd, request);
} else {
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_MOD, fd, & eventItem);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_MOD, fd, &eventItem);
if (epollResult < 0) {
if (errno == ENOENT) {
// Tolerate ENOENT because it means that an older file descriptor was
@@ -495,7 +490,7 @@ int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callb
"being recycled, falling back on EPOLL_CTL_ADD: %s",
this, strerror(errno));
#endif
- epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
+ epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, fd, &eventItem);
if (epollResult < 0) {
ALOGE("Error modifying or adding epoll events for fd %d: %s",
fd, strerror(errno));
@@ -542,7 +537,7 @@ int Looper::removeFd(int fd, int seq) {
// updating the epoll set so that we avoid accidentally leaking callbacks.
mRequests.removeItemsAt(requestIndex);
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_DEL, fd, nullptr);
if (epollResult < 0) {
if (seq != -1 && (errno == EBADF || errno == ENOENT)) {
// Tolerate EBADF or ENOENT when the sequence number is known because it
diff --git a/libshims/utils/VectorImpl.cpp b/libshims/utils/VectorImpl.cpp
index 14b331b..c97a19b 100644
--- a/libshims/utils/VectorImpl.cpp
+++ b/libshims/utils/VectorImpl.cpp
@@ -24,8 +24,6 @@
#include <log/log.h>
-#include <safe_iop.h>
-
#include "SharedBuffer.h"
/*****************************************************************************/
@@ -44,7 +42,7 @@ static inline size_t max(size_t a, size_t b) {
// ----------------------------------------------------------------------------
VectorImpl::VectorImpl(size_t itemSize, uint32_t flags)
- : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize)
+ : mStorage(nullptr), mCount(0), mFlags(flags), mItemSize(itemSize)
{
}
@@ -77,7 +75,7 @@ VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
mCount = rhs.mCount;
SharedBuffer::bufferFromData(mStorage)->acquire();
} else {
- mStorage = 0;
+ mStorage = nullptr;
mCount = 0;
}
}
@@ -89,14 +87,14 @@ void* VectorImpl::editArrayImpl()
if (mStorage) {
const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
SharedBuffer* editable = sb->attemptEdit();
- if (editable == 0) {
+ if (editable == nullptr) {
// If we're here, we're not the only owner of the buffer.
// We must make a copy of it.
editable = SharedBuffer::alloc(sb->size());
// Fail instead of returning a pointer to storage that's not
// editable. Otherwise we'd be editing the contents of a buffer
// for which we're not the only owner, which is undefined behaviour.
- LOG_ALWAYS_FATAL_IF(editable == NULL);
+ LOG_ALWAYS_FATAL_IF(editable == nullptr);
_do_copy(editable->data(), mStorage, mCount);
release_storage();
mStorage = editable->data();
@@ -141,7 +139,7 @@ ssize_t VectorImpl::appendArray(const void* array, size_t length)
ssize_t VectorImpl::insertAt(size_t index, size_t numItems)
{
- return insertAt(0, index, numItems);
+ return insertAt(nullptr, index, numItems);
}
ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems)
@@ -177,7 +175,7 @@ status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
const ssize_t count = size();
if (count > 1) {
void* array = const_cast<void*>(arrayImpl());
- void* temp = 0;
+ void* temp = nullptr;
ssize_t i = 1;
while (i < count) {
void* item = reinterpret_cast<char*>(array) + mItemSize*(i);
@@ -205,7 +203,7 @@ status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
_do_copy(next, curr, 1);
next = curr;
--j;
- curr = NULL;
+ curr = nullptr;
if (j >= 0) {
curr = reinterpret_cast<char*>(array) + mItemSize*(j);
}
@@ -222,7 +220,7 @@ status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
free(temp);
}
}
- return NO_ERROR;
+ return OK;
}
void VectorImpl::pop()
@@ -233,7 +231,7 @@ void VectorImpl::pop()
void VectorImpl::push()
{
- push(0);
+ push(nullptr);
}
void VectorImpl::push(const void* item)
@@ -243,7 +241,7 @@ void VectorImpl::push(const void* item)
ssize_t VectorImpl::add()
{
- return add(0);
+ return add(nullptr);
}
ssize_t VectorImpl::add(const void* item)
@@ -253,7 +251,7 @@ ssize_t VectorImpl::add(const void* item)
ssize_t VectorImpl::replaceAt(size_t index)
{
- return replaceAt(0, index);
+ return replaceAt(nullptr, index);
}
ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
@@ -267,10 +265,10 @@ ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
void* item = editItemLocation(index);
if (item != prototype) {
- if (item == 0)
+ if (item == nullptr)
return NO_MEMORY;
_do_destroy(item, 1);
- if (prototype == 0) {
+ if (prototype == nullptr) {
_do_construct(item, 1);
} else {
_do_copy(item, prototype, 1);
@@ -294,7 +292,7 @@ ssize_t VectorImpl::removeItemsAt(size_t index, size_t count)
void VectorImpl::finish_vector()
{
release_storage();
- mStorage = 0;
+ mStorage = nullptr;
mCount = 0;
}
@@ -315,7 +313,7 @@ void* VectorImpl::editItemLocation(size_t index)
return reinterpret_cast<char*>(buffer) + index*mItemSize;
}
}
- return 0;
+ return nullptr;
}
const void* VectorImpl::itemLocation(size_t index) const
@@ -330,7 +328,7 @@ const void* VectorImpl::itemLocation(size_t index) const
return reinterpret_cast<const char*>(buffer) + index*mItemSize;
}
}
- return 0;
+ return nullptr;
}
ssize_t VectorImpl::setCapacity(size_t new_capacity)
@@ -342,7 +340,7 @@ ssize_t VectorImpl::setCapacity(size_t new_capacity)
}
size_t new_allocation_size = 0;
- LOG_ALWAYS_FATAL_IF(!safe_mul(&new_allocation_size, new_capacity, mItemSize));
+ LOG_ALWAYS_FATAL_IF(__builtin_mul_overflow(new_capacity, mItemSize, &new_allocation_size));
SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size);
if (sb) {
void* array = sb->data();
@@ -356,7 +354,7 @@ ssize_t VectorImpl::setCapacity(size_t new_capacity)
}
ssize_t VectorImpl::resize(size_t size) {
- ssize_t result = NO_ERROR;
+ ssize_t result = OK;
if (size > mCount) {
result = insertAt(mCount, size - mCount);
} else if (size < mCount) {
@@ -386,7 +384,7 @@ void* VectorImpl::_grow(size_t where, size_t amount)
this, (int)where, (int)amount, (int)mCount); // caller already checked
size_t new_size;
- LOG_ALWAYS_FATAL_IF(!safe_add(&new_size, mCount, amount), "new_size overflow");
+ LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(mCount, amount, &new_size), "new_size overflow");
if (capacity() < new_size) {
// NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2)
@@ -397,17 +395,18 @@ void* VectorImpl::_grow(size_t where, size_t amount)
//
// This approximates the old calculation, using (x + (x/2) + 1) instead.
size_t new_capacity = 0;
- LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_size, (new_size / 2)),
- "new_capacity overflow");
- LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_capacity, static_cast<size_t>(1u)),
+ LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(new_size, (new_size / 2), &new_capacity),
"new_capacity overflow");
+ LOG_ALWAYS_FATAL_IF(
+ __builtin_add_overflow(new_capacity, static_cast<size_t>(1u), &new_capacity),
+ "new_capacity overflow");
new_capacity = max(kMinVectorCapacity, new_capacity);
size_t new_alloc_size = 0;
- LOG_ALWAYS_FATAL_IF(!safe_mul(&new_alloc_size, new_capacity, mItemSize),
+ LOG_ALWAYS_FATAL_IF(__builtin_mul_overflow(new_capacity, mItemSize, &new_alloc_size),
"new_alloc_size overflow");
-// ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
+ // ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
if ((mStorage) &&
(mCount==where) &&
(mFlags & HAS_TRIVIAL_COPY) &&
@@ -418,7 +417,7 @@ void* VectorImpl::_grow(size_t where, size_t amount)
if (sb) {
mStorage = sb->data();
} else {
- return NULL;
+ return nullptr;
}
} else {
SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
@@ -435,7 +434,7 @@ void* VectorImpl::_grow(size_t where, size_t amount)
release_storage();
mStorage = const_cast<void*>(array);
} else {
- return NULL;
+ return nullptr;
}
}
} else {
@@ -464,7 +463,7 @@ void VectorImpl::_shrink(size_t where, size_t amount)
this, (int)where, (int)amount, (int)mCount); // caller already checked
size_t new_size;
- LOG_ALWAYS_FATAL_IF(!safe_sub(&new_size, mCount, amount));
+ LOG_ALWAYS_FATAL_IF(__builtin_sub_overflow(mCount, amount, &new_size));
if (new_size < (capacity() / 2)) {
// NOTE: (new_size * 2) is safe because capacity didn't overflow and
@@ -556,15 +555,6 @@ void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) con
do_move_backward(dest, from, num);
}
-void VectorImpl::reservedVectorImpl1() { }
-void VectorImpl::reservedVectorImpl2() { }
-void VectorImpl::reservedVectorImpl3() { }
-void VectorImpl::reservedVectorImpl4() { }
-void VectorImpl::reservedVectorImpl5() { }
-void VectorImpl::reservedVectorImpl6() { }
-void VectorImpl::reservedVectorImpl7() { }
-void VectorImpl::reservedVectorImpl8() { }
-
/*****************************************************************************/
SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
@@ -654,13 +644,13 @@ ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
}
}
}
- return NO_ERROR;
+ return OK;
}
ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
{
// we've merging a sorted vector... nice!
- ssize_t err = NO_ERROR;
+ ssize_t err = OK;
if (!vector.isEmpty()) {
// first take care of the case where the vectors are sorted together
if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
@@ -684,15 +674,6 @@ ssize_t SortedVectorImpl::remove(const void* item)
return i;
}
-void SortedVectorImpl::reservedSortedVectorImpl1() { };
-void SortedVectorImpl::reservedSortedVectorImpl2() { };
-void SortedVectorImpl::reservedSortedVectorImpl3() { };
-void SortedVectorImpl::reservedSortedVectorImpl4() { };
-void SortedVectorImpl::reservedSortedVectorImpl5() { };
-void SortedVectorImpl::reservedSortedVectorImpl6() { };
-void SortedVectorImpl::reservedSortedVectorImpl7() { };
-void SortedVectorImpl::reservedSortedVectorImpl8() { };
-
/*****************************************************************************/
}; // namespace android