1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-03-31 17:25:22 -04:00

Add func "KernAux_PFA_is_available"

This commit is contained in:
Alex Kotov 2021-12-14 04:10:05 +05:00
parent 05e8794bcb
commit a53401924c
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 20 additions and 10 deletions

View file

@ -20,6 +20,9 @@ struct KernAux_PFA {
void KernAux_PFA_initialize(KernAux_PFA pfa)
__attribute__((nonnull));
bool KernAux_PFA_is_available(KernAux_PFA pfa, size_t page_addr)
__attribute__((nonnull));
void KernAux_PFA_mark_available(KernAux_PFA pfa, size_t start, size_t end)
__attribute__((nonnull));

View file

@ -18,6 +18,13 @@ void KernAux_PFA_initialize(const KernAux_PFA pfa)
kernaux_memset(pfa->pages, false, sizeof(pfa->pages));
}
bool KernAux_PFA_is_available(const KernAux_PFA pfa, const size_t page_addr)
{
if (page_addr % KERNAUX_PFA_PAGE_SIZE != 0) return false;
return pfa->pages[page_addr / KERNAUX_PFA_PAGE_SIZE];
}
void KernAux_PFA_mark_available(
const KernAux_PFA pfa,
const size_t start,

View file

@ -13,7 +13,7 @@ int main()
KernAux_PFA_initialize(&pfa);
for (size_t index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(pfa.pages[index] == false);
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
KernAux_PFA_mark_available(&pfa, 0, 654335);
@ -21,41 +21,41 @@ int main()
KernAux_PFA_mark_unavailable(&pfa, 4194304, 6291455); // [4 MB, 6 MB)
for (size_t index = 0; index < 159; ++index) {
assert(pfa.pages[index] == true);
assert(KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 159; index < 256; ++index) {
assert(pfa.pages[index] == false);
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 256; index < 1024; ++index) { // [1 MB, 4 MB)
assert(pfa.pages[index] == true);
assert(KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 1024; index < 1536; ++index) { // [4 MB, 6 MB)
assert(pfa.pages[index] == false);
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 1536; index < 32736; ++index) { // [6 MB, ~127 MB)
assert(pfa.pages[index] == true);
assert(KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 32736; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(pfa.pages[index] == false);
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
const size_t page_addr = KernAux_PFA_alloc_page(&pfa);
assert(page_addr != 0);
assert(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
assert(!pfa.pages[page_addr / KERNAUX_PFA_PAGE_SIZE]);
assert(!KernAux_PFA_is_available(&pfa, page_addr));
KernAux_PFA_free_page(&pfa, page_addr);
assert(pfa.pages[page_addr / KERNAUX_PFA_PAGE_SIZE]);
assert(KernAux_PFA_is_available(&pfa, page_addr));
for (size_t index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
if (pfa.pages[index]) {
if (KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE)) {
assert(KernAux_PFA_alloc_page(&pfa) != 0);
}
}