From: Ben Pfaff Date: Thu, 18 May 2006 19:02:35 +0000 (+0000) Subject: Randomize the bits that bitmap_scan() returns, to make student errors X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=468f548e6848498767eb6ac7b55ab9bca06de462;p=pintos-anon Randomize the bits that bitmap_scan() returns, to make student errors that assume contiguous memory allocation show up. --- diff --git a/src/lib/kernel/bitmap.c b/src/lib/kernel/bitmap.c index d323b89..77edf22 100644 --- a/src/lib/kernel/bitmap.c +++ b/src/lib/kernel/bitmap.c @@ -1,6 +1,7 @@ #include "bitmap.h" #include #include +#include #include #include #include "threads/malloc.h" @@ -289,7 +290,7 @@ bitmap_all (const struct bitmap *b, size_t start, size_t cnt) /* Finding set or unset bits. */ -/* Finds and returns the starting index of the first group of CNT +/* Finds and returns the starting index of a group of CNT consecutive bits in B at or after START that are all set to VALUE. If there is no such group, returns BITMAP_ERROR. */ @@ -302,15 +303,20 @@ bitmap_scan (const struct bitmap *b, size_t start, size_t cnt, bool value) if (cnt <= b->bit_cnt) { size_t last = b->bit_cnt - cnt; - size_t i; - for (i = start; i <= last; i++) - if (!bitmap_contains (b, i, cnt, !value)) - return i; + size_t middle = start + random_ulong () % (last - start + 1); + size_t i = middle; + do + { + if (!bitmap_contains (b, i, cnt, !value)) + return i; + i = i != last ? i + 1 : start; + } + while (i != middle); } return BITMAP_ERROR; } -/* Finds the first group of CNT consecutive bits in B at or after +/* Finds a group of CNT consecutive bits in B at or after START that are all set to VALUE, flips them all to !VALUE, and returns the index of the first bit in the group. If there is no such group, returns BITMAP_ERROR. diff --git a/src/threads/init.c b/src/threads/init.c index 57797be..ef82cb1 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -90,6 +90,9 @@ main (void) argv = read_command_line (); argv = parse_options (argv); + /* Set random seed if parse_options() didn't. */ + random_init (0); + /* Initialize memory system. */ palloc_init (); malloc_init (); @@ -101,9 +104,6 @@ main (void) gdt_init (); #endif - /* Set random seed if parse_options() didn't. */ - random_init (0); - /* Initialize interrupt handlers. */ intr_init (); timer_init ();