Quellcode durchsuchen

Patch support run first demo on dragonos (#5)

* malloc with error

* 修改,解决系统调用错误问题、递归调用问题

* patch-memory-align

* 恢复更改&条件编译

---------

Co-authored-by: longjin <longjin@RinGoTek.cn>
Co-authored-by: su <su@su-plain-machine.lan>
Gou Ngai vor 2 Jahren
Ursprung
Commit
20678720f1

+ 18 - 0
.vscode/c_cpp_properties.json

@@ -0,0 +1,18 @@
+{
+    "configurations": [
+        {
+            "name": "Linux",
+            "includePath": [
+                "${workspaceFolder}/**",
+                "${workspaceFolder}/include"
+            ],
+            "defines": [],
+            "compilerPath": "/usr/bin/clang",
+            "cStandard": "c17",
+            "cppStandard": "c++14",
+            "intelliSenseMode": "linux-clang-x64",
+            "configurationProvider": "ms-vscode.makefile-tools"
+        }
+    ],
+    "version": 4
+}

+ 1 - 0
init_dragonos_toolchain.sh

@@ -6,6 +6,7 @@ fi
 
 DRAGONOS_UNKNOWN_ELF_PATH=$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-dragonos
 mkdir -p ${DRAGONOS_UNKNOWN_ELF_PATH}/lib
+echo $DRAGONOS_UNKNOWN_ELF_PATH
 # 设置工具链配置文件
 echo   \
 "{\

+ 3 - 0
src/c/dlmalloc.c

@@ -665,6 +665,7 @@ MAX_RELEASE_CHECK_RATE   default: 4095 unless not HAVE_MMAP
 #ifndef MALLOC_FAILURE_ACTION
 #define MALLOC_FAILURE_ACTION  errno = ENOMEM;
 #endif  /* MALLOC_FAILURE_ACTION */
+
 #ifndef HAVE_MORECORE
 #if ONLY_MSPACES
 #define HAVE_MORECORE 0
@@ -5445,6 +5446,7 @@ mspace create_mspace(size_t capacity, int locked) {
   return (mspace)m;
 }
 
+
 mspace create_mspace_with_base(void* base, size_t capacity, int locked) {
   mstate m = 0;
   size_t msize;
@@ -5456,6 +5458,7 @@ mspace create_mspace_with_base(void* base, size_t capacity, int locked) {
     m->seg.sflags = EXTERN_BIT;
     set_lock(m, locked);
   }
+
   return (mspace)m;
 }
 

+ 412 - 0
src/c/malloc.c

@@ -0,0 +1,412 @@
+// Copyright (C) DragonOS Community  longjin
+
+// This program 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 2
+// of the License, or (at your option) any later version.
+
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+// Or you can visit https://www.gnu.org/licenses/gpl-2.0.html
+
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdint.h>
+
+#define PAGE_4K_SHIFT 12
+#define PAGE_2M_SHIFT 21
+#define PAGE_1G_SHIFT 30
+#define PAGE_GDT_SHIFT 39
+
+
+
+/***************************/
+
+// 不同大小的页的容量
+#define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT)
+#define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT)
+#define PAGE_1G_SIZE (1UL << PAGE_1G_SHIFT)
+
+// 屏蔽低于x的数值
+#define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
+#define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
+
+// 将addr按照x的上边界对齐
+//#define PAGE_4K_ALIGN(addr) (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
+//#define PAGE_2M_ALIGN(addr) (((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
+
+/**
+ * @brief 显式链表的结点
+ *
+ */
+typedef struct malloc_mem_chunk_t
+{
+    uint64_t length;                 // 整个块所占用的内存区域的大小
+    uint64_t padding;
+    struct malloc_mem_chunk_t *prev; // 上一个结点的指针
+    struct malloc_mem_chunk_t *next; // 下一个结点的指针
+} malloc_mem_chunk_t;
+
+static uint64_t brk_base_addr = 0;    // 堆区域的内存基地址
+static uint64_t brk_max_addr = 0;     // 堆区域的内存最大地址
+static uint64_t brk_managed_addr = 0; // 堆区域已经被管理的地址
+
+// 空闲链表
+//  按start_addr升序排序
+static malloc_mem_chunk_t *malloc_free_list = NULL;
+static malloc_mem_chunk_t *malloc_free_list_end = NULL; // 空闲链表的末尾结点
+
+static uint64_t count_last_free_size = 0; // 统计距离上一次回收内存,已经free了多少内存
+
+/**
+ * @brief 将块插入空闲链表
+ *
+ * @param ck 待插入的块
+ */
+static void malloc_insert_free_list(malloc_mem_chunk_t *ck);
+
+/**
+ * @brief 当堆顶空闲空间大于2个页的空间的时候,释放1个页
+ *
+ */
+static void release_brk();
+
+/**
+ * @brief 在链表中检索符合要求的空闲块(best fit)
+ *
+ * @param size 块的大小
+ * @return malloc_mem_chunk_t*
+ */
+static malloc_mem_chunk_t *malloc_query_free_chunk_bf(uint64_t size)
+{
+    // 在满足best fit的前提下,尽可能的使分配的内存在低地址
+    //  使得总的堆内存可以更快被释放
+
+    if (malloc_free_list == NULL)
+    {
+        return NULL;
+    }
+    malloc_mem_chunk_t *ptr = malloc_free_list;
+    malloc_mem_chunk_t *best = NULL;
+    // printf("query size=%d", size);
+    while (ptr != NULL)
+    {
+        // printf("ptr->length=%#010lx\n", ptr->length);
+        if (ptr->length == size)
+        {
+            best = ptr;
+            break;
+        }
+
+        if (ptr->length > size)
+        {
+            if (best == NULL)
+                best = ptr;
+            else if (best->length > ptr->length)
+                best = ptr;
+        }
+        ptr = ptr->next;
+    }
+
+    return best;
+}
+
+/**
+ * @brief 在链表中检索符合要求的空闲块(first fit)
+ *
+ * @param size
+ * @return malloc_mem_chunk_t*
+ */
+static malloc_mem_chunk_t *malloc_query_free_chunk_ff(uint64_t size)
+{
+    if (malloc_free_list == NULL)
+        return NULL;
+    malloc_mem_chunk_t *ptr = malloc_free_list;
+
+    while (ptr)
+    {
+        if (ptr->length >= size)
+        {
+            return ptr;
+        }
+        ptr = ptr->next;
+    }
+
+    return NULL;
+}
+
+/**
+ * @brief 扩容malloc管理的内存区域
+ *
+ * @param size 扩大的内存大小
+ */
+static int malloc_enlarge(int64_t size)
+{
+    if (brk_base_addr == 0) // 第一次调用,需要初始化
+    {
+        brk_base_addr = sbrk(0);
+        // printf("brk_base_addr=%#018lx\n", brk_base_addr);
+        brk_managed_addr = brk_base_addr;
+        brk_max_addr = sbrk(0);
+    }
+
+    int64_t free_space = brk_max_addr - brk_managed_addr;
+    // printf("size=%ld\tfree_space=%ld\n", size, free_space);
+    if (free_space < size) // 现有堆空间不足
+    {
+        if (sbrk(size - free_space) != (void *)(-1))
+            brk_max_addr = sbrk((0));
+        else
+        {
+            //put_string("malloc_enlarge(): no_mem\n", COLOR_YELLOW, COLOR_BLACK);
+            return -ENOMEM;
+        }
+
+        // printf("brk max addr = %#018lx\n", brk_max_addr);
+    }
+
+    // 扩展管理的堆空间
+    // 在新分配的内存的底部放置header
+    // printf("managed addr = %#018lx\n", brk_managed_addr);
+    malloc_mem_chunk_t *new_ck = (malloc_mem_chunk_t *)brk_managed_addr;
+    new_ck->length = brk_max_addr - brk_managed_addr;
+    // printf("new_ck->start_addr=%#018lx\tbrk_max_addr=%#018lx\tbrk_managed_addr=%#018lx\n", (uint64_t)new_ck, brk_max_addr, brk_managed_addr);
+    new_ck->prev = NULL;
+    new_ck->next = NULL;
+    brk_managed_addr = brk_max_addr;
+
+    malloc_insert_free_list(new_ck);
+
+    return 0;
+}
+
+/**
+ * @brief 合并空闲块
+ *
+ */
+static void malloc_merge_free_chunk()
+{
+    if (malloc_free_list == NULL)
+        return;
+    malloc_mem_chunk_t *ptr = malloc_free_list->next;
+    while (ptr != NULL)
+    {
+        // 内存块连续
+        if (((uint64_t)(ptr->prev) + ptr->prev->length == (uint64_t)ptr))
+        {
+            // printf("merged %#018lx  and %#018lx\n", (uint64_t)ptr, (uint64_t)(ptr->prev));
+            // 将ptr与前面的空闲块合并
+            ptr->prev->length += ptr->length;
+            ptr->prev->next = ptr->next;
+            if (ptr->next == NULL)
+                malloc_free_list_end = ptr->prev;
+            else
+                ptr->next->prev = ptr->prev;
+            // 由于内存组成结构的原因,不需要free掉header
+            ptr = ptr->prev;
+        }
+        ptr = ptr->next;
+    }
+}
+
+/**
+ * @brief 将块插入空闲链表
+ *
+ * @param ck 待插入的块
+ */
+static void malloc_insert_free_list(malloc_mem_chunk_t *ck)
+{
+    if (malloc_free_list == NULL) // 空闲链表为空
+    {
+        malloc_free_list = ck;
+        malloc_free_list_end = ck;
+        ck->prev = ck->next = NULL;
+        return;
+    }
+    else
+    {
+
+        malloc_mem_chunk_t *ptr = malloc_free_list;
+        while (ptr != NULL)
+        {
+            if ((uint64_t)ptr < (uint64_t)ck)
+            {
+                if (ptr->next == NULL) // 当前是最后一个项
+                {
+                    ptr->next = ck;
+                    ck->next = NULL;
+                    ck->prev = ptr;
+                    malloc_free_list_end = ck;
+                    break;
+                }
+                else if ((uint64_t)(ptr->next) > (uint64_t)ck)
+                {
+                    ck->prev = ptr;
+                    ck->next = ptr->next;
+                    ptr->next = ck;
+                    ck->next->prev = ck;
+                    break;
+                }
+            }
+            else // 在ptr之前插入
+            {
+
+                if (ptr->prev == NULL) // 是第一个项
+                {
+                    malloc_free_list = ck;
+                    ck->prev = NULL;
+                    ck->next = ptr;
+                    ptr->prev = ck;
+                    break;
+                }
+                else
+                {
+                    ck->prev = ptr->prev;
+                    ck->next = ptr;
+                    ck->prev->next = ck;
+                    ptr->prev = ck;
+                    break;
+                }
+            }
+            ptr = ptr->next;
+        }
+    }
+}
+
+/**
+ * @brief 获取一块堆内存
+ *
+ * @param size 内存大小
+ * @return void* 内存空间的指针
+ *
+ * 分配内存的时候,结点的prev next指针所占用的空间被当做空闲空间分配出去
+ */
+void *_dragonos_malloc(ssize_t size)
+{
+    
+    // 计算需要分配的块的大小
+    // reserve for len
+    if (size + 2*sizeof(uint64_t) <= sizeof(malloc_mem_chunk_t))
+        size = sizeof(malloc_mem_chunk_t);
+    else
+        size += 2*sizeof(uint64_t);
+
+    // 采用best fit
+    malloc_mem_chunk_t *ck = malloc_query_free_chunk_bf(size);
+
+    if (ck == NULL) // 没有空闲块
+    {
+        
+        // printf("no free blocks\n");
+        // 尝试合并空闲块
+        malloc_merge_free_chunk();
+        ck = malloc_query_free_chunk_bf(size);
+
+        // 找到了合适的块
+        if (ck)
+            goto found;
+
+        // printf("before enlarge\n");
+        // 找不到合适的块,扩容堆区域
+        if (malloc_enlarge(size) == -ENOMEM)
+            return (void *)-ENOMEM; // 内存不足
+
+        malloc_merge_free_chunk(); // 扩容后运行合并,否则会导致碎片
+
+        // 扩容后再次尝试获取
+
+        ck = malloc_query_free_chunk_bf(size);
+    }
+found:;
+
+    // printf("ck = %#018lx\n", (uint64_t)ck);
+    if (ck == NULL)
+        return (void *)-ENOMEM;
+    // printf("ck->prev=%#018lx ck->next=%#018lx\n", ck->prev, ck->next);
+    // 分配空闲块
+    // 从空闲链表取出
+    if (ck->prev == NULL) // 当前是链表的第一个块
+    {
+        malloc_free_list = ck->next;
+    }
+    else
+        ck->prev->next = ck->next;
+
+    if (ck->next != NULL) // 当前不是最后一个块
+        ck->next->prev = ck->prev;
+    else
+        malloc_free_list_end = ck->prev;
+
+    // 当前块剩余的空间还能容纳多一个结点的空间,则分裂当前块
+    if ((int64_t)(ck->length) - size > sizeof(malloc_mem_chunk_t))
+    {
+        // printf("seperate\n");
+        malloc_mem_chunk_t *new_ck = (malloc_mem_chunk_t *)(((uint64_t)ck) + size);
+        new_ck->length = ck->length - size;
+        new_ck->prev = new_ck->next = NULL;
+        // printf("new_ck=%#018lx, new_ck->length=%#010lx\n", (uint64_t)new_ck, new_ck->length);
+        ck->length = size;
+        malloc_insert_free_list(new_ck);
+    }
+    // printf("malloc done: %#018lx, length=%#018lx\n", ((uint64_t)ck + sizeof(uint64_t)), ck->length);
+    // 此时链表结点的指针的空间被分配出去
+    return (void *)((uint64_t)ck + sizeof(uint64_t));
+}
+
+/**
+ * @brief 当堆顶空闲空间大于2个页的空间的时候,释放1个页
+ *
+ */
+static void release_brk()
+{
+    // 先检测最顶上的块
+    // 由于块按照开始地址排列,因此找最后一个块
+    if (malloc_free_list_end == NULL)
+    {
+        printf("release(): free list end is null. \n");
+        return;
+    }
+    if ((uint64_t)malloc_free_list_end + malloc_free_list_end->length == brk_max_addr && (uint64_t)malloc_free_list_end <= brk_max_addr - (PAGE_2M_SIZE << 1))
+    {
+        int64_t delta = ((brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK) - PAGE_2M_SIZE;
+        // printf("(brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK=%#018lx\n ", (brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK);
+        // printf("PAGE_2M_SIZE=%#018lx\n", PAGE_2M_SIZE);
+        // printf("tdfghgbdfggkmfn=%#018lx\n ", (brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK - PAGE_2M_SIZE);
+        // printf("delta=%#018lx\n ", delta);
+        if (delta <= 0) // 不用释放内存
+            return;
+        sbrk(-delta);
+        brk_max_addr = sbrk(0);
+        brk_managed_addr = brk_max_addr;
+
+        malloc_free_list_end->length = brk_max_addr - (uint64_t)malloc_free_list_end;
+    }
+}
+/**
+ * @brief 释放一块堆内存
+ *
+ * @param ptr 堆内存的指针
+ */
+void _dragonos_free(void *ptr)
+{
+    // 找到结点(此时prev和next都处于未初始化的状态)
+    malloc_mem_chunk_t *ck = (malloc_mem_chunk_t *)((uint64_t)ptr - sizeof(uint64_t));
+    // printf("free(): addr = %#018lx\t len=%#018lx\n", (uint64_t)ck, ck->length);
+    count_last_free_size += ck->length;
+
+    malloc_insert_free_list(ck);
+
+    if (count_last_free_size > PAGE_2M_SIZE)
+    {
+        count_last_free_size = 0;
+        malloc_merge_free_chunk();
+        release_brk();
+    }
+}

+ 5 - 0
src/c/unistd.c

@@ -11,19 +11,24 @@ int execl(const char *path, const char* argv0, ...)
 	va_list ap;
 	va_start(ap, argv0);
 	for (argc = 1; va_arg(ap, const char*); argc++);
+
 	va_end(ap);
 	{
 		int i;
 		char *argv[argc+1];
 		va_start(ap, argv0);
 		argv[0] = (char *)argv0;
+
 		for (i = 1; i < argc; i++) {
 			argv[i] = va_arg(ap, char *);
 		}
+
 		argv[i] = NULL;
+		
 		va_end(ap);
 		return execv(path, argv);
 	}
+
 }
 
 int execve(const char *path, char *const *argv, char *const *envp);

+ 2 - 0
src/lib.rs

@@ -15,6 +15,7 @@
 #![feature(stmt_expr_attributes)]
 #![feature(str_internals)]
 #![feature(thread_local)]
+#![feature(vec_into_raw_parts)]
 #![allow(clippy::cast_lossless)]
 #![allow(clippy::cast_ptr_alignment)]
 #![allow(clippy::derive_hash_xor_eq)]
@@ -23,6 +24,7 @@
 // TODO: fix these
 #![warn(unaligned_references)]
 
+
 #[macro_use]
 extern crate alloc;
 extern crate cbitset;

+ 9 - 2
src/platform/allocator/dlmalloc.rs

@@ -31,11 +31,14 @@ impl Allocator {
     pub fn set_book_keeper(&self, mstate: usize) {
         self.mstate.store(mstate, Ordering::Relaxed);
     }
+
     pub fn get_book_keeper(&self) -> usize {
         self.mstate.load(Ordering::Relaxed)
     }
 }
+
 unsafe impl<'a> GlobalAlloc for Allocator {
+
     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
         alloc_align(layout.size(), layout.align()) as *mut u8
     }
@@ -70,13 +73,17 @@ pub fn new_mspace() -> usize {
 pub fn new_mspace() -> usize {
     use core::sync::atomic::AtomicU8;
 
-    static mut space: [[u8; 128 * 8]; 2] = [[0; 128 * 8]; 2];
+    use crate::header::stdlib::malloc;
+
+    static mut space: [[u8; 128 * 16]; 2] = [[0; 128 * 16]; 2];
     static cnt: AtomicU8 = AtomicU8::new(0);
     let x = cnt.fetch_add(1, Ordering::Relaxed);
     if x > 2 {
         panic!("new_mspace: too many mspace");
     }
-    let r = unsafe { create_mspace_with_base(space[x as usize].as_mut_ptr() as *mut c_void, 128 * 8, 0) };
+    //println!("I am here");
+    //println!("{:#?}",unsafe{space[x as usize].as_mut_ptr()});
+    let r = unsafe { create_mspace_with_base(space[x as usize].as_mut_ptr() as *mut c_void, 128 * 16, 0)};
     println!("new_mspace: {:#018x}", r);
     return r;
 }

+ 70 - 0
src/platform/allocator/dragonos_malloc.rs

@@ -0,0 +1,70 @@
+use crate::ALLOCATOR;
+use core::{
+    alloc::{GlobalAlloc, Layout},
+    ptr::null_mut,
+    sync::atomic::{AtomicUsize, Ordering},
+};
+
+use super::types::*;
+
+extern "C" {
+    fn _dragonos_free(ptr: *mut c_void) -> *mut c_void;
+    fn _dragonos_malloc(size: usize) -> *mut c_void;
+}
+
+pub struct Allocator {
+    mstate: AtomicUsize,
+}
+
+pub const NEWALLOCATOR: Allocator = Allocator {
+    mstate: AtomicUsize::new(0),
+};
+
+impl Allocator {
+    pub fn set_book_keeper(&self, mstate: usize) {
+        self.mstate.store(mstate, Ordering::Relaxed);
+    }
+    pub fn get_book_keeper(&self) -> usize {
+        self.mstate.load(Ordering::Relaxed)
+    }
+}
+
+unsafe impl<'a> GlobalAlloc for Allocator {
+    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+        alloc(layout.size()) as *mut u8
+        //alloc_align(layout.size(), layout.align()) as *mut u8
+    }
+
+    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
+        free(ptr as *mut c_void);
+    }
+}
+
+pub unsafe fn alloc(size: usize) -> *mut c_void {
+    // println!("alloc size: {}", size);
+    _dragonos_malloc(size)
+    //mspace_malloc(ALLOCATOR.get_book_keeper(), size)
+}
+
+pub unsafe fn alloc_align(size: usize, alignment: usize) -> *mut c_void {
+    // println!("alloc align size: {}, alignment: {}", size, alignment);
+    // TODO: 实现对齐分配
+    _dragonos_malloc(size)
+    //mspace_memalign(ALLOCATOR.get_book_keeper(), alignment, size)
+}
+
+pub unsafe fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
+    todo!()
+}
+
+pub unsafe fn free(ptr: *mut c_void) {
+    // println!("free ptr: {:#018x}", ptr as usize);
+    _dragonos_free(ptr);
+    //mspace_free(ALLOCATOR.get_book_keeper(), ptr)
+}
+
+#[cfg(target_os = "dragonos")]
+pub fn new_mspace() -> usize {
+    // dbg!("new_mspace");
+    1
+}

+ 5 - 1
src/platform/mod.rs

@@ -4,10 +4,14 @@ use core::{fmt, ptr};
 
 pub use self::allocator::*;
 
-#[cfg(not(feature = "ralloc"))]
+#[cfg(all(not(feature = "ralloc"), not(target_os = "dragonos")))]
 #[path = "allocator/dlmalloc.rs"]
 mod allocator;
 
+#[cfg(all(not(feature = "ralloc"), target_os = "dragonos"))]
+#[path = "allocator/dragonos_malloc.rs"]
+mod allocator;
+
 #[cfg(feature = "ralloc")]
 #[path = "allocator/ralloc.rs"]
 mod allocator;

+ 32 - 20
src/start.rs

@@ -1,5 +1,5 @@
 use alloc::{boxed::Box, vec::Vec};
-use core::{intrinsics, ptr, fmt::Debug};
+use core::{fmt::Debug, intrinsics, ptr};
 
 use crate::{
     header::{libgen, stdio, stdlib},
@@ -35,7 +35,7 @@ impl Stack {
     }
 }
 
-impl Debug for Stack{
+impl Debug for Stack {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("Stack")
             .field("argc", &self.argc)
@@ -45,9 +45,10 @@ impl Debug for Stack{
 }
 
 unsafe fn copy_string_array(array: *const *const c_char, len: usize) -> Vec<*mut c_char> {
-    println!("copy_string_array: array: {:p}, len: {}", array, len);
+    // println!("copy_string_array: array: {:p}, len: {}", array, len);
+
     let mut vec = Vec::with_capacity(len + 1);
-    println!("new vec ok");
+
     for i in 0..len {
         let item = *array.add(i);
         let mut len = 0;
@@ -90,14 +91,18 @@ fn alloc_init() {
         }
     }
     unsafe {
+        // dbg!("in alloc init");
         if let Some(tcb) = ld_so::tcb::Tcb::current() {
+            // println!("tcb.mspace {}",tcb.mspace);
             if tcb.mspace != 0 {
                 ALLOCATOR.set_book_keeper(tcb.mspace);
             } else if ALLOCATOR.get_book_keeper() == 0 {
                 ALLOCATOR.set_book_keeper(new_mspace());
             }
         } else if ALLOCATOR.get_book_keeper() == 0 {
+            // dbg!("TRY");
             ALLOCATOR.set_book_keeper(new_mspace());
+            // dbg!("ALLOCATOR OWARI DAWA");
         }
     }
 }
@@ -131,9 +136,11 @@ extern "C" fn init_array() {
         init_complete = true
     }
 }
+
 fn io_init() {
     unsafe {
-        // Initialize stdin/stdout/stderr, see https://github.com/rust-lang/rust/issues/51718
+        // Initialize stdin/stdout/stderr,
+        // see https://github.com/rust-lang/rust/issues/51718
         stdio::stdin = stdio::default_stdin.get();
         stdio::stdout = stdio::default_stdout.get();
         stdio::stderr = stdio::default_stderr.get();
@@ -180,21 +187,22 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
         fn _init();
         fn main(argc: isize, argv: *mut *mut c_char, envp: *mut *mut c_char) -> c_int;
     }
-    println!("relibc_start: sp={:?}", sp);
+
     // Ensure correct host system before executing more system calls
     relibc_verify_host();
     use core::arch::asm;
-    
+
     // Initialize TLS, if necessary
     ld_so::init(sp);
 
-    println!("alloc init");
+    // println!("alloc init");
 
     // Set up the right allocator...
     // if any memory rust based memory allocation happen before this step .. we are doomed.
     alloc_init();
 
-    println!("alloc init ok");
+    // println!("alloc init ok");
+
     if let Some(tcb) = ld_so::tcb::Tcb::current() {
         // Update TCB mspace
         tcb.mspace = ALLOCATOR.get_book_keeper();
@@ -207,19 +215,23 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
             tcb.linker_ptr = Box::into_raw(Box::new(Mutex::new(linker)));
         }
     }
-    println!("to copy args");
+
+    // println!("to copy args");
     // Set up argc and argv
     let argc = sp.argc;
     let argv = sp.argv();
+
     platform::inner_argv = copy_string_array(argv, argc as usize);
-    println!("copy args ok");
+
+    // println!("copy args ok");
     platform::argv = platform::inner_argv.as_mut_ptr();
     // Special code for program_invocation_name and program_invocation_short_name
     if let Some(arg) = platform::inner_argv.get(0) {
         platform::program_invocation_name = *arg;
         platform::program_invocation_short_name = libgen::basename(*arg);
     }
-    println!("to check environ");
+    // println!("to check environ");
+
     // We check for NULL here since ld.so might already have initialized it for us, and we don't
     // want to overwrite it if constructors in .init_array of dependency libraries have called
     // setenv.
@@ -233,17 +245,17 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
         platform::OUR_ENVIRON = copy_string_array(envp, len);
         platform::environ = platform::OUR_ENVIRON.as_mut_ptr();
     }
-    println!("to get auxvs");
+    // println!("to get auxvs");
     let auxvs = get_auxvs(sp.auxv().cast());
-    println!("to init platform");
+    // println!("to init platform");
     crate::platform::init(auxvs);
 
     // Setup signal stack, otherwise we cannot handle any signals besides SIG_IGN/SIG_DFL behavior.
     #[cfg(target_os = "redox")]
     setup_sigstack();
-    println!("before init_array()");
+    // println!("before init_array()");
     init_array();
-    println!("init_array() ok");
+    // println!("init_array() ok");
 
     // Run preinit array
     {
@@ -255,11 +267,10 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
         }
     }
 
-    println!("before _init()");
+    // println!("before _init()");
     // Call init section
     _init();
-    println!("after _init()");
-
+    // println!("after _init()");
     // Run init array
     {
         let mut f = &__init_array_start as *const _;
@@ -269,7 +280,8 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
             f = f.offset(1);
         }
     }
-    println!("to run main()");
+
+    // println!("to run main()");
     // not argv or envp, because programs like bash try to modify this *const* pointer :|
     stdlib::exit(main(argc, platform::argv, platform::environ));