commit d7dc055bae2ed8019831df3a6bbe2d7f5ac3a7b6
parent 7c01a7e82b69d9e45eeb76573c688d07df45be83
Author: Yersa Nordman <yersa@finwo.nl>
Date: Wed, 27 Dec 2023 23:48:31 +0100
work-in-progress implementation
Diffstat:
5 files changed, 149 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -0,0 +1,31 @@
+finwo/poll
+==========
+
+Basic cross-platform io multiplexing library, focussing on polling
+
+## Why
+
+Yes, there's already modern replacements for [poll(2)][poll2] and
+[select(2)][select2], and things like libevent, that handle io multiplexing
+properly, but none that I could find that were easily embeddable without having
+the resulting binary depend on those libraries.
+
+While initially an easy-to-use wrapper around poll(2), platform-specific
+optimizations (like using epoll/kqueue) will be implemented here without the
+external api changing to allow all my projects to benefit from the work here.
+
+## Installation
+
+This library is intended to be installed through
+[dep](https://github.com/finwo/dep), but can be embedded by dropping in the
+[fpoll.c](src/fpoll.c) and [fpoll.h](src/fpoll.h) files into your project and
+including them during compilation.
+
+```
+dep add finwo/poll
+```
+
+## API
+
+[poll2]: https://man7.org/linux/man-pages/man2/poll.2.html
+[select2]: https://man7.org/linux/man-pages/man2/select.2.html
diff --git a/config.mk b/config.mk
@@ -0,0 +1 @@
+SRC+=__DIRNAME/src/fpoll.c
diff --git a/package.ini b/package.ini
@@ -0,0 +1,9 @@
+[dependencies]
+
+[export]
+config.mk=config.mk
+include/finwo/poll.h=src/fpoll.h
+
+[package]
+deps=lib
+name=finwo/poll
diff --git a/src/fpoll.c b/src/fpoll.c
@@ -0,0 +1,68 @@
+#include <poll.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fpoll.h"
+
+/* struct fpoll { */
+/* struct pollfd *fds; */
+/* int size; */
+/* int limit; */
+/* }; */
+
+struct fpoll * fpoll_create() {
+ struct fpoll *output = calloc(1, sizeof(struct fpoll));
+ return output;
+}
+
+FPOLL_STATUS fpoll_close(struct fpoll *descriptor) {
+ if (descriptor->fds) free(descriptor->fds);
+ free(descriptor);
+ return FPOLL_STATUS_OK;
+}
+
+int fpoll_wait(struct fpoll *descriptor, struct fpoll_ev *evs, int max_evs, int timeout) {
+ return 0;
+}
+
+FPOLL_STATUS fpoll_add(struct fpoll *descriptor, FPOLL_EVENT events, FPOLL_FD filedescriptor) {
+
+ // Initial alloc
+ if (!descriptor->fds) {
+ descriptor->limit = 4;
+ descriptor->fds = calloc(descriptor->limit, sizeof(struct pollfd));
+ }
+
+ // Grow alloc
+ if (descriptor->limit < (descriptor->size + 1)) {
+ descriptor->limit = descriptor->limit * 2;
+ descriptor->fds = realloc(descriptor->fds, descriptor->limit);
+ }
+
+ // Find the filedescriptor in the list
+ struct pollfd *pfd = NULL;
+ int i;
+ for(i = 0 ; i < descriptor->size; i++) {
+ if (filedescriptor != descriptor->fds[i].fd) continue;
+ pfd = &(descriptor->fds[i]);
+ }
+
+ // Assign a new pollfd if needed
+ if (!pfd) {
+ pfd = &(descriptor->fds[descriptor->size]);
+ descriptor->size = descriptor->size + 1;
+ pfd->fd = filedescriptor;
+ pfd->events = 0;
+ }
+
+ // Assign the correct events
+ if (events & FPOLL_IN) pfd->events |= POLLIN;
+ if (events & FPOLL_OUT) pfd->events |= POLLOUT;
+
+ return FPOLL_STATUS_OK;
+}
+
+FPOLL_STATUS fpoll_del(struct fpoll *descriptor, FPOLL_EVENT events, FPOLL_FD filedescriptor) {
+ return FPOLL_STATUS_OK;
+}
diff --git a/src/fpoll.h b/src/fpoll.h
@@ -0,0 +1,40 @@
+#ifndef __FINWO_POLL_H__
+#define __FINWO_POLL_H__
+
+#include <poll.h>
+
+#ifndef FPOLL_STATUS
+#define FPOLL_STATUS int
+#endif
+#define FPOLL_STATUS_OK 0
+#define FPOLL_STATUS_ERROR 1
+
+#ifndef FPOLL_EVENT
+#define FPOLL_EVENT int
+#endif
+#define FPOLL_IN 1
+#define FPOLL_OUT 2
+#define FPOLL_HUP 4
+
+#ifndef FPOLL_FD
+#define FPOLL_FD int
+#endif
+
+struct fpoll {
+ struct pollfd *fds;
+ int size;
+ int limit;
+};
+
+struct fpoll_ev {
+ FPOLL_FD fd;
+ FPOLL_EVENT ev;
+};
+
+struct fpoll * fpoll_create();
+FPOLL_STATUS fpoll_close(struct fpoll *);
+int fpoll_wait(struct fpoll *, struct fpoll_ev *, int max_evs, int timeout);
+FPOLL_STATUS fpoll_add(struct fpoll *, FPOLL_EVENT, FPOLL_FD);
+FPOLL_STATUS fpoll_del(struct fpoll *, FPOLL_EVENT, FPOLL_FD);
+
+#endif // __FINWO_POLL_H__