2019-03-18 17:25:44 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-09-27 17:33:48 +02:00
|
|
|
// OS on Kaleid //
|
2019-03-18 17:25:44 +01:00
|
|
|
// //
|
|
|
|
// Desc: Early and very dumb memory managment //
|
|
|
|
// //
|
|
|
|
// //
|
2020-02-06 14:23:26 +01:00
|
|
|
// Copyright © 2018-2020 The OS/K Team //
|
2019-03-18 17:25:44 +01:00
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2019-05-13 23:22:27 +02:00
|
|
|
#include <mm/heap.h>
|
|
|
|
#include <ex/malloc.h>
|
2019-03-18 17:25:44 +01:00
|
|
|
|
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-05-07 23:16:56 +02:00
|
|
|
|
2019-05-12 18:01:29 +02:00
|
|
|
/* DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", */
|
|
|
|
/* _heap_start, MmGetHeapSize(), brk, req); */
|
2019-03-18 17:25:44 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-20 19:17:05 +01:00
|
|
|
*ptr = (void *)_ALIGN_UP(brk, align);
|
2019-05-12 18:01:29 +02:00
|
|
|
|
2019-03-18 17:25:44 +01:00
|
|
|
if (flags & M_ZEROED) {
|
|
|
|
memzero(*ptr, req);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|