From: Clement Calmels <clement.calmels@fr.ibm.com>

Just use a list instead of array for dynamically allocated stuff. It's
more appropriated.

Signed-off-by: Clement Calmels <clement.calmels@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
---

 src/lxc/mainloop.c |   86 ++++++++++++++++++++++------------------------------
 src/lxc/mainloop.h |    5 +--
 2 files changed, 38 insertions(+), 53 deletions(-)

diff --git a/src/lxc/mainloop.c b/src/lxc/mainloop.c
index 6b656d1..7fc1c5e 100644
--- a/src/lxc/mainloop.c
+++ b/src/lxc/mainloop.c
@@ -63,7 +63,7 @@ int lxc_mainloop(struct lxc_epoll_descr *descr)
 				return 0;
 		}

-		if (!descr->nfds)
+		if (lxc_list_empty(&descr->handlers))
 			return 0;
 	}
 }
@@ -71,8 +71,9 @@ int lxc_mainloop(struct lxc_epoll_descr *descr)
 int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd, 
 			     lxc_mainloop_callback_t callback, void *data)
 {
-	struct epoll_event *ev;
+	struct epoll_event ev;
 	struct mainloop_handler *handler;
+	struct lxc_list *item;

 	handler = malloc(sizeof(*handler));
 	if (!handler)
@@ -82,63 +83,43 @@ int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd,
 	handler->fd = fd;
 	handler->data = data;

-	ev = malloc(sizeof(*descr->ev) * (descr->nfds + 1));
-	if (!ev)
-		goto out_free;
+	ev.events = EPOLLIN;
+	ev.data.ptr = handler;

-	memcpy(ev, descr->ev, sizeof(*descr->ev) * (descr->nfds));
+	if (epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd, &ev) < 0)
+		goto out_free_handler;
+	
+	item = malloc(sizeof(*item));
+	if (!item)
+		goto out_free_handler;

-	ev[descr->nfds].events = EPOLLIN;
-	ev[descr->nfds].data.ptr = handler;
-
-	if (epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd, &ev[descr->nfds]) < 0) {
-		free(ev);
-		goto out_free;
-	}
-
-	free(descr->ev);
-	descr->ev = ev;
-	descr->nfds++;
+	item->elem = handler;
+	lxc_list_add(&descr->handlers, item);
 	return 0;

-out_free:
+out_free_handler:
 	free(handler);
 	return -1;
 }

 int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)
 {
-	struct epoll_event *ev;
 	struct mainloop_handler *handler;
-	int i, j, idx = 0;
+	struct lxc_list *iterator;

-	for (i = 0; i < descr->nfds; i++) {
-		
-		handler = descr->ev[i].data.ptr;
-
-		if (handler->fd != fd)
-			continue;
-
-		if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL))
-			return -1;
+	lxc_list_for_each(iterator, &descr->handlers) {
+		handler = iterator->elem;

-		ev = malloc(sizeof(*ev) * (descr->nfds - 1));
-		if (!ev)
-			return -1;
+		if (handler->fd == fd) {
+			/* found */
+			if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL))
+				return -1;

-		for (j = 0; j < descr->nfds; j++) {
-			if (i == j)
-				continue;
-			ev[idx] = descr->ev[j];
-			idx++;
+			lxc_list_del(iterator);
+			free(iterator->elem);
+			free(iterator);
+			return 0;
 		}
-
-		free(descr->ev[i].data.ptr);
-		free(descr->ev);
-		descr->ev = ev;
-		descr->nfds--;
-		
-		return 0;
 	}

 	return -1;
@@ -146,25 +127,30 @@ int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)

 int lxc_mainloop_open(struct lxc_epoll_descr *descr)
 {
-	descr->nfds = 0;
-	descr->ev = NULL;
-
 	/* hint value passed to epoll create */
 	descr->epfd = epoll_create(2);
 	if (descr->epfd < 0)
 		return -1;

+	lxc_list_init(&descr->handlers);
 	return 0;
 }

 int lxc_mainloop_close(struct lxc_epoll_descr *descr)
 {
 	int i;
+	struct lxc_list *iterator, *next;

-	for (i = 0; i < descr->nfds; i++)
-		free(descr->ev[i].data.ptr);
-	free(descr->ev);
+	iterator = descr->handlers.next;
+	while (iterator != &descr->handlers) {
+		next = iterator->next;

+		lxc_list_del(iterator);
+		free(iterator->elem);
+		free(iterator);
+		iterator = next;
+	}
+	
 	return close(descr->epfd);
 }

diff --git a/src/lxc/mainloop.h b/src/lxc/mainloop.h
index aa17eb5..deeff23 100644
--- a/src/lxc/mainloop.h
+++ b/src/lxc/mainloop.h
@@ -21,12 +21,11 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */

-struct epoll_event;
+#include <list.h>

 struct lxc_epoll_descr {
 	int epfd;
-	int nfds;
-	struct epoll_event *ev;
+	struct lxc_list handlers;
 };

 typedef int (*lxc_mainloop_callback_t)(int fd, void *data, 


--===============1057440843==
Content-Type: text/plain; charset="iso-8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

_______________________________________________
Linux mailing list
Linux@www.lab.toulouse-stg.fr.ibm.com
http://www.lab.toulouse-stg.fr.ibm.com/mailman/listinfo/linux

--===============1057440843==--




