BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / f9d84303bd9a7535d1313e1dd7d79258841ec3e4 / memory / page.c

// Related

Nucleus

Barry Page object 4e8efeb (3 years, 3 months ago)
/*
 * 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;
}