From 497e0669ff90180060fc49dd170fcb5645c429fa Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 29 Jan 2018 21:56:12 +0100 Subject: [PATCH] event/PollResult: use size_t for size and index --- src/event/Loop.cxx | 2 +- src/event/PollGroupEPoll.hxx | 10 +++++----- src/event/PollGroupWinSelect.cxx | 8 ++++---- src/event/PollGroupWinSelect.hxx | 12 ++++++------ src/event/PollResultGeneric.hxx | 8 +++++--- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index af488b899..31d3e85fa 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -188,7 +188,7 @@ EventLoop::Run() } /* invoke sockets */ - for (int i = 0; i < poll_result.GetSize(); ++i) { + for (size_t i = 0; i < poll_result.GetSize(); ++i) { auto events = poll_result.GetEvents(i); if (events != 0) { if (quit) diff --git a/src/event/PollGroupEPoll.hxx b/src/event/PollGroupEPoll.hxx index 12db47752..7b3e0666d 100644 --- a/src/event/PollGroupEPoll.hxx +++ b/src/event/PollGroupEPoll.hxx @@ -33,17 +33,17 @@ class PollResultEPoll friend class PollGroupEPoll; std::array events; - int n_events; + size_t n_events; public: PollResultEPoll() : n_events(0) { } - int GetSize() const { return n_events; } - unsigned GetEvents(int i) const { return events[i].events; } - void *GetObject(int i) const { return events[i].data.ptr; } + size_t GetSize() const { return n_events; } + unsigned GetEvents(size_t i) const { return events[i].events; } + void *GetObject(size_t i) const { return events[i].data.ptr; } void Reset() { n_events = 0; } void Clear(void *obj) { - for (int i = 0; i < n_events; ++i) + for (size_t i = 0; i < n_events; ++i) if (events[i].data.ptr == obj) events[i].events = 0; } diff --git a/src/event/PollGroupWinSelect.cxx b/src/event/PollGroupWinSelect.cxx index fad8667b1..84750c498 100644 --- a/src/event/PollGroupWinSelect.cxx +++ b/src/event/PollGroupWinSelect.cxx @@ -51,7 +51,7 @@ void PollGroupWinSelect::Modify(PollGroupWinSelect::Item &item, int fd, if (index < 0 && HasEvent(events, event_id)) item.index[event_id] = set.Add(fd); else if (index >= 0 && !HasEvent(events, event_id)) { - if (index != set.Size() - 1) { + if (size_t(index) != set.Size() - 1) { set.MoveToEnd(index); items[set[index]].index[event_id] = index; } @@ -142,13 +142,13 @@ void PollGroupWinSelect::ReadEvents(PollResultGeneric &result, int timeout_ms) if (ret == 0 || ret == SOCKET_ERROR) return; - for (int i = 0; i < read_set.Size(); ++i) + for (size_t i = 0; i < read_set.Size(); ++i) items[read_set[i]].events |= READ; - for (int i = 0; i < write_set.Size(); ++i) + for (size_t i = 0; i < write_set.Size(); ++i) items[write_set[i]].events |= WRITE; - for (int i = 0; i < except_set.Size(); ++i) + for (size_t i = 0; i < except_set.Size(); ++i) items[except_set[i]].events |= WRITE; for (auto i = items.begin(); i != items.end(); ++i) diff --git a/src/event/PollGroupWinSelect.hxx b/src/event/PollGroupWinSelect.hxx index f60b52ce3..472851c80 100644 --- a/src/event/PollGroupWinSelect.hxx +++ b/src/event/PollGroupWinSelect.hxx @@ -51,23 +51,23 @@ public: } fd_set *GetPtr() { return &set; } - int Size() { return set.fd_count; } + size_t Size() { return set.fd_count; } bool IsEmpty() { return set.fd_count == 0; } bool IsFull() { return set.fd_count == FD_SETSIZE; } - int operator[](int index) { - assert(index >= 0 && (u_int)index < set.fd_count); + int operator[](size_t index) const noexcept { + assert(index < set.fd_count); return set.fd_array[index]; } - int Add(int fd) { + size_t Add(int fd) { assert(!IsFull()); set.fd_array[set.fd_count] = fd; return set.fd_count++; } - void MoveToEnd(int index) { - assert(index >= 0 && (u_int)index < set.fd_count); + void MoveToEnd(size_t index) { + assert(index < set.fd_count); std::swap(set.fd_array[index], set.fd_array[set.fd_count - 1]); } diff --git a/src/event/PollResultGeneric.hxx b/src/event/PollResultGeneric.hxx index 556379839..fdc3e940c 100644 --- a/src/event/PollResultGeneric.hxx +++ b/src/event/PollResultGeneric.hxx @@ -24,6 +24,8 @@ #include +#include + class PollResultGeneric { struct Item @@ -38,9 +40,9 @@ class PollResultGeneric std::vector items; public: - int GetSize() const { return items.size(); } - unsigned GetEvents(int i) const { return items[i].events; } - void *GetObject(int i) const { return items[i].obj; } + size_t GetSize() const { return items.size(); } + unsigned GetEvents(size_t i) const { return items[i].events; } + void *GetObject(size_t i) const { return items[i].obj; } void Reset() { items.clear(); } void Clear(void *obj) {