From dc4924585e573a2715f62cdb8cfb01a8715aec5f Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 12 Mar 2013 17:40:33 +0100 Subject: [PATCH] Free the initrd after extraction. --- sortix/initrd.cpp | 20 ++++++++++++++++++++ sortix/initrd.h | 1 + sortix/kernel.cpp | 4 +++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/sortix/initrd.cpp b/sortix/initrd.cpp index bd0a2d24..5adefab1 100644 --- a/sortix/initrd.cpp +++ b/sortix/initrd.cpp @@ -344,5 +344,25 @@ bool ExtractFromPhysicalInto(addr_t physaddr, size_t size, Ref desc) return ExtractInto(desc); } +void Delete() +{ + size_t size = initrdsize; + + initrd = NULL; + initrdsize = 0; + + // Unmap the pages and return the physical frames for reallocation. + addr_t mapat = initrd_addr_alloc.from; + for ( size_t i = 0; i < size; i += Page::Size() ) + { + addr_t addr = Memory::Unmap(mapat + i); + Page::Put(addr); + } + Memory::Flush(); + + // Free the used virtual address space. + FreeKernelAddress(&initrd_addr_alloc); +} + } // namespace InitRD } // namespace Sortix diff --git a/sortix/initrd.h b/sortix/initrd.h index 8ee8ee73..caee0d57 100644 --- a/sortix/initrd.h +++ b/sortix/initrd.h @@ -35,6 +35,7 @@ namespace InitRD { bool ExtractInto(Ref desc); bool ExtractFromPhysicalInto(addr_t physaddr, size_t size, Ref desc); +void Delete(); } // namespace InitRD diff --git a/sortix/kernel.cpp b/sortix/kernel.cpp index b7f463ec..ea5dd659 100644 --- a/sortix/kernel.cpp +++ b/sortix/kernel.cpp @@ -361,7 +361,9 @@ static void BootThread(void* /*user*/) // Install the initrd into our fresh RAM filesystem. if ( !InitRD::ExtractFromPhysicalInto(initrd, initrdsize, droot) ) Panic("Unable to extract initrd into RAM root filesystem."); - // TODO: It's safe to free the original initrd now. + + // We no longer need the initrd, so free its resources. + InitRD::Delete(); // Get a descriptor for the /dev directory so we can populate it. if ( droot->mkdir(&ctx, "dev", 0775) != 0 && errno != EEXIST )