Heap & memory allocation stuff
This commit is contained in:
parent
d718561a9c
commit
5b585e5b70
19
Makefile
19
Makefile
|
@ -32,11 +32,11 @@ CCNAME=x86_64-elf-gcc
|
|||
ASMFLAGS=-f elf64
|
||||
LDFLAGS=-melf_x86_64
|
||||
COPTIM=-O2
|
||||
CWARNS=-Wall -Wextra # -Werror=implicit-function-declaration
|
||||
CWARNS=-Wall -Wextra -Werror=implicit-function-declaration
|
||||
CINCLUDES=-Ikaleid/include
|
||||
CFLAGS1=-nostdlib -ffreestanding -mcmodel=large # -std=gnu11
|
||||
CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11
|
||||
CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-strict-aliasing
|
||||
CFLAGS=$(CFLAGS1) $(CFLAGS2) -DNDEBUG
|
||||
CFLAGS=$(CFLAGS1) $(CFLAGS2)
|
||||
KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) -D_OSK_SOURCE -D_KALEID_KERNEL
|
||||
|
||||
#Folders
|
||||
|
@ -62,7 +62,7 @@ all : OS/K
|
|||
|
||||
# Common objects
|
||||
kal_com_obj= $(KOBJDIR)/atoi.o $(KOBJDIR)/ctype.o \
|
||||
$(KOBJDIR)/itoa.o $(KOBJDIR)/memory.o \
|
||||
$(KOBJDIR)/itoa.o $(KOBJDIR)/mem.o \
|
||||
$(KOBJDIR)/rand.o $(KOBJDIR)/sprintf.o \
|
||||
$(KOBJDIR)/status.o $(KOBJDIR)/string.o \
|
||||
$(KOBJDIR)/strtol.o $(KOBJDIR)/argv.o \
|
||||
|
@ -98,7 +98,7 @@ $(KOBJDIR)/utoa.o: $(KERNELDIR)/crtlib/itoa.c
|
|||
$(KOBJDIR)/ultoa.o: $(KERNELDIR)/crtlib/itoa.c
|
||||
@$(KCC) -D_NEED_ULTOA $< -o $@
|
||||
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
|
||||
$(KOBJDIR)/memory.o: $(KERNELDIR)/crtlib/memory.c
|
||||
$(KOBJDIR)/mem.o: $(KERNELDIR)/crtlib/mem.c
|
||||
@$(KCC) -fno-strict-aliasing $< -o $@
|
||||
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
|
||||
$(KOBJDIR)/rand.o: $(KERNELDIR)/crtlib/rand.c
|
||||
|
@ -127,7 +127,8 @@ $(KOBJDIR)/prog.o: $(KERNELDIR)/extras/prog.c
|
|||
kal_kern_obj= $(KOBJDIR)/kernel/cpuid.o $(KOBJDIR)/kernel/init.o \
|
||||
$(KOBJDIR)/kernel/table.o $(KOBJDIR)/kernel/cursor.o \
|
||||
$(KOBJDIR)/kernel/term.o $(KOBJDIR)/kernel/vga.o \
|
||||
$(KOBJDIR)/kernel/panic.o
|
||||
$(KOBJDIR)/kernel/panic.o $(KOBJDIR)/kernel/heap.o \
|
||||
$(KOBJDIR)/kernel/malloc.o
|
||||
|
||||
$(KOBJDIR)/kernel/cpuid.o: $(KERNELDIR)/kernel/cpu/cpuid.c
|
||||
@$(KCC) $< -o $@
|
||||
|
@ -150,6 +151,12 @@ $(KOBJDIR)/kernel/vga.o: $(KERNELDIR)/kernel/io/vga.c
|
|||
$(KOBJDIR)/kernel/panic.o: $(KERNELDIR)/kernel/ke/panic.c
|
||||
@$(KCC) $< -o $@
|
||||
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
|
||||
$(KOBJDIR)/kernel/heap.o: $(KERNELDIR)/kernel/mm/heap.c
|
||||
@$(KCC) $< -o $@
|
||||
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
|
||||
$(KOBJDIR)/kernel/malloc.o: $(KERNELDIR)/kernel/mm/malloc.c
|
||||
@$(KCC) $< -o $@
|
||||
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
|
||||
|
||||
## MISC MAKEFILE ------------------------------------------------------------- #
|
||||
./ProjectTree: ./.stylehlp_sh
|
||||
|
|
13
ProjectTree
13
ProjectTree
|
@ -62,11 +62,14 @@
|
|||
│ ├── atol.o
|
||||
│ ├── atoul.o
|
||||
│ ├── atou.o
|
||||
│ ├── crtlib
|
||||
│ ├── ctype.o
|
||||
│ ├── extras
|
||||
│ ├── itoa.o
|
||||
│ ├── kernel
|
||||
│ │ ├── cpuid.o
|
||||
│ │ ├── cursor.o
|
||||
│ │ ├── heap.o
|
||||
│ │ ├── init
|
||||
│ │ │ ├── init.o
|
||||
│ │ │ └── table.o
|
||||
|
@ -77,12 +80,13 @@
|
|||
│ │ │ └── vga.o
|
||||
│ │ ├── ke
|
||||
│ │ │ └── panic.o
|
||||
│ │ ├── malloc.o
|
||||
│ │ ├── panic.o
|
||||
│ │ ├── table.o
|
||||
│ │ ├── term.o
|
||||
│ │ └── vga.o
|
||||
│ ├── ltoa.o
|
||||
│ ├── memory.o
|
||||
│ ├── mem.o
|
||||
│ ├── prog.o
|
||||
│ ├── rand.o
|
||||
│ ├── sprintf.o
|
||||
|
@ -99,7 +103,7 @@
|
|||
│ │ ├── atoi.c
|
||||
│ │ ├── ctype.c
|
||||
│ │ ├── itoa.c
|
||||
│ │ ├── memory.c
|
||||
│ │ ├── mem.c
|
||||
│ │ ├── rand.c
|
||||
│ │ ├── sprintf.c
|
||||
│ │ ├── status.c
|
||||
|
@ -129,6 +133,7 @@
|
|||
│ │ ├── kernel
|
||||
│ │ │ ├── base.h
|
||||
│ │ │ ├── cpu.h
|
||||
│ │ │ ├── heap.h
|
||||
│ │ │ ├── iomisc.h
|
||||
│ │ │ ├── mm.h
|
||||
│ │ │ ├── panic.h
|
||||
|
@ -151,6 +156,8 @@
|
|||
│ ├── ke
|
||||
│ │ └── panic.c
|
||||
│ ├── mm
|
||||
│ │ ├── heap.c
|
||||
│ │ └── malloc.c
|
||||
│ └── proc
|
||||
│ ├── Makefile
|
||||
│ └── sched.c
|
||||
|
@ -161,4 +168,4 @@
|
|||
├── qemu.log
|
||||
└── Readme.md
|
||||
|
||||
31 directories, 105 files
|
||||
33 directories, 110 files
|
||||
|
|
|
@ -23,9 +23,31 @@
|
|||
//----------------------------------------------------------------------------//
|
||||
|
||||
#include <kalbase.h>
|
||||
#include <extras/malloc.h>
|
||||
|
||||
/* DO NOT compile with strict aliasing on */
|
||||
|
||||
//------------------------------------------//
|
||||
// Memory allocation //
|
||||
//------------------------------------------//
|
||||
|
||||
void *malloc(size_t n)
|
||||
{
|
||||
void *ptr;
|
||||
error_t rc;
|
||||
|
||||
rc = KalAllocMemory(&ptr, n, 0, 0);
|
||||
__set_errno(rc);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void free(void *ptr)
|
||||
{
|
||||
error_t rc = KalFreeMemory(ptr);
|
||||
(void)rc;
|
||||
}
|
||||
|
||||
//------------------------------------------//
|
||||
// memset() family //
|
||||
//------------------------------------------//
|
|
@ -47,17 +47,15 @@ const char *KalGetProgVersion(void)
|
|||
return __progvers;
|
||||
}
|
||||
|
||||
bool KalSetProgVers(const char *vers)
|
||||
error_t KalSetProgVers(const char *vers)
|
||||
{
|
||||
(void)vers;
|
||||
__set_errno(ENOSYS);
|
||||
return false;
|
||||
return ENOSYS;
|
||||
}
|
||||
|
||||
bool KalSetProgName(const char *name)
|
||||
error_t KalSetProgName(const char *name)
|
||||
{
|
||||
(void)name;
|
||||
__set_errno(ENOSYS);
|
||||
return false;
|
||||
return ENOSYS;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,19 @@
|
|||
|
||||
//------------------------------------------//
|
||||
|
||||
#ifndef _NO_UNITS
|
||||
#define KB (1UL << 10)
|
||||
#define MB (1UL << 20)
|
||||
#define GB (1UL << 30)
|
||||
#define TB (1UL << 40)
|
||||
#endif
|
||||
|
||||
#ifndef _ALIGN_UP
|
||||
#define _ALIGN_UP(x, s) (((x) + (s) - 1) & (~((s) - 1)))
|
||||
#endif
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
#ifndef __BEGIN_DECLS
|
||||
#ifdef __cpluplus
|
||||
# define __EXTERN_C extern "C"
|
||||
|
|
|
@ -66,7 +66,7 @@ extern error_t __errno;
|
|||
#define errno __errno
|
||||
#endif
|
||||
|
||||
#define __get_errno(x) error_t x = errno;
|
||||
#define __get_errno(x) error_t x = errno
|
||||
#define __set_errno(x) (errno = (x))
|
||||
|
||||
#else
|
||||
|
@ -183,6 +183,11 @@ unsigned long strtoul(const char *restrict, char **restrict, int);
|
|||
|
||||
//------------------------------------------//
|
||||
|
||||
void *malloc(size_t) __attribute__((__malloc__));
|
||||
void free(void *);
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
int rand(void);
|
||||
void srand(unsigned int);
|
||||
|
||||
|
|
|
@ -69,11 +69,14 @@
|
|||
// Functionality not implemented
|
||||
#define ENOSYS 38
|
||||
|
||||
// Component crashed
|
||||
#define ECRASH 500
|
||||
// Address already in use
|
||||
#define EADDRINUSE 98
|
||||
|
||||
// System is panicking
|
||||
#define EPANIC 600
|
||||
// Failure (unspecified reason)
|
||||
#define EFAILED 256
|
||||
|
||||
// Alignment error
|
||||
#define EALIGN 257
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
|
|
|
@ -35,16 +35,34 @@ extern "C" {
|
|||
|
||||
//------------------------------------------//
|
||||
|
||||
//
|
||||
// ¯\_(ツ)_/¯
|
||||
//
|
||||
#ifndef _STDLIB_H
|
||||
void *malloc(unsigned long);
|
||||
void free(void *);
|
||||
#endif
|
||||
// Flags for KalAllocMemory
|
||||
enum
|
||||
{
|
||||
// Return zeroed-out memory
|
||||
M_ZEROED = 1,
|
||||
|
||||
#define KalAllocMemory malloc
|
||||
#define KalFreeMemory free
|
||||
// Crash if allocation fails (ENABLED by default in the kernel)
|
||||
M_FAILCRASH = 2,
|
||||
|
||||
// Do NOT crash if allocation failed (meaningless outside of the kernel)
|
||||
M_CANFAIL = 4,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
// Default memory allocation alignment (in bytes)
|
||||
// Asking KalAllocMemory for an alignment of 0
|
||||
// will cause it to use this value
|
||||
M_DEFAULT_ALIGNMENT = alignof(QWORD),
|
||||
|
||||
// Minimal memory allocation alignment (in bytes)
|
||||
// Asking KalAllocMemory for an nonzero alignment
|
||||
// lower than this value will cause a EALIGN error
|
||||
M_MINIMAL_ALIGNMENT = M_DEFAULT_ALIGNMENT
|
||||
};
|
||||
|
||||
error_t KalAllocMemory(void **ptr, size_t req, int flags, size_t align);
|
||||
error_t KalFreeMemory(void *ptr);
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
|
|
|
@ -43,8 +43,8 @@ extern const char *__progvers;
|
|||
const char *KalGetProgName(void);
|
||||
const char *KalGetProgVersion(void);
|
||||
|
||||
bool KalSetProgVers(const char *);
|
||||
bool KalSetProgName(const char *);
|
||||
error_t KalSetProgVers(const char *);
|
||||
error_t KalSetProgName(const char *);
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
|
|
|
@ -45,7 +45,6 @@ typedef struct Processor_t Processor_t;
|
|||
|
||||
typedef enum ProcState_t ProcState_t;
|
||||
typedef enum TermColor_t TermColor_t;
|
||||
typedef enum KernelState_t KernelState_t;
|
||||
|
||||
//------------------------------------------//
|
||||
// Multiprocessor misc. //
|
||||
|
|
|
@ -22,8 +22,21 @@
|
|||
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
#ifndef _KALKERN_BASE_H
|
||||
#include <kernel/base.h>
|
||||
#endif
|
||||
|
||||
#ifndef _KALKERN_CPU_H
|
||||
#define _KALKERN_CPU_H
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
#define cpuid(in, a, b, c, d) asm("cpuid" \
|
||||
: "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
|
||||
: "a" (in) \
|
||||
);
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
//----------------------------------------------------------------------------//
|
||||
// GNU GPL OS/K //
|
||||
// //
|
||||
// Desc: Memory related functions //
|
||||
// //
|
||||
// //
|
||||
// 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/>. //
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
#ifndef _KALKERN_BASE_H
|
||||
#include <kernel/base.h>
|
||||
#endif
|
||||
|
||||
#ifndef _KALKERN_MM_H
|
||||
#define _KALKERN_MM_H
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
void *GetMemoryMap(void);
|
||||
|
||||
size_t GetMemorySize(void);
|
||||
size_t GetAvailZoneSize(void *);
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
#define _HEAP_START (4 * MB)
|
||||
|
||||
void InitHeap(void);
|
||||
|
||||
void LockHeap(void);
|
||||
void UnlockHeap(void);
|
||||
|
||||
size_t GetHeapSize(void);
|
||||
size_t GetMaxHeapSize(void);
|
||||
error_t SetMaxHeapSize(size_t);
|
||||
|
||||
error_t GrowHeap(size_t);
|
||||
error_t ShrinkHeap(size_t);
|
||||
|
||||
//------------------------------------------//
|
||||
|
||||
#endif
|
||||
|
|
@ -22,7 +22,6 @@
|
|||
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
|
||||
#ifndef _KALKERN_BASE_H
|
||||
#include <kernel/base.h>
|
||||
#endif
|
||||
|
|
|
@ -21,3 +21,4 @@
|
|||
// You should have received a copy of the GNU General Public License //
|
||||
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
|
|
|
@ -21,13 +21,11 @@
|
|||
// 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 <multiboot/multiboot.h>
|
||||
#include <kernel/term.h>
|
||||
#include <kernel/panic.h>
|
||||
|
||||
|
||||
extern void testf(void);
|
||||
|
||||
//
|
||||
// Entry point of the Kaleid kernel
|
||||
//
|
||||
|
@ -39,8 +37,7 @@ noreturn void StartKern(multiboot_info_t *mbInfo, int mbMagic)
|
|||
// Kernel terminals
|
||||
InitTerms();
|
||||
|
||||
// We're out
|
||||
StartPanic( "We were loaded by : %s\n\n\n"
|
||||
KernLog( "We were loaded by : %s\n\n"
|
||||
"We get\n"
|
||||
" *mbInfo : %p\n"
|
||||
" mbMagic : %x\n"
|
||||
|
@ -56,4 +53,7 @@ noreturn void StartKern(multiboot_info_t *mbInfo, int mbMagic)
|
|||
mbInfo->mmap_addr,
|
||||
mbInfo->mmap_length
|
||||
);
|
||||
|
||||
// Get out
|
||||
CrashSystem();
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ extern void VGA_Init(void);
|
|||
extern Terminal_t VGA_Terminal;
|
||||
|
||||
//
|
||||
// Initialize standard output
|
||||
// Initializes standard output
|
||||
//
|
||||
void InitTerms(void)
|
||||
{
|
||||
|
@ -43,7 +43,7 @@ void InitTerms(void)
|
|||
}
|
||||
|
||||
//
|
||||
// Fill terminal with spaces
|
||||
// Fills terminal with spaces
|
||||
//
|
||||
error_t ClearTerm(Terminal_t *term)
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ error_t ClearTerm(Terminal_t *term)
|
|||
}
|
||||
|
||||
//
|
||||
// Change the color code
|
||||
// Changes the color code
|
||||
//
|
||||
error_t ChTermColor(Terminal_t *term, TermColor_t fgColor, TermColor_t bgColor)
|
||||
{
|
||||
|
@ -81,7 +81,7 @@ error_t ChTermColor(Terminal_t *term, TermColor_t fgColor, TermColor_t bgColor)
|
|||
}
|
||||
|
||||
//
|
||||
// Write a single character on the terminal
|
||||
// Writes a single character on the terminal
|
||||
//
|
||||
error_t PutOnTerm(Terminal_t *term, char ch)
|
||||
{
|
||||
|
@ -98,7 +98,7 @@ error_t PutOnTerm(Terminal_t *term, char ch)
|
|||
}
|
||||
|
||||
//
|
||||
// Print string on terminal
|
||||
// Prints string on terminal
|
||||
//
|
||||
error_t PrintOnTerm(Terminal_t *term, const char *str)
|
||||
{
|
||||
|
@ -117,7 +117,7 @@ error_t PrintOnTerm(Terminal_t *term, const char *str)
|
|||
}
|
||||
|
||||
//
|
||||
// Print formatted string on standard output
|
||||
// Prints formatted string on standard output
|
||||
// Prints at most KLOG_MAX_BUFSIZE characters
|
||||
//
|
||||
error_t KernLog(const char *fmt, ...)
|
||||
|
@ -130,12 +130,12 @@ error_t KernLog(const char *fmt, ...)
|
|||
vsnprintf(logbuf, KLOG_MAX_BUFSIZE, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return PrintOnTerm(stdOut, logbuf);
|
||||
return PrintOnTerm(GetStdOut(), logbuf);
|
||||
}
|
||||
|
||||
#ifndef _NO_DEBUG
|
||||
//
|
||||
// Print formatted string on debug output
|
||||
// Prints formatted string on debug output
|
||||
// Prints at most KLOG_MAX_BUFSIZE characters
|
||||
//
|
||||
error_t DebugLog(const char *fmt, ...)
|
||||
|
@ -148,7 +148,7 @@ error_t DebugLog(const char *fmt, ...)
|
|||
vsnprintf(logbuf, KLOG_MAX_BUFSIZE, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return PrintOnTerm(stdDbg, logbuf);
|
||||
return PrintOnTerm(GetStdDbg(), logbuf);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
#define VGA_ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8)
|
||||
|
||||
//
|
||||
// Clear terminal
|
||||
// Clears terminal
|
||||
//
|
||||
error_t VGA_ClearTermUnlocked(Terminal_t *term)
|
||||
{
|
||||
|
@ -55,7 +55,7 @@ error_t VGA_ClearTermUnlocked(Terminal_t *term)
|
|||
}
|
||||
|
||||
//
|
||||
// Write a single character on the terminal
|
||||
// Writes a single character on the terminal
|
||||
//
|
||||
error_t VGA_PutOnTermUnlocked(Terminal_t *term, char ch)
|
||||
{
|
||||
|
@ -106,7 +106,7 @@ error_t VGA_PutOnTermUnlocked(Terminal_t *term, char ch)
|
|||
}
|
||||
|
||||
//
|
||||
// Print string on terminal
|
||||
// Prints string on terminal
|
||||
//
|
||||
error_t VGA_PrintOnTermUnlocked(Terminal_t *term, const char *str)
|
||||
{
|
||||
|
|
|
@ -37,7 +37,7 @@ noreturn void __assert_handler(const char *msg,
|
|||
|
||||
(void)file; (void)line; (void)func;
|
||||
|
||||
StartPanic("cpu%d: In function '%s', from %s line %s - assert() failed: '%s'",
|
||||
StartPanic("cpu%d: In function '%s', from %s line %d - assert() failed: '%s'",
|
||||
_GetCurCPU(), func, file, line, msg);
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,9 @@ noreturn void StartPanic(const char *fmt, ...)
|
|||
//
|
||||
noreturn void CrashSystem(void)
|
||||
{
|
||||
while (1) {
|
||||
DisableIRQs();
|
||||
HaltCPU();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
//----------------------------------------------------------------------------//
|
||||
// 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;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
//----------------------------------------------------------------------------//
|
||||
// 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>
|
||||
|
||||
error_t KalAllocMemory(void **ptr, size_t req, int flags, size_t align)
|
||||
{
|
||||
error_t rc;
|
||||
size_t brk;
|
||||
|
||||
if (align == 0) align = M_DEFAULT_ALIGNMENT;
|
||||
|
||||
if (align < M_MINIMAL_ALIGNMENT) {
|
||||
return EALIGN;
|
||||
}
|
||||
|
||||
LockHeap();
|
||||
|
||||
brk = _HEAP_START + GetHeapSize();
|
||||
req = _ALIGN_UP(req + brk, align) - brk;
|
||||
|
||||
rc = GrowHeap(req);
|
||||
|
||||
UnlockHeap();
|
||||
|
||||
if (rc) {
|
||||
if ((flags & M_CANFAIL) == 0)
|
||||
return rc;
|
||||
StartPanic("Out of memory");
|
||||
}
|
||||
|
||||
if (flags & M_ZEROED) {
|
||||
memzero(*ptr, req);
|
||||
}
|
||||
|
||||
*ptr = (void *)brk;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
//
|
||||
// Frees allocated memory
|
||||
//
|
||||
error_t KalFreeMemory(void *ptr)
|
||||
{
|
||||
(void)ptr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue