Implemented realloc().
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28454 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
2ef4a8b524
commit
f68fa9d364
@ -24,7 +24,7 @@ static const size_t kInitialHeapSize = 65536;
|
|||||||
* linked list).
|
* linked list).
|
||||||
* When memory is allocated, the smallest free chunk that contains
|
* When memory is allocated, the smallest free chunk that contains
|
||||||
* the requested size is split (or taken as a whole if it can't be
|
* the requested size is split (or taken as a whole if it can't be
|
||||||
* splitted anymore), and it's lower half will be removed from the
|
* splitted anymore), and it's lower half will be removed from the
|
||||||
* free list.
|
* free list.
|
||||||
* The free list is ordered by size, starting with the smallest
|
* The free list is ordered by size, starting with the smallest
|
||||||
* free chunk available. When a chunk is freed, it will be joint
|
* free chunk available. When a chunk is freed, it will be joint
|
||||||
@ -120,7 +120,7 @@ free_chunk::Join(free_chunk *chunk)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
free_chunk::Remove(free_chunk *previous)
|
free_chunk::Remove(free_chunk *previous)
|
||||||
{
|
{
|
||||||
if (previous == NULL) {
|
if (previous == NULL) {
|
||||||
@ -143,7 +143,7 @@ free_chunk::Remove(free_chunk *previous)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
free_chunk::Enqueue()
|
free_chunk::Enqueue()
|
||||||
{
|
{
|
||||||
free_chunk *chunk = sFreeAnchor.next, *last = &sFreeAnchor;
|
free_chunk *chunk = sFreeAnchor.next, *last = &sFreeAnchor;
|
||||||
@ -237,10 +237,31 @@ dump_chunks(void)
|
|||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
realloc(void *buffer, size_t newSize)
|
realloc(void *allocation, size_t newSize)
|
||||||
{
|
{
|
||||||
// not implemented
|
// free, if new size is 0
|
||||||
return NULL;
|
if (newSize == 0) {
|
||||||
|
free(allocation);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// just malloc(), if no previous allocation
|
||||||
|
if (allocation == NULL)
|
||||||
|
return malloc(newSize);
|
||||||
|
|
||||||
|
// we're lazy and don't shrink allocations
|
||||||
|
free_chunk* chunk = free_chunk::SetToAllocated(allocation);
|
||||||
|
if (chunk->Size() >= newSize)
|
||||||
|
return allocation;
|
||||||
|
|
||||||
|
// the allocation needs to grow -- allocate a new one and memcpy()
|
||||||
|
void* newAllocation = malloc(newSize);
|
||||||
|
if (newAllocation != NULL) {
|
||||||
|
memcpy(newAllocation, allocation, chunk->Size());
|
||||||
|
free(allocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newAllocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -322,7 +343,7 @@ free(void *allocated)
|
|||||||
// remove the joined chunk from the list
|
// remove the joined chunk from the list
|
||||||
last->next = freedChunk->next;
|
last->next = freedChunk->next;
|
||||||
chunk = last;
|
chunk = last;
|
||||||
|
|
||||||
if (++joinCount == 2)
|
if (++joinCount == 2)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -331,7 +352,7 @@ free(void *allocated)
|
|||||||
chunk = chunk->next;
|
chunk = chunk->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// enqueue the link at the right position; the
|
// enqueue the link at the right position; the
|
||||||
// free link queue is ordered by size
|
// free link queue is ordered by size
|
||||||
|
|
||||||
freedChunk->Enqueue();
|
freedChunk->Enqueue();
|
||||||
|
Loading…
Reference in New Issue
Block a user