mirror of
https://github.com/tailix/libkernaux.git
synced 2024-11-06 10:56:30 -05:00
Allow to request any amount of memory in PFA
This commit is contained in:
parent
e5056680d2
commit
d5c683d6ab
2 changed files with 22 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
|
|
17
src/pfa.c
17
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;
|
||||
|
|
Loading…
Reference in a new issue