Skip to contentSkip to conversation

for_each_zone

PATCH New Robert Love 1 participant 2 messages Jul 20, 2002

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 for_each_zone

Attached patch implements for_each_zone(zont_t *) which is a helper macro to cleanup code of the form:

	for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next) { 
		for (i = 0; i < MAX_NR_ZONES; ++i) { 
			zone_t * z = pgdat->node_zones + i; 
			/* ... */ 
		} 
	} 

and replace it with:

	for_each_zone(zone) {
		/* ... */
	}

This patch only replaces one use of the above loop with the new macro. Pending code, however, currently in the full rmap patch uses for_each_zone more extensively.

This is from Rik's 2.4-rmap patch and by William Irwin.

This patch requires the previous for_each_pgdat patch, but only to apply cleanly.

Robert Love

include/linux/mmzone.h +37 -0
diff -urN linux-2.5.27/include/linux/mmzone.h linux/include/linux/mmzone.h
@@ -163,6 +163,43 @@
 
 extern pg_data_t contig_page_data;
 
+/*
+ * next_zone - helper magic for for_each_zone()
+ * Thanks to William Lee Irwin III for this piece of ingenuity.
+ */
+static inline zone_t * next_zone(zone_t * zone)
+{
+	pg_data_t *pgdat = zone->zone_pgdat;
+
+	if (zone - pgdat->node_zones < MAX_NR_ZONES - 1)
+		zone++;
+	else if (pgdat->node_next) {
+		pgdat = pgdat->node_next;
+		zone = pgdat->node_zones;
+	} else
+		zone = NULL;
+
+	return zone;
+}
+
+/**
+ * for_each_zone - helper macro to iterate over all memory zones
+ * @zone - pointer to zone_t variable
+ *
+ * The user only needs to declare the zone variable, for_each_zone
+ * fills it in. This basically means for_each_zone() is an
+ * easier to read version of this piece of code:
+ *
+ * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
+ * 	for (i = 0; i < MAX_NR_ZONES; ++i) {
+ * 		zone_t * z = pgdat->node_zones + i;
+ * 		...
+ * 	}
+ * }
+ */
+#define for_each_zone(zone) \
+	for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
+
 #ifndef CONFIG_DISCONTIGMEM
 
 #define NODE_DATA(nid)		(&contig_page_data)
mm/page_alloc.c +5 -5
diff -urN linux-2.5.27/mm/page_alloc.c linux/mm/page_alloc.c
@@ -477,12 +477,12 @@
  */
 unsigned int nr_free_pages(void)
 {
-	unsigned int i, sum = 0;
-	pg_data_t *pgdat;
+	unsigned int sum;
+	zone_t *zone;
 
-	for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
-		for (i = 0; i < MAX_NR_ZONES; ++i)
-			sum += pgdat->node_zones[i].free_pages;
+	sum = 0;
+	for_each_zone(zone)
+		sum += zone->free_pages;
 
 	return sum;
 }
#1 lore raw

PATCH 1/1 for_each_zone, updated

Quoted 2 lines · Robert Love

Attached patch implements for_each_zone(zont_t *) which is a helper macro to cleanup code of the form:

Attached patch implements for_each_zone, now using Martin Bligh's newly renamed pgdat_next.

This patch applies on top of the updated for_each_pgdat patch.

Please, apply.

Robert Love

include/linux/mmzone.h +37 -0
diff -urN linux-2.5.27-rml/include/linux/mmzone.h linux/include/linux/mmzone.h
@@ -177,6 +177,43 @@
 #define for_each_pgdat(pgdat) \
 	for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
 
+/*
+ * next_zone - helper magic for for_each_zone()
+ * Thanks to William Lee Irwin III for this piece of ingenuity.
+ */
+static inline zone_t * next_zone(zone_t * zone)
+{
+	pg_data_t *pgdat = zone->zone_pgdat;
+
+	if (zone - pgdat->node_zones < MAX_NR_ZONES - 1)
+		zone++;
+	else if (pgdat->pgdat_next) {
+		pgdat = pgdat->pgdat_next;
+		zone = pgdat->node_zones;
+	} else
+		zone = NULL;
+
+	return zone;
+}
+
+/**
+ * for_each_zone - helper macro to iterate over all memory zones
+ * @zone - pointer to zone_t variable
+ *
+ * The user only needs to declare the zone variable, for_each_zone
+ * fills it in. This basically means for_each_zone() is an
+ * easier to read version of this piece of code:
+ *
+ * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
+ * 	for (i = 0; i < MAX_NR_ZONES; ++i) {
+ * 		zone_t * z = pgdat->node_zones + i;
+ * 		...
+ * 	}
+ * }
+ */
+#define for_each_zone(zone) \
+	for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
+
 #ifndef CONFIG_DISCONTIGMEM
 
 #define NODE_DATA(nid)		(&contig_page_data)
mm/page_alloc.c +5 -5
diff -urN linux-2.5.27-rml/mm/page_alloc.c linux/mm/page_alloc.c
@@ -477,12 +477,12 @@
  */
 unsigned int nr_free_pages(void)
 {
-	unsigned int i, sum = 0;
-	pg_data_t *pgdat;
+	unsigned int sum;
+	zone_t *zone;
 
-	for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
-		for (i = 0; i < MAX_NR_ZONES; ++i)
-			sum += pgdat->node_zones[i].free_pages;
+	sum = 0;
+	for_each_zone(zone)
+		sum += zone->free_pages;
 
 	return sum;
 }