lib.[ch], dvd-sector-copy.c: Publish a couple of generally handy utilities.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 25 Mar 2022 19:56:41 +0000 (19:56 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 25 Mar 2022 19:56:41 +0000 (19:56 +0000)
dvd-sector-copy.c
lib.c
lib.h

index 6f14bb5..fe99587 100644 (file)
@@ -8,30 +8,6 @@ static void usage(FILE *fp)
          prog);
 }
 
-static double tvdiff(const struct timeval *tv_lo,
-                    const struct timeval *tv_hi)
-{
-  return ((tv_hi->tv_sec - tv_lo->tv_sec) +
-         (tv_hi->tv_usec - tv_lo->tv_usec)/1.0e6);
-}
-
-#define DEFVEC(vtype, etype)                                           \
-       typedef struct { etype *v; size_t n, sz; } vtype
-#define VEC_INIT { 0, 0, 0 }
-#define VEC_FREE(vv) do {                                              \
-  free((vv)->v); (vv)->v 0; (vv)->n = (vv)->sz = 0;                    \
-} while (0)
-#define VEC_PUSH(p, vv) do {                                           \
-  size_t _want;                                                                \
-  if ((vv)->n >= (vv)->sz) {                                           \
-    (vv)->sz = (vv)->sz ? 2*(vv)->sz : 32;                             \
-    _want = (vv)->sz*sizeof(*(vv)->v);                                 \
-    (vv)->v = realloc((vv)->v, _want);                                 \
-    if (!(vv)->v) bail("out of memory allocating %zu bytes", _want);   \
-  }                                                                    \
-  (p) = &(vv)->v[(vv)->n++];                                           \
-} while (0)
-
 #define MAXFILES (1 + 2*99 + 1)
 struct file {
   ident id;
diff --git a/lib.c b/lib.c
index 55c4b48..7021661 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -111,6 +111,12 @@ void sit(double t)
   }
 }
 
+double tvdiff(const struct timeval *tv_lo, const struct timeval *tv_hi)
+{
+  return ((tv_hi->tv_sec - tv_lo->tv_sec) +
+         (tv_hi->tv_usec - tv_lo->tv_usec)/1.0e6);
+}
+
 void carefully_write(int fd, const void *buf, size_t sz)
 {
   const unsigned char *p = buf;
diff --git a/lib.h b/lib.h
index ebb13ac..e9abbaa 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -143,6 +143,37 @@ extern PRINTF_LIKE(2, 3) NORETURN
         * exit with status code 2.
         */
 
+/*----- Resizing vectors --------------------------------------------------*/
+
+#define DEFVEC(vtype, etype)                                           \
+       typedef struct { etype *v; size_t n, sz; } vtype
+#define VEC_INIT { 0, 0, 0 }
+       /* Define VTYPE as a (structured) type for vectors holding elements
+        * of ETYPE.
+        *
+        * A vector V has `V.n' elements, addressed as `V.v[0]' up to
+        * `V.v[V.n - 1]'.
+        */
+
+#define VEC_FREE(vv) do {                                              \
+  free((vv)->v); (vv)->v 0; (vv)->n = (vv)->sz = 0;                    \
+} while (0)
+       /* Free the vector VV.  It's safe to free a vector multiple times. */
+
+#define VEC_PUSH(p, vv) do {                                           \
+  size_t _want;                                                                \
+  if ((vv)->n >= (vv)->sz) {                                           \
+    (vv)->sz = (vv)->sz ? 2*(vv)->sz : 32;                             \
+    _want = (vv)->sz*sizeof(*(vv)->v);                                 \
+    (vv)->v = realloc((vv)->v, _want);                                 \
+    if (!(vv)->v) bail("out of memory allocating %zu bytes", _want);   \
+  }                                                                    \
+  (p) = &(vv)->v[(vv)->n++];                                           \
+} while (0)
+       /* Add an initialized element to the end of vector VV, storing its
+        * address in P.
+        */
+
 /*----- Parsing utilities -------------------------------------------------*/
 
 #define PNF_JUNK 1u
@@ -160,6 +191,12 @@ extern long parse_int(const char **p_inout, unsigned f,
 
 /*----- System utilities --------------------------------------------------*/
 
+extern double tvdiff(const struct timeval *tv_lo,
+                    const struct timeval *tv_hi);
+       /* Return the (signed) difference from TV_LO to TV_HI, as a floating-
+        * point number of seconds.
+        */
+
 extern void sit(double t);
        /* Do nothing for T seconds.  As implied by the type, T may be
         * fractional.