event/PollResult: use size_t for size and index

This commit is contained in:
Max Kellermann 2018-01-29 21:56:12 +01:00
parent 13ac74e0a2
commit 497e0669ff
5 changed files with 21 additions and 19 deletions

View File

@ -188,7 +188,7 @@ EventLoop::Run()
} }
/* invoke sockets */ /* 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); auto events = poll_result.GetEvents(i);
if (events != 0) { if (events != 0) {
if (quit) if (quit)

View File

@ -33,17 +33,17 @@ class PollResultEPoll
friend class PollGroupEPoll; friend class PollGroupEPoll;
std::array<epoll_event, 16> events; std::array<epoll_event, 16> events;
int n_events; size_t n_events;
public: public:
PollResultEPoll() : n_events(0) { } PollResultEPoll() : n_events(0) { }
int GetSize() const { return n_events; } size_t GetSize() const { return n_events; }
unsigned GetEvents(int i) const { return events[i].events; } unsigned GetEvents(size_t i) const { return events[i].events; }
void *GetObject(int i) const { return events[i].data.ptr; } void *GetObject(size_t i) const { return events[i].data.ptr; }
void Reset() { n_events = 0; } void Reset() { n_events = 0; }
void Clear(void *obj) { 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) if (events[i].data.ptr == obj)
events[i].events = 0; events[i].events = 0;
} }

View File

@ -51,7 +51,7 @@ void PollGroupWinSelect::Modify(PollGroupWinSelect::Item &item, int fd,
if (index < 0 && HasEvent(events, event_id)) if (index < 0 && HasEvent(events, event_id))
item.index[event_id] = set.Add(fd); item.index[event_id] = set.Add(fd);
else if (index >= 0 && !HasEvent(events, event_id)) { else if (index >= 0 && !HasEvent(events, event_id)) {
if (index != set.Size() - 1) { if (size_t(index) != set.Size() - 1) {
set.MoveToEnd(index); set.MoveToEnd(index);
items[set[index]].index[event_id] = 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) if (ret == 0 || ret == SOCKET_ERROR)
return; 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; 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; 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; items[except_set[i]].events |= WRITE;
for (auto i = items.begin(); i != items.end(); ++i) for (auto i = items.begin(); i != items.end(); ++i)

View File

@ -51,23 +51,23 @@ public:
} }
fd_set *GetPtr() { return &set; } 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 IsEmpty() { return set.fd_count == 0; }
bool IsFull() { return set.fd_count == FD_SETSIZE; } bool IsFull() { return set.fd_count == FD_SETSIZE; }
int operator[](int index) { int operator[](size_t index) const noexcept {
assert(index >= 0 && (u_int)index < set.fd_count); assert(index < set.fd_count);
return set.fd_array[index]; return set.fd_array[index];
} }
int Add(int fd) { size_t Add(int fd) {
assert(!IsFull()); assert(!IsFull());
set.fd_array[set.fd_count] = fd; set.fd_array[set.fd_count] = fd;
return set.fd_count++; return set.fd_count++;
} }
void MoveToEnd(int index) { void MoveToEnd(size_t index) {
assert(index >= 0 && (u_int)index < set.fd_count); assert(index < set.fd_count);
std::swap(set.fd_array[index], set.fd_array[set.fd_count - 1]); std::swap(set.fd_array[index], set.fd_array[set.fd_count - 1]);
} }

View File

@ -24,6 +24,8 @@
#include <vector> #include <vector>
#include <stddef.h>
class PollResultGeneric class PollResultGeneric
{ {
struct Item struct Item
@ -38,9 +40,9 @@ class PollResultGeneric
std::vector<Item> items; std::vector<Item> items;
public: public:
int GetSize() const { return items.size(); } size_t GetSize() const { return items.size(); }
unsigned GetEvents(int i) const { return items[i].events; } unsigned GetEvents(size_t i) const { return items[i].events; }
void *GetObject(int i) const { return items[i].obj; } void *GetObject(size_t i) const { return items[i].obj; }
void Reset() { items.clear(); } void Reset() { items.clear(); }
void Clear(void *obj) { void Clear(void *obj) {