2016-08-10 22:30:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "malloc.h"
|
|
|
|
|
2018-06-23 16:08:03 +00:00
|
|
|
#if ACKCONF_WANT_MALLOC
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
void* realloc(void* ptr, size_t size)
|
2016-08-10 22:30:32 +00:00
|
|
|
{
|
|
|
|
block_t* h;
|
|
|
|
size_t nblocks;
|
|
|
|
void* newptr;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return malloc(size);
|
|
|
|
|
|
|
|
h = BLOCKOF(ptr);
|
|
|
|
|
|
|
|
nblocks = BLOCKCOUNT(size);
|
|
|
|
/* Overflow check. */
|
|
|
|
if (nblocks < size)
|
|
|
|
return NULL;
|
2018-06-21 20:33:47 +00:00
|
|
|
|
2016-08-10 22:30:32 +00:00
|
|
|
/* Shrinking the block? Don't bother doing anything (it's never worth it). */
|
|
|
|
if (nblocks <= h->size)
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
/* Allocate and copy. */
|
|
|
|
|
|
|
|
newptr = malloc(size);
|
|
|
|
if (!newptr)
|
|
|
|
return NULL;
|
|
|
|
memcpy(newptr, ptr, h->size * sizeof(block_t));
|
|
|
|
free(ptr);
|
|
|
|
return newptr;
|
|
|
|
}
|
2018-06-23 16:08:03 +00:00
|
|
|
|
|
|
|
#endif
|