- kdevelop3 project files
[lightOS.git] / kernel / page_allocator.cpp
blob935d730f82e21485164fdd0535e39ddee32afb4f
1 /*
2 lightOS kernel
3 Copyright (C) 2006-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <kernel/context.hpp>
20 #include <kernel/page_allocator.hpp>
21 #include <kernel/virtual_memory.hpp>
22 using kernel::page_allocator;
23 using kernel::virtual_memory;
25 // TODO: Remove
26 #include <kernel/log.hpp>
28 SINGLETON_INSTANCE(kernel::page_allocator);
30 page_allocator::page_allocator()
31 : m_size(0), m_stack_size(0), m_page_count(0), m_stack(virtual_memory::page_stack_begin<void*>()),
32 m_stack_top(virtual_memory::page_stack_begin<void*>())
36 page_allocator::~page_allocator()
40 void *page_allocator::allocate()
42 SCOPED_LOCK(m_lock, scope);
44 if (UNLIKELY(m_stack_size == 0))
46 // TODO: We might want to unmap the pages that are used as the stack itself, we might want to do that at
47 // once
48 // TODO: Remove when other parts of the kernel honor this error
49 FATAL("page_allocator: No free pages");
50 return 0;
53 --m_size;
54 --m_stack_size;
55 return *m_stack++;
58 void page_allocator::free(void *page)
60 SCOPED_LOCK(m_lock, scope);
62 if (UNLIKELY(m_stack == m_stack_top))
64 kernel_context &Context = kernel_context::instance();
65 if (Context.create_page_tables(page,
66 adjust_pointer(m_stack, - virtual_memory::page_size),
67 lightOS::context::write | lightOS::context::global) == true)
69 ++m_size;
70 return;
72 m_stack_top -= page_size_in<void*>::value;
75 ++m_size;
76 ++m_stack_size;
77 *--m_stack = page;