Clean mmap.c a bit
This commit is contained in:
parent
0f6830b34d
commit
ea10ad5df1
@ -3,6 +3,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} }
|
||||||
|
|
||||||
#include "mmap.h"
|
#include "mmap.h"
|
||||||
|
|
||||||
struct MMap* mmap_init(){
|
struct MMap* mmap_init(){
|
||||||
@ -70,31 +72,25 @@ int check_page_alloc(struct MMap *mmap) {
|
|||||||
int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
||||||
int key = -1;
|
int key = -1;
|
||||||
fseek(mmap->swap, 0, SEEK_SET);
|
fseek(mmap->swap, 0, SEEK_SET);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
|
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
|
||||||
fread(&key, sizeof(int), 1, mmap->swap);
|
fread(&key, sizeof(int), 1, mmap->swap);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (key == -1) {
|
if (key == -1) {
|
||||||
fseek(mmap->swap, i, SEEK_SET);
|
fseek(mmap->swap, i, SEEK_SET);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
|
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fwrite(page->data, 4096, 1, mmap->swap);
|
fwrite(page->data, 4096, 1, mmap->swap);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,36 +100,29 @@ int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
|||||||
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
|
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
|
||||||
int key = -1;
|
int key = -1;
|
||||||
fseek(mmap->swap, 0, SEEK_SET);
|
fseek(mmap->swap, 0, SEEK_SET);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
|
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
|
||||||
int ret = fread(&key, sizeof(int), 1, mmap->swap);
|
int ret = fread(&key, sizeof(int), 1, mmap->swap);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
}
|
if (key != page->key)
|
||||||
if (key == page->key) {
|
continue;
|
||||||
|
|
||||||
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
|
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
fread(page->data, 4096, 1, mmap->swap);
|
fread(page->data, 4096, 1, mmap->swap);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
fseek(mmap->swap, i, SEEK_SET);
|
fseek(mmap->swap, i, SEEK_SET);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
int tmp = -1;
|
int tmp = -1;
|
||||||
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
||||||
if( ferror(mmap->swap) ) {
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return page;
|
return page;
|
||||||
} else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user