Skip to content

Update memalloc.c #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
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
45 changes: 31 additions & 14 deletions memalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,13 @@ void free(void *block)
heap and release memory to OS. Else, we will keep the block
but mark it as free.
*/
header->is_free = 1;
if ((char*)block + header->size == programbreak) {
if (head == tail) {
head = tail = NULL;
} else {
tmp = head;
while (tmp) {
if(tmp->next == tail) {
tmp->next = NULL;
tail = tmp;
}
tmp = tmp->next;
}
}
/*
sbrk() with a negative argument decrements the program break.
So memory is released by the program to OS.
*/
sbrk(0 - header->size - sizeof(struct header_t));
sbrk(release_size());
/* Note: This lock does not really assure thread
safety, because sbrk() itself is not really
thread safe. Suppose there occurs a foregin sbrk(N)
Expand All @@ -72,10 +61,38 @@ void free(void *block)
pthread_mutex_unlock(&global_malloc_lock);
return;
}
header->is_free = 1;
pthread_mutex_unlock(&global_malloc_lock);
}

size_t release_size()
{
/* This calculates the size of all trailing elements */
struct header_t *new_tail, *tmp;
size_t release_size = 0;
new_tail = tmp = head;
while (tmp) {
if(tmp->next == NULL) {
new_tail->next = NULL;
tail = new_tail;
}
if(!tmp->is_free) {
new_tail = tmp;
release_size = 0;
} else {
release_size -= tmp->size - sizeof(struct header_t);
}
tmp = tmp->next;
}
/* Head equals tail can indicate two things:
* this is the last remaining element on the heap
* there are no remaining elements on the heap
*/
if (head == tail && head->is_free) {
head = tail = NULL;
}
return release_size;
}

void *malloc(size_t size)
{
size_t total_size;
Expand Down