Created
December 2, 2011 11:26
-
-
Save KarlHerler/1422907 to your computer and use it in GitHub Desktop.
A malloc written for the OS course in ÅA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int bitmap[20480]; | |
int myalloc(int blocksize) { | |
int i=0; //counter | |
int x=0; //done | |
int z=0; //potential spot | |
while (x==0) { | |
if(i<20480) { //we're still in a valid range | |
if (bitmap[i]==0) { //unallocated space | |
z++; | |
if (z==blocksize) { //it fits! | |
int j; | |
for (j=i-blocksize;j<i;j++) {bitmap[j]=1;} //allocate the space we need | |
x=1; //call it done. | |
if (i-blocksize>-1) { return i-blocksize; } else { return 0; } | |
} | |
} else {z=0;} //didn't fit. :( | |
i++; | |
} else { | |
x=1; | |
return -1; | |
} | |
} | |
} | |
int myfree(int pos, int blocksize) { | |
for (pos;pos<blocksize;pos++) { bitmap[pos-1]=0; } | |
return 1; //I really don't know what to return, so I'll return a 1, because I like ones, they are cute. | |
} | |
tester(int x) { | |
if (x==-1) { printf("Shit went south\n"); } | |
} | |
main() { | |
tester(myalloc(9000)); | |
tester(myalloc(2000)); | |
tester(myalloc(9000)); | |
tester(myfree(9000, 2000)); | |
tester(myalloc(1500)); | |
tester(myalloc(1500)); //fails | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment