style: format all sources

This commit is contained in:
Bert Belder 2017-09-01 15:11:08 +02:00
parent c7ebd8f042
commit 005581d797
6 changed files with 1149 additions and 1166 deletions

View File

@ -1,4 +1,3 @@
#ifndef EPOLL_H_
#define EPOLL_H_
@ -7,7 +6,6 @@
#include <stdint.h>
#define EPOLLIN 0x001
#define EPOLLPRI 0x002
#define EPOLLOUT 0x004
@ -23,7 +21,6 @@
/* #define EPOLLET (1 << 30) Not supported */
#define EPOLLONESHOT (1 << 31)
#define EPOLL_CTL_ADD 1
#define EPOLL_CTL_MOD 2
#define EPOLL_CTL_DEL 3
@ -31,8 +28,8 @@
typedef void* epoll_t;
typedef union epoll_data {
void *ptr;
int fd;
void* ptr;
int fd;
uint32_t u32;
uint64_t u64;
/* Windows-specific extensions. */
@ -41,18 +38,22 @@ typedef union epoll_data {
} epoll_data_t;
struct epoll_event {
uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
};
epoll_t epoll_create();
int epoll_close(epoll_t epoll_hnd);
int epoll_ctl(epoll_t epoll_hnd, int op, SOCKET sock, struct epoll_event* event);
int epoll_ctl(epoll_t epoll_hnd,
int op,
SOCKET sock,
struct epoll_event* event);
int epoll_wait(epoll_t epoll_hnd, struct epoll_event* events, int maxevents, int timeout);
int epoll_wait(epoll_t epoll_hnd,
struct epoll_event* events,
int maxevents,
int timeout);
#endif /* EPOLL_H_ */
#endif /* EPOLL_H_ */

View File

@ -1,4 +1,3 @@
#include <assert.h>
#include <epoll.h>
#include <stdint.h>
@ -7,38 +6,34 @@
#include "msafd.h"
#include "tree.h"
#define ARRAY_COUNT(a) (sizeof(a) / (sizeof((a)[0])))
#define EPOLL__EVENT_MASK 0xffff
#define EPOLL__SOCK_LISTED 0x1
#define EPOLL__SOCK_DELETED 0x2
#define EPOLL__EVENT_MASK 0xffff
#define EPOLL__SOCK_LISTED 0x1
#define EPOLL__SOCK_DELETED 0x2
typedef struct epoll_port_data_s epoll_port_data_t;
typedef struct epoll_op_s epoll_op_t;
typedef struct epoll_sock_data_s epoll_sock_data_t;
static int epoll__initialize();
static SOCKET epoll__get_peer_socket(epoll_port_data_t* port_data,
WSAPROTOCOL_INFOW* protocol_info);
WSAPROTOCOL_INFOW* protocol_info);
static SOCKET epoll__create_peer_socket(HANDLE iocp,
WSAPROTOCOL_INFOW* protocol_info);
WSAPROTOCOL_INFOW* protocol_info);
static int epoll__compare_sock_data(epoll_sock_data_t* a,
epoll_sock_data_t* b);
epoll_sock_data_t* b);
static int epoll__submit_poll_op(epoll_port_data_t* port_data,
epoll_sock_data_t* sock_data);
static int epoll__afd_poll(SOCKET socket, AFD_POLL_INFO* info,
OVERLAPPED* overlapped);
epoll_sock_data_t* sock_data);
static int epoll__afd_poll(SOCKET socket,
AFD_POLL_INFO* info,
OVERLAPPED* overlapped);
static int epoll__ntstatus_to_winsock_error(NTSTATUS status);
static int epoll__initialized = 0;
static PNTDEVICEIOCONTROLFILE pNtDeviceIoControlFile;
/* State associated with a epoll handle. */
struct epoll_port_data_s {
HANDLE iocp;
@ -48,7 +43,6 @@ struct epoll_port_data_s {
size_t pending_ops_count;
};
/* State associated with a socket that is registered to the epoll port. */
typedef struct epoll_sock_data_s {
SOCKET sock;
@ -73,11 +67,10 @@ struct epoll_op_s {
epoll_sock_data_t* sock_data;
};
RB_GENERATE_STATIC(epoll_sock_data_tree,
epoll_sock_data_s,
tree_entry, epoll__compare_sock_data)
tree_entry,
epoll__compare_sock_data)
epoll_t epoll_create() {
epoll_port_data_t* port_data;
@ -97,10 +90,7 @@ epoll_t epoll_create() {
return NULL;
}
iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
NULL,
0,
0);
iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (iocp == INVALID_HANDLE_VALUE) {
free(port_data);
return NULL;
@ -116,9 +106,10 @@ epoll_t epoll_create() {
return (epoll_t) port_data;
}
int epoll_ctl(epoll_t port_handle, int op, SOCKET sock,
struct epoll_event* ev) {
int epoll_ctl(epoll_t port_handle,
int op,
SOCKET sock,
struct epoll_event* ev) {
epoll_port_data_t* port_data;
SOCKET base_sock;
@ -213,9 +204,8 @@ int epoll_ctl(epoll_t port_handle, int op, SOCKET sock,
epoll_sock_data_t* sock_data;
lookup.sock = sock;
sock_data = RB_FIND(epoll_sock_data_tree,
&port_data->sock_data_tree,
&lookup);
sock_data =
RB_FIND(epoll_sock_data_tree, &port_data->sock_data_tree, &lookup);
if (sock_data == NULL) {
/* Socket has not been registered with epoll instance. */
SetLastError(ERROR_NOT_FOUND);
@ -254,9 +244,8 @@ int epoll_ctl(epoll_t port_handle, int op, SOCKET sock,
epoll_sock_data_t* sock_data;
lookup.sock = sock;
sock_data = RB_FIND(epoll_sock_data_tree,
&port_data->sock_data_tree,
&lookup);
sock_data =
RB_FIND(epoll_sock_data_tree, &port_data->sock_data_tree, &lookup);
if (sock_data == NULL) {
/* Socket has not been registered with epoll instance. */
SetLastError(ERROR_NOT_FOUND);
@ -271,9 +260,11 @@ int epoll_ctl(epoll_t port_handle, int op, SOCKET sock,
/* Remove from attention list. */
if (sock_data->flags & EPOLL__SOCK_LISTED) {
if (sock_data->attn_list_prev != NULL)
sock_data->attn_list_prev->attn_list_next = sock_data->attn_list_next;
sock_data->attn_list_prev->attn_list_next =
sock_data->attn_list_next;
if (sock_data->attn_list_next != NULL)
sock_data->attn_list_next->attn_list_prev = sock_data->attn_list_prev;
sock_data->attn_list_next->attn_list_prev =
sock_data->attn_list_prev;
if (port_data->attn_list == sock_data)
port_data->attn_list = sock_data->attn_list_next;
sock_data->attn_list_prev = NULL;
@ -299,9 +290,10 @@ int epoll_ctl(epoll_t port_handle, int op, SOCKET sock,
}
}
int epoll_wait(epoll_t port_handle, struct epoll_event* events, int maxevents,
int timeout) {
int epoll_wait(epoll_t port_handle,
struct epoll_event* events,
int maxevents,
int timeout) {
epoll_port_data_t* port_data;
DWORD due;
DWORD gqcs_timeout;
@ -327,7 +319,8 @@ int epoll_wait(epoll_t port_handle, struct epoll_event* events, int maxevents,
OVERLAPPED_ENTRY entries[64];
int num_events = 0;
/* Create overlapped poll operations for all sockets on the attention list. */
/* Create overlapped poll operations for all sockets on the attention list.
*/
while (port_data->attn_list != NULL) {
epoll_sock_data_t* sock_data = port_data->attn_list;
assert(sock_data->flags & EPOLL__SOCK_LISTED);
@ -345,7 +338,8 @@ int epoll_wait(epoll_t port_handle, struct epoll_event* events, int maxevents,
if (WSAGetLastError() != WSAENOTSOCK)
return -1;
/* Skip to the next attention list item already, because we're about */
/* Skip to the next attention list item already, because we're about
*/
/* to delete the currently selected socket. */
port_data->attn_list = sock_data->attn_list_next;
sock_data->flags &= ~EPOLL__SOCK_LISTED;
@ -369,12 +363,8 @@ int epoll_wait(epoll_t port_handle, struct epoll_event* events, int maxevents,
if ((int) max_entries > maxevents)
max_entries = maxevents;
result = GetQueuedCompletionStatusEx(port_data->iocp,
entries,
max_entries,
&count,
gqcs_timeout,
FALSE);
result = GetQueuedCompletionStatusEx(
port_data->iocp, entries, max_entries, &count, gqcs_timeout, FALSE);
if (!result) {
DWORD error = GetLastError();
@ -498,7 +488,6 @@ int epoll_wait(epoll_t port_handle, struct epoll_event* events, int maxevents,
return 0;
}
int epoll_close(epoll_t port_handle) {
epoll_port_data_t* port_data;
epoll_sock_data_t* sock_data;
@ -527,11 +516,11 @@ int epoll_close(epoll_t port_handle) {
ULONG count, i;
result = GetQueuedCompletionStatusEx(port_data->iocp,
entries,
ARRAY_COUNT(entries),
&count,
INFINITE,
FALSE);
entries,
ARRAY_COUNT(entries),
&count,
INFINITE,
FALSE);
if (!result) {
DWORD error = GetLastError();
@ -541,9 +530,8 @@ int epoll_close(epoll_t port_handle) {
port_data->pending_ops_count -= count;
for (i = 0; i < count; i++) {
epoll_op_t* op = CONTAINING_RECORD(entries[i].lpOverlapped,
epoll_op_t,
overlapped);
epoll_op_t* op =
CONTAINING_RECORD(entries[i].lpOverlapped, epoll_op_t, overlapped);
free(op);
}
}
@ -566,7 +554,6 @@ int epoll_close(epoll_t port_handle) {
return 0;
}
int epoll__initialize() {
HMODULE ntdll;
int r;
@ -580,17 +567,16 @@ int epoll__initialize() {
if (ntdll == NULL)
return -1;
pNtDeviceIoControlFile = (PNTDEVICEIOCONTROLFILE) GetProcAddress(ntdll,
"NtDeviceIoControlFile");
pNtDeviceIoControlFile =
(PNTDEVICEIOCONTROLFILE) GetProcAddress(ntdll, "NtDeviceIoControlFile");
if (pNtDeviceIoControlFile == NULL)
return -1;
return 0;
}
SOCKET epoll__get_peer_socket(epoll_port_data_t* port_data,
WSAPROTOCOL_INFOW* protocol_info) {
WSAPROTOCOL_INFOW* protocol_info) {
int index, i;
SOCKET peer_socket;
@ -621,9 +607,8 @@ SOCKET epoll__get_peer_socket(epoll_port_data_t* port_data,
return peer_socket;
}
SOCKET epoll__create_peer_socket(HANDLE iocp,
WSAPROTOCOL_INFOW* protocol_info) {
WSAPROTOCOL_INFOW* protocol_info) {
SOCKET sock = 0;
sock = WSASocketW(protocol_info->iAddressFamily,
@ -640,29 +625,23 @@ SOCKET epoll__create_peer_socket(HANDLE iocp,
goto error;
};
if (CreateIoCompletionPort((HANDLE) sock,
iocp,
0,
0) == NULL) {
if (CreateIoCompletionPort((HANDLE) sock, iocp, 0, 0) == NULL) {
goto error;
}
return sock;
error:
error:
closesocket(sock);
return INVALID_SOCKET;
}
int epoll__compare_sock_data(epoll_sock_data_t* a,
epoll_sock_data_t* b) {
int epoll__compare_sock_data(epoll_sock_data_t* a, epoll_sock_data_t* b) {
return a->sock - b->sock;
}
int epoll__submit_poll_op(epoll_port_data_t* port_data,
epoll_sock_data_t* sock_data) {
epoll_sock_data_t* sock_data) {
epoll_op_t* op;
int registered_events;
DWORD result, afd_events;
@ -697,9 +676,8 @@ int epoll__submit_poll_op(epoll_port_data_t* port_data,
op->poll_info.Handles[0].Status = 0;
op->poll_info.Handles[0].Events = afd_events;
result = epoll__afd_poll(sock_data->peer_sock,
&op->poll_info,
&op->overlapped);
result =
epoll__afd_poll(sock_data->peer_sock, &op->poll_info, &op->overlapped);
if (result != 0) {
DWORD error = WSAGetLastError();
if (error != WSA_IO_PENDING) {
@ -717,9 +695,9 @@ int epoll__submit_poll_op(epoll_port_data_t* port_data,
return 0;
}
int epoll__afd_poll(SOCKET socket, AFD_POLL_INFO* info,
OVERLAPPED* overlapped) {
int epoll__afd_poll(SOCKET socket,
AFD_POLL_INFO* info,
OVERLAPPED* overlapped) {
IO_STATUS_BLOCK iosb;
IO_STATUS_BLOCK* iosb_ptr;
HANDLE event = NULL;
@ -804,7 +782,6 @@ int epoll__afd_poll(SOCKET socket, AFD_POLL_INFO* info,
}
}
int epoll__ntstatus_to_winsock_error(NTSTATUS status) {
switch (status) {
case STATUS_SUCCESS:
@ -913,10 +890,10 @@ int epoll__ntstatus_to_winsock_error(NTSTATUS status) {
(status & (ERROR_SEVERITY_ERROR | ERROR_SEVERITY_WARNING))) {
/* It's a windows error that has been previously mapped to an */
/* ntstatus code. */
return (DWORD) (status & 0xffff);
return (DWORD)(status & 0xffff);
} else {
/* The default fallback for unmappable ntstatus codes. */
return WSAEINVAL;
}
}
}
}

View File

@ -1,4 +1,3 @@
#ifndef EPOLL_MSAFD_H_
#define EPOLL_MSAFD_H_
@ -6,7 +5,7 @@
#include "ntapi.h"
#ifndef SIO_BASE_HANDLE
# define SIO_BASE_HANDLE 0x48000022
#define SIO_BASE_HANDLE 0x48000022
#endif
/* clang-format off */
@ -46,12 +45,11 @@
#define FSCTL_AFD_BASE FILE_DEVICE_NETWORK
#define _AFD_CONTROL_CODE(operation, method) \
((FSCTL_AFD_BASE) << 12 | (operation << 2) | method)
((FSCTL_AFD_BASE) << 12 | (operation << 2) | method)
#define AFD_POLL 9
#define AFD_POLL 9
#define IOCTL_AFD_POLL \
_AFD_CONTROL_CODE(AFD_POLL, METHOD_BUFFERED)
#define IOCTL_AFD_POLL _AFD_CONTROL_CODE(AFD_POLL, METHOD_BUFFERED)
typedef struct _AFD_POLL_HANDLE_INFO {
HANDLE Handle;
@ -67,12 +65,17 @@ typedef struct _AFD_POLL_INFO {
} AFD_POLL_INFO, *PAFD_POLL_INFO;
static const GUID AFD_PROVIDER_IDS[] = {
{0xe70f1aa0, 0xab8b, 0x11cf,
{0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}},
{0xf9eab0c0, 0x26d4, 0x11d0,
{0xbb, 0xbf, 0x00, 0xaa, 0x00, 0x6c, 0x34, 0xe4}},
{0x9fc48064, 0x7298, 0x43e4,
{0xb7, 0xbd, 0x18, 0x1f, 0x20, 0x89, 0x79, 0x2a}}
};
{0xe70f1aa0,
0xab8b,
0x11cf,
{0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}},
{0xf9eab0c0,
0x26d4,
0x11d0,
{0xbb, 0xbf, 0x00, 0xaa, 0x00, 0x6c, 0x34, 0xe4}},
{0x9fc48064,
0x7298,
0x43e4,
{0xb7, 0xbd, 0x18, 0x1f, 0x20, 0x89, 0x79, 0x2a}}};
#endif /* EPOLL_MSAFD_H_ */
#endif /* EPOLL_MSAFD_H_ */

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,10 @@
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <WinSock2.h>
#include <Windows.h>
#include <assert.h>
#include <stdio.h>
#include <epoll.h>
#include <stdio.h>
static const char PING[] = "PING";
static const int NUM_PINGERS = 10000;
@ -54,14 +53,15 @@ int main(int argc, char* argv[]) {
for (i = 0; i < NUM_PINGERS; i++) {
SOCKET sock;
struct epoll_event ev;
sock = socket(AF_INET, SOCK_STREAM, 0);
r = ioctlsocket(sock, FIONBIO, &one);
assert(r == 0);
r = connect(sock, (struct sockaddr*) &addr, sizeof addr);
/* Unlike unix, windows sets the error to EWOULDBLOCK when the connection */
/* Unlike unix, windows sets the error to EWOULDBLOCK when the connection
*/
/* is being established in the background. */
assert(r == 0 || WSAGetLastError() == WSAEWOULDBLOCK);
@ -74,19 +74,18 @@ int main(int argc, char* argv[]) {
{
SOCKET sock;
struct epoll_event ev;
sock = socket(AF_INET, SOCK_STREAM, 0);
r = ioctlsocket(sock, FIONBIO, &one);
assert(r == 0);
ev.events = EPOLLOUT | EPOLLERR;
ev.data.sock = sock;
r = epoll_ctl(epoll_hnd, EPOLL_CTL_ADD, sock, &ev);
assert(r == 0);
}
ticks_start = GetTickCount();
ticks_last = ticks_start;
@ -97,11 +96,14 @@ int main(int argc, char* argv[]) {
ticks = GetTickCount();
if (ticks >= ticks_last + 1000) {
printf("%lld pings (%f per sec), %lld sent\n", pings, (double) pings / (ticks - ticks_start) * 1000, pings_sent);
printf("%lld pings (%f per sec), %lld sent\n",
pings,
(double) pings / (ticks - ticks_start) * 1000,
pings_sent);
ticks_last = ticks;
// if (ticks - ticks_start > RUN_TIME)
// break;
// if (ticks - ticks_start > RUN_TIME)
// break;
}
count = epoll_wait(epoll_hnd, events, 15, 1000);
@ -124,7 +126,7 @@ int main(int argc, char* argv[]) {
assert(r == 0);
continue;
}
if (revents & EPOLLIN) {
char buf[1024];
WSABUF wsa_buf;
@ -134,7 +136,7 @@ int main(int argc, char* argv[]) {
wsa_buf.buf = buf;
wsa_buf.len = sizeof buf;
flags = 0;
r = WSARecv(sock, &wsa_buf, 1, &bytes, &flags, NULL, NULL);
assert(r >= 0);
@ -144,11 +146,11 @@ int main(int argc, char* argv[]) {
ev.data.sock = sock;
ev.events = EPOLLOUT;
//r = epoll_ctl(epoll_hnd, EPOLL_CTL_DEL, sock, &ev);
//assert(r == 0);
// r = epoll_ctl(epoll_hnd, EPOLL_CTL_DEL, sock, &ev);
// assert(r == 0);
r = epoll_ctl(epoll_hnd, EPOLL_CTL_MOD, sock, &ev);
assert(r == 0);
pings++;
continue;
@ -165,7 +167,7 @@ int main(int argc, char* argv[]) {
r = WSASend(sock, &wsa_buf, 1, &bytes, 0, NULL, NULL);
assert(r >= 0);
assert(bytes == sizeof PING);
ev.data.sock = sock;
ev.events = EPOLLIN;
@ -184,5 +186,3 @@ int main(int argc, char* argv[]) {
r = epoll_close(epoll_hnd);
assert(r == 0);
}

View File

@ -50,8 +50,8 @@
* The maximum height of a red-black tree is 2lg (n+1).
*/
#ifndef EPOLL_TREE_H_
#define EPOLL_TREE_H_
#ifndef EPOLL_TREE_H_
#define EPOLL_TREE_H_
/* clang-format off */
@ -761,4 +761,4 @@ name##_RB_MINMAX(struct name *head, int val) \
/* clang-format on */
#endif /* EPOLL_TREE_H_ */
#endif /* EPOLL_TREE_H_ */