]> granicus.if.org Git - libevent/commitdiff
malloc fd only if it is needed.
authorNiels Provos <provos@gmail.com>
Fri, 2 Jan 2009 18:18:30 +0000 (18:18 +0000)
committerNiels Provos <provos@gmail.com>
Fri, 2 Jan 2009 18:18:30 +0000 (18:18 +0000)
svn:r982

evmap.c

diff --git a/evmap.c b/evmap.c
index 1a753bc32bff6e8ade8304db25a3d845b7a6518c..183fcfad1b73578aa46437306ebf0975f63d4d8c 100644 (file)
--- a/evmap.c
+++ b/evmap.c
 #include "evmap.h"
 #include "mm-internal.h"
 
-#define GET_SLOT(map, slot, type) (struct type *)((map)->entries[slot])
+#define GET_SLOT(x, map, slot, type)                   \
+       (x) = (struct type *)((map)->entries[slot])
+#define GET_SLOT_AND_CTOR(x, map, slot, type, ctor)                    \
+       do {                                                            \
+               if ((map)->entries[slot] == NULL) {                     \
+                       assert(ctor != NULL);                           \
+                       (map)->entries[slot]=mm_malloc(sizeof(struct type)); \
+                       assert((map)->entries[slot] != NULL);           \
+                       (ctor)((struct type *)(map)->entries[slot]);    \
+               }                                                       \
+               (x) = (struct type *)((map)->entries[slot]);            \
+       } while (0)                                                     \
 
 static int
-evmap_make_space(
-       struct event_map *map, int slot, int msize, void (*ctor)(void *))
+evmap_make_space(struct event_map *map, int slot, int msize)
 {
        if (map->nentries <= slot) {
-               int i;
                int nentries = map->nentries ? map->nentries : 32;
                void **tmp;
 
@@ -74,11 +83,8 @@ evmap_make_space(
                if (tmp == NULL)
                        return (-1);
                
-               for (i = map->nentries; i < nentries; ++i) {
-                       tmp[i] = mm_malloc(msize);
-                       assert(tmp[i] != NULL);
-                       (*ctor)(tmp[i]);
-               }
+               memset(&tmp[map->nentries], 0,
+                   (nentries - map->nentries)  * sizeof(void *));
 
                map->nentries = nentries;
                map->entries = tmp;
@@ -94,8 +100,10 @@ evmap_clear(struct event_map *ctx)
        ctx->nentries = 0;
        if (ctx->entries != NULL) {
                int i;
-               for (i = 0; i < ctx->nentries; ++i)
-                       mm_free(ctx->entries[i]);
+               for (i = 0; i < ctx->nentries; ++i) {
+                       if (ctx->entries[i] != NULL)
+                               mm_free(ctx->entries[i]);
+               }
                mm_free(ctx->entries);
                ctx->entries = NULL;
        }
@@ -130,11 +138,10 @@ evmap_io_add(struct event_base *base, int fd, struct event *ev)
        short res = 0, old = 0;
 
        if (fd >= io->nentries) {
-               if (evmap_make_space(io, fd,
-                       sizeof(struct evmap_io), evmap_io_init) == -1)
+               if (evmap_make_space(io, fd, sizeof(struct evmap_io)) == -1)
                        return (-1);
        }
-       ctx = GET_SLOT(io, fd, evmap_io);
+       GET_SLOT_AND_CTOR(ctx, io, fd, evmap_io, evmap_io_init);
 
        nread = ctx->nread;
        nwrite = ctx->nwrite;
@@ -181,7 +188,7 @@ evmap_io_del(struct event_base *base, int fd, struct event *ev)
        if (fd >= io->nentries)
                return (-1);
        
-       ctx = GET_SLOT(io, fd, evmap_io);
+       GET_SLOT(ctx, io, fd, evmap_io);
 
        nread = ctx->nread;
        nwrite = ctx->nwrite;
@@ -222,7 +229,7 @@ evmap_io_active(struct event_base *base, int fd, short events)
        struct event *ev;
 
        assert(fd < io->nentries);
-       ctx = GET_SLOT(io, fd, evmap_io);
+       GET_SLOT(ctx, io, fd, evmap_io);
 
        TAILQ_FOREACH(ev, &ctx->events, ev_io_next) {
                if (ev->ev_events & events)
@@ -254,11 +261,10 @@ evmap_signal_add(struct event_base *base, int sig, struct event *ev)
 
        if (sig >= map->nentries) {
                if (evmap_make_space(
-                       map, sig, sizeof(struct evmap_signal),
-                       evmap_signal_init) == -1)
+                       map, sig, sizeof(struct evmap_signal)) == -1)
                        return (-1);
        }
-       ctx = GET_SLOT(map, sig, evmap_signal);
+       GET_SLOT_AND_CTOR(ctx, map, sig, evmap_signal, evmap_signal_init);
 
        if (TAILQ_EMPTY(&ctx->events)) {
                if (evsel->add(base, EVENT_SIGNAL(ev), 0, EV_SIGNAL) == -1)
@@ -280,7 +286,7 @@ evmap_signal_del(struct event_base *base, int sig, struct event *ev)
        if (sig >= map->nentries)
                return (-1);
        
-       ctx = GET_SLOT(map, sig, evmap_signal);
+       GET_SLOT(ctx, map, sig, evmap_signal);
 
        if (TAILQ_FIRST(&ctx->events) == TAILQ_LAST(&ctx->events, event_list)) {
                if (evsel->del(base, EVENT_SIGNAL(ev), 0, EV_SIGNAL) == -1)
@@ -300,7 +306,7 @@ evmap_signal_active(struct event_base *base, int sig, int ncalls)
        struct event *ev;
 
        assert(sig < map->nentries);
-       ctx = GET_SLOT(map, sig, evmap_signal);
+       GET_SLOT(ctx, map, sig, evmap_signal);
 
        TAILQ_FOREACH(ev, &ctx->events, ev_signal_next)
                event_active(ev, EV_SIGNAL, ncalls);