1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/kernel/mm/heap.c
Julian Barathieu f67e1baa25 Heap fix
2019-03-26 10:34:36 +01:00

140 lines
3.8 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Early and very dumb heap managment //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel/heap.h>
#include <kernel/mm.h>
#include <kernel/term.h>
// Least address out of the heap
static void *_heap_end;
// Maximal value of the heap
static size_t _heap_max;
// Lock NOT used internally, but used by KalAllocMemory() & co.
static Lock_t _heap_lock = ExINITLOCK(KLOCK_SPINLOCK);
//
// Initializes heap managment
//
void MmInitHeap(void)
{
assert(_heap_end == NULL);
// Get the first available zone address
_heap_start = MmGetFirstAvailZone((void*)0);
// Align it
_heap_start = (void *)_ALIGN_UP((size_t)_heap_start, alignof(QWORD));
// Initialize the heap
_heap_end = _heap_start;
_heap_max = MmGetAvailZoneSize(_heap_end);
KernLog("[InitHeap] Start address : %p, Max length : %u Mio\n\n",
_heap_start, _heap_max / MB);
}
//
// Acquires control of the heap's lock
//
void MmLockHeap(void)
{
ExAcquireLock(&_heap_lock);
}
//
// Releases control of the heap's lock
//
void MmUnlockHeap(void)
{
ExReleaseLock(&_heap_lock);
}
//
// Returns the heap's current size
//
size_t MmGetHeapSize(void)
{
return (size_t)_heap_end - (size_t)_heap_start;
}
//
// Returns the heap's maximum size
//
size_t MmGetMaxHeapSize(void)
{
return _heap_max;
}
//
// Changes the heap's maximal size
//
error_t MmSetMaxHeapSize(size_t new)
{
if (new > MmGetAvailZoneSize((void *)_heap_start)) {
return ENOMEM;
}
if (new < (size_t)_heap_end - (size_t)_heap_start) {
return EADDRINUSE;
}
_heap_max = new;
return EOK;
}
//
// Extends the heap's size
//
error_t MmGrowHeap(size_t req)
{
assert(req % alignof(QWORD) == 0);
if ((size_t)_heap_end + req > (size_t)_heap_start + _heap_max) {
return ENOMEM;
}
_heap_end += req;
return EOK;
}
//
// Reduces the heap's size
//
error_t MmShrinkHeap(size_t req)
{
assert(req % alignof(QWORD) == 0);
if (req > (size_t)_heap_end - (size_t)_heap_start) {
return EADDRINUSE;
}
_heap_end -= req;
return EOK;
}