From c987d0e63da46e2927507cbf2b986d592a841d52 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 14 Dec 2021 06:21:47 +0500 Subject: [PATCH] Rewrite PFA --- include/kernaux/pfa.h | 11 +++++------ src/pfa.c | 43 +++++++++++++------------------------------ 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/include/kernaux/pfa.h b/include/kernaux/pfa.h index 8fe0af2..7276b8f 100644 --- a/include/kernaux/pfa.h +++ b/include/kernaux/pfa.h @@ -11,6 +11,11 @@ extern "C" { #define KERNAUX_PFA_PAGE_SIZE (4 * 1024) #define KERNAUX_PFA_PAGES_COUNT_MAX (1024 * 1024) +#define KernAux_PFA_alloc_page(pfa) \ + (KernAux_PFA_alloc_pages((pfa), KERNAUX_PFA_PAGE_SIZE)) +#define KernAux_PFA_free_page(pfa, page_addr) \ + (KernAux_PFA_free_pages((pfa), (page_addr), KERNAUX_PFA_PAGE_SIZE)) + typedef struct KernAux_PFA *KernAux_PFA; struct KernAux_PFA { @@ -29,15 +34,9 @@ __attribute__((nonnull)); void KernAux_PFA_mark_unavailable(KernAux_PFA pfa, size_t start, size_t end) __attribute__((nonnull)); -size_t KernAux_PFA_alloc_page(KernAux_PFA pfa) -__attribute__((nonnull)); - size_t KernAux_PFA_alloc_pages(KernAux_PFA pfa, size_t mem_size) __attribute__((nonnull)); -void KernAux_PFA_free_page(KernAux_PFA pfa, size_t page_addr) -__attribute__((nonnull)); - void KernAux_PFA_free_pages(KernAux_PFA pfa, size_t page_addr, size_t mem_size) __attribute__((nonnull)); diff --git a/src/pfa.c b/src/pfa.c index 82c9ece..3e3da51 100644 --- a/src/pfa.c +++ b/src/pfa.c @@ -68,53 +68,36 @@ void KernAux_PFA_mark( } } -size_t KernAux_PFA_alloc_page(const KernAux_PFA pfa) -{ - // We start from 1 because 0 indicates failure. - // It is not very useful to alloc page at address 0; - for (size_t index = 1; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) { - if (pfa->pages[index]) { - pfa->pages[index] = false; - return index * KERNAUX_PFA_PAGE_SIZE; - } - } - - return 0; -} - size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, const size_t mem_size) { if (mem_size % KERNAUX_PFA_PAGE_SIZE != 0) return 0; - const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE; + const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE; // We start from 1 because 0 indicates failure. // It is not very useful to alloc page at address 0; - for (size_t start = 1; start < KERNAUX_PFA_PAGES_COUNT_MAX;) { - size_t end = start; - for (; end < start + pages_count; ++end) { - if (!pfa->pages[end]) break; + for (size_t index = 1, start = 0; + index < KERNAUX_PFA_PAGES_COUNT_MAX; + ++index) + { + if (!pfa->pages[index]) { + start = 0; + continue; } - if (end >= KERNAUX_PFA_PAGES_COUNT_MAX) return 0; - if (end == start + pages_count - 1) { - for (size_t index = start; index <= end; ++index) { + + if (start == 0) start = index; + + if (index - start + 1 == pages_count) { + for (; index >= start; --index) { pfa->pages[index] = false; } return start * KERNAUX_PFA_PAGE_SIZE; } - start = end + 1; } return 0; } -void KernAux_PFA_free_page(const KernAux_PFA pfa, size_t page_addr) -{ - if (page_addr % KERNAUX_PFA_PAGE_SIZE != 0) return; - - pfa->pages[page_addr / KERNAUX_PFA_PAGE_SIZE] = true; -} - void KernAux_PFA_free_pages( const KernAux_PFA pfa, const size_t page_addr,