Refactor: abstract new page allocation to one general function and make helper functions static

This commit is contained in:
EDiasAlberto
2024-11-27 19:41:22 +00:00
parent c74a8c55aa
commit 4f84a83611
4 changed files with 34 additions and 28 deletions

View File

@@ -7,9 +7,28 @@
#define MAX_STACK_ACCESS_DIST 32
/* Validates a given address for being <=32 bytes away from the stack pointer or
above the stack */
bool needs_new_page (void *addr, void *esp)
static bool needs_new_page (const void *addr, const void *esp);
static bool grow_stack (const void *addr);
bool
try_alloc_new_page (const void *ptr, const void *esp)
{
if (needs_new_page (ptr, esp))
{
if (!grow_stack (ptr))
return 0;
else
return 1;
}
else
return 0;
}
/* Validates a given address for being a stack query and not a generic erroneous
address
*/
static bool
needs_new_page (const void *addr, const void *esp)
{
return (is_user_vaddr (addr) &&
(uint32_t*)addr >= ((uint32_t*)esp - MAX_STACK_ACCESS_DIST) &&
@@ -18,7 +37,8 @@ bool needs_new_page (void *addr, void *esp)
}
/* Extends the stack by the necessary number of pages */
bool grow_stack (void *addr)
static bool
grow_stack (const void *addr)
{
struct thread *t = thread_current ();
void *last_page = pg_round_down (addr);
@@ -35,6 +55,4 @@ bool grow_stack (void *addr)
return false;
}
return true;
}