memlib.c

来自「实现一个简单的单进程内核,。该内核启用二级虚拟页表映射」· C语言 代码 · 共 53 行

C
53
字号
/* memlib.c - functions for modeling the memory system *//* $begin memlib */#include <syscall.h>#ifndef NULL#define NULL 0#endif/* private global variables */static char *mem_start_brk;  /* points to first byte of the heap */static char *mem_brk;        /* points to first invalid byte (byte after				end of heap) */static char *mem_max_addr;   /* max virtual address for the heap *//*  * mem_init - initializes the memory system model */void mem_init(int max_heap_addr){  /* Find out start of heap. */  mem_start_brk = (char*)brk((void*)NULL);  /* Heap starts out being zero-sized. */  mem_brk = mem_start_brk;  /* The max address for the heap. */  mem_max_addr = (void*)max_heap_addr;}/*  * mem_sbrk - simply uses the the sbrk function. Extends the heap  *    by incr bytes and returns the start address of the new area. In *    this model, the heap cannot be shrunk. */void *mem_sbrk(int incr){    char *old_brk = mem_brk;    /* Error check the request. */    if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {      return (void *)-1;    }    /* Issue a SBRK for more memory. */    if (!((int)brk((void*)(mem_brk + incr)) - (int)mem_brk)) {      return (void *)-1;    }    mem_brk += incr;    return (void *)old_brk;}/* $end memlib */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?