Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions pintos/include/lib/kernel/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,76 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "list.h"

/* Hash element. */
struct hash_elem {
struct list_elem list_elem;
struct list_elem list_elem;
};

/* Converts pointer to hash element HASH_ELEM into a pointer to
* the structure that HASH_ELEM is embedded inside. Supply the
* name of the outer structure STRUCT and the member name MEMBER
* of the hash element. See the big comment at the top of the
* file for an example. */
#define hash_entry(HASH_ELEM, STRUCT, MEMBER) \
((STRUCT *) ((uint8_t *) &(HASH_ELEM)->list_elem \
- offsetof (STRUCT, MEMBER.list_elem)))
#define hash_entry(HASH_ELEM, STRUCT, MEMBER) \
((STRUCT*)((uint8_t*)&(HASH_ELEM)->list_elem - offsetof(STRUCT, MEMBER.list_elem)))

/* Computes and returns the hash value for hash element E, given
* auxiliary data AUX. */
typedef uint64_t hash_hash_func (const struct hash_elem *e, void *aux);
typedef uint64_t hash_hash_func(const struct hash_elem* e, void* aux);

/* Compares the value of two hash elements A and B, given
* auxiliary data AUX. Returns true if A is less than B, or
* false if A is greater than or equal to B. */
typedef bool hash_less_func (const struct hash_elem *a,
const struct hash_elem *b,
void *aux);
typedef bool hash_less_func(const struct hash_elem* a, const struct hash_elem* b, void* aux);

/* Performs some operation on hash element E, given auxiliary
* data AUX. */
typedef void hash_action_func (struct hash_elem *e, void *aux);
typedef void hash_action_func(struct hash_elem* e, void* aux);

/* Hash table. */
struct hash {
size_t elem_cnt; /* Number of elements in table. */
size_t bucket_cnt; /* Number of buckets, a power of 2. */
struct list *buckets; /* Array of `bucket_cnt' lists. */
hash_hash_func *hash; /* Hash function. */
hash_less_func *less; /* Comparison function. */
void *aux; /* Auxiliary data for `hash' and `less'. */
size_t elem_cnt; /* Number of elements in table. */
size_t bucket_cnt; /* Number of buckets, a power of 2. */
struct list* buckets; /* Array of `bucket_cnt' lists. */
hash_hash_func* hash; /* Hash function. */
hash_less_func* less; /* Comparison function. */
void* aux; /* Auxiliary data for `hash' and `less'. */
};

/* A hash table iterator. */
struct hash_iterator {
struct hash *hash; /* The hash table. */
struct list *bucket; /* Current bucket. */
struct hash_elem *elem; /* Current hash element in current bucket. */
struct hash* hash; /* The hash table. */
struct list* bucket; /* Current bucket. */
struct hash_elem* elem; /* Current hash element in current bucket. */
};

/* Basic life cycle. */
bool hash_init (struct hash *, hash_hash_func *, hash_less_func *, void *aux);
void hash_clear (struct hash *, hash_action_func *);
void hash_destroy (struct hash *, hash_action_func *);
bool hash_init(struct hash*, hash_hash_func*, hash_less_func*, void* aux);
void hash_clear(struct hash*, hash_action_func*);
void hash_destroy(struct hash*, hash_action_func*);

/* Search, insertion, deletion. */
struct hash_elem *hash_insert (struct hash *, struct hash_elem *);
struct hash_elem *hash_replace (struct hash *, struct hash_elem *);
struct hash_elem *hash_find (struct hash *, struct hash_elem *);
struct hash_elem *hash_delete (struct hash *, struct hash_elem *);
struct hash_elem* hash_insert(struct hash*, struct hash_elem*);
struct hash_elem* hash_replace(struct hash*, struct hash_elem*);
struct hash_elem* hash_find(struct hash*, struct hash_elem*);
struct hash_elem* hash_delete(struct hash*, struct hash_elem*);

/* Iteration. */
void hash_apply (struct hash *, hash_action_func *);
void hash_first (struct hash_iterator *, struct hash *);
struct hash_elem *hash_next (struct hash_iterator *);
struct hash_elem *hash_cur (struct hash_iterator *);
void hash_apply(struct hash*, hash_action_func*);
void hash_first(struct hash_iterator*, struct hash*);
struct hash_elem* hash_next(struct hash_iterator*);
struct hash_elem* hash_cur(struct hash_iterator*);

/* Information. */
size_t hash_size (struct hash *);
bool hash_empty (struct hash *);
size_t hash_size(struct hash*);
bool hash_empty(struct hash*);

/* Sample hash functions. */
uint64_t hash_bytes (const void *, size_t);
uint64_t hash_string (const char *);
uint64_t hash_int (int);
uint64_t hash_bytes(const void*, size_t);
uint64_t hash_string(const char*);
uint64_t hash_int(int);

#endif /* lib/kernel/hash.h */
129 changes: 67 additions & 62 deletions pintos/include/vm/vm.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
#ifndef VM_VM_H
#define VM_VM_H
#include <stdbool.h>

#include "threads/palloc.h"

enum vm_type {
/* page not initialized */
VM_UNINIT = 0,
/* page not related to the file, aka anonymous page */
VM_ANON = 1,
/* page that realated to the file */
VM_FILE = 2,
/* page that hold the page cache, for project 4 */
VM_PAGE_CACHE = 3,

/* Bit flags to store state */

/* Auxillary bit flag marker for store information. You can add more
* markers, until the value is fit in the int. */
VM_MARKER_0 = (1 << 3),
VM_MARKER_1 = (1 << 4),

/* DO NOT EXCEED THIS VALUE. */
VM_MARKER_END = (1 << 31),
/* page not initialized */
VM_UNINIT = 0,
/* page not related to the file, aka anonymous page */
VM_ANON = 1,
/* page that realated to the file */
VM_FILE = 2,
/* page that hold the page cache, for project 4 */
VM_PAGE_CACHE = 3,

/* Bit flags to store state */

/* Auxillary bit flag marker for store information. You can add more
* markers, until the value is fit in the int. */
VM_MARKER_0 = (1 << 3),
VM_MARKER_1 = (1 << 4),

/* DO NOT EXCEED THIS VALUE. */
VM_MARKER_END = (1 << 31),
};

#include "vm/uninit.h"
#include <hash.h> // SPT 구조체에 pages 추가를 위한 헤더 파일

#include "vm/anon.h"
#include "vm/file.h"
#include "vm/uninit.h"
#ifdef EFILESYS
#include "filesys/page_cache.h"
#endif
Expand All @@ -41,72 +44,74 @@ struct thread;
* uninit_page, file_page, anon_page, and page cache (project4).
* DO NOT REMOVE/MODIFY PREDEFINED MEMBER OF THIS STRUCTURE. */
struct page {
const struct page_operations *operations;
void *va; /* Address in terms of user space */
struct frame *frame; /* Back reference for frame */

/* Your implementation */

/* Per-type data are binded into the union.
* Each function automatically detects the current union */
union {
struct uninit_page uninit;
struct anon_page anon;
struct file_page file;
const struct page_operations* operations;
void* va; /* Address in terms of user space */
struct frame* frame; /* Back reference for frame */

/* Your implementation */
struct hash_elem hash_elem;

bool writable;

/* Per-type data are binded into the union.
* Each function automatically detects the current union */
union {
struct uninit_page uninit;
struct anon_page anon;
struct file_page file;
#ifdef EFILESYS
struct page_cache page_cache;
struct page_cache page_cache;
#endif
};
};
};

/* The representation of "frame" */
struct frame {
void *kva;
struct page *page;
void* kva;
struct page* page;
};

/* The function table for page operations.
* This is one way of implementing "interface" in C.
* Put the table of "method" into the struct's member, and
* call it whenever you needed. */
struct page_operations {
bool (*swap_in) (struct page *, void *);
bool (*swap_out) (struct page *);
void (*destroy) (struct page *);
enum vm_type type;
bool (*swap_in)(struct page*, void*);
bool (*swap_out)(struct page*);
void (*destroy)(struct page*);
enum vm_type type;
};

#define swap_in(page, v) (page)->operations->swap_in ((page), v)
#define swap_out(page) (page)->operations->swap_out (page)
#define swap_in(page, v) (page)->operations->swap_in((page), v)
#define swap_out(page) (page)->operations->swap_out(page)
#define destroy(page) \
if ((page)->operations->destroy) (page)->operations->destroy (page)
if ((page)->operations->destroy) (page)->operations->destroy(page)

/* Representation of current process's memory space.
* We don't want to force you to obey any specific design for this struct.
* All designs up to you for this. */
struct supplemental_page_table {
struct hash pages;
};

#include "threads/thread.h"
void supplemental_page_table_init (struct supplemental_page_table *spt);
bool supplemental_page_table_copy (struct supplemental_page_table *dst,
struct supplemental_page_table *src);
void supplemental_page_table_kill (struct supplemental_page_table *spt);
struct page *spt_find_page (struct supplemental_page_table *spt,
void *va);
bool spt_insert_page (struct supplemental_page_table *spt, struct page *page);
void spt_remove_page (struct supplemental_page_table *spt, struct page *page);

void vm_init (void);
bool vm_try_handle_fault (struct intr_frame *f, void *addr, bool user,
bool write, bool not_present);
void supplemental_page_table_init(struct supplemental_page_table* spt);
bool supplemental_page_table_copy(struct supplemental_page_table* dst,
struct supplemental_page_table* src);
void supplemental_page_table_kill(struct supplemental_page_table* spt);
struct page* spt_find_page(struct supplemental_page_table* spt, void* va);
bool spt_insert_page(struct supplemental_page_table* spt, struct page* page);
void spt_remove_page(struct supplemental_page_table* spt, struct page* page);

void vm_init(void);
bool vm_try_handle_fault(struct intr_frame* f, void* addr, bool user, bool write, bool not_present);

#define vm_alloc_page(type, upage, writable) \
vm_alloc_page_with_initializer ((type), (upage), (writable), NULL, NULL)
bool vm_alloc_page_with_initializer (enum vm_type type, void *upage,
bool writable, vm_initializer *init, void *aux);
void vm_dealloc_page (struct page *page);
bool vm_claim_page (void *va);
enum vm_type page_get_type (struct page *page);

#endif /* VM_VM_H */
vm_alloc_page_with_initializer((type), (upage), (writable), NULL, NULL)
bool vm_alloc_page_with_initializer(enum vm_type type, void* upage, bool writable,
vm_initializer* init, void* aux);
void vm_dealloc_page(struct page* page);
bool vm_claim_page(void* va);
enum vm_type page_get_type(struct page* page);

#endif /* VM_VM_H */
Loading