Re: [Mesa-dev] [PATCH 8/9] r600g/compute: add util functions to add and remove items from lists
On Fri, Jun 13, 2014 at 10:35:37PM +0200, Bruno Jiménez wrote: --- src/gallium/drivers/r600/compute_memory_pool.c | 158 - 1 file changed, 78 insertions(+), 80 deletions(-) There are double linked list helper functions in gallium util, which we should use rather than writing our own. See auxiliary/util/u_double_list.h I would recommend leaving these wrappers, but replacing the implementation with the gallium helpers. Then when you are done, you can remove any wrappers which map 1 to 1 to a gallium helper. -Tom 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; - +
[Mesa-dev] [PATCH 8/9] r600g/compute: add util functions to add and remove items from lists
--- 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; -