Optimize for size a bit

- Remove double book keeping, the kernel does all the heavy lifting now
- Trim the amount of #includes
- Use LIST_ instead of TAILQ_ now that we no longer handle timers ourselves

Original LibUEvent

$ size libuevent.a libuevent.so.1
   text	   data	    bss	    dec	    hex	filename
   2610	      0	      8	   2618	    a3a	libuevent.o (ex libuevent.a)
   5266	    648	     16	   5930	   172a	libuevent.so.1

Refactored libuev:

$ size libuev.a libuev.so.1
   text	   data	    bss	    dec	    hex	filename
   1557	      0	      0	   1557	    615	libuev.o (ex libuev.a)
   4186	    672	      8	   4866	   1302	libuev.so.1

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2013-07-22 23:07:15 +02:00
1 parent ed22e88c81
commit 6ed1352404
2 files changed
+19 -45

No files matched your search

+2 -4
View File
@@ -53,8 +53,7 @@ typedef enum {
/* I/O event watcher */ /* I/O event watcher */
typedef struct uev_io { typedef struct uev_io {
TAILQ_ENTRY(uev_io) link; LIST_ENTRY(uev_io) link;
TAILQ_ENTRY(uev_io) gc;
uev_type_t type; uev_type_t type;
@@ -75,8 +74,7 @@ typedef struct {
int efd; /* For epoll() */ int efd; /* For epoll() */
struct epoll_event *events; struct epoll_event *events;
TAILQ_HEAD(, uev_io) active_list; LIST_HEAD(, uev_io) watchers;
TAILQ_HEAD(, uev_io) inactive_list;
} uev_t; } uev_t;
/* Generic callback for watchers */ /* Generic callback for watchers */
+17 -41
View File
@@ -23,18 +23,11 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <stdlib.h> /* calloc(), free() */
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include <sys/times.h> #include <unistd.h> /* close(), read() */
#include <time.h>
#include <unistd.h>
#include "libuev/uev.h" #include "libuev/uev.h"
@@ -65,34 +58,24 @@ static uev_io_t *new_watcher(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir,
return NULL; return NULL;
} }
TAILQ_INSERT_TAIL(&ctx->active_list, w, link); LIST_INSERT_HEAD(&ctx->watchers, w, link);
return w; return w;
} }
static int delete_watcher(uev_t *ctx, uev_io_t *w) static int delete_watcher(uev_t *ctx, uev_io_t *w)
{ {
uev_io_t *tmp;
if (!ctx || !w) { if (!ctx || !w) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
/* Check if already removed */
TAILQ_FOREACH(tmp, &ctx->inactive_list, gc) {
if (tmp == w) {
errno = EALREADY;
return -1;
}
}
/* Remove from kernel */ /* Remove from kernel */
epoll_ctl(ctx->efd, EPOLL_CTL_DEL, w->fd, NULL); epoll_ctl(ctx->efd, EPOLL_CTL_DEL, w->fd, NULL);
/* Mark as inactive */ /* Remove from internal list */
w->handler = NULL; LIST_REMOVE(w, link);
TAILQ_INSERT_TAIL(&ctx->inactive_list, w, gc); free(w);
return 0; return 0;
} }
@@ -184,6 +167,11 @@ int uev_timer_set(uev_t *ctx, uev_io_t *w, int timeout, int period)
*/ */
int uev_timer_delete(uev_t *ctx, uev_io_t *w) int uev_timer_delete(uev_t *ctx, uev_io_t *w)
{ {
if (!ctx || !w) {
errno = EINVAL;
return -1;
}
uev_timer_set(ctx, w, 0, 0); uev_timer_set(ctx, w, 0, 0);
return delete_watcher(ctx, w); return delete_watcher(ctx, w);
} }
@@ -214,9 +202,7 @@ uev_t *uev_ctx_create(void)
} }
ctx->efd = fd; ctx->efd = fd;
LIST_INIT(&ctx->watchers);
TAILQ_INIT(&ctx->active_list);
TAILQ_INIT(&ctx->inactive_list);
return ctx; return ctx;
} }
@@ -228,10 +214,8 @@ void uev_ctx_delete(uev_t *ctx)
{ {
uev_io_t *w, *tmp; uev_io_t *w, *tmp;
TAILQ_FOREACH_SAFE(w, &ctx->active_list, link, tmp) { LIST_FOREACH_SAFE(w, &ctx->watchers, link, tmp)
TAILQ_REMOVE(&ctx->active_list, w, link); delete_watcher(ctx, w);
free(w);
}
close(ctx->efd); close(ctx->efd);
free(ctx->events); free(ctx->events);
@@ -244,7 +228,7 @@ void uev_ctx_delete(uev_t *ctx)
int uev_run(uev_t *ctx) int uev_run(uev_t *ctx)
{ {
int result = 0; int result = 0;
uev_io_t *w, *tmp; uev_io_t *w;
if (!ctx) { if (!ctx) {
errno = EINVAL; errno = EINVAL;
@@ -255,7 +239,7 @@ int uev_run(uev_t *ctx)
ctx->running = 1; ctx->running = 1;
/* Start all dormant timers */ /* Start all dormant timers */
TAILQ_FOREACH(w, &ctx->active_list, link) { LIST_FOREACH(w, &ctx->watchers, link) {
if (UEV_TIMER_TYPE == w->type) if (UEV_TIMER_TYPE == w->type)
uev_timer_set(ctx, w, w->timeout, w->period); uev_timer_set(ctx, w, w->timeout, w->period);
} }
@@ -269,6 +253,7 @@ int uev_run(uev_t *ctx)
/* Error in poll. Cannot continue */ /* Error in poll. Cannot continue */
result = 2; result = 2;
ctx->running = 0;
break; break;
} }
@@ -293,15 +278,6 @@ int uev_run(uev_t *ctx)
uev_timer_delete(ctx, w); uev_timer_delete(ctx, w);
} }
} }
/* Garbage collect */
if (!TAILQ_EMPTY(&ctx->inactive_list)) {
TAILQ_FOREACH_SAFE(w, &ctx->inactive_list, gc, tmp) {
TAILQ_REMOVE(&ctx->inactive_list, w, gc);
TAILQ_REMOVE(&ctx->active_list, w, link);
free(w);
}
}
} }
ctx->running = 0; ctx->running = 0;