dtc: Merge refs and labels into single "markers" list (v2)

Currently, every 'data' object, used to represent property values, has
two lists of fixup structures - one for labels and one for references.
Sometimes we want to look at them separately, but other times we need
to consider both types of fixup.

I'm planning to implement string references, where a full path rather
than a phandle is substituted into a property value.  Adding yet
another list of fixups for that would start to get silly.  So, this
patch merges the "refs" and "labels" lists into a single list of
"markers", each of which has a type field indicating if it represents
a label or a phandle reference.  String references or any other new
type of in-data marker will then just need a new type value - merging
data blocks and other common manipulations will just work.

While I was at it I made some cleanups to the handling of fixups which
simplify things further.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
diff --git a/data.c b/data.c
index b6cd00e..46cc081 100644
--- a/data.c
+++ b/data.c
@@ -20,28 +20,16 @@
 
 #include "dtc.h"
 
-void fixup_free(struct fixup *f)
-{
-	free(f->ref);
-	free(f);
-}
-
 void data_free(struct data d)
 {
-	struct fixup *f, *nf;
+	struct marker *m, *nm;
 
-	f = d.refs;
-	while (f) {
-		nf = f->next;
-		fixup_free(f);
-		f = nf;
-	}
-
-	f = d.labels;
-	while (f) {
-		nf = f->next;
-		fixup_free(f);
-		f = nf;
+	m = d.markers;
+	while (m) {
+		nm = m->next;
+		free(m->ref);
+		free(m);
+		m = nm;
 	}
 
 	assert(!d.val || d.asize);
@@ -214,37 +202,29 @@
 	return d;
 }
 
-void fixup_merge(struct fixup **fd, struct fixup **fd2, int d1_len)
+struct data data_append_markers(struct data d, struct marker *m)
 {
-	struct fixup **ff;
-	struct fixup *f, *f2;
+	struct marker **mp = &d.markers;
 
-	/* Extract d2's fixups */
-	f2 = *fd2;
-	*fd2 = NULL;
-
-	/* Tack them onto d's list of fixups */
-	ff = fd;
-	while (*ff)
-		ff = &((*ff)->next);
-	*ff = f2;
-
-	/* And correct them for their new position */
-	for (f = f2; f; f = f->next)
-		f->offset += d1_len;
-
-
+	/* Find the end of the markerlist */
+	while (*mp)
+		mp = &((*mp)->next);
+	*mp = m;
+	return d;
 }
 
 struct data data_merge(struct data d1, struct data d2)
 {
 	struct data d;
+	struct marker *m2 = d2.markers;
 
-	d = data_append_data(d1, d2.val, d2.len);
+	d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
 
-	fixup_merge(&d.refs, &d2.refs, d1.len);
-	fixup_merge(&d.labels, &d2.labels, d1.len);
+	/* Adjust for the length of d1 */
+	for_each_marker(m2)
+		m2->offset += d1.len;
 
+	d2.markers = NULL; /* So data_free() doesn't clobber them */
 	data_free(d2);
 
 	return d;
@@ -294,42 +274,17 @@
 	return data_append_zeroes(d, newlen - d.len);
 }
 
-struct data data_add_fixup(struct data d, char *ref)
+struct data data_add_marker(struct data d, enum markertype type, char *ref)
 {
-	struct fixup *f;
-	struct data nd;
+	struct marker *m;
 
-	f = xmalloc(sizeof(*f));
-	f->offset = d.len;
-	f->ref = ref;
-	f->next = d.refs;
+	m = xmalloc(sizeof(*m));
+	m->offset = d.len;
+	m->type = type;
+	m->ref = ref;
+	m->next = NULL;
 
-	nd = d;
-	nd.refs = f;
-
-	return nd;
-}
-
-struct data data_add_label(struct data d, char *label)
-{
-	struct fixup *f, **p;
-	struct data nd;
-
-	f = xmalloc(sizeof(*f));
-	f->offset = d.len;
-	f->ref = label;
-
-	nd = d;
-	p = &nd.labels;
-
-	/* adding to end keeps them sorted */
-	while (*p)
-		p = &((*p)->next);
-
-	f->next = *p;
-	*p = f;
-
-	return nd;
+	return data_append_markers(d, m);
 }
 
 int data_is_one_string(struct data d)