130 lines
3.5 KiB
C
130 lines
3.5 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>
|
||
|
|
||
|
// 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 = INITLOCK(KLOCK_SPINLOCK);
|
||
|
|
||
|
// Debugging stub
|
||
|
size_t GetAvailZoneSize(void *x) { (void)x; return 8 * MB; }
|
||
|
|
||
|
//
|
||
|
// Initializes heap managment
|
||
|
//
|
||
|
void InitHeap(void)
|
||
|
{
|
||
|
assert(_heap_end == NULL);
|
||
|
_heap_end = (void *)_HEAP_START;
|
||
|
_heap_max = lmin(8 * MB, GetAvailZoneSize((void *)_HEAP_START));
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Aquires control of the heap's lock
|
||
|
//
|
||
|
void LockHeap(void)
|
||
|
{
|
||
|
AquireLock(&_heap_lock);
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Releases control of the heap's lock
|
||
|
//
|
||
|
void UnlockHeap(void)
|
||
|
{
|
||
|
ReleaseLock(&_heap_lock);
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Returns the heap's current size
|
||
|
//
|
||
|
size_t GetHeapSize(void)
|
||
|
{
|
||
|
return (size_t)_heap_end - _HEAP_START;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Returns the heap's maximum size
|
||
|
//
|
||
|
size_t GetMaxHeapSize(void)
|
||
|
{
|
||
|
return _heap_max;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Changes the heap's maximal size
|
||
|
//
|
||
|
error_t SetMaxHeapSize(size_t new)
|
||
|
{
|
||
|
if (new > GetAvailZoneSize((void *)_HEAP_START)) {
|
||
|
return ENOMEM;
|
||
|
}
|
||
|
|
||
|
if (new < (size_t)_heap_end - _HEAP_START) {
|
||
|
return EADDRINUSE;
|
||
|
}
|
||
|
|
||
|
_heap_max = new;
|
||
|
|
||
|
return EOK;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Extends the heap's size
|
||
|
//
|
||
|
error_t GrowHeap(size_t req)
|
||
|
{
|
||
|
assert(req % alignof(QWORD));
|
||
|
|
||
|
if ((size_t)_heap_end + req > _HEAP_START + _heap_max) {
|
||
|
return ENOMEM;
|
||
|
}
|
||
|
|
||
|
_heap_end += req;
|
||
|
|
||
|
return EOK;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Reduces the heap's size
|
||
|
//
|
||
|
error_t ShrinkHeap(size_t req)
|
||
|
{
|
||
|
assert(req % alignof(QWORD));
|
||
|
|
||
|
if (req > (size_t)_heap_end - _HEAP_START) {
|
||
|
return EADDRINUSE;
|
||
|
}
|
||
|
|
||
|
_heap_end -= req;
|
||
|
|
||
|
return EOK;
|
||
|
}
|