From 16fd74f0cafaf903c065047241a87ee2649238e1 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 30 Nov 2020 23:07:18 +0500 Subject: [PATCH] Implement PFA initialization --- include/kernaux/pfa.h | 2 +- src/pfa.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/kernaux/pfa.h b/include/kernaux/pfa.h index 5489c2c..d624008 100644 --- a/include/kernaux/pfa.h +++ b/include/kernaux/pfa.h @@ -3,10 +3,10 @@ #include +#define KERNAUX_PFA_PAGE_SIZE (4 * 1024) #define KERNAUX_PFA_ZONES_COUNT_MAX 10 #define KERNAUX_PFA_ZONE_NAME_SIZE_MAX 256 #define KERNAUX_PFA_ZONE_NAME_SLEN_MAX (KERNAUX_PFA_ZONE_NAME_SIZE_MAX - 1) -#define KERNAUX_PFA_PAGE_SIZE (4 * 1024) #define KERNAUX_PFA_ZONE_PAGES_COUNT_MAX (1024 * 1024) #define KERNAUX_PFA_ZONE_PAGE_LIST_SIZE (KERNAUX_PFA_ZONE_PAGES_COUNT_MAX / 8) diff --git a/src/pfa.c b/src/pfa.c index 9f2b3de..0e36ca1 100644 --- a/src/pfa.c +++ b/src/pfa.c @@ -1,6 +1,7 @@ #include "config.h" #include +#include kernaux_bool KernAux_PFA_initialize_start( struct KernAux_PFA *const pfa @@ -14,6 +15,8 @@ kernaux_bool KernAux_PFA_initialize_start( } pfa->zones_count = 0; + + return KERNAUX_TRUE; } kernaux_bool KernAux_PFA_initialize_add_zone( @@ -29,6 +32,34 @@ kernaux_bool KernAux_PFA_initialize_add_zone( if (pfa->zones_count >= KERNAUX_PFA_ZONES_COUNT_MAX) { return KERNAUX_FALSE; } + + if (start >= end) { + return KERNAUX_FALSE; + } + + if (start % KERNAUX_PFA_PAGE_SIZE != 0) { + return KERNAUX_FALSE; + } + + if ((end + 1) % KERNAUX_PFA_PAGE_SIZE != 0) { + return KERNAUX_FALSE; + } + + const unsigned int name_slen = kernaux_strlen(name); + + if (name_slen > KERNAUX_PFA_ZONE_NAME_SLEN_MAX) { + return KERNAUX_FALSE; + } + + struct KernAux_PFA_Zone *const zone = &pfa->zones[pfa->zones_count++]; + + kernaux_strncpy(zone->name, name, name_slen); + + zone->start = start; + zone->end = end; + zone->size = end - start + 1; + + return KERNAUX_TRUE; } kernaux_bool KernAux_PFA_initialize_finish( @@ -37,4 +68,8 @@ kernaux_bool KernAux_PFA_initialize_finish( if (pfa->initialized) { return KERNAUX_FALSE; } + + pfa->initialized = KERNAUX_TRUE; + + return KERNAUX_TRUE; }