Skip to main content
Code Review

Return to Revisions

3 of 3
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/

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.

Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
default

AltStyle によって変換されたページ (->オリジナル) /