Use `size_t` instead of `unsigned int` in PFA

This commit is contained in:
Alex Kotov 2021-12-14 03:59:51 +05:00
parent 5a5fb67ea8
commit 87bb83a91f
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 34 additions and 41 deletions

View File

@ -6,6 +6,7 @@ extern "C" {
#endif
#include <stdbool.h>
#include <stddef.h>
#define KERNAUX_PFA_PAGE_SIZE (4 * 1024)
#define KERNAUX_PFA_PAGES_COUNT_MAX (1024 * 1024)
@ -19,22 +20,22 @@ __attribute__((nonnull));
void KernAux_PFA_mark_available(
struct KernAux_PFA *pfa,
unsigned int start,
unsigned int end
size_t start,
size_t end
)
__attribute__((nonnull));
void KernAux_PFA_mark_unavailable(
struct KernAux_PFA *pfa,
unsigned int start,
unsigned int end
size_t start,
size_t end
)
__attribute__((nonnull));
unsigned int KernAux_PFA_alloc_page(struct KernAux_PFA *pfa)
size_t KernAux_PFA_alloc_page(struct KernAux_PFA *pfa)
__attribute__((nonnull));
void KernAux_PFA_free_page(struct KernAux_PFA *pfa, unsigned int page_addr)
void KernAux_PFA_free_page(struct KernAux_PFA *pfa, size_t page_addr)
__attribute__((nonnull));
#ifdef __cplusplus

View File

@ -8,8 +8,8 @@
static void KernAux_PFA_mark(
struct KernAux_PFA *pfa,
bool status,
unsigned int start,
unsigned int end
size_t start,
size_t end
)
__attribute__((nonnull));
@ -20,16 +20,16 @@ void KernAux_PFA_initialize(struct KernAux_PFA *const pfa)
void KernAux_PFA_mark_available(
struct KernAux_PFA *const pfa,
unsigned int start,
unsigned int end
const size_t start,
const size_t end
) {
KernAux_PFA_mark(pfa, true, start, end);
}
void KernAux_PFA_mark_unavailable(
struct KernAux_PFA *const pfa,
unsigned int start,
unsigned int end
const size_t start,
const size_t end
) {
KernAux_PFA_mark(pfa, false, start, end);
}
@ -37,15 +37,13 @@ void KernAux_PFA_mark_unavailable(
void KernAux_PFA_mark(
struct KernAux_PFA *const pfa,
const bool status,
unsigned int start,
unsigned int end
size_t start,
size_t end
) {
if (start >= end) {
return;
}
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;
const size_t start_rem = start % KERNAUX_PFA_PAGE_SIZE;
const size_t end_rem = (end + 1) % KERNAUX_PFA_PAGE_SIZE;
if (start_rem != 0) {
start = start - start_rem + KERNAUX_PFA_PAGE_SIZE;
@ -55,20 +53,20 @@ void KernAux_PFA_mark(
end = end - end_rem;
}
const unsigned int start_index = start / KERNAUX_PFA_PAGE_SIZE;
const unsigned int end_index = end / KERNAUX_PFA_PAGE_SIZE;
const size_t start_index = start / KERNAUX_PFA_PAGE_SIZE;
const size_t end_index = end / KERNAUX_PFA_PAGE_SIZE;
for (unsigned int index = start_index; index <= end_index; ++index) {
for (size_t index = start_index; index <= end_index; ++index) {
pfa->pages[index] = status;
}
}
unsigned int KernAux_PFA_alloc_page(struct KernAux_PFA *pfa)
size_t KernAux_PFA_alloc_page(struct KernAux_PFA *const 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) {
for (size_t index = 1; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
if (pfa->pages[index]) {
pfa->pages[index] = false;
return index * KERNAUX_PFA_PAGE_SIZE;
@ -78,11 +76,9 @@ unsigned int KernAux_PFA_alloc_page(struct KernAux_PFA *pfa)
return 0;
}
void KernAux_PFA_free_page(struct KernAux_PFA *pfa, unsigned int page_addr)
void KernAux_PFA_free_page(struct KernAux_PFA *const pfa, size_t page_addr)
{
if (page_addr % KERNAUX_PFA_PAGE_SIZE != 0) {
return;
}
if (page_addr % KERNAUX_PFA_PAGE_SIZE != 0) return;
pfa->pages[page_addr / KERNAUX_PFA_PAGE_SIZE] = true;
}

View File

@ -12,7 +12,7 @@ int main()
KernAux_PFA_initialize(&pfa);
for (unsigned int index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
for (size_t index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(pfa.pages[index] == false);
}
@ -20,35 +20,31 @@ int main()
KernAux_PFA_mark_available(&pfa, 1048576, 134086655);
KernAux_PFA_mark_unavailable(&pfa, 4194304, 6291455); // [4 MB, 6 MB)
for (unsigned int index = 0; index < 159; ++index) {
for (size_t index = 0; index < 159; ++index) {
assert(pfa.pages[index] == true);
}
for (unsigned int index = 159; index < 256; ++index) {
for (size_t index = 159; index < 256; ++index) {
assert(pfa.pages[index] == false);
}
for (unsigned int index = 256; index < 1024; ++index) { // [1 MB, 4 MB)
for (size_t index = 256; index < 1024; ++index) { // [1 MB, 4 MB)
assert(pfa.pages[index] == true);
}
for (unsigned int index = 1024; index < 1536; ++index) { // [4 MB, 6 MB)
for (size_t index = 1024; index < 1536; ++index) { // [4 MB, 6 MB)
assert(pfa.pages[index] == false);
}
for (unsigned int index = 1536; index < 32736; ++index) { // [6 MB, ~127 MB)
for (size_t index = 1536; index < 32736; ++index) { // [6 MB, ~127 MB)
assert(pfa.pages[index] == true);
}
for (
unsigned int index = 32736;
index < KERNAUX_PFA_PAGES_COUNT_MAX;
++index
) {
for (size_t index = 32736; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(pfa.pages[index] == false);
}
unsigned int page_addr = KernAux_PFA_alloc_page(&pfa);
const size_t page_addr = KernAux_PFA_alloc_page(&pfa);
assert(page_addr != 0);
assert(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
@ -58,7 +54,7 @@ int main()
assert(pfa.pages[page_addr / KERNAUX_PFA_PAGE_SIZE]);
for (unsigned int index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
for (size_t index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
if (pfa.pages[index]) {
assert(KernAux_PFA_alloc_page(&pfa) != 0);
}