summaryrefslogtreecommitdiff
path: root/include/memory.h
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-01-28 19:22:09 -0500
committerFreya Murphy <freya@freyacat.org>2024-01-28 19:22:09 -0500
commit6957948f3af47f5b57770ac1692843bba768c285 (patch)
tree247d3709d6414fa68a582b9bd6e8deb62547760c /include/memory.h
parentMerge remote-tracking branch 'origin/main' (diff)
downloadcorn-6957948f3af47f5b57770ac1692843bba768c285.tar.gz
corn-6957948f3af47f5b57770ac1692843bba768c285.tar.bz2
corn-6957948f3af47f5b57770ac1692843bba768c285.zip
memory changes
Diffstat (limited to 'include/memory.h')
-rw-r--r--include/memory.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/include/memory.h b/include/memory.h
new file mode 100644
index 0000000..bec05f8
--- /dev/null
+++ b/include/memory.h
@@ -0,0 +1,107 @@
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+
+struct memory_segment {
+ uint64_t addr;
+ uint64_t len;
+ uint32_t type;
+ uint32_t reserved;
+} __attribute__((packed));
+
+struct memory_map {
+ uint32_t size;
+ uint32_t version;
+ struct memory_segment entries[];
+} __attribute__((packed));
+
+/**
+ * Initalize system memory allocator
+ */
+extern void memory_init(struct memory_map *map);
+
+/**
+ * Disabled cpu interupts to not interfere with
+ * current memory allocations.
+ */
+extern void memory_lock(void);
+
+/**
+ * Reenabled cpu interupts
+ */
+extern void memory_unlock(void);
+
+/**
+ * @returns how much memory the system has
+ */
+extern uint64_t memory_total(void);
+
+/**
+ * @returns how much memory is free
+ */
+extern uint64_t memory_free(void);
+
+/**
+ * @returns how much memory is used
+ */
+extern uint64_t memory_used(void);
+
+/**
+ * Allocates a single page in memory.
+ * @returns the page if allocated or NULL on failure
+ */
+extern void *alloc_page(void);
+
+/**
+ * Allocats count pages in memory
+ * @param count - the number of continious pages to allocate
+ * @returns the pages if allocated or NULL on failure
+ */
+extern void *alloc_pages(int count);
+
+/**
+ * Frees a single page in memory
+ * @param page - the pointer to the page
+ */
+extern void free_page(void *page);
+
+/**
+ * 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
+ */
+extern void *mmap(void *addr, size_t len);
+
+/**
+ * Unmaps mapped address from the mmap function
+ * @param addr - the address returned from mmap
+ * @param len - the length allocated
+ */
+extern void unmap(void *addr);
+
+/**
+ * Allocates size_t bytes in memory
+ * @param size - the amount of bytes to allocate
+ * @retruns the address allocated or NULL on failure
+ */
+extern void *malloc(size_t size);
+
+/**
+ * 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
+ */
+extern void *realloc(void *ptr, size_t size);
+
+/**
+ * Frees a allocated pointer in memory
+ * @param ptr - the pointer to free
+ */
+extern void free(void *ptr);