1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-27 11:14:42 -05:00

Alloc and free pages in PFA

This commit is contained in:
Alex Kotov 2020-12-01 04:38:55 +05:00
parent af544e9e37
commit 2d8c689ac1
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 41 additions and 3 deletions

View file

@ -14,9 +14,7 @@ struct KernAux_PFA {
kernaux_bool pages[KERNAUX_PFA_PAGES_COUNT_MAX];
};
void KernAux_PFA_initialize(
struct KernAux_PFA *pfa
)
void KernAux_PFA_initialize(struct KernAux_PFA *pfa)
__attribute__((nonnull));
void KernAux_PFA_mark_available(
@ -26,6 +24,12 @@ void KernAux_PFA_mark_available(
)
__attribute__((nonnull));
unsigned int KernAux_PFA_alloc_page(struct KernAux_PFA *pfa)
__attribute__((nonnull));
void KernAux_PFA_free_page(struct KernAux_PFA *pfa, unsigned int page_addr)
__attribute__((nonnull));
#ifdef __cplusplus
}
#endif

View file

@ -35,3 +35,27 @@ void KernAux_PFA_mark_available(
pfa->pages[index] = KERNAUX_TRUE;
}
}
unsigned int KernAux_PFA_alloc_page(struct KernAux_PFA *pfa)
{
// We start from 1 because 0 indicates failure.
// It is not very usefull to alloc page at address 0;
//
for (unsigned int index = 1; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
if (pfa->pages[index]) {
pfa->pages[index] = KERNAUX_FALSE;
return index * KERNAUX_PFA_PAGE_SIZE;
}
}
return 0;
}
void KernAux_PFA_free_page(struct KernAux_PFA *pfa, unsigned int page_addr)
{
if (page_addr % KERNAUX_PFA_PAGE_SIZE != 0) {
return;
}
pfa->pages[page_addr / KERNAUX_PFA_PAGE_SIZE] = KERNAUX_TRUE;
}

View file

@ -35,5 +35,15 @@ int main()
assert(pfa.pages[index] == KERNAUX_FALSE);
}
unsigned int 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]);
KernAux_PFA_free_page(&pfa, page_addr);
assert(pfa.pages[page_addr / KERNAUX_PFA_PAGE_SIZE]);
return 0;
}