[cairo] Better coverage from cairo performance suite (and some results)

Daniel Amelang daniel.amelang at gmail.com
Fri Oct 20 16:52:44 PDT 2006


> On Thu, 19 Oct 2006 13:49:03 -0700, Ian Osgood wrote:
> > Very, very cool. But it doesn't build for me on Mac OS X. This system
> > has neither strndup() nor getline().

On 10/19/06, Carl Worth <cworth at cworth.org> wrote:
> Patches anyone?

Sure. I've been looking for a chance to ease into contributing, so
here goes. Notice that the getline implementation is not complete, but
it is sufficient for our purposes. I figured that if/when the need
arises, someone else can flesh it out. To that end, I added a check in
the body of getline to raise an error if getline is ever used in a way
that this simple implementation doesn't support.

The strndup implementation is complete, IMO. I've hammered it a good bit.

Any resemblance to other implementations of these functions in other
libraries (in glibc, newlib, etc) is purely coincidental. I wrote
these 'blind'. Heh...given the result, you might not be surprised :)

Dan
-------------- next part --------------
From: Dan Amelang <dan at amelang.net>
Date: Fri Oct 20 16:11:27 2006 -0700
Subject: [PATCH] Provide watered-down but sufficient implementations of getline and strndup to allow for building cairo-perf-diff on platforms without GNU extensions.

---

 perf/cairo-perf-diff.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 56 insertions(+), 3 deletions(-)

21a714f37cc4707cd9c2a42b5ffc925a595fe1bd
diff --git a/perf/cairo-perf-diff.c b/perf/cairo-perf-diff.c
index eb2ef9f..ead722b 100644
--- a/perf/cairo-perf-diff.c
+++ b/perf/cairo-perf-diff.c
@@ -23,11 +23,10 @@
  * SOFTWARE.
  *
  * Authors: Carl Worth <cworth at cworth.org>
+ *          Dan Amelang <dan at amelang.net>
  */
 
-/* We use _GNU_SOURCE for getline. If someone wants to avoid that
- * dependence they could conditionally provide a custom implementation
- * of getline instead. */
+/* We use _GNU_SOURCE for getline and strndup. */
 #ifndef _GNU_SOURCE
 # define _GNU_SOURCE
 #endif
@@ -38,6 +37,60 @@
 #include <ctype.h>
 #include <math.h>
 
+/* We conditionally provide a custom implementation of getline and strndup
+ * as needed. These aren't necessary full-fledged general purpose
+ * implementations. They just get the job done for our purposes.
+ */
+#ifndef __USE_GNU
+
+#define POORMANS_GETLINE_BUFFER_SIZE (65536)
+ssize_t
+getline (char **lineptr, size_t *n, FILE *stream)
+{
+    if (!*lineptr)
+    {
+        *n = POORMANS_GETLINE_BUFFER_SIZE;
+        *lineptr = (char *) malloc (*n);
+    }
+
+    if (!fgets (*lineptr, *n, stream))
+        return -1;
+
+    if (!feof (stream) && !strchr (*lineptr, '\n'))
+    {
+        fprintf (stderr, "The poor man's implementation of getline in "
+                          __FILE__ " needs a bigger buffer. Perhaps it's "
+                         "time for a complete implementation of getline.\n");
+        exit (0);
+    }
+
+    return strlen (*lineptr);
+}
+#undef POORMANS_GETLINE_BUFFER_SIZE
+
+char *
+strndup (const char *s, size_t n)
+{
+    size_t len;
+    char *sdup;
+
+    if (!s)
+        return NULL;
+
+    len = strlen (s);
+    len = (n < len ? n : len);
+    sdup = (char *) malloc (len + 1);
+    if (sdup)
+    {
+        memcpy (sdup, s, len);
+        sdup[len] = '\0';
+    }
+
+    return sdup;
+}
+
+#endif
+
 typedef struct _test_report {
     int id;
     char *backend;
-- 
1.2.6


More information about the cairo mailing list