Hi.

This patch adds two functions, timeval_add and timeval_sub,
to libiberty.  GDB has use for them in a few places and since
they're general purpose I wish to check them into libiberty.

Ok to check in?

2011-09-19  Doug Evans  <d...@google.com>

        include/
        * timeval-utils.h: New file.

        libiberty/
        * timeval-utils.c: New file.
        * Makefile.in (CFILES): Add it.
        (REQUIRED_OFILES): Add timeval-utils.$(objext).
        (INSTALLED_HEADERS): Add timeval-utils.h.
        (timeval-utils.$(objext)): Add rule.

Index: include/timeval-utils.h
===================================================================
RCS file: include/timeval-utils.h
diff -N include/timeval-utils.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ include/timeval-utils.h     20 Sep 2011 00:46:44 -0000
@@ -0,0 +1,40 @@
+/* Basic struct timeval utilities.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of the libiberty library.
+Libiberty is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public
+License as published by the Free Software Foundation; either
+version 2 of the License, or (at your option) any later version.
+
+Libiberty is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with libiberty; see the file COPYING.LIB.  If not,
+write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
+
+#ifndef TIMEVAL_UTILS_H
+#define TIMEVAL_UTILS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* forward decl */
+struct timeval;
+
+extern void timeval_add (struct timeval *result,
+                        const struct timeval *a, const struct timeval *b);
+
+extern void timeval_sub (struct timeval *result,
+                        const struct timeval *a, const struct timeval *b);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* TIMEVAL_UTILS_H */
Index: libiberty/Makefile.in
===================================================================
RCS file: /cvs/src/src/libiberty/Makefile.in,v
retrieving revision 1.107
diff -u -p -r1.107 Makefile.in
--- libiberty/Makefile.in       22 Jul 2011 14:37:58 -0000      1.107
+++ libiberty/Makefile.in       20 Sep 2011 00:46:44 -0000
@@ -152,7 +152,7 @@ CFILES = alloca.c argv.c asprintf.c atex
         strcasecmp.c strchr.c strdup.c strerror.c strncasecmp.c        \
         strncmp.c strrchr.c strsignal.c strstr.c strtod.c strtol.c     \
         strtoul.c strndup.c strverscmp.c                               \
-       tmpnam.c                                                        \
+       timeval-utils.c tmpnam.c                                        \
        unlink-if-ordinary.c                                            \
        vasprintf.c vfork.c vfprintf.c vprintf.c vsnprintf.c vsprintf.c \
        waitpid.c                                                       \
@@ -184,8 +184,8 @@ REQUIRED_OFILES =                                           
        \
        ./simple-object-elf.$(objext) ./simple-object-mach-o.$(objext)  \
        ./sort.$(objext) ./spaces.$(objext)                             \
        ./splay-tree.$(objext) ./stack-limit.$(objext)                  \
-       ./strerror.$(objext)                                            \
-       ./strsignal.$(objext) ./unlink-if-ordinary.$(objext)            \
+       ./strerror.$(objext) ./strsignal.$(objext)                      \
+       ./timeval-utils.$(objext) ./unlink-if-ordinary.$(objext)        \
        ./xatexit.$(objext) ./xexit.$(objext) ./xmalloc.$(objext)       \
        ./xmemdup.$(objext) ./xstrdup.$(objext) ./xstrerror.$(objext)   \
        ./xstrndup.$(objext)
@@ -235,7 +235,8 @@ INSTALLED_HEADERS =                     
        $(INCDIR)/partition.h                                           \
        $(INCDIR)/safe-ctype.h                                          \
        $(INCDIR)/sort.h                                                \
-       $(INCDIR)/splay-tree.h
+       $(INCDIR)/splay-tree.h \
+       $(INCDIR)/timeval-utils.h
 
 $(TARGETLIB): $(REQUIRED_OFILES) $(EXTRA_OFILES) $(LIBOBJS)
        -rm -f $(TARGETLIB) pic/$(TARGETLIB)
@@ -1141,6 +1142,13 @@ $(CONFIGURED_OFILES): stamp-picdir
        else true; fi
        $(COMPILE.c) $(srcdir)/strverscmp.c $(OUTPUT_OPTION)
 
+./timeval-utils.$(objext): $(srcdir)/timeval-utils.c config.h \
+       $(INCDIR)/timeval-utils.h
+       if [ x"$(PICFLAG)" != x ]; then \
+         $(COMPILE.c) $(PICFLAG) $(srcdir)/timeval-utils.c -o pic/$@; \
+       else true; fi
+       $(COMPILE.c) $(srcdir)/timeval-utils.c $(OUTPUT_OPTION)
+
 ./tmpnam.$(objext): $(srcdir)/tmpnam.c
        if [ x"$(PICFLAG)" != x ]; then \
          $(COMPILE.c) $(PICFLAG) $(srcdir)/tmpnam.c -o pic/$@; \
Index: libiberty/timeval-utils.c
===================================================================
RCS file: libiberty/timeval-utils.c
diff -N libiberty/timeval-utils.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ libiberty/timeval-utils.c   20 Sep 2011 00:46:44 -0000
@@ -0,0 +1,87 @@
+/* Basic struct timeval utilities.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+This file is part of the libiberty library.
+Libiberty is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public
+License as published by the Free Software Foundation; either
+version 2 of the License, or (at your option) any later version.
+
+Libiberty is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with libiberty; see the file COPYING.LIB.  If not,
+write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
+
+#include "config.h"
+
+/* On some systems (such as WindISS), you must include <sys/types.h>
+   to get the definition of "time_t" before you include <time.h>.  */
+#include <sys/types.h>
+
+#ifdef TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  ifdef HAVE_TIME_H
+#   include <time.h>
+#  endif
+# endif
+#endif
+
+#include "timeval-utils.h"
+
+/* 
+
+@deftypefn Extension void timeval_add (struct timeval *@var{a}, @
+  struct timeval *@var{b}, struct timeval *@var{result})
+
+Adds @var{a} to @var{b} and stores the result in @var{result}.
+
+@end deftypefn
+
+*/ 
+
+void
+timeval_add (struct timeval *result,
+            const struct timeval *a, const struct timeval *b)
+{
+  result->tv_sec = a->tv_sec + b->tv_sec;
+  result->tv_usec = a->tv_usec + b->tv_usec;
+  if (result->tv_usec >= 1000000)
+    {
+      ++result->tv_sec;
+      result->tv_usec -= 1000000;
+    }
+}
+
+/* 
+
+@deftypefn Extension void timeval_sub (struct timeval *@var{a}, @
+  struct timeval *@var{b}, struct timeval *@var{result})
+
+Subtracts @var{b} from @var{a} and stores the result in @var{result}.
+
+@end deftypefn
+
+*/ 
+
+void
+timeval_sub (struct timeval *result,
+            const struct timeval *a, const struct timeval *b)
+{
+  result->tv_sec = a->tv_sec - b->tv_sec;
+  result->tv_usec = a->tv_usec - b->tv_usec;
+  if (result->tv_usec < 0)
+    {
+      --result->tv_sec;
+      result->tv_usec += 1000000;
+    }
+}

Reply via email to