I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}
I think you can simplify your code by quite a bit by using sentinel values on your list. See this answer about lists this answer about lists read the bit about sentinals and how it makes things easier.
I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}
I think you can simplify your code by quite a bit by using sentinel values on your list. See this answer about lists read the bit about sentinals and how it makes things easier.
I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}
I think you can simplify your code by quite a bit by using sentinel values on your list. See this answer about lists read the bit about sentinals and how it makes things easier.
I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}
I think you can simplify your code by quite a bit by using sentinel values on your list. See this answer about lists read the bit about sentinals and how it makes things easier.
I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}
I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}
I think you can simplify your code by quite a bit by using sentinel values on your list. See this answer about lists read the bit about sentinals and how it makes things easier.
I think this is a bug:
if (current->size == nblocks)
{
//we've found an exact match, remove the chunk from the free list
if (prev == NULL) //remove the first chunk
_dTail = NULL;
^^^^^^^^^^^^^^^
You are just removing the head of the list
Not NULL(ing) the list.
I think the line should be:
_dTail = current->next;
else
prev->next = current->next;
return current + 1;
}
The only thing I don't like is:
unsigned short next; //in blocks of sizeof(Header)
This because you have to keep doing maths to covert it to/from a pointer. I think your code would become much simpler if you just convert this too:
Header* next; // next block in list
I would also remove the assert:
assert(nbytes % sizeof(Header) == 0);
and replace it with code to move the size up.
if (nbytes % sizeof(Header) != 0)
{
nbytes += (sizeof(Header) - (nbytes % sizeof(Header)));
}