allocation

This commit is contained in:
Adrien Bourmault 2019-03-18 18:00:40 +01:00
commit 8a7b559fe9
20 changed files with 407 additions and 62 deletions

View File

@ -32,11 +32,11 @@ CCNAME=x86_64-elf-gcc
ASMFLAGS=-f elf64 ASMFLAGS=-f elf64
LDFLAGS=-melf_x86_64 LDFLAGS=-melf_x86_64
COPTIM=-O2 COPTIM=-O2
CWARNS=-Wall -Wextra # -Werror=implicit-function-declaration CWARNS=-Wall -Wextra -Werror=implicit-function-declaration
CINCLUDES=-Ikaleid/include 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 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 KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) -D_OSK_SOURCE -D_KALEID_KERNEL
#Folders #Folders
@ -62,7 +62,7 @@ all : OS/K
# Common objects # Common objects
kal_com_obj= $(KOBJDIR)/atoi.o $(KOBJDIR)/ctype.o \ 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)/rand.o $(KOBJDIR)/sprintf.o \
$(KOBJDIR)/status.o $(KOBJDIR)/string.o \ $(KOBJDIR)/status.o $(KOBJDIR)/string.o \
$(KOBJDIR)/strtol.o $(KOBJDIR)/argv.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 $(KOBJDIR)/ultoa.o: $(KERNELDIR)/crtlib/itoa.c
@$(KCC) -D_NEED_ULTOA $< -o $@ @$(KCC) -D_NEED_ULTOA $< -o $@
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/memory.o: $(KERNELDIR)/crtlib/memory.c $(KOBJDIR)/mem.o: $(KERNELDIR)/crtlib/mem.c
@$(KCC) -fno-strict-aliasing $< -o $@ @$(KCC) -fno-strict-aliasing $< -o $@
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/rand.o: $(KERNELDIR)/crtlib/rand.c $(KOBJDIR)/rand.o: $(KERNELDIR)/crtlib/rand.c
@ -124,10 +124,18 @@ $(KOBJDIR)/prog.o: $(KERNELDIR)/extras/prog.c
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}
# Kernel objects # Kernel objects
<<<<<<< HEAD
kal_kern_obj= $(KOBJDIR)/kernel/cpuid.o $(KOBJDIR)/kernel/init.o \ kal_kern_obj= $(KOBJDIR)/kernel/cpuid.o $(KOBJDIR)/kernel/init.o \
$(KOBJDIR)/kernel/table.o $(KOBJDIR)/kernel/cursor.o \ $(KOBJDIR)/kernel/table.o $(KOBJDIR)/kernel/cursor.o \
$(KOBJDIR)/kernel/term.o $(KOBJDIR)/kernel/vga.o \ $(KOBJDIR)/kernel/term.o $(KOBJDIR)/kernel/vga.o \
$(KOBJDIR)/kernel/panic.o $(KOBJDIR)/kernel/map.o \ $(KOBJDIR)/kernel/panic.o $(KOBJDIR)/kernel/map.o \
=======
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/heap.o \
$(KOBJDIR)/kernel/malloc.o
>>>>>>> BetterTerm
$(KOBJDIR)/kernel/cpuid.o: $(KERNELDIR)/kernel/cpu/cpuid.c $(KOBJDIR)/kernel/cpuid.o: $(KERNELDIR)/kernel/cpu/cpuid.c
@$(KCC) $< -o $@ @$(KCC) $< -o $@
@ -150,7 +158,14 @@ $(KOBJDIR)/kernel/vga.o: $(KERNELDIR)/kernel/io/vga.c
$(KOBJDIR)/kernel/panic.o: $(KERNELDIR)/kernel/ke/panic.c $(KOBJDIR)/kernel/panic.o: $(KERNELDIR)/kernel/ke/panic.c
@$(KCC) $< -o $@ @$(KCC) $< -o $@
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}
<<<<<<< HEAD
$(KOBJDIR)/kernel/map.o: $(KERNELDIR)/kernel/mm/map.c $(KOBJDIR)/kernel/map.o: $(KERNELDIR)/kernel/mm/map.c
=======
$(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
>>>>>>> BetterTerm
@$(KCC) $< -o $@ @$(KCC) $< -o $@
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}

View File

@ -62,11 +62,14 @@
│   ├── atol.o │   ├── atol.o
│   ├── atoul.o │   ├── atoul.o
│   ├── atou.o │   ├── atou.o
│   ├── crtlib
│   ├── ctype.o │   ├── ctype.o
│   ├── extras
│   ├── itoa.o │   ├── itoa.o
│   ├── kernel │   ├── kernel
│   │   ├── cpuid.o │   │   ├── cpuid.o
│   │   ├── cursor.o │   │   ├── cursor.o
│   │   ├── heap.o
│   │   ├── init │   │   ├── init
│   │   │   ├── init.o │   │   │   ├── init.o
│   │   │   └── table.o │   │   │   └── table.o
@ -77,12 +80,13 @@
│   │   │   └── vga.o │   │   │   └── vga.o
│   │   ├── ke │   │   ├── ke
│   │   │   └── panic.o │   │   │   └── panic.o
│   │   ├── malloc.o
│   │   ├── panic.o │   │   ├── panic.o
│   │   ├── table.o │   │   ├── table.o
│   │   ├── term.o │   │   ├── term.o
│   │   └── vga.o │   │   └── vga.o
│   ├── ltoa.o │   ├── ltoa.o
│   ├── memory.o │   ├── mem.o
│   ├── prog.o │   ├── prog.o
│   ├── rand.o │   ├── rand.o
│   ├── sprintf.o │   ├── sprintf.o
@ -99,7 +103,7 @@
│   │   ├── atoi.c │   │   ├── atoi.c
│   │   ├── ctype.c │   │   ├── ctype.c
│   │   ├── itoa.c │   │   ├── itoa.c
│   │   ├── memory.c │   │   ├── mem.c
│   │   ├── rand.c │   │   ├── rand.c
│   │   ├── sprintf.c │   │   ├── sprintf.c
│   │   ├── status.c │   │   ├── status.c
@ -129,6 +133,7 @@
│   │   ├── kernel │   │   ├── kernel
│   │   │   ├── base.h │   │   │   ├── base.h
│   │   │   ├── cpu.h │   │   │   ├── cpu.h
│   │   │   ├── heap.h
│   │   │   ├── iomisc.h │   │   │   ├── iomisc.h
│   │   │   ├── mm.h │   │   │   ├── mm.h
│   │   │   ├── panic.h │   │   │   ├── panic.h
@ -151,6 +156,8 @@
│   ├── ke │   ├── ke
│   │   └── panic.c │   │   └── panic.c
│   ├── mm │   ├── mm
│   │   ├── heap.c
│   │   └── malloc.c
│   └── proc │   └── proc
│   ├── Makefile │   ├── Makefile
│   └── sched.c │   └── sched.c
@ -161,4 +168,4 @@
├── qemu.log ├── qemu.log
└── Readme.md └── Readme.md
31 directories, 105 files 33 directories, 110 files

View File

@ -23,9 +23,31 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <kalbase.h> #include <kalbase.h>
#include <extras/malloc.h>
/* DO NOT compile with strict aliasing on */ /* 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 // // memset() family //
//------------------------------------------// //------------------------------------------//

View File

@ -47,17 +47,15 @@ const char *KalGetProgVersion(void)
return __progvers; return __progvers;
} }
bool KalSetProgVers(const char *vers) error_t KalSetProgVers(const char *vers)
{ {
(void)vers; (void)vers;
__set_errno(ENOSYS); return ENOSYS;
return false;
} }
bool KalSetProgName(const char *name) error_t KalSetProgName(const char *name)
{ {
(void)name; (void)name;
__set_errno(ENOSYS); return ENOSYS;
return false;
} }

View File

@ -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 #ifndef __BEGIN_DECLS
#ifdef __cpluplus #ifdef __cpluplus
# define __EXTERN_C extern "C" # define __EXTERN_C extern "C"

View File

@ -66,7 +66,7 @@ extern error_t __errno;
#define errno __errno #define errno __errno
#endif #endif
#define __get_errno(x) error_t x = errno; #define __get_errno(x) error_t x = errno
#define __set_errno(x) (errno = (x)) #define __set_errno(x) (errno = (x))
#else #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); int rand(void);
void srand(unsigned int); void srand(unsigned int);

View File

@ -28,52 +28,55 @@
//------------------------------------------// //------------------------------------------//
// Everything went fine // Everything went fine
#define EOK 0 #define EOK 0
// Operation not permitted // Operation not permitted
#define EPERM 1 #define EPERM 1
// No such file or directory // No such file or directory
#define ENOENT 2 #define ENOENT 2
// No such process // No such process
#define ESRCH 3 #define ESRCH 3
// Syscall interrupted (e.g. by signal) // Syscall interrupted (e.g. by signal)
#define EINTR 4 #define EINTR 4
// I/0 error // I/0 error
#define EIO 5 #define EIO 5
// No such device or address // No such device or address
#define ENXIO 6 #define ENXIO 6
// Argument list too long // Argument list too long
#define E2BIG 7 #define E2BIG 7
// Not an executable format // Not an executable format
#define ENOEXEC 8 #define ENOEXEC 8
// Bad file number // Bad file number
#define EBADF 9 #define EBADF 9
// Try again // Try again
#define EAGAIN 11 #define EAGAIN 11
// Out of memory // Out of memory
#define ENOMEM 12 #define ENOMEM 12
// Invalid argument // Invalid argument
#define EINVAL 22 #define EINVAL 22
// Functionality not implemented // Functionality not implemented
#define ENOSYS 38 #define ENOSYS 38
// Component crashed // Address already in use
#define ECRASH 500 #define EADDRINUSE 98
// System is panicking // Failure (unspecified reason)
#define EPANIC 600 #define EFAILED 256
// Alignment error
#define EALIGN 257
//------------------------------------------// //------------------------------------------//

View File

@ -35,16 +35,34 @@ extern "C" {
//------------------------------------------// //------------------------------------------//
// // Flags for KalAllocMemory
// ¯\_(ツ)_/¯ enum
// {
#ifndef _STDLIB_H // Return zeroed-out memory
void *malloc(unsigned long); M_ZEROED = 1,
void free(void *);
#endif
#define KalAllocMemory malloc // Crash if allocation fails (ENABLED by default in the kernel)
#define KalFreeMemory free 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);
//------------------------------------------// //------------------------------------------//

View File

@ -43,8 +43,8 @@ extern const char *__progvers;
const char *KalGetProgName(void); const char *KalGetProgName(void);
const char *KalGetProgVersion(void); const char *KalGetProgVersion(void);
bool KalSetProgVers(const char *); error_t KalSetProgVers(const char *);
bool KalSetProgName(const char *); error_t KalSetProgName(const char *);
//------------------------------------------// //------------------------------------------//

View File

@ -46,7 +46,6 @@ typedef struct MemoryMap_t MemoryMap_t;
typedef enum ProcState_t ProcState_t; typedef enum ProcState_t ProcState_t;
typedef enum TermColor_t TermColor_t; typedef enum TermColor_t TermColor_t;
typedef enum KernelState_t KernelState_t;
//------------------------------------------// //------------------------------------------//
// Multiprocessor misc. // // Multiprocessor misc. //

View File

@ -22,8 +22,21 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // 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" \ #define cpuid(in, a, b, c, d) asm("cpuid" \
: "=a" (a), "=b" (b), "=c" (c), "=d" (d) \ : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
: "a" (in) \ : "a" (in) \
); );
//------------------------------------------//
#endif

View File

@ -0,0 +1,51 @@
//----------------------------------------------------------------------------//
// 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
//------------------------------------------//
#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

View File

@ -22,7 +22,6 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#ifndef _KALKERN_BASE_H #ifndef _KALKERN_BASE_H
#include <kernel/base.h> #include <kernel/base.h>
#endif #endif

View File

@ -21,6 +21,7 @@
// You should have received a copy of the GNU General Public License // // You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <multiboot/multiboot.h> #include <multiboot/multiboot.h>
#include <kernel/term.h> #include <kernel/term.h>
#include <kernel/panic.h> #include <kernel/panic.h>
@ -44,3 +45,4 @@ noreturn void StartKern(multiboot_info_t *mbInfo, int mbMagic)
KernLog("Yup, we have magic : %x", mbMagic); KernLog("Yup, we have magic : %x", mbMagic);
CrashSystem(); //yay CrashSystem(); //yay
} }

View File

@ -30,4 +30,3 @@ Processor_t cpuTable[NCPUS] = {0};
MemoryMap_t memTab = {0}; MemoryMap_t memTab = {0};
Terminal_t *stdOut = 0, *stdDbg = 0; Terminal_t *stdOut = 0, *stdDbg = 0;

View File

@ -28,7 +28,7 @@ extern void VGA_Init(void);
extern Terminal_t VGA_Terminal; extern Terminal_t VGA_Terminal;
// //
// Initialize standard output // Initializes standard output
// //
void InitTerms(void) void InitTerms(void)
{ {
@ -43,7 +43,7 @@ void InitTerms(void)
} }
// //
// Fill terminal with spaces // Fills terminal with spaces
// //
error_t ClearTerm(Terminal_t *term) 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) 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) 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) 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 // Prints at most KLOG_MAX_BUFSIZE characters
// //
error_t KernLog(const char *fmt, ...) error_t KernLog(const char *fmt, ...)
@ -130,12 +130,12 @@ error_t KernLog(const char *fmt, ...)
vsnprintf(logbuf, KLOG_MAX_BUFSIZE, fmt, ap); vsnprintf(logbuf, KLOG_MAX_BUFSIZE, fmt, ap);
va_end(ap); va_end(ap);
return PrintOnTerm(stdOut, logbuf); return PrintOnTerm(GetStdOut(), logbuf);
} }
#ifndef _NO_DEBUG #ifndef _NO_DEBUG
// //
// Print formatted string on debug output // Prints formatted string on debug output
// Prints at most KLOG_MAX_BUFSIZE characters // Prints at most KLOG_MAX_BUFSIZE characters
// //
error_t DebugLog(const char *fmt, ...) error_t DebugLog(const char *fmt, ...)
@ -148,7 +148,7 @@ error_t DebugLog(const char *fmt, ...)
vsnprintf(logbuf, KLOG_MAX_BUFSIZE, fmt, ap); vsnprintf(logbuf, KLOG_MAX_BUFSIZE, fmt, ap);
va_end(ap); va_end(ap);
return PrintOnTerm(stdDbg, logbuf); return PrintOnTerm(GetStdDbg(), logbuf);
} }
#endif #endif

View File

@ -37,7 +37,7 @@
#define VGA_ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8) #define VGA_ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8)
// //
// Clear terminal // Clears terminal
// //
error_t VGA_ClearTermUnlocked(Terminal_t *term) 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) 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) error_t VGA_PrintOnTermUnlocked(Terminal_t *term, const char *str)
{ {

View File

@ -37,7 +37,7 @@ noreturn void __assert_handler(const char *msg,
(void)file; (void)line; (void)func; (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); _GetCurCPU(), func, file, line, msg);
} }
@ -80,7 +80,9 @@ noreturn void StartPanic(const char *fmt, ...)
// //
noreturn void CrashSystem(void) noreturn void CrashSystem(void)
{ {
DisableIRQs(); while (1) {
HaltCPU(); DisableIRQs();
HaltCPU();
}
} }

129
kaleid/kernel/mm/heap.c Normal file
View File

@ -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;
}

70
kaleid/kernel/mm/malloc.c Normal file
View File

@ -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;
}