implement heap_realloc
This commit is contained in:
parent
3f01b5abfd
commit
ebef59e3da
3 changed files with 17 additions and 0 deletions
14
heap.c
14
heap.c
|
|
@ -1,4 +1,5 @@
|
|||
#include "heap.h"
|
||||
#include "cutil.h"
|
||||
|
||||
static mblock_t* mblock_get_next(mblock_t* block)
|
||||
{
|
||||
|
|
@ -111,6 +112,19 @@ void* heap_alloc(mheap_t* heap, size_t size)
|
|||
return block->data;
|
||||
}
|
||||
|
||||
void* heap_realloc(mheap_t* heap, void* mem, size_t size)
|
||||
{
|
||||
mblock_t* block = (mblock_t*)((u8*)mem - MBLOCK_SIZE);
|
||||
(void)heap;
|
||||
|
||||
heap_free(heap, mem);
|
||||
void* newMem = heap_alloc(heap, size);
|
||||
if (mem != newMem)
|
||||
cu_memcpy(newMem, mem, mblock_get_size(block));
|
||||
|
||||
return newMem;
|
||||
}
|
||||
|
||||
void heap_free(mheap_t* heap, void* mem)
|
||||
{
|
||||
mblock_t* block = (mblock_t*)((u8*)mem - MBLOCK_SIZE);
|
||||
|
|
|
|||
1
heap.h
1
heap.h
|
|
@ -29,6 +29,7 @@ void heap_init(mheap_t* heap, void* data, uword size);
|
|||
void heap_join(mblock_t* start, int dir);
|
||||
void heap_split(mblock_t* block, size_t req_size);
|
||||
void* heap_alloc(mheap_t* heap, size_t size);
|
||||
void* heap_realloc(mheap_t* heap, void* mem, size_t size);
|
||||
void heap_free(mheap_t* heap, void* mem);
|
||||
|
||||
#endif
|
||||
2
test.c
2
test.c
|
|
@ -200,6 +200,8 @@ int main()
|
|||
printf("heap_alloc\t%p\n", m2);
|
||||
printf("heap_alloc\t%p\n", m3);
|
||||
printf("heap_alloc\t%p\n", m4);
|
||||
void* m5 = heap_realloc(&heap, m4, 10);
|
||||
printf("heap_realloc\t%p\t%p\n", m4, m5);
|
||||
|
||||
cutil_exit();
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue