8000 Sunday Updates by paksh727 · Pull Request #4 · gjbauer/cs4310-hw06 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Sunday Updates #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
38 changes: 25 additions & 13 deletions pmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ char *pstrdup(char *arg) {
long free_list_length() {
long length = 0;
node *current = mem;
while (current) {
while (current!=NULL) {
if(current->next == NULL){
//printf("IT IS NULL\n");
//exit(EXIT_FAILURE);
}
length++;
printf("current : %u\n", current->size);
//printf("null : %d\n", NULL);
printf("current->next : %u\n", current->next->size);
current = current->next;
}
stats.free_length += length;
// stats.free_length += length;
return length;
}

Expand Down Expand Up @@ -61,6 +68,8 @@ void* pmalloc(size_t size) {
#endif
size += sizeof(header); // Add space for storing the size.

if (size<24) size=24;

if (size < PAGE_SIZE) {
// Try to find a free block in the list
node* prev = NULL;
Expand All @@ -82,7 +91,7 @@ void* pmalloc(size_t size) {
else {
int k = mem->size - size;
mem = (void*)((char*)curr + size);
mem->size = k;
mem->size = k;
//printf("top of memory reporting size : %u\n", mem->size);
}
}
Expand All @@ -102,6 +111,7 @@ void* pmalloc(size_t size) {
}
stats.pages_mapped += 1;
new_block->size = size; //PAGE_SIZE - sizeof(header); // Adjust for the header
//new_block->prev = mem;
mem = (void*)((char*)new_block + size);
mem->size = PAGE_SIZE - size;
//printf("top of memory reporting size : %u\n", mem->size);
Expand Down Expand Up @@ -143,13 +153,13 @@ void pfree(void* item) {
//printf("%u\n", (node*)((char*)block));
//printf("%u\n", (node*)((char*)mem));
if (block->size>=PAGE_SIZE) {
munmap(&block, block->size);
stats.pages_unmapped+= block->size/PAGE_SIZE;
block->next=mem;
mem=block;
}
else {
if (block->size<PAGE_SIZE) {
node *curr = mem;
node *prev = NULL;
while ((void*)block>(void*)curr&&curr) { // Kepp the blocks sorted by where they appear in memory ;)
while ((void*)block>(void*)curr&&curr) {//&&(void*)((char*)block+block->size)!=mem) { // Keep the blocks sorted by where they appear in memory ;)
prev = curr;
curr = curr->next;
}
Expand All @@ -160,26 +170,27 @@ void pfree(void* item) {
else {
block->next = mem;
mem = block;
}
pnodemerge(); // Run this command everytime you call free to merge mergeable sections...
freeuapages();
}
}
pnodemerge(); // Run this command everytime you call free to merge mergeable sections...
freeuapages();
}

void freeuapages() {
node *curr = mem;
node *prev = NULL;
while (curr) {
if (curr->size>=PAGE_SIZE) {
if (curr->size==PAGE_SIZE&&!curr->next) {
return;
}
if (curr->size==PAGE_SIZE&&!curr->next) {
return;
}
if (prev) {
prev->next=curr->next;
} else {
mem=curr->next;
}
munmap(&curr, curr->size);
stats.pages_unmapped+=curr->size/PAGE_SIZE;
}
if (curr)
curr=curr->next;
Expand All @@ -191,6 +202,7 @@ void freeuapages() {
void pnodemerge() {
node *curr = mem;
node *prev = NULL;
if ()
while (curr) {
//printf("curr+size : %u\n", (node*)((char*)curr+curr->size));
//printf("curr->next : %u\n", (node*)((char*)curr->next));
Expand Down
1 change: 1 addition & 0 deletions pmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct pm_stats {
typedef struct node {
size_t size;
struct node *next;
struct node *prev;
} node;

typedef struct header {
Expand Down
Binary file added tests/01-hello
Binary file not shown.
Binary file added tests/02-array-sum
Binary file not shown.
Binary file added tests/03-list-sum
Binary file not shown.
Binary file added tests/04-two-lists
Binary file not shown.
Binary file added tests/05-many-sizes
Binary file not shown.
0