Minor cleanups
[lightOS.git] / trunk / kernel / page_allocator.cpp
blob574ce6d1bf65b3f9880d23f7154fd3e52185af2d
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 <cstdint>
20 #include <kernel/debug.hpp>
21 #include <kernel/context.hpp>
22 #include <kernel/processor.hpp>
23 #include <kernel/page_allocator.hpp>
24 #include <lightOS/c++utils.hpp>
25 using namespace std;
26 using namespace kernel;
28 size_t page_allocator::mPageSize = PAGE_SIZE;
29 page_allocator page_allocator::m_instance = page_allocator();
31 page_allocator::page_allocator()
32 : mCapacity(0), mSize(0), mStack(reinterpret_cast<uintptr_t*>(KERNEL_MEMORY_PAGE_ALLOCATOR_START))
36 page_allocator::~page_allocator()
40 void *page_allocator::allocate()
42 if (mSize == 0)
44 FATAL("page_allocator: No free pages" << endl);
45 processor::halt();
48 LOCK(mLock);
50 --mSize;
51 void *Page = reinterpret_cast<void*>(*mStack++);
53 UNLOCK(mLock);
55 return Page;
58 void page_allocator::free(void *page)
60 LOCK(mLock);
62 if (mSize == mCapacity &&
63 (reinterpret_cast<uintptr_t>(mStack) & (mPageSize - 1)) == 0)
65 kernelContext &Context = kernelContext::instance();
66 if (Context.create_page_tables( page,
67 adjust_pointer(mStack, - mPageSize),
68 lightOS::context::write | lightOS::context::global) == true)
70 UNLOCK(mLock);
71 return;
75 ++mSize;
76 if (mSize > mCapacity)++mCapacity;
77 *--mStack = reinterpret_cast<uintptr_t>(page);
79 UNLOCK(mLock);