1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-04-07 17:32:45 -04:00

Initialize PFA and mark available memory

This commit is contained in:
Alex Kotov 2020-12-01 03:54:09 +05:00
parent 377e05f4f8
commit af544e9e37
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 86 additions and 0 deletions

View file

@ -1,10 +1,31 @@
#ifndef KERNAUX_INCLUDED_PFA
#define KERNAUX_INCLUDED_PFA 1
#include <kernaux/stdlib.h>
#define KERNAUX_PFA_PAGE_SIZE (4 * 1024)
#define KERNAUX_PFA_PAGES_COUNT_MAX (1024 * 1024)
#ifdef __cplusplus
extern "C" {
#endif
struct KernAux_PFA {
kernaux_bool pages[KERNAUX_PFA_PAGES_COUNT_MAX];
};
void KernAux_PFA_initialize(
struct KernAux_PFA *pfa
)
__attribute__((nonnull));
void KernAux_PFA_mark_available(
struct KernAux_PFA *pfa,
unsigned int start,
unsigned int end
)
__attribute__((nonnull));
#ifdef __cplusplus
}
#endif

View file

@ -1,3 +1,37 @@
#include "config.h"
#include <kernaux/pfa.h>
#include <kernaux/stdlib.h>
void KernAux_PFA_initialize(struct KernAux_PFA *const pfa)
{
kernaux_memset(pfa->pages, KERNAUX_FALSE, sizeof(pfa->pages));
}
void KernAux_PFA_mark_available(
struct KernAux_PFA *const pfa,
unsigned int start,
unsigned int end
) {
if (start >= end) {
return;
}
const unsigned int start_rem = start % KERNAUX_PFA_PAGE_SIZE;
const unsigned int end_rem = (end + 1) % KERNAUX_PFA_PAGE_SIZE;
if (start_rem != 0) {
start = start - start_rem + KERNAUX_PFA_PAGE_SIZE;
}
if (end_rem != 0) {
end = end - end_rem;
}
const unsigned int start_index = start / KERNAUX_PFA_PAGE_SIZE;
const unsigned int end_index = end / KERNAUX_PFA_PAGE_SIZE;
for (unsigned int index = start_index; index <= end_index; ++index) {
pfa->pages[index] = KERNAUX_TRUE;
}
}

View file

@ -4,5 +4,36 @@
int main()
{
struct KernAux_PFA pfa;
KernAux_PFA_initialize(&pfa);
for (unsigned int index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(pfa.pages[index] == KERNAUX_FALSE);
}
KernAux_PFA_mark_available(&pfa, 0, 654335);
KernAux_PFA_mark_available(&pfa, 1048576, 134086655);
for (unsigned int index = 0; index < 159; ++index) {
assert(pfa.pages[index] == KERNAUX_TRUE);
}
for (unsigned int index = 159; index < 256; ++index) {
assert(pfa.pages[index] == KERNAUX_FALSE);
}
for (unsigned int index = 256; index < 32736; ++index) {
assert(pfa.pages[index] == KERNAUX_TRUE);
}
for (
unsigned int index = 32736;
index < KERNAUX_PFA_PAGES_COUNT_MAX;
++index
) {
assert(pfa.pages[index] == KERNAUX_FALSE);
}
return 0;
}