X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/003db17f7a36a8098827f40df8fedc087b5a26f6..0fd474df7001837bde53da0e42e83081827c9641:/lunaix-os/kernel/mm/valloc.c diff --git a/lunaix-os/kernel/mm/valloc.c b/lunaix-os/kernel/mm/valloc.c index 5191a1a..d2baa35 100644 --- a/lunaix-os/kernel/mm/valloc.c +++ b/lunaix-os/kernel/mm/valloc.c @@ -1,15 +1,40 @@ #include #include #include +#include #define CLASS_LEN(class) (sizeof(class) / sizeof(class[0])) -static char piles_names[][PILE_NAME_MAXLEN] = { - "valloc_8", "valloc_16", "valloc_32", "valloc_64", "valloc_128", - "valloc_256", "valloc_512", "valloc_1k", "valloc_2k", "valloc_4k" +// threshold to use external cake metadata +#define EXTERN_THRESHOLD 128 + +static char piles_names[][PILE_NAME_MAXLEN] = +{ + "valloc_8", "valloc_16", "valloc_32", "valloc_64", + "valloc_128", "valloc_256", "valloc_512", "valloc_1k", + "valloc_2k", "valloc_4k", "valloc_8k" +}; + +#define M128 (4) +#define M1K (M128 + 3) + +static int page_counts[] = +{ + [0] = 1, + [1] = 1, + [2] = 1, + [3] = 1, + [M128 ] = 1, + [M128 + 1] = 2, + [M128 + 2] = 2, + [M1K ] = 4, + [M1K + 1 ] = 4, + [M1K + 2 ] = 8, + [M1K + 3 ] = 8 }; -static char piles_names_dma[][PILE_NAME_MAXLEN] = { +static char piles_names_dma[][PILE_NAME_MAXLEN] = +{ "valloc_dma_128", "valloc_dma_256", "valloc_dma_512", "valloc_dma_1k", "valloc_dma_2k", "valloc_dma_4k" }; @@ -20,32 +45,40 @@ static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)]; void valloc_init() { + int opts = 0; for (size_t i = 0; i < CLASS_LEN(piles_names); i++) { int size = 1 << (i + 3); - piles[i] = cake_new_pile(&piles_names[i], size, size > 1024 ? 4 : 1, 0); + if (size >= EXTERN_THRESHOLD) { + opts |= PILE_FL_EXTERN; + } + piles[i] = cake_new_pile(piles_names[i], size, page_counts[i], opts); } + opts = PILE_ALIGN_CACHE; // DMA 内存保证128字节对齐 for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) { int size = 1 << (i + 7); + if (size >= EXTERN_THRESHOLD) { + opts |= PILE_FL_EXTERN; + } piles_dma[i] = cake_new_pile( - &piles_names_dma[i], size, size > 1024 ? 4 : 1, PILE_CACHELINE); + piles_names_dma[i], size, page_counts[M128 + i], opts); } } void* -__valloc(unsigned int size, struct cake_pile** segregate_list, size_t len) +__valloc(unsigned int size, + struct cake_pile** segregate_list, + size_t len, + size_t boffset) { - size_t i = 0; - for (; i < len; i++) { - if (segregate_list[i]->piece_size >= size) { - goto found_class; - } - } + size_t i = ilog2(size); + i += (size - (1 << i) != 0); + i -= boffset; - return NULL; + if (i >= len) + i = 0; -found_class: return cake_grab(segregate_list[i]); } @@ -63,13 +96,13 @@ __vfree(void* ptr, struct cake_pile** segregate_list, size_t len) void* valloc(unsigned int size) { - return __valloc(size, &piles, CLASS_LEN(piles_names)); + return __valloc(size, piles, CLASS_LEN(piles_names), 3); } void* vzalloc(unsigned int size) { - void* ptr = __valloc(size, &piles, CLASS_LEN(piles_names)); + void* ptr = __valloc(size, piles, CLASS_LEN(piles_names), 3); memset(ptr, 0, size); return ptr; } @@ -78,11 +111,11 @@ void* vcalloc(unsigned int size, unsigned int count) { unsigned int alloc_size; - if (__builtin_umul_overflow(size, count, &alloc_size)) { + if (umul_of(size, count, &alloc_size)) { return 0; } - void* ptr = __valloc(alloc_size, &piles, CLASS_LEN(piles_names)); + void* ptr = __valloc(alloc_size, piles, CLASS_LEN(piles_names), 3); memset(ptr, 0, alloc_size); return ptr; } @@ -90,19 +123,29 @@ vcalloc(unsigned int size, unsigned int count) void vfree(void* ptr) { - __vfree(ptr, &piles, CLASS_LEN(piles_names)); + __vfree(ptr, piles, CLASS_LEN(piles_names)); +} + +void +vfree_safe(void* ptr) +{ + if (!ptr) { + return; + } + + __vfree(ptr, piles, CLASS_LEN(piles_names)); } void* valloc_dma(unsigned int size) { - return __valloc(size, &piles_dma, CLASS_LEN(piles_names_dma)); + return __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7); } void* vzalloc_dma(unsigned int size) { - void* ptr = __valloc(size, &piles_dma, CLASS_LEN(piles_names_dma)); + void* ptr = __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7); memset(ptr, 0, size); return ptr; } @@ -110,5 +153,10 @@ vzalloc_dma(unsigned int size) void vfree_dma(void* ptr) { - __vfree(ptr, &piles_dma, CLASS_LEN(piles_names_dma)); + __vfree(ptr, piles_dma, CLASS_LEN(piles_names_dma)); +} + +inline void must_inline +valloc_ensure_valid(void* ptr) { + cake_ensure_valid(ptr); } \ No newline at end of file