Skip to content

Commit c76e4e2

Browse files
committed
fixed looping
1 parent 7d7d9d8 commit c76e4e2

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

mdriver

148 Bytes
Binary file not shown.

mm.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ team_t team = {
4949
#define FOOTER_SIZE 8
5050

5151
size_t* freeList = (size_t*)0xFFFFFFFF;
52+
int freeListSize = 0;
5253

5354
/*
5455
* mm_init - initialize the malloc package.
@@ -78,7 +79,7 @@ void *mm_malloc(size_t size)
7879
// if (nextPtr == (size_t*)0xFFFFFFFF) {
7980
p = mem_sbrk(newSize);
8081
// break;
81-
// } else if (*(int*)((char*)nextPtr - sizeof(int)) >= size) {
82+
// } else if (*(int*)((char*)nextPtr - 4) >= size) {
8283
// p = (void*)((char*)nextPtr - 5);
8384
// *lastPtr = *nextPtr;
8485
// break;
@@ -106,25 +107,47 @@ void *mm_malloc(size_t size)
106107
void mm_free(void *pointer)
107108
{
108109
int size;
110+
int found = 0;
109111
char* ptr = (char*)pointer - HEADER_SIZE; //points to the start of the free block
110112
size_t* nextPtr;
111113
size_t* lastPtr;
114+
115+
/* ---Vars for loop debugging--- */
116+
size_t* storedPtr;
117+
int stored = 0;
118+
int loopSize = 0;
119+
int loopEnd = 0;
112120
int j = 0; //LL node counter - for debugging
121+
/* ------ */
113122

114123
size = *(int*)((char*)ptr + 1); //extracts size from malloc'ed metadata
115124

116125
nextPtr = (size_t*)freeList;
117126
lastPtr = (size_t*)&freeList;
118127

119-
while(1) {
120-
if ((nextPtr == (size_t*)0xFFFFFFFF) /*|| (*(int*)((char*)nextPtr - sizeof(int)) >= size)*/) { //if found or at end
128+
while(found == 0) {
129+
if ((nextPtr == (size_t*)0xFFFFFFFF) || (*(int*)((char*)nextPtr - 4) >= size)) { //if found or at end
121130
*lastPtr = (size_t*)((char*)ptr + 5);
122131
*(size_t*)((char*)ptr + 5) = (size_t*)nextPtr;
123-
break;
132+
freeListSize++;
133+
found = 1;
124134
} else { //if not, keep looking through list
125135
lastPtr = nextPtr;
126-
nextPtr = (size_t)*nextPtr;
136+
nextPtr = (size_t*)*nextPtr;
127137
j++;
138+
if (j > freeListSize) { //loop occuring - fix loop
139+
if (stored == 0) {
140+
stored = 1;
141+
storedPtr = lastPtr;
142+
}
143+
if (storedPtr == nextPtr && loopSize > 2) {
144+
*nextPtr = (size_t*)0xFFFFFFFF;
145+
loopEnd = 1;
146+
}
147+
if (loopEnd == 0) {
148+
loopSize++;
149+
}
150+
}
128151
}
129152
}
130153

mm.o

400 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)