[Mesa-dev] [PATCH 8/9] r600g/compute: add util functions to add and remove items from lists

Bruno Jiménez brunojimen at gmail.com
Fri Jun 13 13:35:37 PDT 2014


---
 src/gallium/drivers/r600/compute_memory_pool.c | 158 ++++++++++++-------------
 1 file changed, 78 insertions(+), 80 deletions(-)

diff --git a/src/gallium/drivers/r600/compute_memory_pool.c b/src/gallium/drivers/r600/compute_memory_pool.c
index 624b50d..26b9f98 100644
--- a/src/gallium/drivers/r600/compute_memory_pool.c
+++ b/src/gallium/drivers/r600/compute_memory_pool.c
@@ -43,6 +43,73 @@
 #include <inttypes.h>
 
 #define ITEM_ALIGNMENT 1024
+
+static inline void list_add_item_front(struct compute_memory_item **list,
+    struct compute_memory_item *item)
+{
+	if (*list != NULL) {
+		item->next = *list;
+		(*list)->prev = item;
+	}
+	else {
+		item->next = NULL;
+	}
+
+	*list = item;
+	item->prev = NULL;
+}
+
+static inline void list_add_item_tail(struct compute_memory_item **list,
+    struct compute_memory_item *item)
+{
+	struct compute_memory_item *last_item = NULL;
+
+	if (*list != NULL) {
+		for (last_item = *list; last_item->next != NULL; last_item = last_item->next);
+
+		last_item->next = item;
+		item->prev = last_item;
+	}
+	else {
+		*list = item;
+		item->prev = NULL;
+	}
+
+	item->next = NULL;
+}
+
+static inline void list_add_item_after(struct compute_memory_item **list,
+		struct compute_memory_item *item, struct compute_memory_item *pos)
+{
+	if (pos == NULL) {
+		list_add_item_front(list, item);
+	}
+	else {
+		if (pos->next != NULL) {
+			pos->next->prev = item;
+		}
+
+		item->prev = pos;
+		item->next = pos->next;
+		pos->next = item;
+	}
+}
+
+static inline void list_remove_item(struct compute_memory_item **list,
+		struct compute_memory_item *item)
+{
+	if (item->prev == NULL) {
+		*list = item->next;
+	}
+	else {
+		item->prev->next = item->next;
+	}
+
+	if (item->next != NULL) {
+		item->next->prev = item->prev;
+	}
+}
+
 /**
  * Creates a new pool
  */
@@ -299,6 +366,7 @@ int compute_memory_promote_item(struct compute_memory_pool *pool,
 		struct compute_memory_item *item, struct pipe_context *pipe,
 		int64_t allocated)
 {
+	struct compute_memory_item *pos;
 	int64_t start_in_dw;
 	int err = 0;
 
@@ -327,40 +395,12 @@ int compute_memory_promote_item(struct compute_memory_pool *pool,
 			item->size_in_dw, item->size_in_dw * 4);
 
 	/* Remove the item from the unallocated list */
-	if (item->prev == NULL)
-		pool->unallocated_list = item->next;
-	else
-		item->prev->next = item->next;
-
-	if (item->next != NULL)
-		item->next->prev = item->prev;
+	list_remove_item(&pool->unallocated_list, item);
 
+	/* Add it back to the item_list */
+	pos = compute_memory_postalloc_chunk(pool, start_in_dw);
+	list_add_item_after(&pool->item_list, item, pos);
 	item->start_in_dw = start_in_dw;
-	item->next = NULL;
-	item->prev = NULL;
-
-	if (pool->item_list) {
-		struct compute_memory_item *pos;
-
-		pos = compute_memory_postalloc_chunk(pool, start_in_dw);
-		if (pos) {
-			item->prev = pos;
-			item->next = pos->next;
-			pos->next = item;
-			if (item->next) {
-				item->next->prev = item;
-			}
-		} else {
-			/* Add item to the front of the list */
-			item->next = pool->item_list;
-			item->prev = pool->item_list->prev;
-			pool->item_list->prev = item;
-			pool->item_list = item;
-		}
-	}
-	else {
-		pool->item_list = item;
-	}
 
 	((struct r600_context *)pipe)->b.b.resource_copy_region(pipe,
 		(struct pipe_resource *)pool->bo,
@@ -387,26 +427,11 @@ void compute_memory_demote_item(struct compute_memory_pool *pool,
 	struct compute_memory_item *item, struct pipe_context *pipe)
 {
 	/* First, we remove the item from the item_list */
-	if (item->prev == NULL)
-		pool->item_list = item->next;
-	else
-		item->prev->next = item->next;
-
-	if (item->next != NULL)
-		item->next->prev = item->prev;
-
+	list_remove_item(&pool->item_list, item);
 
 	/* Now we add it to the beginning of the unallocated list
 	 * NOTE: we could also add it to the end, but this is easier */
-	item->next = NULL;
-	item->prev = NULL;
-	if (pool->unallocated_list) {
-		item->next = pool->unallocated_list;
-		item->next->prev = item;
-		pool->unallocated_list = item;
-	}
-	else
-		pool->unallocated_list = item;
+	list_add_item_front(&pool->unallocated_list, item);
 
 	/* We check if the intermediate buffer exists, and if it
 	 * doesn't, we create it again */
@@ -438,16 +463,7 @@ void compute_memory_free(struct compute_memory_pool* pool, int64_t id)
 		next = item->next;
 
 		if (item->id == id) {
-			if (item->prev) {
-				item->prev->next = item->next;
-			}
-			else {
-				pool->item_list = item->next;
-			}
-
-			if (item->next) {
-				item->next->prev = item->prev;
-			}
+			list_remove_item(&pool->item_list, item);
 
 			if (item->real_buffer) {
 				pool->screen->b.b.resource_destroy(
@@ -465,16 +481,7 @@ void compute_memory_free(struct compute_memory_pool* pool, int64_t id)
 		next = item->next;
 
 		if (item->id == id) {
-			if (item->prev) {
-				item->prev->next = item->next;
-			}
-			else {
-				pool->unallocated_list = item->next;
-			}
-
-			if (item->next) {
-				item->next->prev = item->prev;
-			}
+			list_remove_item(&pool->unallocated_list, item);
 
 			if (item->real_buffer) {
 				pool->screen->b.b.resource_destroy(
@@ -501,7 +508,7 @@ struct compute_memory_item* compute_memory_alloc(
 	struct compute_memory_pool* pool,
 	int64_t size_in_dw)
 {
-	struct compute_memory_item *new_item = NULL, *last_item = NULL;
+	struct compute_memory_item *new_item = NULL;
 
 	COMPUTE_DBG(pool->screen, "* compute_memory_alloc() size_in_dw = %ld (%ld bytes)\n",
 			size_in_dw, 4 * size_in_dw);
@@ -518,16 +525,7 @@ struct compute_memory_item* compute_memory_alloc(
 	new_item->real_buffer = (struct r600_resource*)r600_compute_buffer_alloc_vram(
 							pool->screen, size_in_dw * 4);
 
-	if (pool->unallocated_list) {
-		for (last_item = pool->unallocated_list; last_item->next;
-						last_item = last_item->next);
-
-		last_item->next = new_item;
-		new_item->prev = last_item;
-	}
-	else {
-		pool->unallocated_list = new_item;
-	}
+	list_add_item_tail(&pool->unallocated_list, new_item);
 
 	COMPUTE_DBG(pool->screen, "  + Adding item %p id = %u size = %u (%u bytes)\n",
 			new_item, new_item->id, new_item->size_in_dw,
-- 
2.0.0



More information about the mesa-dev mailing list