2019-03-18 17:25:44 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
|
|
|
// Desc: Early and very dumb memory 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 <extras/malloc.h>
|
|
|
|
|
2019-04-06 07:53:58 +02:00
|
|
|
error_t KalAllocMemory(void **ptr, size_t req)
|
|
|
|
{
|
|
|
|
return KalAllocMemoryEx(ptr, req, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align)
|
2019-03-18 17:25:44 +01:00
|
|
|
{
|
|
|
|
error_t rc;
|
|
|
|
size_t brk;
|
|
|
|
|
|
|
|
if (align == 0) align = M_DEFAULT_ALIGNMENT;
|
|
|
|
|
|
|
|
if (align < M_MINIMAL_ALIGNMENT) {
|
|
|
|
return EALIGN;
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:44:59 +01:00
|
|
|
MmLockHeap();
|
2019-03-18 17:25:44 +01:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
brk = (size_t)_heap_start + MmGetHeapSize();
|
2019-03-18 17:25:44 +01:00
|
|
|
req = _ALIGN_UP(req + brk, align) - brk;
|
|
|
|
|
2019-03-24 14:44:59 +01:00
|
|
|
rc = MmGrowHeap(req);
|
2019-03-18 17:25:44 +01:00
|
|
|
|
2019-03-24 14:44:59 +01:00
|
|
|
MmUnlockHeap();
|
2019-03-18 17:25:44 +01:00
|
|
|
|
|
|
|
if (rc) {
|
2019-03-19 13:37:23 +01:00
|
|
|
if ((flags & M_CANFAIL) != 0)
|
2019-03-18 17:25:44 +01:00
|
|
|
return rc;
|
2019-03-26 10:34:36 +01:00
|
|
|
KeStartPanic("KalAllocMemory: Out of memory");
|
2019-03-18 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & M_ZEROED) {
|
|
|
|
memzero(*ptr, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = (void *)brk;
|
|
|
|
|
2019-03-25 17:33:51 +01:00
|
|
|
assert(*ptr);
|
2019-03-18 17:25:44 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Frees allocated memory
|
|
|
|
//
|
|
|
|
error_t KalFreeMemory(void *ptr)
|
|
|
|
{
|
|
|
|
(void)ptr;
|
2019-03-22 13:18:20 +01:00
|
|
|
return EOK;
|
2019-03-18 17:25:44 +01:00
|
|
|
}
|
|
|
|
|