[or-cvs] Revise windows patch; make it use logging code; make it mor...

Nick Mathewson nickm at seul.org
Thu Mar 10 19:32:07 UTC 2005


Update of /home/or/cvsroot/libevent/WIN32-Code
In directory moria.mit.edu:/tmp/cvs-serv3026/WIN32-Code

Modified Files:
	win32.c 
Log Message:
Revise windows patch; make it use logging code; make it more correct.

Index: win32.c
===================================================================
RCS file: /home/or/cvsroot/libevent/WIN32-Code/win32.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- win32.c	22 Feb 2005 04:22:34 -0000	1.2
+++ win32.c	10 Mar 2005 19:32:05 -0000	1.3
@@ -38,19 +38,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <err.h>
 #include <assert.h>
 
-#ifdef USE_LOG
 #include "log.h"
-#else
-#define LOG_DBG(x)
-#define log_error(x)	perror(x)
-#endif
-
 #include "event.h"
 #include "event-internal.h"
 
+#define XFREE(ptr) do { if (ptr) free(ptr); } while(0)
+
 extern struct event_list timequeue;
 extern struct event_list addqueue;
 extern struct event_list signalqueue;
@@ -166,19 +161,34 @@
 		return NULL;
 	winop->fd_setsz = NEVENT;
 	size = FD_SET_ALLOC_SIZE(NEVENT);
-	winop->readset_in = malloc(size); // XXXX check
-	winop->writeset_in = malloc(size); // XXXX check;
-	winop->readset_out = malloc(size);
-	winop->writeset_out = malloc(size);
-	winop->exset_out = malloc(size);
+	if (!(winop->readset_in = malloc(size)))
+        goto err;
+	if (!(winop->writeset_in = malloc(size)))
+        goto err;
+	if (!(winop->readset_out = malloc(size)))
+        goto err;
+	if (!(winop->writeset_out = malloc(size)))
+        goto err;
+	if (!(winop->exset_out = malloc(size)))
+        goto err;
 	winop->n_events = 0;
 	winop->n_events_alloc = NEVENT;
-	winop->events = malloc(NEVENT*sizeof(struct event*));
+	if (!(winop->events = malloc(NEVENT*sizeof(struct event*))))
+        goto err;
 	winop->readset_in->fd_count = winop->writeset_in->fd_count = 0;
 	winop->readset_out->fd_count = winop->writeset_out->fd_count
 		= winop->exset_out->fd_count = 0;
 
 	return (winop);
+err:
+    XFREE(winop->readset_in);
+    XFREE(winop->writeset_in);
+    XFREE(winop->readset_out);
+    XFREE(winop->writeset_out);
+    XFREE(winop->exset_out);
+    XFREE(winop->events);
+    XFREE(winop);
+    return (NULL);
 }
 
 int
@@ -194,8 +204,8 @@
 
 	if (ev->ev_events & EV_SIGNAL) {
 		if (ev->ev_events & (EV_READ|EV_WRITE))
-			errx(1, "%s: EV_SIGNAL incompatible use",
-			     __func__);
+			event_errx(1, "%s: EV_SIGNAL incompatible use",
+			           __func__);
 		if((int)signal(EVENT_SIGNAL(ev), signal_handler) == -1)
 			return (-1);
 
@@ -204,24 +214,28 @@
 	if (!(ev->ev_events & (EV_READ|EV_WRITE)))
 		return (0);
 
-
 	for (i=0;i<win32op->n_events;++i) {
 		if(win32op->events[i] == ev) {
-			/* printf("Found event for %d\n", ev->ev_fd); */
+            event_debug(("%s: Event for %d already inserted.", __func__, (int)ev->ev_fd));
 			return (0);
 		}
 	}
-	/* printf("Add %d for %d (%p)\n", ev->ev_fd, ev->ev_events, ev); */
+    event_debug(("%s: adding event for %d", __func__, (int)ev->ev_fd));
 	if (ev->ev_events & EV_READ) {
-		do_fd_set(win32op, ev->ev_fd, 1);
+		if (do_fd_set(win32op, ev->ev_fd, 1)<0)
+            return (-1);
 	}
 	if (ev->ev_events & EV_WRITE) {
-		do_fd_set(win32op, ev->ev_fd, 0);
+		if (do_fd_set(win32op, ev->ev_fd, 0)<0)
+            return (-1);
 	}
 
 	if (win32op->n_events_alloc == win32op->n_events) {
+        size_t sz;
 		win32op->n_events_alloc *= 2;
-		win32op->events = realloc(win32op->events, sizeof(struct event)*win32op->n_events_alloc);
+        sz = sizeof(struct event*)*win32op->n_events_alloc;
+		if (!(win32op->events = realloc(win32op->events, sz)))
+            return (-1);          
 	}
 	win32op->events[win32op->n_events++] = ev;
 
@@ -245,8 +259,11 @@
 			break;
 		}
 	}
-	if (found < 0)
+    if (found < 0) {
+        event_debug(("%s: Unable to remove non-inserted event for %d", __func__, ev->ev_fd));
 		return (-1);
+    }
+    event_debug(("%s: Removing event for %d", __func__, ev->ev_fd)); 
 	if (ev->ev_events & EV_READ)
 		do_fd_clear(win32op, ev->ev_fd, 1);
 	if (ev->ev_events & EV_WRITE)
@@ -283,45 +300,32 @@
 	int i;
 	int fd_count;
 
-	/* printf("Waiting...\n"); */
-
 	fd_set_copy(win32op->readset_out, win32op->readset_in);
 	fd_set_copy(win32op->exset_out, win32op->readset_in);
 	fd_set_copy(win32op->writeset_out, win32op->writeset_in);
-	/* 
-	dump_fd_set(win32op->writeset_in);
-	printf("On readlist:");
-	dump_fd_set(win32op->readset_out);
-	printf("On writelist:");
-	dump_fd_set(win32op->writeset_out);
-	printf("On exlist:");
-	dump_fd_set(win32op->exset_out);
-	*/
 
 	fd_count = (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
 		win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
 
+    if (!fd_count) {
+        /* Windows doesn't like you to call select() with no sockets. */
+        Sleep(timeval_to_ms(tv));
+        signal_process();
+        return (0);
+    }
+
 	res = select(fd_count,
 		     (struct fd_set*)win32op->readset_out,
 		     (struct fd_set*)win32op->writeset_out,
 		     (struct fd_set*)win32op->exset_out, tv);
 
-	/* printf("Wait said %d\n",res); */
+    event_debug(("%s: select returned %d", __func__, res));
 
 	if(res <= 0) {
 		signal_process();
 		return res;
 	}
 
-	/*
-	printf("On readlist:");
-	dump_fd_set(win32op->readset_out);
-	printf("On writelist:");
-	dump_fd_set(win32op->writeset_out);
-	printf("On exlist:");
-	dump_fd_set(win32op->exset_out);
-	*/
-
 	for (i=0;i<win32op->n_events;++i) {
 		struct event *ev;
 		int got = 0;
@@ -339,13 +343,11 @@
 		}
 		if (!got)
 			continue;
-		/* printf("I will call something now.\n"); */
 		if (!(ev->ev_events & EV_PERSIST)) {
 			event_del(ev);
 		}
 		event_active(ev,got,1);
 	}
-	/* printf("Done with loop.\n"); */
 
 	if (signal_recalc() == -1)
 		return (-1);



More information about the tor-commits mailing list