2020-12-06 23:46:37 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2020-11-30 06:40:33 -05:00
|
|
|
#include "config.h"
|
2020-12-06 23:46:37 -05:00
|
|
|
#endif
|
2020-11-30 06:40:33 -05:00
|
|
|
|
2021-12-17 18:43:19 -05:00
|
|
|
#include <kernaux/assert.h>
|
2022-06-07 01:19:14 -04:00
|
|
|
#include <kernaux/libc.h>
|
2020-11-27 05:42:13 -05:00
|
|
|
#include <kernaux/pfa.h>
|
2020-11-30 17:54:09 -05:00
|
|
|
|
2021-12-14 13:50:27 -05:00
|
|
|
#define PAGE_INDEX(page_addr) ((page_addr) / KERNAUX_PFA_PAGE_SIZE)
|
|
|
|
|
|
|
|
#define FLAG_INDEX_FROM_INDEX(page_index) ((page_index) / 8)
|
|
|
|
#define FLAG_MASK_FROM_INDEX(page_index) (1 << ((page_index) % 8))
|
|
|
|
|
|
|
|
#define FLAG_INDEX_FROM_ADDR(page_addr) \
|
|
|
|
(FLAG_INDEX_FROM_INDEX(PAGE_INDEX(page_addr)))
|
|
|
|
#define FLAG_MASK_FROM_ADDR(page_addr) \
|
|
|
|
(FLAG_MASK_FROM_INDEX(PAGE_INDEX(page_addr)))
|
|
|
|
|
2021-12-14 13:54:47 -05:00
|
|
|
#define GET_FLAG_FROM_INDEX(pfa, page_index) \
|
|
|
|
(!!((pfa)->flags[FLAG_INDEX_FROM_INDEX(page_index)] & \
|
|
|
|
FLAG_MASK_FROM_INDEX(page_index)))
|
|
|
|
#define GET_FLAG_FROM_ADDR(pfa, page_addr) \
|
|
|
|
(!!((pfa)->flags[FLAG_INDEX_FROM_ADDR(page_addr)] & \
|
|
|
|
FLAG_MASK_FROM_ADDR(page_addr)))
|
|
|
|
|
2021-12-14 14:01:23 -05:00
|
|
|
#define FLAG_TRUE_FROM_INDEX(pfa, page_index) \
|
|
|
|
((pfa)->flags[FLAG_INDEX_FROM_INDEX(page_index)] |= \
|
|
|
|
FLAG_MASK_FROM_INDEX(page_index))
|
|
|
|
#define FLAG_FALSE_FROM_INDEX(pfa, page_index) \
|
|
|
|
((pfa)->flags[FLAG_INDEX_FROM_INDEX(page_index)] &= \
|
|
|
|
~FLAG_MASK_FROM_INDEX(page_index))
|
|
|
|
|
2020-12-03 22:04:42 -05:00
|
|
|
static void KernAux_PFA_mark(
|
2021-12-13 18:03:13 -05:00
|
|
|
KernAux_PFA pfa,
|
2020-12-06 05:16:15 -05:00
|
|
|
bool status,
|
2021-12-13 17:59:51 -05:00
|
|
|
size_t start,
|
|
|
|
size_t end
|
2021-12-14 17:10:28 -05:00
|
|
|
);
|
2020-12-03 22:04:42 -05:00
|
|
|
|
2021-12-13 18:03:13 -05:00
|
|
|
void KernAux_PFA_initialize(const KernAux_PFA pfa)
|
2020-11-30 17:54:09 -05:00
|
|
|
{
|
2021-12-17 18:43:19 -05:00
|
|
|
KERNAUX_NOTNULL_RETURN(pfa);
|
2021-12-15 05:45:40 -05:00
|
|
|
memset(pfa->flags, 0, sizeof(pfa->flags));
|
2020-11-30 17:54:09 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:10:05 -05:00
|
|
|
bool KernAux_PFA_is_available(const KernAux_PFA pfa, const size_t page_addr)
|
|
|
|
{
|
2021-12-17 18:43:19 -05:00
|
|
|
KERNAUX_NOTNULL_RETVAL(pfa, false);
|
|
|
|
KERNAUX_ASSERT_RETVAL(page_addr % KERNAUX_PFA_PAGE_SIZE == 0, false);
|
2021-12-13 18:10:05 -05:00
|
|
|
|
2021-12-14 13:54:47 -05:00
|
|
|
return GET_FLAG_FROM_ADDR(pfa, page_addr);
|
2021-12-13 18:10:05 -05:00
|
|
|
}
|
|
|
|
|
2020-11-30 17:54:09 -05:00
|
|
|
void KernAux_PFA_mark_available(
|
2021-12-13 18:03:13 -05:00
|
|
|
const KernAux_PFA pfa,
|
2021-12-13 17:59:51 -05:00
|
|
|
const size_t start,
|
|
|
|
const size_t end
|
2020-12-03 22:04:42 -05:00
|
|
|
) {
|
2020-12-06 05:16:15 -05:00
|
|
|
KernAux_PFA_mark(pfa, true, start, end);
|
2020-12-03 22:04:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void KernAux_PFA_mark_unavailable(
|
2021-12-13 18:03:13 -05:00
|
|
|
const KernAux_PFA pfa,
|
2021-12-13 17:59:51 -05:00
|
|
|
const size_t start,
|
|
|
|
const size_t end
|
2020-12-03 22:04:42 -05:00
|
|
|
) {
|
2020-12-06 05:16:15 -05:00
|
|
|
KernAux_PFA_mark(pfa, false, start, end);
|
2020-12-03 22:04:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void KernAux_PFA_mark(
|
2021-12-13 18:03:13 -05:00
|
|
|
const KernAux_PFA pfa,
|
2020-12-06 05:16:15 -05:00
|
|
|
const bool status,
|
2021-12-13 17:59:51 -05:00
|
|
|
size_t start,
|
|
|
|
size_t end
|
2020-11-30 17:54:09 -05:00
|
|
|
) {
|
2021-12-17 18:43:19 -05:00
|
|
|
KERNAUX_NOTNULL_RETURN(pfa);
|
|
|
|
KERNAUX_ASSERT_RETURN(start < end);
|
2020-11-30 17:54:09 -05:00
|
|
|
|
2021-12-13 17:59:51 -05:00
|
|
|
const size_t start_rem = start % KERNAUX_PFA_PAGE_SIZE;
|
|
|
|
const size_t end_rem = (end + 1) % KERNAUX_PFA_PAGE_SIZE;
|
2020-11-30 17:54:09 -05:00
|
|
|
|
|
|
|
if (start_rem != 0) {
|
|
|
|
start = start - start_rem + KERNAUX_PFA_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_rem != 0) {
|
|
|
|
end = end - end_rem;
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:59:51 -05:00
|
|
|
const size_t start_index = start / KERNAUX_PFA_PAGE_SIZE;
|
|
|
|
const size_t end_index = end / KERNAUX_PFA_PAGE_SIZE;
|
2020-11-30 17:54:09 -05:00
|
|
|
|
2021-12-13 17:59:51 -05:00
|
|
|
for (size_t index = start_index; index <= end_index; ++index) {
|
2021-12-14 13:41:11 -05:00
|
|
|
if (status) {
|
2021-12-14 14:01:23 -05:00
|
|
|
FLAG_TRUE_FROM_INDEX(pfa, index);
|
2021-12-14 13:41:11 -05:00
|
|
|
} else {
|
2021-12-14 14:01:23 -05:00
|
|
|
FLAG_FALSE_FROM_INDEX(pfa, index);
|
2021-12-14 13:41:11 -05:00
|
|
|
}
|
2020-11-30 17:54:09 -05:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 18:38:55 -05:00
|
|
|
|
2021-12-14 11:07:01 -05:00
|
|
|
size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, size_t mem_size)
|
2021-12-13 19:39:54 -05:00
|
|
|
{
|
2021-12-17 18:43:19 -05:00
|
|
|
KERNAUX_NOTNULL_RETVAL(pfa, 0);
|
2021-12-14 16:01:27 -05:00
|
|
|
|
2021-12-14 11:07:01 -05:00
|
|
|
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;
|
|
|
|
}
|
2021-12-13 19:39:54 -05:00
|
|
|
|
2021-12-13 20:21:47 -05:00
|
|
|
const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE;
|
2021-12-13 19:39:54 -05:00
|
|
|
|
|
|
|
// We start from 1 because 0 indicates failure.
|
|
|
|
// It is not very useful to alloc page at address 0;
|
2021-12-13 20:21:47 -05:00
|
|
|
for (size_t index = 1, start = 0;
|
|
|
|
index < KERNAUX_PFA_PAGES_COUNT_MAX;
|
|
|
|
++index)
|
|
|
|
{
|
2021-12-14 13:54:47 -05:00
|
|
|
if (!GET_FLAG_FROM_INDEX(pfa, index)) {
|
2021-12-13 20:21:47 -05:00
|
|
|
start = 0;
|
|
|
|
continue;
|
2021-12-13 19:39:54 -05:00
|
|
|
}
|
2021-12-13 20:21:47 -05:00
|
|
|
|
|
|
|
if (start == 0) start = index;
|
|
|
|
|
|
|
|
if (index - start + 1 == pages_count) {
|
|
|
|
for (; index >= start; --index) {
|
2021-12-14 14:01:23 -05:00
|
|
|
FLAG_FALSE_FROM_INDEX(pfa, index);
|
2021-12-13 19:39:54 -05:00
|
|
|
}
|
|
|
|
return start * KERNAUX_PFA_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KernAux_PFA_free_pages(
|
|
|
|
const KernAux_PFA pfa,
|
|
|
|
const size_t page_addr,
|
2021-12-14 11:07:01 -05:00
|
|
|
size_t mem_size
|
2021-12-13 19:39:54 -05:00
|
|
|
) {
|
2021-12-17 18:43:19 -05:00
|
|
|
KERNAUX_NOTNULL_RETURN(pfa);
|
|
|
|
KERNAUX_ASSERT_RETURN(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
|
2021-12-14 11:07:01 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-12-13 19:39:54 -05:00
|
|
|
|
|
|
|
const size_t start_index = page_addr / KERNAUX_PFA_PAGE_SIZE;
|
|
|
|
const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE;
|
|
|
|
|
|
|
|
for (size_t index = 0; index < pages_count; ++index) {
|
2021-12-14 14:01:23 -05:00
|
|
|
FLAG_TRUE_FROM_INDEX(pfa, start_index + index);
|
2021-12-13 19:39:54 -05:00
|
|
|
}
|
|
|
|
}
|