|
|
|
@ -7,6 +7,7 @@
|
|
|
|
|
#include <gay/config.h>
|
|
|
|
|
#include <gay/kprintf.h>
|
|
|
|
|
#include <gay/mm.h>
|
|
|
|
|
#include <gay/mutex.h>
|
|
|
|
|
#include <gay/types.h>
|
|
|
|
|
#include <gay/util.h>
|
|
|
|
|
|
|
|
|
@ -62,6 +63,7 @@ struct cache_pool {
|
|
|
|
|
usize free_entries;
|
|
|
|
|
};
|
|
|
|
|
static struct cache_pool caches[CACHE_LEVELS];
|
|
|
|
|
MTX(caches_lock);
|
|
|
|
|
|
|
|
|
|
#define LONG_BIT_MASK (~(LONG_BIT - 1))
|
|
|
|
|
|
|
|
|
@ -220,6 +222,12 @@ void *get_pages(usize count, enum mm_flags flags)
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags & MM_NOSLEEP) {
|
|
|
|
|
kprintf("get_pages(): MM_NOSLEEP requested, this is not implemented yet :(\n");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
mtx_lock(&caches_lock);
|
|
|
|
|
|
|
|
|
|
struct clist *entry;
|
|
|
|
|
int entry_level;
|
|
|
|
|
for (entry_level = level; entry_level < CACHE_LEVELS; entry_level++) {
|
|
|
|
@ -228,8 +236,10 @@ void *get_pages(usize count, enum mm_flags flags)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (entry_level == CACHE_LEVELS)
|
|
|
|
|
if (entry_level == CACHE_LEVELS) {
|
|
|
|
|
mtx_unlock(&caches_lock);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clist_del(entry);
|
|
|
|
|
caches[entry_level].free_entries--;
|
|
|
|
@ -243,6 +253,7 @@ void *get_pages(usize count, enum mm_flags flags)
|
|
|
|
|
}
|
|
|
|
|
bit_set(caches[level].bitmap, bit_number);
|
|
|
|
|
|
|
|
|
|
mtx_unlock(&caches_lock);
|
|
|
|
|
return (void *)entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -254,11 +265,14 @@ void free_pages(void *ptr, usize count)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mtx_lock(&caches_lock);
|
|
|
|
|
|
|
|
|
|
usize bit_number = get_bit_number(ptr, level);
|
|
|
|
|
|
|
|
|
|
# if CFG_DEBUG_PAGE_ALLOCS
|
|
|
|
|
if (!bit_tst(caches[level].bitmap, bit_number)) {
|
|
|
|
|
kprintf("free_pages(%p, %zu): double free!\n", ptr, count);
|
|
|
|
|
mtx_unlock(&caches_lock);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
# endif
|
|
|
|
@ -273,6 +287,8 @@ void free_pages(void *ptr, usize count)
|
|
|
|
|
level++;
|
|
|
|
|
bit_number >>= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mtx_unlock(&caches_lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline usize get_bit_number(void *ptr, int level)
|
|
|
|
|