simplify list rotation code
PATCH 1/1 simplify list rotation code
After the other parts of my patch were vetoed against, how about this one?
Instead of counting the list length, it simply rotates by up to 64 elements. This is O(1) instead of O(n) and makes the code nicer to read.
Jörn
Signature · 136 lines
When I am working on a problem I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong. -- R. Buckminster Fuller Signed-off-by: Jörn Engel <[email protected]> --- fs/jffs2/scan.c | 104 +++++++------------------------------------------------- 1 files changed, 13 insertions(+), 91 deletions(-) --- linux-2.6.11cow/fs/jffs2/scan.c~jffs2_rotate 2005-03-22 23:29:29.000000000 +0100 +++ linux-2.6.11cow/fs/jffs2/scan.c 2005-03-25 16:23:30.000000000 +0100 @@ -809,108 +809,30 @@ static int jffs2_scan_dirent_node(struct return 0; } -static int count_list(struct list_head *l) -{ - uint32_t count = 0; - struct list_head *tmp; - list_for_each(tmp, l) { - count++; - } - return count; -} - -/* Note: This breaks if list_empty(head). I don't care. You - might, if you copy this code and use it elsewhere :) */ static void rotate_list(struct list_head *head, uint32_t count) { struct list_head *n = head->next; + if (list_empty(head)) + return; + list_del(head); - while(count--) { + while (count--) n = n->next; - } + list_add(head, n); } + void jffs2_rotate_lists(struct jffs2_sb_info *c) { - uint32_t x; - uint32_t rotateby; - - x = count_list(&c->clean_list); - if (x) { - rotateby = pseudo_random % x; - D1(printk(KERN_DEBUG "Rotating clean_list by %d\n", rotateby)); - - rotate_list((&c->clean_list), rotateby); - - D1(printk(KERN_DEBUG "Erase block at front of clean_list is at %08x\n", - list_entry(c->clean_list.next, struct jffs2_eraseblock, list)->offset)); - } else { - D1(printk(KERN_DEBUG "Not rotating empty clean_list\n")); - } - - x = count_list(&c->very_dirty_list); - if (x) { - rotateby = pseudo_random % x; - D1(printk(KERN_DEBUG "Rotating very_dirty_list by %d\n", rotateby)); - - rotate_list((&c->very_dirty_list), rotateby); - - D1(printk(KERN_DEBUG "Erase block at front of very_dirty_list is at %08x\n", - list_entry(c->very_dirty_list.next, struct jffs2_eraseblock, list)->offset)); - } else { - D1(printk(KERN_DEBUG "Not rotating empty very_dirty_list\n")); - } - - x = count_list(&c->dirty_list); - if (x) { - rotateby = pseudo_random % x; - D1(printk(KERN_DEBUG "Rotating dirty_list by %d\n", rotateby)); - - rotate_list((&c->dirty_list), rotateby); - - D1(printk(KERN_DEBUG "Erase block at front of dirty_list is at %08x\n", - list_entry(c->dirty_list.next, struct jffs2_eraseblock, list)->offset)); - } else { - D1(printk(KERN_DEBUG "Not rotating empty dirty_list\n")); - } - - x = count_list(&c->erasable_list); - if (x) { - rotateby = pseudo_random % x; - D1(printk(KERN_DEBUG "Rotating erasable_list by %d\n", rotateby)); - - rotate_list((&c->erasable_list), rotateby); - - D1(printk(KERN_DEBUG "Erase block at front of erasable_list is at %08x\n", - list_entry(c->erasable_list.next, struct jffs2_eraseblock, list)->offset)); - } else { - D1(printk(KERN_DEBUG "Not rotating empty erasable_list\n")); - } - - if (c->nr_erasing_blocks) { - rotateby = pseudo_random % c->nr_erasing_blocks; - D1(printk(KERN_DEBUG "Rotating erase_pending_list by %d\n", rotateby)); - - rotate_list((&c->erase_pending_list), rotateby); - - D1(printk(KERN_DEBUG "Erase block at front of erase_pending_list is at %08x\n", - list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list)->offset)); - } else { - D1(printk(KERN_DEBUG "Not rotating empty erase_pending_list\n")); - } - - if (c->nr_free_blocks) { - rotateby = pseudo_random % c->nr_free_blocks; - D1(printk(KERN_DEBUG "Rotating free_list by %d\n", rotateby)); - - rotate_list((&c->free_list), rotateby); + uint32_t rotateby = pseudo_random % 64; - D1(printk(KERN_DEBUG "Erase block at front of free_list is at %08x\n", - list_entry(c->free_list.next, struct jffs2_eraseblock, list)->offset)); - } else { - D1(printk(KERN_DEBUG "Not rotating empty free_list\n")); - } + rotate_list((&c->clean_list), rotateby); + rotate_list((&c->very_dirty_list), rotateby); + rotate_list((&c->dirty_list), rotateby); + rotate_list((&c->erasable_list), rotateby); + rotate_list((&c->erase_pending_list), rotateby); + rotate_list((&c->free_list), rotateby); }