@@ -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()