Nucleus
Barry Page object 4e8efeb (3 years, 3 months ago)
diff --git a/include/nucleus/memory.h b/include/nucleus/memory.h
index 1e7f3e0..737176b 100644
--- a/include/nucleus/memory.h
+++ b/include/nucleus/memory.h
@@ -3,6 +3,7 @@
#include <stdint.h>
#include <stddef.h>
+#include <nucleus/object.h>
#define PAGE_SIZE 0x1000
@@ -12,6 +13,7 @@
typedef uint32_t page_t;
typedef uint32_t page_table_t;
typedef uint32_t page_dir_t;
+typedef struct Page Page;
/* Page Table Entry flags */
enum PTEFlag {
@@ -35,6 +37,8 @@ enum PDEFlag {
PDE_ACCESS = (1 << 5),
};
+extern ObjectType pageType;
+
/* Flush Translation Lookaside Buffer */
static inline void
flush_tlb(uintptr_t addr)
@@ -54,4 +58,6 @@ void cpu_load_paging(void);
void *kmalloc(size_t size);
void kfree(void *addr);
+Page *find_page(ObjectList *cache, off_t offset);
+
#endif
diff --git a/memory/page.c b/memory/page.c
new file mode 100644
index 0000000..8dd07d8
--- /dev/null
+++ b/memory/page.c
@@ -0,0 +1,64 @@
+/*
+ * This file implements the Page object. A Page refers to a page of data in a
+ * file or region of memory. The offset tracks how far into the file the page
+ * is, and the frame tracks where in physical memory the data is located.
+ */
+
+#include <nucleus/object.h>
+#include <nucleus/memory.h>
+
+/* Structure for a Page in a Cache */
+struct Page {
+ Object obj;
+ off_t offset;
+ page_t frame;
+};
+
+/* Information for find callback */
+struct FindData {
+ off_t offset;
+ Page *result;
+};
+
+static void page_delete(Object *);
+
+/* Page object type */
+ObjectType pageType = {
+ .size = sizeof(Page),
+ .delete = page_delete,
+};
+
+/* Destroy a Page object */
+static void
+page_delete(Object *obj)
+{
+ Page *page = (void *) obj;
+ free_frame(PAGE_ADDR(page->frame));
+}
+
+/* Callback for finding a Page by offset */
+static int
+compare_page_offset(void *object, void *data)
+{
+ Page *page = object;
+ struct FindData *find = data;
+ if (page->offset == find->offset) {
+ find->result = get(page);
+ return 1;
+ }
+ return 0;
+}
+
+/* Find a Page by offset in a cache */
+Page *
+find_page(ObjectList *cache, off_t offset)
+{
+ lock(cache);
+ struct FindData data = {
+ .offset = offset,
+ .result = NULL,
+ };
+ iterate(cache, compare_page_offset, &data);
+ unlock(cache);
+ return data.result;
+}