2019-03-18 17:25:44 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// 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/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2020-01-12 16:53:23 +01:00
|
|
|
#include <mm/map.h>
|
2019-05-13 23:22:27 +02:00
|
|
|
#include <mm/heap.h>
|
|
|
|
#include <ex/lock.h>
|
|
|
|
#include <init/boot.h>
|
2019-03-18 17:25:44 +01:00
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
// Start address of the heap
|
|
|
|
void *_heap_start;
|
|
|
|
|
|
|
|
// End of the heap
|
|
|
|
/*static*/ void *_heap_end;
|
2019-03-18 17:25:44 +01:00
|
|
|
|
|
|
|
// Maximal value of the heap
|
2019-05-07 22:32:17 +02:00
|
|
|
/*static*/ size_t _heap_max;
|
2019-03-18 17:25:44 +01:00
|
|
|
|
|
|
|
// Lock NOT used internally, but used by KalAllocMemory() & co.
|
2019-05-13 23:22:27 +02:00
|
|
|
static Lock_t _heap_lock = 0;
|
2019-03-18 17:25:44 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Initializes heap managment
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
void MmInitHeap(void)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
|
|
|
assert(_heap_end == NULL);
|
2019-03-25 23:10:06 +01:00
|
|
|
|
|
|
|
// Get the first available zone address
|
|
|
|
_heap_start = MmGetFirstAvailZone((void*)0);
|
2019-03-26 10:34:36 +01:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
// Align it
|
2019-03-26 10:34:36 +01:00
|
|
|
_heap_start = (void *)_ALIGN_UP((size_t)_heap_start, alignof(QWORD));
|
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
// Initialize the heap
|
|
|
|
_heap_end = _heap_start;
|
2019-05-13 20:34:41 +02:00
|
|
|
_heap_max = lmin(MM_HEAP_MAX, MmGetAvailZoneSize(_heap_end));
|
2019-03-26 10:34:36 +01:00
|
|
|
|
2019-05-17 21:10:22 +02:00
|
|
|
if ((ulong)_heap_start == _heap_max)
|
|
|
|
KeStartPanic("The heap failed to initialize ! (Not enough memory)");
|
2019-03-18 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-03-19 13:37:23 +01:00
|
|
|
// Acquires control of the heap's lock
|
2019-03-18 17:25:44 +01:00
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
void MmLockHeap(void)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-03-24 14:44:59 +01:00
|
|
|
ExAcquireLock(&_heap_lock);
|
2019-03-18 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Releases control of the heap's lock
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
void MmUnlockHeap(void)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-03-24 14:44:59 +01:00
|
|
|
ExReleaseLock(&_heap_lock);
|
2019-03-18 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Returns the heap's current size
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
size_t MmGetHeapSize(void)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-03-25 23:10:06 +01:00
|
|
|
return (size_t)_heap_end - (size_t)_heap_start;
|
2019-03-18 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Returns the heap's maximum size
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
size_t MmGetMaxHeapSize(void)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-03-18 17:25:44 +01:00
|
|
|
return _heap_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Changes the heap's maximal size
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
error_t MmSetMaxHeapSize(size_t new)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
if (new > MmGetAvailZoneSize((void *)_heap_start)) {
|
2019-03-18 17:25:44 +01:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
if (new < (size_t)_heap_end - (size_t)_heap_start) {
|
2019-03-18 17:25:44 +01:00
|
|
|
return EADDRINUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_heap_max = new;
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Extends the heap's size
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
error_t MmGrowHeap(size_t req)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-03-25 17:33:51 +01:00
|
|
|
assert(req % alignof(QWORD) == 0);
|
2019-03-18 17:25:44 +01:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
if ((size_t)_heap_end + req > (size_t)_heap_start + _heap_max) {
|
2019-05-07 22:32:17 +02:00
|
|
|
assert(!"Can't extend heap that much");
|
2019-03-18 17:25:44 +01:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
_heap_end += req;
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Reduces the heap's size
|
|
|
|
//
|
2019-03-24 14:44:59 +01:00
|
|
|
error_t MmShrinkHeap(size_t req)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-03-25 17:33:51 +01:00
|
|
|
assert(req % alignof(QWORD) == 0);
|
2019-03-18 17:25:44 +01:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
if (req > (size_t)_heap_end - (size_t)_heap_start) {
|
2019-05-07 22:32:17 +02:00
|
|
|
assert(!"Can't shrink heap that much");
|
2019-03-18 17:25:44 +01:00
|
|
|
return EADDRINUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_heap_end -= req;
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
2019-05-13 02:00:02 +02:00
|
|
|
|
2019-05-13 15:18:00 +02:00
|
|
|
|