Skip to contentSkip to conversation

dtc: Merge refs and labels into single "markers" list

PATCH Applied David Gibson 2 participants 3 messages Nov 22, 2007 – Nov 26, 2007

lore
Share & tools
lore
#0 lore raw

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 messy. 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.

---

Applies after my patch with the new checking/fixup infrastructure.

 checks.c     |   22 +++++-------
 data.c       |  101 ++++++++++++++++-------------------------------------------
 dtc-parser.y |   11 +++---
 dtc.h        |   24 +++++++++-----
 flattree.c   |   11 +++---
 treesource.c |   62 ++++++++++++++++++------------------
 6 files changed, 98 insertions(+), 133 deletions(-)
checks.c +9 -13
Index: dtc/checks.c
===================================================================
@@ -224,33 +224,29 @@
 static void fixup_references(struct check *c, struct node *dt,
 			     struct node *node, struct property *prop)
 {
-      struct fixup *f = prop->val.refs;
+      struct marker *m = prop->val.markers;
       struct node *refnode;
       cell_t phandle;
 
-      while (f) {
-	      if (f->ref[0] == '/') {
+      for_each_marker_of_type(m, REF_PHANDLE) {
+	      if (m->ref[0] == '/') {
 		      /* Reference to full path */
-		      refnode = get_node_by_path(dt, f->ref);
+		      refnode = get_node_by_path(dt, m->ref);
 		      if (! refnode)
 			      FAIL(c, "Reference to non-existent node \"%s\"\n",
-				   f->ref);
+				   m->ref);
 	      } else {
-		      refnode = get_node_by_label(dt, f->ref);
+		      refnode = get_node_by_label(dt, m->ref);
 		      if (! refnode)
 			      FAIL(c, "Reference to non-existent node label \"%s\"\n",
-				   f->ref);
+				   m->ref);
 	      }
 
 	      phandle = get_node_phandle(dt, refnode);
 
-	      assert(f->offset + sizeof(cell_t) <= prop->val.len);
+	      assert(m->offset + sizeof(cell_t) <= prop->val.len);
 
-	      *((cell_t *)(prop->val.val + f->offset)) = cpu_to_be32(phandle);
-
-              prop->val.refs = f->next;
-              fixup_free(f);
-              f = prop->val.refs;
+	      *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
       }
 }
 CHECK(references, NULL, NULL, fixup_references, NULL, ERROR,
data.c +28 -73

135 lines not on this page · raw message

dtc-parser.y +6 -5
Index: dtc/dtc-parser.y
===================================================================
@@ -194,7 +194,7 @@
 		}
 	| propdata DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
@@ -209,7 +209,7 @@
 		}
 	| propdataprefix DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
@@ -224,11 +224,12 @@
 		}
 	| celllist DT_REF
 		{
-			$$ = data_append_cell(data_add_fixup($1, $2), -1);
+			$$ = data_append_cell(data_add_marker($1, REF_PHANDLE,
+							      $2), -1);
 		}
 	| celllist DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
@@ -262,7 +263,7 @@
 		}
 	| bytestring DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
dtc.h +17 -7
Index: dtc/dtc.h
===================================================================
@@ -101,23 +101,34 @@
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
 /* Data blobs */
-struct fixup {
+enum markertype {
+	REF_PHANDLE,
+	LABEL,
+};
+
+struct  marker {
+	enum markertype type;
 	int offset;
 	char *ref;
-	struct fixup *next;
+	struct marker *next;
 };
 
 struct data {
 	int len;
 	char *val;
 	int asize;
-	struct fixup *refs;
-	struct fixup *labels;
+	struct marker *markers;
 };
 
+
 #define empty_data ((struct data){ /* all .members = 0 or NULL */ })
 
-void fixup_free(struct fixup *f);
+#define for_each_marker(m) \
+	for (; (m); (m) = (m)->next)
+#define for_each_marker_of_type(m, t) \
+	for_each_marker(m) \
+		if ((m)->type == (t))
+
 void data_free(struct data d);
 
 struct data data_grow_for(struct data d, int xlen);
@@ -135,8 +146,7 @@
 struct data data_append_zeroes(struct data d, int len);
 struct data data_append_align(struct data d, int align);
 
-struct data data_add_fixup(struct data d, char *ref);
-struct data data_add_label(struct data d, char *label);
+struct data data_add_marker(struct data d, enum markertype type, char *ref);
 
 int data_is_one_string(struct data d);
 
flattree.c +6 -5
Index: dtc/flattree.c
===================================================================
@@ -162,12 +162,13 @@
 {
 	FILE *f = e;
 	int off = 0;
-	struct fixup *l;
+	struct marker *m;
 
-	l = d.labels;
-	while (l) {
-		emit_offset_label(f, l->ref, l->offset);
-		l = l->next;
+	m = d.markers;
+	while (m) {
+		if (m->type == LABEL)
+			emit_offset_label(f, m->ref, m->offset);
+		m = m->next;
 	}
 
 	while ((d.len - off) >= sizeof(u32)) {
treesource.c +32 -30

123 lines not on this page · raw message

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#1 lore raw

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.

---

Updated to apply on top of the new version of the checking infrastructure patch.

 checks.c     |   20 ++++-------
 data.c       |  101 ++++++++++++++++-------------------------------------------
 dtc-parser.y |   11 +++---
 dtc.h        |   24 +++++++++-----
 flattree.c   |   11 +++---
 treesource.c |   62 ++++++++++++++++++------------------
 6 files changed, 97 insertions(+), 132 deletions(-)
checks.c +8 -12
Index: dtc/checks.c
===================================================================
@@ -243,26 +243,22 @@
 static void fixup_phandle_references(struct check *c, struct node *dt,
 				     struct node *node, struct property *prop)
 {
-      struct fixup *f = prop->val.refs;
+      struct marker *m = prop->val.markers;
       struct node *refnode;
       cell_t phandle;
 
-      while (f) {
-	      assert(f->offset + sizeof(cell_t) <= prop->val.len);
+      for_each_marker_of_type(m, REF_PHANDLE) {
+	      assert(m->offset + sizeof(cell_t) <= prop->val.len);
 
-	      refnode = get_node_by_ref(dt, f->ref);
+	      refnode = get_node_by_ref(dt, m->ref);
 	      if (! refnode) {
 		      FAIL(c, "Reference to non-existent node or label \"%s\"\n",
-			   f->ref);
-	      } else {
-		      phandle = get_node_phandle(dt, refnode);
-
-		      *((cell_t *)(prop->val.val + f->offset)) = cpu_to_be32(phandle);
+			   m->ref);
+		      continue;
 	      }
 
-              prop->val.refs = f->next;
-              fixup_free(f);
-              f = prop->val.refs;
+	      phandle = get_node_phandle(dt, refnode);
+	      *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
       }
 }
 CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
data.c +28 -73

135 lines not on this page · raw message

dtc-parser.y +6 -5
Index: dtc/dtc-parser.y
===================================================================
@@ -194,7 +194,7 @@
 		}
 	| propdata DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
@@ -209,7 +209,7 @@
 		}
 	| propdataprefix DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
@@ -224,11 +224,12 @@
 		}
 	| celllist DT_REF
 		{
-			$$ = data_append_cell(data_add_fixup($1, $2), -1);
+			$$ = data_append_cell(data_add_marker($1, REF_PHANDLE,
+							      $2), -1);
 		}
 	| celllist DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
@@ -262,7 +263,7 @@
 		}
 	| bytestring DT_LABEL
 		{
-			$$ = data_add_label($1, $2);
+			$$ = data_add_marker($1, LABEL, $2);
 		}
 	;
 
dtc.h +17 -7
Index: dtc/dtc.h
===================================================================
@@ -101,23 +101,34 @@
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
 /* Data blobs */
-struct fixup {
+enum markertype {
+	REF_PHANDLE,
+	LABEL,
+};
+
+struct  marker {
+	enum markertype type;
 	int offset;
 	char *ref;
-	struct fixup *next;
+	struct marker *next;
 };
 
 struct data {
 	int len;
 	char *val;
 	int asize;
-	struct fixup *refs;
-	struct fixup *labels;
+	struct marker *markers;
 };
 
+
 #define empty_data ((struct data){ /* all .members = 0 or NULL */ })
 
-void fixup_free(struct fixup *f);
+#define for_each_marker(m) \
+	for (; (m); (m) = (m)->next)
+#define for_each_marker_of_type(m, t) \
+	for_each_marker(m) \
+		if ((m)->type == (t))
+
 void data_free(struct data d);
 
 struct data data_grow_for(struct data d, int xlen);
@@ -135,8 +146,7 @@
 struct data data_append_zeroes(struct data d, int len);
 struct data data_append_align(struct data d, int align);
 
-struct data data_add_fixup(struct data d, char *ref);
-struct data data_add_label(struct data d, char *label);
+struct data data_add_marker(struct data d, enum markertype type, char *ref);
 
 int data_is_one_string(struct data d);
 
flattree.c +6 -5
Index: dtc/flattree.c
===================================================================
@@ -162,12 +162,13 @@
 {
 	FILE *f = e;
 	int off = 0;
-	struct fixup *l;
+	struct marker *m;
 
-	l = d.labels;
-	while (l) {
-		emit_offset_label(f, l->ref, l->offset);
-		l = l->next;
+	m = d.markers;
+	while (m) {
+		if (m->type == LABEL)
+			emit_offset_label(f, m->ref, m->offset);
+		m = m->next;
 	}
 
 	while ((d.len - off) >= sizeof(u32)) {
treesource.c +32 -30

123 lines not on this page · raw message

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Applied
#2 lore raw
In reply to David Gibson

So, like, the other day David Gibson mumbled:

Quoted 14 lines

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 messy. 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.

Quoted 4 lines

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

Applied.

Thanks,
jdl