fix: wrong rsize in realloc

This commit is contained in:
Renge 2022-03-25 23:39:39 -04:00
parent 2b554b4c5c
commit c227704733
2 changed files with 7 additions and 5 deletions

View File

@ -4,15 +4,15 @@
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
sf_set_magic(0x0); sf_set_magic(0x0);
size_t sz_x = sizeof(int), sz_y = 10, sz_x1 = sizeof(int) * 20; size_t sz_x = sizeof(double) * 8, sz_y = sizeof(int);
void *x = sf_malloc(sz_x); void *x = sf_malloc(sz_x);
/* void *y = */ sf_malloc(sz_y); sf_realloc(x, sz_y);
x = sf_realloc(x, sz_x1);
// cr_assert_not_null(x, "x is NULL!"); // cr_assert_not_null(x, "x is NULL!");
// sf_block *bp = (sf_block *)((char *)x - 16); // sf_block *bp = (sf_block *)((char *)x - 16);
sf_show_heap(); sf_show_heap();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -167,7 +167,7 @@ void *sf_realloc(void *pp, sf_size_t rsize)
sf_block *block = (sf_block *)(((intptr_t)pp) - 2 * sizeof(sf_header)); sf_block *block = (sf_block *)(((intptr_t)pp) - 2 * sizeof(sf_header));
valid_pointer(block); valid_pointer(block);
sf_size_t size = get_block_size(block->header); sf_size_t size = get_block_size(block->header);
sf_size_t new_size = get_min_size(size); sf_size_t new_size = get_min_size(rsize);
if (new_size >= size) if (new_size >= size)
{ {
sf_block *new = (sf_block *)(((intptr_t)sf_malloc(rsize)) - 2 * sizeof(sf_header)); sf_block *new = (sf_block *)(((intptr_t)sf_malloc(rsize)) - 2 * sizeof(sf_header));
@ -184,7 +184,9 @@ void *sf_realloc(void *pp, sf_size_t rsize)
else else
{ {
sf_block *ptr = split_block(block, new_size); sf_block *ptr = split_block(block, new_size);
put_block(ptr); set_entire_header(block, rsize, new_size, 1, get_prv_alloc(block->header), 0);
set_entire_header(ptr, 0, get_block_size(ptr->header), 0, get_prv_alloc(ptr->header), 0);
release_block(ptr);
return block->body.payload; return block->body.payload;
} }
} }