diff mbox

[kvm-unit-tests,23/32] lib: x86: store free pages in ascending order

Message ID 20170421005004.137260-24-dmatlack@google.com (mailing list archive)
State New, archived
Headers show

Commit Message

David Matlack April 21, 2017, 12:49 a.m. UTC
From: Peter Feiner <pfeiner@google.com>

Makes allocation of contiguous physical pages simpler.

Signed-off-by: Peter Feiner <pfeiner@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
---
 lib/x86/vm.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
diff mbox

Patch

diff --git a/lib/x86/vm.c b/lib/x86/vm.c
index cda4c5f46da3..8bd0c88e141e 100644
--- a/lib/x86/vm.c
+++ b/lib/x86/vm.c
@@ -7,12 +7,29 @@  static void *vfree_top = 0;
 
 static void free_memory(void *mem, unsigned long size)
 {
-    while (size >= PAGE_SIZE) {
-	*(void **)mem = free;
+	void *end;
+
+	assert_msg((unsigned long) mem % PAGE_SIZE == 0,
+		   "mem not page aligned: %p", mem);
+
+	assert_msg(size % PAGE_SIZE == 0, "size not page aligned: 0x%lx", size);
+
+	assert_msg(size == 0 || mem + size > mem,
+		   "mem + size overflow: %p + 0x%lx", mem, size);
+
+	if (size == 0) {
+		free = NULL;
+		return;
+	}
+
 	free = mem;
-	mem += PAGE_SIZE;
-	size -= PAGE_SIZE;
-    }
+	end = mem + size;
+	while (mem + PAGE_SIZE != end) {
+		*(void **)mem = (mem + PAGE_SIZE);
+		mem += PAGE_SIZE;
+	}
+
+	*(void **)mem = NULL;
 }
 
 void *alloc_page()