FreeBSD/sys/kern/kern_malloc.c
/*
 *	realloc: change the size of a memory block
 */
change the size of a memory block
change the size of a memory block
void *
realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
{
	uma_slab_t slab;
	unsigned long alloc;
	void *newaddr;
	KASSERT(mtp->ks_magic == M_MAGIC,
	    ("realloc: bad malloc type magic"));
	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
	    ("realloc: called with spinlock or critical section held"));
	if (addr == NULL)
		return (malloc(size, mtp, flags));
#ifdef DEBUG_MEMGUARD
	if (is_memguard_addr(addr))
		return (memguard_realloc(addr, size, mtp, flags));
#endif
#ifdef DEBUG_REDZONE
	slab = NULL;
	alloc = redzone_get_size(addr);
#else
	slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
	KASSERT(slab != NULL,
	    ("realloc: address %p out of range", (void *)addr));
	if (!(slab->us_flags & UMA_SLAB_MALLOC))
		alloc = slab->us_keg->uk_size;
	else
		alloc = slab->us_size;
	if (size <= alloc
	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
		return (addr);
#endif 
	if ((newaddr = malloc(size, mtp, flags)) == NULL)
		return (NULL);
	bcopy(addr, newaddr, min(size, alloc));
	free(addr, mtp);
	return (newaddr);
}
void * realloc ( void * addr , unsigned long size , struct malloc_type * mtp , int flags ) { uma_slab_t slab ; unsigned long alloc ; void * newaddr ; KASSERT ( mtp -> ks_magic == M_MAGIC , ( " " ) ) ; KASSERT ( curthread -> td_critnest == 0 || SCHEDULER_STOPPED ( ) , ( " " ) ) ; if ( addr == NULL ) return ( malloc ( size , mtp , flags ) ) ; #ifdef DEBUG_MEMGUARD if ( is_memguard_addr ( addr ) ) return ( memguard_realloc ( addr , size , mtp , flags ) ) ; #endif #ifdef DEBUG_REDZONE slab = NULL ; alloc = redzone_get_size ( addr ) ; #else slab = vtoslab ( ( vm_offset_t ) addr & ~ ( UMA_SLAB_MASK ) ) ; KASSERT ( slab != NULL , ( " " , ( void * ) addr ) ) ; if ( ! ( slab -> us_flags & UMA_SLAB_MALLOC ) ) alloc = slab -> us_keg -> uk_size ; else alloc = slab -> us_size ; if ( size <= alloc && ( size > ( alloc >> REALLOC_FRACTION ) || alloc == MINALLOCSIZE ) ) return ( addr ) ; #endif if ( ( newaddr = malloc ( size , mtp , flags ) ) == NULL ) return ( NULL ) ; bcopy ( addr , newaddr , min ( size , alloc ) ) ; free ( addr , mtp ) ; return ( newaddr ) ; }
realloc: change the size of a memory block
realloc : change the size of a memory block
/* realloc(NULL, ...) is equivalent to malloc(...) */ /*
	 * XXX: Should report free of old memory and alloc of new memory to
	 * per-CPU stats.
	 */ /* Sanity check */ /* Get the size of the original block */ /* Reuse the original block if appropriate */ /* !DEBUG_REDZONE */ /* Allocate a new, bigger (or smaller) block */ /* Copy over original contents */