Memory Management - HerobrinesArmy/entropy GitHub Wiki

Memory Management Specifications.

Ideas:

  • Swapping pages is hard because it means that every process needs to be able to handle the location of it's memory being changed in the middle of executing. It is unlikely that a page can be swapped back to the same location in memory. This is why I have not implemented swapping.
  • Defragmentation is hard for the same reason. I have however implemented garbage collecting in the form of fn_clear_process , which frees all memory belonging to that process.
  • The process manager should start processes by doing a fn_request_memory to allocate enough memory for the whole process, even if it's larger than one page. Processes can then call fn_request_page to allocate more memory if they need it, fn_free_page if they no longer need it. They can also call fn_request_memory if they really need several pages in a row, this should be used only if necessary though. When processes are ended, fn_clear_process can be used to make sure all pages used by that process are freed again. fn_clear_page and fn_copy_page are just utilities.

Function:

The memory management should be able to perform the following tasks:

  • Allocate memory.
  • Deallocate memory.
  • Defragmantize/Garbage collect. (Basically closing the gaps in memory when you deallocate a page).
  • Swap pages. Swapping one page with another (handy for buffering).
  • Clear. Zero-ing out a specified page.

Implemented functions:

Pages are always referred to by their number between 0 and 127, not by their address.

  • fn_init_MM - Reserves memory for [current_process] based on the location of free_space_start . This label should always be last to ensure proper reservation. Arguments: None, Return: None
  • fn_request_page - Requests one page of memory for [current_process] . This page is the first available page and is appended to the list of pages owned by [current_process] . Arguments: A=number of pages requested (0-127), Return: A=first allocated page (0-127) or -1 if out of space.
  • fn_free_page - Frees page A if it belongs to [current_process] . This page is removed from the list of pages owned by [current_process] . Arguments: A=page to be freed (0-127), Return: A=0 on success or 0x101 if no permission.
  • fn_clear_page - Clears page A, setting all words to 0. Arguments A=page to be cleared (0-127), Return: None.
  • fn_copy_page - Copies page A to page B. Arguments: A=source page (0-127), B=destination page (0-127), Return: None
  • fn_clear_process - Frees all memory owned by process A. Arguments: A=page to be freed (0-127), Return: A=0 on success or 0x101 if no permission.
  • fn_request_memory - Requests A pages of memory for [current_process] . The first row of A free pages found is appended to the list of pages owned by [current_process] . Arguments: A=number of pages requested (0-127), Return: A=first allocated page (0-127) or -1 if out of space.

Specifications:

  • All pages are 512 words in size. Note that this is equal to the amount of words on a sector, on a floppy. Meaning that exchanging data between the two will be easy.
  • The memory manager keeps track of a list of 128 words, one for each sector, that stores the next sector of the process the sector belongs to. This list is called page_header_start . 0xffff means end of process and 0x0000 means unallocated.
  • The memory manager keeps track of a list of 64 words, one for each process, that stores the first sector of that process. This list is called process_first_pages_start . 0xffff means end of process and 0x0000 means unallocated. This means no more than 64 processes are supported.
  • The memory uses a variable called [current_process] which is currently located in the memory manager. It should be updated by a process manager so the memory manager knows which process to assign the memory to.