Coverage report for bus/expirelist.c.gcov

        -:    0:Source:expirelist.c
        -:    0:Graph:expirelist.gcno
        -:    0:Data:expirelist.gcda
        -:    0:Runs:10118
        -:    0:Programs:2
        -:    1:/* -*- mode: C; c-file-style: "gnu" -*- */
        -:    2:/* expirelist.c  List of items that expire
        -:    3: *
        -:    4: * Copyright (C) 2003  Red Hat, Inc.
        -:    5: *
        -:    6: * Licensed under the Academic Free License version 2.1
        -:    7: *
        -:    8: * This program is free software; you can redistribute it and/or modify
        -:    9: * it under the terms of the GNU General Public License as published by
        -:   10: * the Free Software Foundation; either version 2 of the License, or
        -:   11: * (at your option) any later version.
        -:   12: *
        -:   13: * This program is distributed in the hope that it will be useful,
        -:   14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
        -:   15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        -:   16: * GNU General Public License for more details.
        -:   17: *
        -:   18: * You should have received a copy of the GNU General Public License
        -:   19: * along with this program; if not, write to the Free Software
        -:   20: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
        -:   21: *
        -:   22: */
        -:   23:
        -:   24:#include "expirelist.h"
        -:   25:#include "test.h"
        -:   26:#include <dbus/dbus-internals.h>
        -:   27:#include <dbus/dbus-mainloop.h>
        -:   28:#include <dbus/dbus-timeout.h>
        -:   29:
        -:   30:static dbus_bool_t expire_timeout_handler (void *data);
        -:   31:
        -:   32:static void
        -:   33:call_timeout_callback (DBusTimeout   *timeout,
        -:   34:                       void          *data)
function call_timeout_callback called 1590 returned 100% blocks executed 100%
     1590:   35:{
        -:   36:  /* can return FALSE on OOM but we just let it fire again later */
     1590:   37:  dbus_timeout_handle (timeout);
call    0 returned 100%
     1590:   38:}
        -:   39:
        -:   40:BusExpireList*
        -:   41:bus_expire_list_new (DBusLoop      *loop,
        -:   42:                     int            expire_after,
        -:   43:                     BusExpireFunc  expire_func,
        -:   44:                     void          *data)
function bus_expire_list_new called 4 returned 100% blocks executed 56%
        4:   45:{
        -:   46:  BusExpireList *list;
        -:   47:
        4:   48:  list = dbus_new0 (BusExpireList, 1);
call    0 returned 100%
        4:   49:  if (list == NULL)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
    #####:   50:    return NULL;
        -:   51:
        4:   52:  list->expire_func = expire_func;
        4:   53:  list->data = data;
        4:   54:  list->loop = loop;
        4:   55:  list->expire_after = expire_after;
        -:   56:
        4:   57:  list->timeout = _dbus_timeout_new (100, /* irrelevant */
call    0 returned 100%
        -:   58:                                     expire_timeout_handler,
        -:   59:                                     list, NULL);
        4:   60:  if (list->timeout == NULL)
branch  0 taken 100% (fallthrough)
branch  1 taken 0%
    #####:   61:    goto failed;
        -:   62:
        4:   63:  _dbus_timeout_set_enabled (list->timeout, FALSE);
call    0 returned 100%
        -:   64:
        4:   65:  if (!_dbus_loop_add_timeout (list->loop,
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
        -:   66:                               list->timeout,
        -:   67:                               call_timeout_callback, NULL, NULL))
    #####:   68:    goto failed;
        -:   69:
        4:   70:  return list;
        -:   71:
    #####:   72: failed:
    #####:   73:  if (list->timeout)
branch  0 never executed
branch  1 never executed
    #####:   74:    _dbus_timeout_unref (list->timeout);
call    0 never executed
        -:   75:
    #####:   76:  dbus_free (list);
call    0 never executed
        -:   77:
    #####:   78:  return NULL;
        -:   79:}
        -:   80:
        -:   81:void
        -:   82:bus_expire_list_free (BusExpireList *list)
function bus_expire_list_free called 4 returned 100% blocks executed 100%
        4:   83:{
        4:   84:  _dbus_assert (list->items == NULL);
call    0 returned 100%
        -:   85:
        4:   86:  _dbus_loop_remove_timeout (list->loop, list->timeout,
call    0 returned 100%
        -:   87:                             call_timeout_callback, NULL);
        -:   88:
        4:   89:  _dbus_timeout_unref (list->timeout);
call    0 returned 100%
        -:   90:
        4:   91:  dbus_free (list);
call    0 returned 100%
        4:   92:}
        -:   93:
        -:   94:void
        -:   95:bus_expire_timeout_set_interval (DBusTimeout *timeout,
        -:   96:                                 int          next_interval)
function bus_expire_timeout_set_interval called 17038 returned 100% blocks executed 90%
    17038:   97:{
    17038:   98:  if (next_interval >= 0)
branch  0 taken 55% (fallthrough)
branch  1 taken 45%
        -:   99:    {
     9404:  100:      _dbus_timeout_set_interval (timeout,
call    0 returned 100%
        -:  101:                                  next_interval);
     9404:  102:      _dbus_timeout_set_enabled (timeout, TRUE);
call    0 returned 100%
        -:  103:
     9404:  104:      _dbus_verbose ("Enabled expire timeout with interval %d\n",
call    0 returned 100%
        -:  105:                     next_interval);
        -:  106:    }
     7634:  107:  else if (dbus_timeout_get_enabled (timeout))
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0%
        -:  108:    {
     7634:  109:      _dbus_timeout_set_enabled (timeout, FALSE);
call    0 returned 100%
        -:  110:
     7634:  111:      _dbus_verbose ("Disabled expire timeout\n");
call    0 returned 100%
        -:  112:    }
        -:  113:  else
    #####:  114:    _dbus_verbose ("No need to disable expire timeout\n");
call    0 never executed
    17038:  115:}
        -:  116:
        -:  117:static int
        -:  118:do_expiration_with_current_time (BusExpireList *list,
        -:  119:                                 long           tv_sec,
        -:  120:                                 long           tv_usec)
function do_expiration_with_current_time called 1593 returned 100% blocks executed 94%
     1593:  121:{
        -:  122:  DBusList *link;
        -:  123:  int next_interval;
        -:  124:
     1593:  125:  next_interval = -1;
        -:  126:  
     1593:  127:  link = _dbus_list_get_first_link (&list->items);
call    0 returned 100%
     4725:  128:  while (link != NULL)
branch  0 taken 51%
branch  1 taken 49% (fallthrough)
        -:  129:    {
     1593:  130:      DBusList *next = _dbus_list_get_next_link (&list->items, link);
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
        -:  131:      double elapsed;
        -:  132:      BusExpireItem *item;
        -:  133:
     1593:  134:      item = link->data;
        -:  135:
     1593:  136:      elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
        -:  137:                                            item->added_tv_usec,
        -:  138:                                            tv_sec, tv_usec);
        -:  139:
     1593:  140:      if (elapsed >= (double) list->expire_after)
branch  0 taken 99% (fallthrough)
branch  1 taken 1%
        -:  141:        {
     1591:  142:          _dbus_verbose ("Expiring an item %p\n", item);
call    0 returned 100%
        -:  143:
        -:  144:          /* If the expire function fails, we just end up expiring
        -:  145:           * this item next time we walk through the list. This would
        -:  146:           * be an indeterminate time normally, so we set up the
        -:  147:           * next_interval to be "shortly" (just enough to avoid
        -:  148:           * a busy loop)
        -:  149:           */
     1591:  150:          if (!(* list->expire_func) (list, link, list->data))
call    0 returned 100%
branch  1 taken 3% (fallthrough)
branch  2 taken 97%
        -:  151:            {
       52:  152:              next_interval = _dbus_get_oom_wait ();
call    0 returned 100%
       52:  153:              break;
        -:  154:            }
        -:  155:        }
        -:  156:      else
        -:  157:        {
        -:  158:          /* We can end the loop, since the connections are in oldest-first order */
        2:  159:          next_interval = ((double)list->expire_after) - elapsed;
        2:  160:          _dbus_verbose ("Item %p expires in %d milliseconds\n",
call    0 returned 100%
        -:  161:                         item, next_interval);
        -:  162:
        2:  163:          break;
        -:  164:        }
        -:  165:
     1539:  166:      link = next;
        -:  167:    }
        -:  168:
     1593:  169:  return next_interval;
        -:  170:}
        -:  171:
        -:  172:static void
        -:  173:bus_expirelist_expire (BusExpireList *list)
function bus_expirelist_expire called 1590 returned 100% blocks executed 100%
     1590:  174:{
        -:  175:  int next_interval;
        -:  176:
     1590:  177:  next_interval = -1;
        -:  178:
     1590:  179:  if (list->items != NULL)
branch  0 taken 100% (fallthrough)
branch  1 taken 0%
        -:  180:    {
        -:  181:      long tv_sec, tv_usec;
        -:  182:
     1590:  183:      _dbus_get_current_time (&tv_sec, &tv_usec);
call    0 returned 100%
        -:  184:
     1590:  185:      next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
call    0 returned 100%
        -:  186:    }
        -:  187:
     1590:  188:  bus_expire_timeout_set_interval (list->timeout, next_interval);
call    0 returned 100%
     1590:  189:}
        -:  190:
        -:  191:static dbus_bool_t
        -:  192:expire_timeout_handler (void *data)
function expire_timeout_handler called 1590 returned 100% blocks executed 100%
     1590:  193:{
     1590:  194:  BusExpireList *list = data;
        -:  195:
     1590:  196:  _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
call    0 returned 100%
        -:  197:
        -:  198:  /* note that this may remove the timeout */
     1590:  199:  bus_expirelist_expire (list);
call    0 returned 100%
        -:  200:
     1590:  201:  return TRUE;
        -:  202:}
        -:  203:
        -:  204:#ifdef DBUS_BUILD_TESTS
        -:  205:
        -:  206:typedef struct
        -:  207:{
        -:  208:  BusExpireItem item;
        -:  209:  int expire_count;
        -:  210:} TestExpireItem;
        -:  211:
        -:  212:static dbus_bool_t
        -:  213:test_expire_func (BusExpireList *list,
        -:  214:                  DBusList      *link,
        -:  215:                  void          *data)
function test_expire_func called 1 returned 100% blocks executed 100%
        1:  216:{
        -:  217:  TestExpireItem *t;
        -:  218:
        1:  219:  t = (TestExpireItem*) link->data;
        -:  220:
        1:  221:  t->expire_count += 1;
        -:  222:
        1:  223:  return TRUE;
        -:  224:}
        -:  225:
        -:  226:static void
        -:  227:time_add_milliseconds (long *tv_sec,
        -:  228:                       long *tv_usec,
        -:  229:                       int   milliseconds)
function time_add_milliseconds called 2 returned 100% blocks executed 100%
        2:  230:{
        2:  231:  *tv_sec = *tv_sec + milliseconds / 1000;
        2:  232:  *tv_usec = *tv_usec + milliseconds * 1000;
        2:  233:  if (*tv_usec >= 1000000)
branch  0 taken 100% (fallthrough)
branch  1 taken 0%
        -:  234:    {
        2:  235:      *tv_usec -= 1000000;
        2:  236:      *tv_sec += 1;
        -:  237:    }
        2:  238:}
        -:  239:
        -:  240:dbus_bool_t
        -:  241:bus_expire_list_test (const DBusString *test_data_dir)
function bus_expire_list_test called 1 returned 100% blocks executed 96%
        1:  242:{
        -:  243:  DBusLoop *loop;
        -:  244:  BusExpireList *list;
        -:  245:  long tv_sec, tv_usec;
        -:  246:  long tv_sec_not_expired, tv_usec_not_expired;
        -:  247:  long tv_sec_expired, tv_usec_expired;
        -:  248:  long tv_sec_past, tv_usec_past;
        -:  249:  TestExpireItem *item;
        -:  250:  int next_interval;
        -:  251:  
        1:  252:  loop = _dbus_loop_new ();
call    0 returned 100%
        1:  253:  _dbus_assert (loop != NULL);
call    0 returned 100%
        -:  254:
        -:  255:#define EXPIRE_AFTER 100
        -:  256:  
        1:  257:  list = bus_expire_list_new (loop, EXPIRE_AFTER,
call    0 returned 100%
        -:  258:                              test_expire_func, NULL);
        1:  259:  _dbus_assert (list != NULL);
call    0 returned 100%
        -:  260:
        1:  261:  _dbus_get_current_time (&tv_sec, &tv_usec);
call    0 returned 100%
        -:  262:
        1:  263:  tv_sec_not_expired = tv_sec;
        1:  264:  tv_usec_not_expired = tv_usec;
        1:  265:  time_add_milliseconds (&tv_sec_not_expired,
call    0 returned 100%
        -:  266:                         &tv_usec_not_expired, EXPIRE_AFTER - 1);
        -:  267:
        1:  268:  tv_sec_expired = tv_sec;
        1:  269:  tv_usec_expired = tv_usec;
        1:  270:  time_add_milliseconds (&tv_sec_expired,
call    0 returned 100%
        -:  271:                         &tv_usec_expired, EXPIRE_AFTER);
        -:  272:  
        -:  273:
        1:  274:  tv_sec_past = tv_sec - 1;
        1:  275:  tv_usec_past = tv_usec;
        -:  276:
        1:  277:  item = dbus_new0 (TestExpireItem, 1);
call    0 returned 100%
        -:  278:
        1:  279:  item->item.added_tv_sec = tv_sec;
        1:  280:  item->item.added_tv_usec = tv_usec;
        1:  281:  if (!_dbus_list_append (&list->items, item))
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
    #####:  282:    _dbus_assert_not_reached ("out of memory");
call    0 never executed
        -:  283:
        1:  284:  next_interval =
call    0 returned 100%
        -:  285:    do_expiration_with_current_time (list, tv_sec_not_expired,
        -:  286:                                     tv_usec_not_expired);
        1:  287:  _dbus_assert (item->expire_count == 0);
call    0 returned 100%
        1:  288:  _dbus_verbose ("next_interval = %d\n", next_interval);
call    0 returned 100%
        1:  289:  _dbus_assert (next_interval == 1);
call    0 returned 100%
        -:  290:  
        1:  291:  next_interval =
call    0 returned 100%
        -:  292:    do_expiration_with_current_time (list, tv_sec_expired,
        -:  293:                                     tv_usec_expired);
        1:  294:  _dbus_assert (item->expire_count == 1);
call    0 returned 100%
        1:  295:  _dbus_verbose ("next_interval = %d\n", next_interval);
call    0 returned 100%
        1:  296:  _dbus_assert (next_interval == -1);
call    0 returned 100%
        -:  297:
        1:  298:  next_interval =
call    0 returned 100%
        -:  299:    do_expiration_with_current_time (list, tv_sec_past,
        -:  300:                                     tv_usec_past);
        1:  301:  _dbus_assert (item->expire_count == 1);
call    0 returned 100%
        1:  302:  _dbus_verbose ("next_interval = %d\n", next_interval);
call    0 returned 100%
        1:  303:  _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
call    0 returned 100%
        -:  304:
        1:  305:  _dbus_list_clear (&list->items);
call    0 returned 100%
        1:  306:  dbus_free (item);
call    0 returned 100%
        -:  307:  
        1:  308:  bus_expire_list_free (list);
call    0 returned 100%
        1:  309:  _dbus_loop_unref (loop);
call    0 returned 100%
        -:  310:  
        1:  311:  return TRUE;
        -:  312:}
        -:  313:
        -:  314:#endif /* DBUS_BUILD_TESTS */