Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: akoskovacs/Akosix
base: f862d067a2
...
head fork: akoskovacs/Akosix
compare: 77238ee0cd
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
4 Makefile
@@ -6,8 +6,8 @@ RM = rm
SH = bash
QEMU = qemu
-CFLAGS := -std=c99 -Wall -O3 -march=i586 -nostdinc -fno-builtin -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -Wall -Wextra -Werror -ffreestanding -Wno-unused -ggdb
-ASFLAGS := -Wall -Wextra -Werror -ggdb
+CFLAGS := -std=c99 -Wall -O3 -march=i586 -nostdinc -fno-builtin -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -Wall -Wextra -Werror -ffreestanding -Wno-unused -ggdb -m32
+ASFLAGS := -Wall -Wextra -Werror -ggdb -m32
LDFLAGS := -nostartfiles -nodefaultlibs -nostdlib -static -ggdb -T linker.ld
INCLUDES := -I include -I lib/include
TARGET := akx_kernel
View
2  include/kernel.h
@@ -4,3 +4,5 @@
int vsnprintf(char *, size_t, const char *, va_list);
int snprintf(char *, size_t, const char *, ...);
int kprintf(const char *fmt, ...);
+
+void hang();
View
4 include/types.h
@@ -1,10 +1,6 @@
#ifndef TYPES_H
#define TYPES_H
-#ifdef __x86_64__
-#error "Sorry, but you cannot compile Akosix in a 64-bit environment, yet. :-("
-#endif
-
typedef unsigned long size_t;
typedef unsigned long long off_t;
typedef unsigned long paddr_t;
View
2  lib/include/queue.h
@@ -33,7 +33,7 @@
#ifndef QUEUE_H
#define QUEUE_H
-#include <type.h>
+#include <types.h>
/*
* This file defines four types of data structures: singly-linked lists,
View
152 mm/memory.c
@@ -1,24 +1,33 @@
-#include <types.h>
-#include <multiboot.h>
-#include <memory.h>
#include <kernel.h>
+#include <memory.h>
+#include <multiboot.h>
+#include <queue.h>
#include <string.h>
+#include <types.h>
-struct kmem_block {
-#define KMEM_BLOCK_MAGIC 0xDEAD
- uint16_t kb_magic;
-#define KMEM_BLOCK_FREE 0x0000
-#define KMEM_BLOCK_USED 0x0001
-#define KMEM_ALIGNED 0x0002
- uint16_t kb_flags;
- size_t kb_size;
- struct kmem_block *kb_prev;
- struct kmem_block *kb_next;
+struct kmalloc_area {
+#define KMALLOC_AREA_MAGIC 0xDEAD
+ uint16_t ka_magic;
+#define KMALLOC_AREA_FREE 0x0000
+#define KMALLOC_AREA_USED 0x0001
+#define KMEM_ALIGNED 0x0002
+ uint16_t ka_flags;
+ size_t ka_size;
+ LIST_ENTRY(kmalloc_area) ka_entries;
+ STAILQ_ENTRY(kmalloc_area) ka_free_entries;
};
-struct kmem_block *first;
-struct kmem_block *last;
-struct kmem_block *last_freed;
+#define CHECK_KMALLOC_AREA_MAGIC(ptr) do { \
+if ((ptr)->ka_magic != KMALLOC_AREA_MAGIC) { \
+ kprintf("Panic! Kernel heap corrupted at: %p\n", (void *)ptr); \
+ hang(); } } while (0)
+
+static size_t biggest_free_size = 0;
+struct kmalloc_area *last_mem_area = NULL;
+static LIST_HEAD(, kmalloc_area) mem_areas =
+ LIST_HEAD_INITIALIZER(mem_areas);
+static STAILQ_HEAD(, kmalloc_area) free_mem_areas =
+ STAILQ_HEAD_INITIALIZER(free_mem_areas);
void memory_init(struct multiboot_info *mbi)
{
@@ -75,56 +84,57 @@ void *contract_kheap(size_t size)
void *kmalloc(size_t size, malloc_flags_t flags)
{
- void *ptr = NULL;
- struct kmem_block *malloc = NULL;
+ void *ptr;
+ struct kmalloc_area *marea;
+ struct kmalloc_area *tmp, *mtmp;
if (size == 0)
return expand_kheap(0);
-
+
if (flags & M_NORMAL) {
- if (last_freed != NULL && last_freed->kb_size == size
- && last_freed->kb_flags & KMEM_BLOCK_FREE) {
- malloc = last_freed;
- ptr += sizeof(struct kmem_block);
- last_freed = NULL;
+ if ((!STAILQ_EMPTY(&free_mem_areas)) && (size <= biggest_free_size))
+ STAILQ_FOREACH_MUTABLE(tmp, &free_mem_areas, ka_free_entries, mtmp) {
+ if (tmp->ka_size >= size) {
+ marea = tmp;
+ ptr += sizeof(struct kmalloc_area);
+ STAILQ_REMOVE(&free_mem_areas, tmp, kmalloc_area, ka_free_entries);
+ }
+ }
} else {
- ptr = expand_kheap(sizeof(struct kmem_block) + size);
+ ptr = expand_kheap(sizeof(struct kmalloc_area) + size);
if (ptr == NULL)
return NULL;
- malloc = (struct kmem_block *)ptr;
- ptr += sizeof(struct kmem_block);
+ marea = (struct kmalloc_area *)ptr;
+ ptr += sizeof(struct kmalloc_area);
}
} else if (flags & M_ALIGNED) {
void *kbrk = expand_kheap(0);
void *aligned_ptr = PAGE_ALIGN(kbrk) + PAGE_SIZE;
- if ((unsigned)(aligned_ptr - kbrk) > (sizeof(struct kmem_block)*2)) {
+ if (((unsigned int)(aligned_ptr - kbrk)) > (sizeof(struct kmalloc_area)*2)) {
/* We need a free block to cover the gap between the last allocated
* area and the new aligned area. It must have enough room to con-
- * tain it's own kmem_block and the new aligned kmem_block. */
- kfree(kmalloc(aligned_ptr-kbrk-(sizeof(struct kmem_block)*2), M_NORMAL));
+ * tain it's own kmalloc_area and the new aligned kmalloc_area. */
+ kfree(kmalloc(aligned_ptr-kbrk-(sizeof(struct kmalloc_area)*2), M_NORMAL));
} else { /* Too small to create a free gap */
/* TODO */
}
- malloc = (struct kmem_block *)
- expand_kheap(sizeof(struct kmem_block) + size);
- ptr = (void *)malloc + sizeof(struct kmem_block);
+ marea = (struct kmalloc_area *)
+ expand_kheap(sizeof(struct kmalloc_area) + size);
+ ptr = (void *)marea + sizeof(struct kmalloc_area);
+ } else {
+ return NULL;
}
- malloc->kb_size = size;
- malloc->kb_prev = last;
- malloc->kb_magic = KMEM_BLOCK_MAGIC;
- malloc->kb_flags = KMEM_BLOCK_USED;
- if (last != NULL) {
- if (last->kb_magic != KMEM_BLOCK_MAGIC) {
- kprintf("Panic! Kernel heap corrupted at: %p\n", last);
- return NULL;
- }
- last->kb_next = malloc;
- }
- if (first == NULL) {
- first = malloc;
- last = malloc;
+ marea->ka_size = size;
+ marea->ka_magic = KMALLOC_AREA_MAGIC;
+ marea->ka_flags = KMALLOC_AREA_USED;
+ if (last_mem_area != NULL) {
+ CHECK_KMALLOC_AREA_MAGIC(last_mem_area);
+ LIST_INSERT_AFTER(last_mem_area, marea, ka_entries);
+ } else {
+ LIST_INSERT_HEAD(&mem_areas, marea, ka_entries);
}
+ last_mem_area = marea;
if (flags & M_ZEROED) {
memclr(ptr, sizeof(size));
@@ -134,56 +144,36 @@ void *kmalloc(size_t size, malloc_flags_t flags)
void kfree(void *ptr)
{
- struct kmem_block *malloc;
- struct kmem_block *tmp;
- size_t size = 0;
- int blocks = 0;
+ struct kmalloc_area *marea;
if (ptr == NULL)
return;
- malloc = (struct kmem_block *)ptr - sizeof(struct kmem_block);
- if (malloc->kb_magic != KMEM_BLOCK_MAGIC) {
- kprintf("Panic! Kernel heap corrupted at: %p\n", last);
- return;
- }
-
- malloc->kb_flags = KMEM_BLOCK_FREE;
- tmp = malloc;
- while (tmp != NULL && tmp->kb_flags & KMEM_BLOCK_FREE) {
- size += tmp->kb_size;
- tmp = tmp->kb_prev;
- blocks++;
- }
- malloc = tmp;
+ marea = (struct kmalloc_area *)ptr - sizeof(struct kmalloc_area);
+ CHECK_KMALLOC_AREA_MAGIC(marea);
+ if (marea->ka_size > biggest_free_size)
+ biggest_free_size = marea->ka_size;
- tmp = malloc->kb_next;
- while (tmp != NULL && tmp->kb_flags & KMEM_BLOCK_FREE) {
- size += tmp->kb_size;
- tmp = tmp->kb_next;
- blocks++;
- }
- size += (blocks - 1) * sizeof(struct kmem_block);
- malloc->kb_size = size;
- malloc->kb_flags |= KMEM_BLOCK_FREE;
- last_freed = malloc;
+ marea->ka_flags &= KMALLOC_AREA_USED;
+ STAILQ_INSERT_TAIL(&free_mem_areas, marea, ka_free_entries);
}
#if defined DEBUG && defined DEBUG_MALLOC
void dump_kmallocs(void)
{
- struct kmem_block *tmp = first;
+ struct kmalloc_area *tmp;
kprintf("-------- DUMPING ALLOCATIONS --------\n");
- if (first == NULL) {
+ if (LIST_EMPTY(&mem_areas)) {
kprintf("No allocations, yet.\n");
} else {
- while (tmp->kb_next != NULL) {
+ LIST_FOREACH(tmp, &mem_areas, ka_entries) {
kprintf("Allocation size: %d\n"
"Allocation start: %p\n"
+ "Type: %s\n"
"Magic %s\n\n"
- , tmp->kb_size, (void *)tmp + sizeof(struct kmem_block)
- , (tmp->kb_magic == KMEM_BLOCK_MAGIC) ? "correct" : "uncorrect");
- tmp = tmp->kb_next;
+ , tmp->ka_size, (void *)tmp + sizeof(struct kmalloc_area)
+ , (tmp->ka_flags & KMALLOC_AREA_USED) ? "used" : "free"
+ , (tmp->ka_magic == KMALLOC_AREA_MAGIC) ? "correct" : "uncorrect");
}
}
kprintf("---- END OF DUMPING ALLOCATIONS -----\n");

No commit comments for this range

Something went wrong with that request. Please try again.