Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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.

added 234 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

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.

Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

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)));
}
lang-c

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