From d5c683d6ab248b8e880725c76a9aaf28aa7e70f9 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 14 Dec 2021 21:07:01 +0500 Subject: [PATCH] Allow to request any amount of memory in PFA --- examples/pfa.c | 9 +++++++++ src/pfa.c | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/pfa.c b/examples/pfa.c index d3a9fac..bbc3629 100644 --- a/examples/pfa.c +++ b/examples/pfa.c @@ -59,5 +59,14 @@ int main() } } + // You can also request amounts of memory which are not factors of page + // size. + { + const size_t page_addr = KernAux_PFA_alloc_pages(&pfa, 123); + assert(!KernAux_PFA_is_available(&pfa, page_addr)); + KernAux_PFA_free_pages(&pfa, page_addr, 123); + assert(KernAux_PFA_is_available(&pfa, page_addr)); + } + return 0; } diff --git a/src/pfa.c b/src/pfa.c index 3e3da51..108a4bc 100644 --- a/src/pfa.c +++ b/src/pfa.c @@ -68,9 +68,13 @@ void KernAux_PFA_mark( } } -size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, const size_t mem_size) +size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, size_t mem_size) { - if (mem_size % KERNAUX_PFA_PAGE_SIZE != 0) return 0; + const size_t mem_rem = mem_size % KERNAUX_PFA_PAGE_SIZE; + + if (mem_rem != 0) { + mem_size = mem_size - mem_rem + KERNAUX_PFA_PAGE_SIZE; + } const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE; @@ -101,10 +105,15 @@ size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, const size_t mem_size) void KernAux_PFA_free_pages( const KernAux_PFA pfa, const size_t page_addr, - const size_t mem_size + size_t mem_size ) { if (page_addr % KERNAUX_PFA_PAGE_SIZE != 0) return; - if (mem_size % KERNAUX_PFA_PAGE_SIZE != 0) return; + + const size_t mem_rem = mem_size % KERNAUX_PFA_PAGE_SIZE; + + if (mem_rem != 0) { + mem_size = mem_size - mem_rem + KERNAUX_PFA_PAGE_SIZE; + } const size_t start_index = page_addr / KERNAUX_PFA_PAGE_SIZE; const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE;