corn/include/memory.h

109 lines
2.5 KiB
C
Raw Normal View History

2024-01-29 00:22:09 +00:00
#pragma once
#include <stddef.h>
#include <stdint.h>
2024-01-31 17:49:06 +00:00
#include <shim.h>
2024-01-29 00:22:09 +00:00
/**
* Initalize system memory allocator
*/
2024-02-01 03:30:35 +00:00
void memory_init(struct memory_map *map);
2024-01-29 00:22:09 +00:00
/**
* Disabled cpu interupts to not interfere with
* current memory allocations.
*/
2024-02-01 03:30:35 +00:00
void memory_lock(void);
2024-01-29 00:22:09 +00:00
/**
* Reenabled cpu interupts
*/
2024-02-01 03:30:35 +00:00
void memory_unlock(void);
2024-01-29 00:22:09 +00:00
/**
* @returns how much memory the system has
*/
2024-02-01 03:30:35 +00:00
uint64_t memory_total(void);
2024-01-29 00:22:09 +00:00
/**
* @returns how much memory is free
*/
2024-02-01 03:30:35 +00:00
uint64_t memory_free(void);
2024-01-29 00:22:09 +00:00
/**
* @returns how much memory is used
*/
2024-02-01 03:30:35 +00:00
uint64_t memory_used(void);
2024-01-29 00:22:09 +00:00
/**
* Allocates a single page in memory.
* @returns the page if allocated or NULL on failure
*/
2024-02-01 03:30:35 +00:00
void *alloc_page(void);
2024-01-29 00:22:09 +00:00
/**
* Allocats count pages in memory
* @param count - the number of continious pages to allocate
* @returns the pages if allocated or NULL on failure
*/
2024-02-01 03:30:35 +00:00
void *alloc_pages(int count);
2024-01-29 00:22:09 +00:00
/**
2024-01-31 17:49:06 +00:00
* Frees a signle page in memory.
* Must be a page aligned allocated vitural pointer.
* Freeing in the middle of a block is allowed.
2024-01-29 00:22:09 +00:00
* @param page - the pointer to the page
*/
2024-02-01 03:30:35 +00:00
void free_page(void *page);
2024-01-31 17:49:06 +00:00
// TODO: implement free_page
/**
* Frees block of pages in memory.
* Must be a page aligned allocated vitural pointer.
* Freeing int he middle of a block is allowed,
* free_pages will from *page to end of block allocated.
* @param page - the pointer to the page
*/
2024-02-01 03:30:35 +00:00
void free_pages(void *page);
2024-01-31 17:49:06 +00:00
// TODO: implement freeing in middle of block
2024-01-29 00:22:09 +00:00
/**
* Allocates at least len bytes of memory starting at
* physical address addr. Returned address can be
* any virtural address.
* @param addr - the physical address to map
* @param len - the minimum length to map
* @param writable - if this memory should be writable
* @param user - if this memory should be user writable
*/
2024-02-01 03:30:35 +00:00
void *mmap(void *addr, size_t len);
2024-01-29 00:22:09 +00:00
/**
* Unmaps mapped address from the mmap function
* @param addr - the address returned from mmap
* @param len - the length allocated
*/
2024-02-01 03:30:35 +00:00
void unmap(void *addr);
2024-01-29 00:22:09 +00:00
/**
2024-01-31 17:49:06 +00:00
* Allocates size_t bytes in memory
2024-01-29 00:22:09 +00:00
* @param size - the amount of bytes to allocate
* @retruns the address allocated or NULL on failure
*/
2024-02-01 03:30:35 +00:00
void *kalloc(size_t size);
2024-01-29 00:22:09 +00:00
/**
* Reallocates a given allocated ptr to a new size of bytes in memory.
* If ptr is NULL it will allocate new memory.
* @param ptr - the pointer to reallocate
* @param size - the amount of bytes to set the pointer to
* @returns the address allocated or NULL on failure
*/
2024-02-01 03:30:35 +00:00
void *krealloc(void *ptr, size_t size);
2024-01-29 00:22:09 +00:00
/**
* Frees a allocated pointer in memory
* @param ptr - the pointer to free
*/
2024-02-01 03:30:35 +00:00
void kfree(void *ptr);