Bug fix in grow function and race fix in dispatcher
authorKevin Klues <klueska@cs.berkeley.edu>
Sun, 1 Jun 2014 17:59:13 +0000 (10:59 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Sun, 1 Jun 2014 22:13:35 +0000 (15:13 -0700)
user/benchutil/alarm_dispatch.c

index ed70097..d34d5fb 100644 (file)
@@ -27,7 +27,9 @@ static void dispatch_alarm(struct event_msg *ev_msg, unsigned int ev_type)
        if (ev_msg) {
                // There is a slight race here if you don't disable the alarm before
                // deregistering its handler.  Make sure you do this properly.
-               dispatch.handlers[ev_msg->ev_arg2](ev_msg, ev_type);
+               handle_event_t handler = dispatch.handlers[ev_msg->ev_arg2];
+               if (handler)
+                       handler(ev_msg, ev_type);
        }
 }
 
@@ -45,8 +47,9 @@ static void init_alarm_dispatch()
 static void __maybe_grow_handler_array(int index)
 {
        if (dispatch.length <= index) {
-               int new_size = dispatch.length + GROWTH_INC*(index/GROWTH_INC);
-               dispatch.handlers = realloc(dispatch.handlers, new_size);
+               int new_size = GROWTH_INC * (1 + index/GROWTH_INC);
+               dispatch.handlers = realloc(dispatch.handlers,
+                                           new_size * sizeof(handle_event_t));
                for (int i=dispatch.length; i<new_size; i++)
                        dispatch.handlers[i] = NULL;
                dispatch.length = new_size;